UserServices.php 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user;
  12. use app\jobs\UserJob;
  13. use app\services\activity\bargain\StoreBargainServices;
  14. use app\services\activity\combination\StoreCombinationServices;
  15. use app\services\activity\seckill\StoreSeckillServices;
  16. use app\services\agent\AgentLevelServices;
  17. use app\services\BaseServices;
  18. use app\dao\user\UserDao;
  19. use app\services\activity\coupon\StoreCouponUserServices;
  20. use app\services\diy\DiyServices;
  21. use app\services\kefu\service\StoreServiceRecordServices;
  22. use app\services\kefu\service\StoreServiceServices;
  23. use app\services\order\OtherOrderServices;
  24. use app\services\order\StoreOrderCreateServices;
  25. use app\services\order\StoreOrderServices;
  26. use app\services\order\StoreOrderTakeServices;
  27. use app\services\other\QrcodeServices;
  28. use app\services\product\product\StoreProductRelationServices;
  29. use app\services\message\MessageSystemServices;
  30. use app\services\system\SystemUserLevelServices;
  31. use app\services\user\member\MemberCardServices;
  32. use app\services\wechat\WechatUserServices;
  33. use crmeb\exceptions\AdminException;
  34. use crmeb\exceptions\ApiException;
  35. use crmeb\services\CacheService;
  36. use crmeb\services\FormBuilder as Form;
  37. use crmeb\services\FormBuilder;
  38. use crmeb\services\app\WechatService;
  39. use think\Exception;
  40. use think\facade\Route as Url;
  41. /**
  42. * Class UserServices
  43. * @package app\services\user
  44. * @method array getUserInfoArray(array $where, string $field, string $key) 根据条件查询对应的用户信息以数组形式返回
  45. * @method update($id, array $data, ?string $key = null) 修改数据
  46. * @method get($id, ?array $field = [], ?array $with = []) 获取一条数据
  47. * @method count(array $where) 获取指定条件下的数量
  48. * @method value(array $where, string $field) 获取指定的键值
  49. * @method bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
  50. * @method bcDec($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度减法
  51. * @method getTrendData($time, $type, $timeType)
  52. * @method incPayCount(int $uid) 用户支付成功个数增加
  53. */
  54. class UserServices extends BaseServices
  55. {
  56. /**
  57. * UserServices constructor.
  58. * @param UserDao $dao
  59. */
  60. public function __construct(UserDao $dao)
  61. {
  62. $this->dao = $dao;
  63. }
  64. /**
  65. * 获取用户信息
  66. * @param int $uid
  67. * @param string $field
  68. * @return array|\think\Model|null
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @author 吴汐
  73. * @email 442384644@qq.com
  74. * @date 2023/03/01
  75. */
  76. public function getUserInfo(int $uid, $field = '*')
  77. {
  78. if (is_string($field)) $field = explode(',', $field);
  79. return $this->dao->get($uid, $field);
  80. }
  81. /**
  82. * 获取用户列表
  83. * @param array $where
  84. * @param string $field
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function getUserList(array $where, string $field): array
  91. {
  92. [$page, $limit] = $this->getPageValue();
  93. $list = $this->dao->getList($where, $field, $page, $limit);
  94. $count = $this->getCount($where);
  95. return compact('list', 'count');
  96. }
  97. /**
  98. * 列表条数
  99. * @param array $where
  100. * @return int
  101. */
  102. public function getCount(array $where, bool $is_list = false)
  103. {
  104. return $this->dao->getCount($where, $is_list);
  105. }
  106. /**
  107. * 保存用户信息
  108. * @param $user
  109. * @param int $spreadUid
  110. * @param string $userType
  111. * @return User|\think\Model
  112. * @throws Exception
  113. */
  114. public function setUserInfo($user, int $spreadUid = 0, string $userType = 'wechat')
  115. {
  116. $data = [
  117. 'account' => $user['account'] ?? 'wx' . rand(1, 9999) . time(),
  118. 'pwd' => $user['pwd'] ?? md5('123456'),
  119. 'nickname' => $user['nickname'] ?? '',
  120. 'avatar' => $user['headimgurl'] ?? '',
  121. 'phone' => $user['phone'] ?? '',
  122. 'add_time' => time(),
  123. 'add_ip' => app()->request->ip(),
  124. 'last_time' => time(),
  125. 'last_ip' => app()->request->ip(),
  126. 'user_type' => $userType,
  127. 'staff_id' => $user['staff_id'] ?? 0,
  128. 'agent_id' => $user['agent_id'] ?? 0,
  129. 'division_id' => $user['division_id'] ?? 0,
  130. ];
  131. if ($spreadUid) {
  132. $data['spread_uid'] = $spreadUid;
  133. $data['spread_time'] = time();
  134. }
  135. $res = $this->dao->save($data);
  136. if (!$res)
  137. throw new AdminException(400684);
  138. //新用户注册奖励
  139. $this->rewardNewUser((int)$res->uid);
  140. //用户生成后置事件
  141. event('UserRegisterListener', [$spreadUid, $userType, $user['nickname'], $res->uid, 1]);
  142. //推送消息
  143. event('NoticeListener', [['spreadUid' => $spreadUid, 'user_type' => $userType, 'nickname' => $user['nickname']], 'bind_spread_uid']);
  144. return $res;
  145. }
  146. /**
  147. * 某些条件用户佣金总和
  148. * @param array $where
  149. * @return mixed
  150. */
  151. public function getSumBrokerage(array $where)
  152. {
  153. return $this->dao->getWhereSumField($where, 'brokerage_price');
  154. }
  155. /**
  156. * 根据条件获取用户指定字段列表
  157. * @param array $where
  158. * @param string $field
  159. * @param string $key
  160. * @return array
  161. */
  162. public function getColumn(array $where, string $field = '*', string $key = '')
  163. {
  164. return $this->dao->getColumn($where, $field, $key);
  165. }
  166. /**
  167. * 获取某个用户的推广下线
  168. */
  169. public function getSpreadList($uid)
  170. {
  171. $one_uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
  172. $two_uids = $this->dao->getColumn([['spread_uid', 'in', $one_uids], ['spread_uid', '<>', 0]], 'uid');
  173. $uids = array_merge($one_uids, $two_uids);
  174. [$page, $limit] = $this->getPageValue();
  175. $list = $this->dao->getList(['uid' => $uids], 'uid,nickname,real_name,avatar,add_time', $page, $limit);
  176. foreach ($list as $k => $user) {
  177. $list[$k]['type'] = in_array($user['uid'], $one_uids) ? '一级' : '二级';
  178. $list[$k]['add_time'] = date('Y-m-d', $user['add_time']);
  179. }
  180. $count = count($uids);
  181. return compact('count', 'list');
  182. }
  183. /**查找多个uid信息
  184. * @param $uids
  185. * @param bool $field
  186. * @return UserDao|bool|\crmeb\basic\BaseModel|mixed|\think\Collection
  187. */
  188. public function getUserListByUids($uids, $field = false)
  189. {
  190. if (!$uids || !is_array($uids)) return false;
  191. return $this->dao->getUserListByUids($uids, $field);
  192. }
  193. /**
  194. * 获取分销用户
  195. * @param array $where
  196. * @param string $field
  197. * @return array
  198. * @throws \think\db\exception\DataNotFoundException
  199. * @throws \think\db\exception\DbException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. */
  202. public function getAgentUserList(array $where = [], string $field = '*', $is_page = true)
  203. {
  204. $where_data['status'] = 1;
  205. $where_data['is_promoter'] = 1;
  206. $where_data['spread_open'] = 1;
  207. //人人分销时 去除分销员字段的限制
  208. $store_brokerage_statu = sys_config('store_brokerage_statu');
  209. if ($store_brokerage_statu == 2) unset($where_data['is_promoter']);
  210. if (isset($where['nickname']) && $where['nickname'] !== '') {
  211. $where_data['like'] = $where['nickname'];
  212. }
  213. if (isset($where['data']) && $where['data']) {
  214. $where_data['time'] = $where['data'];
  215. }
  216. [$page, $limit] = $this->getPageValue($is_page);
  217. $list = $this->dao->getAgentUserList($where_data, $field, $page, $limit);
  218. $count = $this->dao->count($where_data);
  219. return compact('count', 'list');
  220. }
  221. /**
  222. * 获取分销员ids
  223. * @param array $where
  224. * @return array
  225. */
  226. public function getAgentUserIds(array $where)
  227. {
  228. $where['status'] = 1;
  229. if (sys_config('store_brokerage_statu') != 2) $where['is_promoter'] = 1;
  230. $where['spread_open'] = 1;
  231. if (isset($where['nickname']) && $where['nickname'] !== '') {
  232. $where['like'] = $where['nickname'];
  233. }
  234. if (isset($where['data']) && $where['data']) {
  235. $where['time'] = $where['data'];
  236. }
  237. return $this->dao->getAgentUserIds($where);
  238. }
  239. /**
  240. * 获取推广人列表
  241. * @param array $where
  242. * @param string $field
  243. * @param int $page
  244. * @param int $limit
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\DbException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. */
  249. public function getSairList(array $where, string $field = '*')
  250. {
  251. $where_data = [];
  252. if (isset($where['uid'])) {
  253. if (isset($where['type'])) {
  254. $type = (int)$where['type'];
  255. $type = in_array($type, [1, 2]) ? $type : 0;
  256. $uids = $this->getUserSpredadUids((int)$where['uid'], $type);
  257. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  258. }
  259. if (isset($where['data']) && $where['data']) {
  260. $where_data['time'] = $where['data'];
  261. }
  262. if (isset($where['nickname']) && $where['nickname']) {
  263. $where_data['like'] = $where['nickname'];
  264. }
  265. $where_data['status'] = 1;
  266. }
  267. [$page, $limit] = $this->getPageValue();
  268. $list = $this->dao->getSairList($where_data, '*', $page, $limit);
  269. $count = $this->dao->count($where_data);
  270. return compact('list', 'count');
  271. }
  272. /**
  273. * 获取推广人统计
  274. * @param array $where
  275. * @param string $field
  276. * @param int $page
  277. * @param int $limit
  278. * @throws \think\db\exception\DataNotFoundException
  279. * @throws \think\db\exception\DbException
  280. * @throws \think\db\exception\ModelNotFoundException
  281. */
  282. public function getSairCount(array $where)
  283. {
  284. $where_data = [];
  285. if (isset($where['uid'])) {
  286. if (isset($where['type'])) {
  287. $uids = $this->getColumn(['spread_uid' => $where['uid']], 'uid');
  288. switch ((int)$where['type']) {
  289. case 1:
  290. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  291. break;
  292. case 2:
  293. if (count($uids))
  294. $spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid');
  295. else
  296. $spread_uid_two = [];
  297. $where_data['uid'] = count($spread_uid_two) > 0 ? $spread_uid_two : 0;
  298. break;
  299. default:
  300. if (count($uids)) {
  301. if ($spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid')) {
  302. $uids = array_merge($uids, $spread_uid_two);
  303. $uids = array_unique($uids);
  304. $uids = array_merge($uids);
  305. }
  306. }
  307. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  308. break;
  309. }
  310. }
  311. if (isset($where['data']) && $where['data']) {
  312. $where_data['time'] = $where['data'];
  313. }
  314. if (isset($where['nickname']) && $where['nickname']) {
  315. $where_data['like'] = $where['nickname'];
  316. }
  317. $where_data['status'] = 1;
  318. }
  319. return $this->dao->count($where_data);
  320. }
  321. /**
  322. * 写入用户信息
  323. * @param array $data
  324. * @return bool
  325. */
  326. public function create(array $data)
  327. {
  328. if (!$this->dao->save($data))
  329. throw new AdminException(100000);
  330. return true;
  331. }
  332. /**
  333. * 重置密码
  334. * @param $id
  335. * @param string $password
  336. * @return mixed
  337. */
  338. public function resetPwd(int $uid, string $password)
  339. {
  340. if (!$this->dao->update($uid, ['pwd' => $password]))
  341. throw new AdminException(400685);
  342. return true;
  343. }
  344. /**
  345. * 增加推广人数
  346. * @param int $uid
  347. * @param int $num
  348. * @return bool
  349. * @throws Exception
  350. */
  351. public function incSpreadCount(int $uid, int $num = 1)
  352. {
  353. if (!$this->dao->incField($uid, 'spread_count', $num))
  354. throw new AdminException(400686);
  355. return true;
  356. }
  357. /**
  358. * 设置用户登录类型
  359. * @param int $uid
  360. * @param string $type
  361. * @return bool
  362. * @throws Exception
  363. */
  364. public function setLoginType(int $uid, string $type = 'h5')
  365. {
  366. if (!$this->dao->update($uid, ['login_type' => $type]))
  367. throw new AdminException(400687);
  368. return true;
  369. }
  370. /**
  371. * 设置推广员
  372. * @param int $uid
  373. * @param int $is_promoter
  374. * @return bool
  375. * @throws Exception
  376. */
  377. public function setIsPromoter(int $uid, $is_promoter = 1)
  378. {
  379. if (!$this->dao->update($uid, ['is_promoter' => $is_promoter]))
  380. throw new AdminException(400688);
  381. return true;
  382. }
  383. /**
  384. * 设置用户分组
  385. * @param $uids
  386. * @param int $group_id
  387. */
  388. public function setUserGroup($uids, int $group_id)
  389. {
  390. return $this->dao->batchUpdate($uids, ['group_id' => $group_id], 'uid');
  391. }
  392. /**
  393. * 增加用户余额
  394. * @param int $uid
  395. * @param float $old_now_money
  396. * @param float $now_money
  397. * @return bool
  398. * @throws Exception
  399. */
  400. public function addNowMoney(int $uid, $old_now_money, $now_money)
  401. {
  402. if (!$this->dao->update($uid, ['now_money' => bcadd($old_now_money, $now_money, 2)]))
  403. throw new AdminException(400689);
  404. return true;
  405. }
  406. /**
  407. * 减少用户余额
  408. * @param int $uid
  409. * @param float $old_now_money
  410. * @param float $now_money
  411. * @return bool
  412. * @throws Exception
  413. */
  414. public function cutNowMoney(int $uid, $old_now_money, $now_money)
  415. {
  416. if ($old_now_money > $now_money) {
  417. $money = ['now_money' => bcsub($old_now_money, $now_money, 2)];
  418. } else {
  419. $money = ['now_money' => 0];
  420. }
  421. if (!$this->dao->update($uid, $money, 'uid'))
  422. throw new AdminException(400690);
  423. return true;
  424. }
  425. /**
  426. * 减少用户佣金
  427. * @param int $uid
  428. * @param float $brokerage_price
  429. * @param float $price
  430. * @return bool
  431. * @throws Exception
  432. */
  433. public function cutBrokeragePrice(int $uid, $brokerage_price, $price)
  434. {
  435. if (!$this->dao->update($uid, ['brokerage_price' => bcsub($brokerage_price, $price, 2)]))
  436. throw new AdminException(400691);
  437. return true;
  438. }
  439. /**
  440. * 增加用户积分
  441. * @param int $uid
  442. * @param float $old_integral
  443. * @param float $integral
  444. * @return bool
  445. * @throws Exception
  446. */
  447. public function addIntegral(int $uid, $old_integral, $integral)
  448. {
  449. if (!$this->dao->update($uid, ['integral' => bcadd($old_integral, $integral, 2)]))
  450. throw new AdminException(400692);
  451. return true;
  452. }
  453. /**
  454. * 减少用户积分
  455. * @param int $uid
  456. * @param float $old_integral
  457. * @param float $integral
  458. * @return bool
  459. * @throws Exception
  460. */
  461. public function cutIntegral(int $uid, $old_integral, $integral)
  462. {
  463. if (!$this->dao->update($uid, ['integral' => bcsub($old_integral, $integral, 2)]))
  464. throw new AdminException(400693);
  465. return true;
  466. }
  467. /**
  468. * 增加用户经验
  469. * @param int $uid
  470. * @param float $old_exp
  471. * @param float $exp
  472. * @return bool
  473. * @throws Exception
  474. */
  475. public function addExp(int $uid, float $old_exp, float $exp)
  476. {
  477. if (!$this->dao->update($uid, ['exp' => bcadd($old_exp, $exp, 2)]))
  478. throw new AdminException(400694);
  479. return true;
  480. }
  481. /**
  482. * 减少用户经验
  483. * @param int $uid
  484. * @param float $old_exp
  485. * @param float $exp
  486. * @return bool
  487. * @throws Exception
  488. */
  489. public function cutExp(int $uid, float $old_exp, float $exp)
  490. {
  491. if (!$this->dao->update($uid, ['exp' => bcsub($old_exp, $exp, 2)]))
  492. throw new AdminException(400695);
  493. return true;
  494. }
  495. /**
  496. * 获取用户标签
  497. * @param $uid
  498. * @return \think\Collection
  499. * @throws \think\db\exception\DataNotFoundException
  500. * @throws \think\db\exception\DbException
  501. * @throws \think\db\exception\ModelNotFoundException
  502. */
  503. public function getUserLablel(array $uids)
  504. {
  505. /** @var UserLabelRelationServices $services */
  506. $services = app()->make(UserLabelRelationServices::class);
  507. $userlabels = $services->getUserLabelList($uids);
  508. $data = [];
  509. foreach ($uids as $uid) {
  510. $labels = array_filter($userlabels, function ($item) use ($uid) {
  511. if ($item['uid'] == $uid) {
  512. return true;
  513. }
  514. });
  515. $data[$uid] = implode(',', array_column($labels, 'label_name'));
  516. }
  517. return $data;
  518. }
  519. /**
  520. * 会员列表
  521. * @param array $where
  522. * @return array
  523. */
  524. public function index(array $where)
  525. {
  526. /** @var UserWechatuserServices $userWechatUser */
  527. $userWechatUser = app()->make(UserWechatuserServices::class);
  528. $fields = 'u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.user_type as w_user_type,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time';
  529. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  530. if ($list) {
  531. $uids = array_column($list, 'uid');
  532. $userlabel = $this->getUserLablel($uids);
  533. $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
  534. $userExtract = app()->make(UserExtractServices::class)->getUsersSumList($uids);
  535. $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
  536. $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
  537. $spread_names = $this->dao->getColumn([['uid', 'in', array_unique(array_column($list, 'spread_uid'))]], 'nickname', 'uid');
  538. foreach ($list as &$item) {
  539. if (empty($item['addres'])) {
  540. if (!empty($item['country']) || !empty($item['province']) || !empty($item['city'])) {
  541. $item['addres'] = $item['country'] . $item['province'] . $item['city'];
  542. }
  543. }
  544. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  545. $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
  546. $item['extract_count_price'] = $userExtract[$item['uid']] ?? 0;//累计提现
  547. $item['spread_uid_nickname'] = $item['spread_uid'] ? ($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid'] : '无';
  548. //用户类型
  549. if ($item['user_type'] == 'routine') {
  550. $item['user_type'] = '小程序';
  551. } else if ($item['user_type'] == 'wechat') {
  552. $item['user_type'] = '公众号';
  553. } else if ($item['user_type'] == 'h5') {
  554. $item['user_type'] = 'H5';
  555. } else if ($item['user_type'] == 'pc') {
  556. $item['user_type'] = 'PC';
  557. } else if ($item['user_type'] == 'app' || $item['user_type'] == 'apple') {
  558. $item['user_type'] = 'APP';
  559. } else $item['user_type'] = '其他';
  560. if ($item['sex'] == 1) {
  561. $item['sex'] = '男';
  562. } else if ($item['sex'] == 2) {
  563. $item['sex'] = '女';
  564. } else $item['sex'] = '保密';
  565. //等级名称
  566. $item['level'] = $levelName[$item['level']] ?? '无';
  567. //分组名称
  568. $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
  569. //用户等级
  570. $item['vip_name'] = false;
  571. $levelinfo = $userLevel[$item['uid']] ?? null;
  572. if ($levelinfo) {
  573. if ($levelinfo && ($levelinfo['is_forever'] || time() < $levelinfo['valid_time'])) {
  574. $item['vip_name'] = $item['level'] != '无' ? $item['level'] : false;
  575. }
  576. }
  577. $item['labels'] = $userlabel[$item['uid']] ?? '';
  578. $item['isMember'] = $item['is_money_level'] > 0 ? 1 : 0;
  579. if (strpos($item['avatar'], '/statics/system_images/') !== false) {
  580. $item['avatar'] = set_file_url($item['avatar']);
  581. }
  582. }
  583. }
  584. return compact('count', 'list');
  585. }
  586. /**
  587. * 获取修改页面数据
  588. * @param int $id
  589. * @return array
  590. * @throws \FormBuilder\Exception\FormBuilderException
  591. */
  592. public function edit(int $id)
  593. {
  594. $user = $this->getUserInfo($id);
  595. if (!$user)
  596. throw new AdminException(100026);
  597. $f = array();
  598. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(true);
  599. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
  600. $f[] = Form::input('phone', '手机号码', $user->getData('phone'));
  601. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : '');
  602. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
  603. $f[] = Form::input('addres', '用户地址', $user->getData('addres'));
  604. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  605. $f[] = Form::input('pwd', '登录密码')->type('password')->placeholder('不改密码请留空');
  606. $f[] = Form::input('true_pwd', '确认密码')->type('password')->placeholder('不改密码请留空');
  607. //查询高于当前会员的所有会员等级
  608. // $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  609. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  610. $setOptionLevel = function () use ($systemLevelList) {
  611. $menus = [];
  612. foreach ($systemLevelList as $menu) {
  613. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  614. }
  615. return $menus;
  616. };
  617. $f[] = Form::select('level', '用户等级', (int)$user->getData('level'))->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  618. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  619. $setOptionGroup = function () use ($systemGroupList) {
  620. $menus = [];
  621. foreach ($systemGroupList as $menu) {
  622. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  623. }
  624. return $menus;
  625. };
  626. $f[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  627. $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
  628. $labels = app()->make(UserLabelRelationServices::class)->getUserLabels($user['uid']);
  629. $setOptionLabel = function () use ($systemLabelList) {
  630. $menus = [];
  631. foreach ($systemLabelList as $menu) {
  632. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  633. }
  634. return $menus;
  635. };
  636. $f[] = Form::select('label_id', '用户标签', $labels)->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  637. $f[] = Form::radio('spread_open', '推广资格', $user->getData('spread_open'))->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  638. //分销模式 人人分销
  639. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  640. if ($storeBrokerageStatus == 1) {
  641. $f[] = Form::radio('is_promoter', '推广员权限', $user->getData('is_promoter'))->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  642. }
  643. $f[] = Form::radio('status', '用户状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  644. return create_form('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
  645. }
  646. /**
  647. * 添加用户表单
  648. * @param int $id
  649. * @return array
  650. * @throws \FormBuilder\Exception\FormBuilderException
  651. */
  652. public function saveForm()
  653. {
  654. $f = array();
  655. $f[] = Form::input('real_name', '真实姓名', '')->placeholder('请输入真实姓名');
  656. $f[] = Form::input('phone', '手机号码', '')->placeholder('请输入手机号码')->required();
  657. $f[] = Form::date('birthday', '生日', '')->placeholder('请选择生日');
  658. $f[] = Form::input('card_id', '身份证号', '')->placeholder('请输入身份证号');
  659. $f[] = Form::input('addres', '用户地址', '')->placeholder('请输入用户地址');
  660. $f[] = Form::textarea('mark', '用户备注', '')->placeholder('请输入用户备注');
  661. $f[] = Form::input('pwd', '登录密码')->type('password')->placeholder('请输入登录密码');
  662. $f[] = Form::input('true_pwd', '确认密码')->type('password')->placeholder('请再次确认密码');
  663. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  664. $setOptionLevel = function () use ($systemLevelList) {
  665. $menus = [];
  666. foreach ($systemLevelList as $menu) {
  667. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  668. }
  669. return $menus;
  670. };
  671. $f[] = Form::select('level', '用户等级', '')->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  672. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  673. $setOptionGroup = function () use ($systemGroupList) {
  674. $menus = [];
  675. foreach ($systemGroupList as $menu) {
  676. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  677. }
  678. return $menus;
  679. };
  680. $f[] = Form::select('group_id', '用户分组', '')->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  681. $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
  682. $setOptionLabel = function () use ($systemLabelList) {
  683. $menus = [];
  684. foreach ($systemLabelList as $menu) {
  685. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  686. }
  687. return $menus;
  688. };
  689. $f[] = Form::select('label_id', '用户标签', '')->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  690. $f[] = Form::radio('spread_open', '推广资格', 1)->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  691. //分销模式 人人分销
  692. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  693. if ($storeBrokerageStatus == 1) {
  694. $f[] = Form::radio('is_promoter', '推广员权限', 0)->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  695. }
  696. $f[] = Form::radio('status', '用户状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  697. return create_form('添加用户', $f, $this->url('/user/user'), 'POST');
  698. }
  699. /**
  700. * 修改提交处理
  701. * @param int $id
  702. * @param array $data
  703. * @return bool
  704. * @throws Exception
  705. * @throws \think\db\exception\DataNotFoundException
  706. * @throws \think\db\exception\ModelNotFoundException
  707. */
  708. public function updateInfo(int $id, array $data)
  709. {
  710. $user = $this->getUserInfo($id);
  711. if (!$user) {
  712. throw new AdminException(100026);
  713. }
  714. $res1 = false;
  715. $res2 = false;
  716. $edit = array();
  717. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  718. /** @var UserMoneyServices $userMoneyServices */
  719. $userMoneyServices = app()->make(UserMoneyServices::class);
  720. if ($data['money_status'] == 1) {//增加
  721. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  722. $res1 = $userMoneyServices->income('system_add', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  723. //增加充值记录
  724. $recharge_data = [
  725. 'order_id' => app()->make(StoreOrderCreateServices::class)->getNewOrderId('cz'),
  726. 'uid' => $id,
  727. 'price' => $data['money'],
  728. 'recharge_type' => 'system',
  729. 'paid' => 1,
  730. 'add_time' => time(),
  731. 'give_price' => 0,
  732. 'channel_type' => 'system',
  733. 'pay_time' => time(),
  734. ];
  735. /** @var UserRechargeServices $rechargeServices */
  736. $rechargeServices = app()->make(UserRechargeServices::class);
  737. $rechargeServices->save($recharge_data);
  738. } else if ($data['money_status'] == 2) {//减少
  739. if ($user['now_money'] > $data['money']) {
  740. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  741. } else {
  742. $edit['now_money'] = 0;
  743. $data['money'] = $user['now_money'];
  744. }
  745. $res1 = $userMoneyServices->income('system_sub', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  746. }
  747. event('OutPushListener', ['user_update_push', ['uid' => $id, 'type' => 'money', 'value' => $data['money_status'] == 2 ? -floatval($data['money']) : $data['money']]]);
  748. } else {
  749. $res1 = true;
  750. }
  751. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  752. /** @var UserBillServices $userBill */
  753. $userBill = app()->make(UserBillServices::class);
  754. $integral_data = ['link_id' => $data['adminId'] ?? 0, 'number' => $data['integration']];
  755. if ($data['integration_status'] == 1) {//增加
  756. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  757. $integral_data['balance'] = $edit['integral'];
  758. $integral_data['title'] = '系统增加积分';
  759. $integral_data['mark'] = '系统增加了' . floatval($data['integration']) . '积分';
  760. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  761. } else if ($data['integration_status'] == 2) {//减少
  762. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  763. $integral_data['balance'] = $edit['integral'];
  764. $integral_data['title'] = '系统减少积分';
  765. $integral_data['mark'] = '系统扣除了' . floatval($data['integration']) . '积分';
  766. $res2 = $userBill->expendIntegral($user['uid'], 'system_sub', $integral_data);
  767. }
  768. event('OutPushListener', ['user_update_push', ['uid' => $id, 'type' => 'point', 'value' => $data['integration_status'] == 2 ? -intval($data['integration']) : $data['integration']]]);
  769. } else {
  770. $res2 = true;
  771. }
  772. //修改基本信息
  773. if (!isset($data['is_other']) || !$data['is_other']) {
  774. app()->make(UserLabelRelationServices::class)->setUserLable([$id], $data['label_id']);
  775. if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
  776. $edit['pwd'] = $data['pwd'];
  777. }
  778. if (isset($data['spread_open'])) {
  779. $edit['spread_open'] = $data['spread_open'];
  780. }
  781. $edit['status'] = $data['status'];
  782. $edit['real_name'] = $data['real_name'];
  783. $edit['card_id'] = $data['card_id'];
  784. $edit['birthday'] = strtotime($data['birthday']);
  785. $edit['mark'] = $data['mark'];
  786. $edit['is_promoter'] = $data['is_promoter'];
  787. $edit['level'] = $data['level'];
  788. $edit['phone'] = $data['phone'];
  789. $edit['addres'] = $data['addres'];
  790. $edit['group_id'] = $data['group_id'];
  791. if ($user['level'] != $data['level']) {
  792. /** @var UserLevelServices $userLevelService */
  793. $userLevelService = app()->make(UserLevelServices::class);
  794. $userLevelService->setUserLevel((int)$user['uid'], (int)$data['level']);
  795. }
  796. }
  797. if ($edit) $res3 = $this->dao->update($id, $edit);
  798. else $res3 = true;
  799. if ($res1 && $res2 && $res3)
  800. return true;
  801. else throw new AdminException(100007);
  802. }
  803. /**
  804. * 编辑其他
  805. * @param $id
  806. * @return mixed
  807. * @throws \FormBuilder\Exception\FormBuilderException
  808. */
  809. public function editOther($id)
  810. {
  811. $user = $this->getUserInfo($id);
  812. if (!$user) {
  813. throw new AdminException(100026);
  814. }
  815. $f = array();
  816. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  817. $f[] = Form::number('money', '余额', 0)->min(0)->max(999999.99);
  818. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  819. $f[] = Form::number('integration', '积分', 0)->min(0)->precision(0)->max(999999);
  820. return create_form('修改其他', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
  821. }
  822. /**
  823. * 设置会员分组
  824. * @param $id
  825. * @return mixed
  826. */
  827. public function setGroup($uids)
  828. {
  829. /** @var UserGroupServices $groupServices */
  830. $groupServices = app()->make(UserGroupServices::class);
  831. $userGroup = $groupServices->getGroupList();
  832. if (count($uids) == 1) {
  833. $user = $this->getUserInfo($uids[0], ['group_id']);
  834. $setOptionUserGroup = function () use ($userGroup) {
  835. $menus = [];
  836. foreach ($userGroup as $menu) {
  837. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  838. }
  839. return $menus;
  840. };
  841. $field[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  842. } else {
  843. $setOptionUserGroup = function () use ($userGroup) {
  844. $menus = [];
  845. foreach ($userGroup as $menu) {
  846. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  847. }
  848. return $menus;
  849. };
  850. $field[] = Form::select('group_id', '用户分组')->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  851. }
  852. $field[] = Form::hidden('uids', implode(',', $uids));
  853. return create_form('设置用户分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
  854. }
  855. /**
  856. * 保存会员分组
  857. * @param $id
  858. * @return mixed
  859. */
  860. public function saveSetGroup($uids, int $group_id)
  861. {
  862. /** @var UserGroupServices $userGroup */
  863. $userGroup = app()->make(UserGroupServices::class);
  864. if (!$userGroup->getGroup($group_id)) {
  865. throw new AdminException(400696);
  866. }
  867. if (!$this->setUserGroup($uids, $group_id)) {
  868. throw new AdminException(400697);
  869. }
  870. return true;
  871. }
  872. /**
  873. * 设置用户标签
  874. * @param $uids
  875. * @return mixed
  876. */
  877. public function setLabel($uids)
  878. {
  879. /** @var UserLabelServices $labelServices */
  880. $labelServices = app()->make(UserLabelServices::class);
  881. $userLabel = $labelServices->getLabelList();
  882. if (count($uids) == 1) {
  883. $lids = app()->make(UserLabelRelationServices::class)->getUserLabels($uids[0]);
  884. $setOptionUserLabel = function () use ($userLabel) {
  885. $menus = [];
  886. foreach ($userLabel as $menu) {
  887. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  888. }
  889. return $menus;
  890. };
  891. $field[] = Form::select('label_id', '用户标签', $lids)->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  892. } else {
  893. $setOptionUserLabel = function () use ($userLabel) {
  894. $menus = [];
  895. foreach ($userLabel as $menu) {
  896. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  897. }
  898. return $menus;
  899. };
  900. $field[] = Form::select('label_id', '用户标签')->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  901. }
  902. $field[] = Form::hidden('uids', implode(',', $uids));
  903. return create_form('设置用户标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
  904. }
  905. /**
  906. * 保存用户标签
  907. * @return mixed
  908. */
  909. public function saveSetLabel($uids, $lable_id)
  910. {
  911. foreach ($lable_id as $id) {
  912. if (!app()->make(UserLabelServices::class)->getLable((int)$id)) {
  913. throw new AdminException(400698);
  914. }
  915. }
  916. /** @var UserLabelRelationServices $services */
  917. $services = app()->make(UserLabelRelationServices::class);
  918. if (!$services->setUserLable($uids, $lable_id)) {
  919. throw new AdminException(400668);
  920. }
  921. return true;
  922. }
  923. /**
  924. * 赠送会员等级
  925. * @param int $uid
  926. * @return mixed
  927. * */
  928. public function giveLevel($id)
  929. {
  930. if (!$this->getUserInfo($id)) {
  931. throw new AdminException(400214);
  932. }
  933. //查询高于当前会员的所有会员等级
  934. $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  935. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', $grade ?? 0], 'id,name');
  936. $setOptionlevel = function () use ($systemLevelList) {
  937. $menus = [];
  938. foreach ($systemLevelList as $menu) {
  939. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  940. }
  941. return $menus;
  942. };
  943. $field[] = Form::select('level_id', '用户等级')->setOptions(FormBuilder::setOptions($setOptionlevel))->filterable(true);
  944. return create_form('赠送等级', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
  945. }
  946. /**
  947. * 执行赠送会员等级
  948. * @param int $id
  949. * @param int $level_id
  950. * @return mixed
  951. * @throws \think\db\exception\DataNotFoundException
  952. * @throws \think\db\exception\DbException
  953. * @throws \think\db\exception\ModelNotFoundException
  954. */
  955. public function saveGiveLevel(int $id, int $level_id)
  956. {
  957. if (!$this->getUserInfo($id)) {
  958. throw new AdminException(400214);
  959. }
  960. /** @var SystemUserLevelServices $systemLevelServices */
  961. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  962. /** @var UserLevelServices $userLevelServices */
  963. $userLevelServices = app()->make(UserLevelServices::class);
  964. //查询当前选择的会员等级
  965. $systemLevel = $systemLevelServices->getLevel($level_id);
  966. if (!$systemLevel) throw new AdminException(400699);
  967. //检查是否拥有此会员等级
  968. $level = $userLevelServices->getWhereLevel(['uid' => $id, 'level_id' => $level_id], 'valid_time,is_forever');
  969. if ($level && $level['status'] == 1 && $level['is_del'] == 0) {
  970. throw new AdminException(400700);
  971. }
  972. //保存会员信息
  973. if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
  974. throw new AdminException(400219);
  975. }
  976. return true;
  977. }
  978. /**
  979. * 赠送付费会员时长
  980. * @param $id
  981. * @return array
  982. * @throws \FormBuilder\Exception\FormBuilderException
  983. * @author 吴汐
  984. * @email 442384644@qq.com
  985. * @date 2023/03/01
  986. */
  987. public function giveLevelTime($id)
  988. {
  989. $userInfo = $this->getUserInfo($id);
  990. if (!$userInfo) {
  991. throw new AdminException(400214);
  992. }
  993. $timeDiff = $userInfo['is_ever_level'] == 1 ? '永久' : date('Y-m-d H:i:s', $userInfo['overdue_time']);
  994. $dayDiff = $userInfo['overdue_time'] > time() ? intval(($userInfo['overdue_time'] - time()) / 86400) : 0;
  995. $field[] = Form::input('time_diff', '到期时间', $timeDiff)->style(['width' => '200px'])->readonly(true);
  996. if ($userInfo['is_ever_level'] == 0) {
  997. $field[] = Form::input('day_diff', '剩余天数', $dayDiff)->style(['width' => '200px'])->readonly(true);
  998. $field[] = Form::number('days', '增加时长(天)')->precision(0)->style(['width' => '200px'])->required();
  999. }
  1000. return create_form('赠送付费会员时长', $field, Url::buildUrl('/user/save_give_level_time/' . $id), 'PUT');
  1001. }
  1002. /**
  1003. * 执行赠送付费会员时长
  1004. * @param int $id
  1005. * @param int $days
  1006. * @return mixed
  1007. * @throws \think\db\exception\DataNotFoundException
  1008. * @throws \think\db\exception\DbException
  1009. * @throws \think\db\exception\ModelNotFoundException
  1010. */
  1011. public function saveGiveLevelTime(int $id, int $days)
  1012. {
  1013. $userInfo = $this->getUserInfo($id);
  1014. if ($userInfo->is_ever_level == 1) {
  1015. return true;
  1016. }
  1017. if (!$userInfo) {
  1018. throw new AdminException(400214);
  1019. }
  1020. if ($days == 0) throw new AdminException(400701);
  1021. if ($days < -1) throw new AdminException(400702);
  1022. if ($userInfo->is_money_level == 0) {
  1023. $userInfo->is_money_level = 3;
  1024. if ($days == -1) {
  1025. $userInfo->is_ever_level = 1;
  1026. $time = 0;
  1027. } else {
  1028. $userInfo->overdue_time = $time = time() + ($days * 86400);
  1029. }
  1030. } else {
  1031. if ($days == -1) {
  1032. $userInfo->is_ever_level = 1;
  1033. $time = 0;
  1034. } else {
  1035. $userInfo->overdue_time = $time = $userInfo->overdue_time + ($days * 86400);
  1036. }
  1037. }
  1038. $userInfo->save();
  1039. /** @var StoreOrderCreateServices $storeOrderCreateService */
  1040. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  1041. $orderInfo = [
  1042. 'uid' => $id,
  1043. 'order_id' => $storeOrderCreateService->getNewOrderId(),
  1044. 'type' => 3,
  1045. 'member_type' => 0,
  1046. 'pay_type' => 'admin',
  1047. 'paid' => 1,
  1048. 'pay_time' => time(),
  1049. 'is_free' => 1,
  1050. 'overdue_time' => $time,
  1051. 'vip_day' => $days,
  1052. 'add_time' => time()
  1053. ];
  1054. /** @var OtherOrderServices $otherOrder */
  1055. $otherOrder = app()->make(OtherOrderServices::class);
  1056. $otherOrder->save($orderInfo);
  1057. return true;
  1058. }
  1059. /**
  1060. * 清除会员等级
  1061. * @paran int $uid
  1062. * @paran boolean
  1063. * */
  1064. public function cleanUpLevel($uid)
  1065. {
  1066. if (!$this->getUserInfo($uid))
  1067. throw new AdminException(400214);
  1068. /** @var UserLevelServices $services */
  1069. $services = app()->make(UserLevelServices::class);
  1070. return $this->transaction(function () use ($uid, $services) {
  1071. $res = $services->delUserLevel($uid);
  1072. $res1 = $this->dao->update($uid, ['clean_time' => time(), 'level' => 0, 'exp' => 0], 'uid');
  1073. if (!$res && !$res1)
  1074. throw new AdminException(400186);
  1075. return true;
  1076. });
  1077. }
  1078. /**
  1079. * 用户详细信息
  1080. * @param int $uid
  1081. * @param array $userIfno
  1082. * @return array
  1083. * @throws \think\db\exception\DataNotFoundException
  1084. * @throws \think\db\exception\DbException
  1085. * @throws \think\db\exception\ModelNotFoundException
  1086. */
  1087. public function getUserDetailed(int $uid, $userIfno = [])
  1088. {
  1089. /** @var UserAddressServices $userAddress */
  1090. $userAddress = app()->make(UserAddressServices::class);
  1091. $field = 'real_name,phone,province,city,district,detail,post_code';
  1092. $address = $userAddress->getUserDefaultAddress($uid, $field);
  1093. if (!$address) {
  1094. $address = $userAddress->getUserAddressList($uid, $field);
  1095. $address = $address[0] ?? [];
  1096. }
  1097. $userInfo = $this->getUserInfo($uid);
  1098. return [
  1099. ['name' => '默认收货地址', 'value' => $address ? '收货人:' . $address['real_name'] . '邮编:' . $address['post_code'] . ' 收货人电话:' . $address['phone'] . ' 地址:' . $address['province'] . ' ' . $address['city'] . ' ' . $address['district'] . ' ' . $address['detail'] : ''],
  1100. ['name' => '手机号码', 'value' => $userInfo['phone']],
  1101. ['name' => '姓名', 'value' => ''],
  1102. ['name' => '微信昵称', 'value' => $userInfo['nickname']],
  1103. ['name' => '头像', 'value' => $userInfo['avatar']],
  1104. ['name' => '邮箱', 'value' => ''],
  1105. ['name' => '生日', 'value' => ''],
  1106. ['name' => '积分', 'value' => $userInfo['integral']],
  1107. ['name' => '上级推广人', 'value' => $userInfo['spread_uid'] ? $this->getUserInfo($userInfo['spread_uid'], ['nickname'])['nickname'] ?? '' : ''],
  1108. ['name' => '账户余额', 'value' => $userInfo['now_money']],
  1109. ['name' => '佣金总收入', 'value' => app()->make(UserBillServices::class)->getBrokerageSum($uid)],
  1110. ['name' => '提现总金额', 'value' => app()->make(UserExtractServices::class)->getUserExtract($uid)],
  1111. ];
  1112. }
  1113. /**
  1114. * 获取用户详情里面的用户消费能力和用户余额积分等
  1115. * @param $uid
  1116. * @return array[]
  1117. */
  1118. public function getHeaderList(int $uid, $userInfo = [])
  1119. {
  1120. if (!$userInfo) {
  1121. $userInfo = $this->getUserInfo($uid);
  1122. }
  1123. /** @var StoreOrderServices $orderServices */
  1124. $orderServices = app()->make(StoreOrderServices::class);
  1125. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  1126. return [
  1127. [
  1128. 'title' => '余额',
  1129. 'value' => $userInfo['now_money'] ?? 0,
  1130. 'key' => '元',
  1131. ],
  1132. [
  1133. 'title' => '总计订单',
  1134. 'value' => $orderServices->count($where),
  1135. 'key' => '笔',
  1136. ],
  1137. [
  1138. 'title' => '总消费金额',
  1139. 'value' => $orderServices->together($where, 'pay_price'),
  1140. 'key' => '元',
  1141. ],
  1142. [
  1143. 'title' => '积分',
  1144. 'value' => $userInfo['integral'] ?? 0,
  1145. 'key' => '',
  1146. ],
  1147. [
  1148. 'title' => '本月订单',
  1149. 'value' => $orderServices->count($where + ['time' => 'month']),
  1150. 'key' => '笔',
  1151. ],
  1152. [
  1153. 'title' => '本月消费金额',
  1154. 'value' => $orderServices->together($where + ['time' => 'month'], 'pay_price'),
  1155. 'key' => '元',
  1156. ]
  1157. ];
  1158. }
  1159. /**
  1160. * 获取用户记录里的积分总数和签到总数和余额变动总数
  1161. * @param $uid
  1162. * @return array
  1163. */
  1164. public function getUserBillCountData($uid)
  1165. {
  1166. /** @var UserBillServices $userBill */
  1167. $userBill = app()->make(UserBillServices::class);
  1168. $integral_count = $userBill->getIntegralCount($uid);
  1169. $sign_count = $userBill->getSignCount($uid);
  1170. $balanceChang_count = $userBill->getBrokerageCount($uid);
  1171. return [$integral_count, $sign_count, $balanceChang_count];
  1172. }
  1173. /**
  1174. * 用户详情
  1175. * @param int $uid
  1176. * @return array
  1177. * @throws \think\db\exception\DataNotFoundException
  1178. * @throws \think\db\exception\DbException
  1179. * @throws \think\db\exception\ModelNotFoundException
  1180. */
  1181. public function read(int $uid)
  1182. {
  1183. $userInfo = $this->getUserInfo($uid);
  1184. if (!$userInfo) {
  1185. throw new AdminException(100026);
  1186. }
  1187. $userInfo['avatar'] = strpos($userInfo['avatar'], 'http') === false ? (sys_config('site_url') . $userInfo['avatar']) : $userInfo['avatar'];
  1188. $userInfo['overdue_time'] = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1189. $userInfo['birthday'] = $userInfo['birthday'] < 0 ? 0 : $userInfo['birthday'];
  1190. if ($userInfo['addres'] == '') {
  1191. $defaultAddressInfo = app()->make(UserAddressServices::class)->getUserDefaultAddress($uid);
  1192. if ($defaultAddressInfo) {
  1193. $userInfo['addres'] = $defaultAddressInfo['province'] . $defaultAddressInfo['city'] . $defaultAddressInfo['district'] . $defaultAddressInfo['detail'];
  1194. } else {
  1195. $userInfo['addres'] = '';
  1196. }
  1197. }
  1198. $userInfo['vip_name'] = app()->make(SystemUserLevelServices::class)->value(['grade' => $userInfo['level']], 'name');
  1199. $userInfo['group_name'] = app()->make(UserGroupServices::class)->value(['id' => $userInfo['group_id']], 'group_name');
  1200. $userInfo['spread_uid_nickname'] = $this->dao->value(['uid' => $userInfo['spread_uid']], 'nickname') . '(' . $userInfo['spread_uid'] . ')';
  1201. $userInfo['label_list'] = implode(',', array_column(app()->make(UserLabelRelationServices::class)->getUserLabelList([$uid]), 'label_name'));
  1202. return [
  1203. 'uid' => $uid,
  1204. 'userinfo' => $this->getUserDetailed($uid, $userInfo),
  1205. 'headerList' => $this->getHeaderList($uid, $userInfo),
  1206. 'count' => $this->getUserBillCountData($uid),
  1207. 'ps_info' => $userInfo
  1208. ];
  1209. }
  1210. /**
  1211. * 获取好友
  1212. * @param int $id
  1213. * @param string $field
  1214. * @return array
  1215. * @throws \think\db\exception\DataNotFoundException
  1216. * @throws \think\db\exception\DbException
  1217. * @throws \think\db\exception\ModelNotFoundException
  1218. */
  1219. public function getFriendList(int $id, string $field = 'uid,nickname,level,add_time,spread_time')
  1220. {
  1221. [$page, $limit] = $this->getPageValue();
  1222. $list = $this->dao->getList(['spread_uid' => $id], $field, $page, $limit);
  1223. /** @var SystemUserLevelServices $systemLevelServices */
  1224. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  1225. $systemLevelList = $systemLevelServices->getWhereLevelList([], 'id,name');
  1226. if ($systemLevelServices) $systemLevelServices = array_combine(array_column($systemLevelList, 'id'), $systemLevelList);
  1227. foreach ($list as &$item) {
  1228. $item['type'] = $systemLevelServices[$item['level']]['name'] ?? '暂无';
  1229. $item['add_time'] = $item['spread_time'] && is_numeric($item['spread_time']) ? date('Y-m-d H:i:s', $item['spread_time']) : '';
  1230. }
  1231. $count = $this->dao->count(['spread_uid' => $id]);
  1232. return compact('list', 'count');
  1233. }
  1234. /**
  1235. * 获取单个用户信息
  1236. * @param $id 用户id
  1237. * @return mixed
  1238. */
  1239. public function oneUserInfo(int $id, string $type)
  1240. {
  1241. switch ($type) {
  1242. case 'spread':
  1243. // /** @var UserFriendsServices $services */
  1244. // $services = app()->make(UserFriendsServices::class);
  1245. // return $services->getFriendList(['uid' => $id], ['level', 'nickname']);
  1246. return $this->getFriendList($id);
  1247. case 'order':
  1248. /** @var StoreOrderServices $services */
  1249. $services = app()->make(StoreOrderServices::class);
  1250. return $services->getUserOrderList($id);
  1251. case 'integral':
  1252. /** @var UserBillServices $services */
  1253. $services = app()->make(UserBillServices::class);
  1254. return $services->getIntegralList($id, [], 'title,number,balance,mark,add_time,frozen_time');
  1255. case 'sign':
  1256. /** @var UserBillServices $services */
  1257. $services = app()->make(UserBillServices::class);
  1258. return $services->getSignList($id, [], 'title,number,mark,add_time');
  1259. case 'coupon':
  1260. /** @var StoreCouponUserServices $services */
  1261. $services = app()->make(StoreCouponUserServices::class);
  1262. return $services->getUserCouponList($id);
  1263. case 'balance_change':
  1264. /** @var UserMoneyServices $services */
  1265. $services = app()->make(UserMoneyServices::class);
  1266. return $services->balanceList(['uid' => $id]);
  1267. default:
  1268. throw new AdminException(100100);
  1269. }
  1270. }
  1271. /**获取特定时间用户访问量
  1272. * @param $time
  1273. * @param $week
  1274. * @return int
  1275. */
  1276. public function todayLastVisits($time, $week)
  1277. {
  1278. return $this->dao->todayLastVisit($time, $week);
  1279. }
  1280. /**获取特定时间新增用户
  1281. * @param $time
  1282. * @param $week
  1283. * @return int
  1284. */
  1285. public function todayAddVisits($time, $week)
  1286. {
  1287. return $this->dao->todayAddVisit($time, $week);
  1288. }
  1289. /**
  1290. * 用户图表
  1291. */
  1292. public function userChart()
  1293. {
  1294. $starday = date('Y-m-d', strtotime('-30 day'));
  1295. $yesterday = date('Y-m-d', strtotime('+1 day'));
  1296. $user_list = $this->dao->userList($starday, $yesterday);
  1297. $chartdata = [];
  1298. $data = [];
  1299. $chartdata['legend'] = ['用户数'];//分类
  1300. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1301. $chartdata['xAxis'] = [date('m-d')];//X轴值
  1302. $chartdata['series'] = [0];//分类1值
  1303. if (!empty($user_list)) {
  1304. foreach ($user_list as $k => $v) {
  1305. $data['day'][] = $v['day'];
  1306. $data['count'][] = $v['count'];
  1307. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  1308. $chartdata['yAxis']['maxnum'] = $v['count'];
  1309. }
  1310. $chartdata['xAxis'] = $data['day'];//X轴值
  1311. $chartdata['series'] = $data['count'];//分类1值
  1312. }
  1313. $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
  1314. $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  1315. $pay[0] = $this->dao->count(['pay_count' => 0]);
  1316. $pay[1] = $this->dao->count(['pay_count' => 1]);
  1317. $pay[2] = $this->dao->userCount(1);
  1318. $pay[3] = $this->dao->userCount(2);
  1319. foreach ($pay as $key => $item) {
  1320. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  1321. }
  1322. $chartdata['bing_data'] = $bing_data;
  1323. return $chartdata;
  1324. }
  1325. /***********************************************/
  1326. /************ 前端api services *****************/
  1327. /***********************************************/
  1328. /**
  1329. * 用户信息
  1330. * @param $info
  1331. * @return mixed
  1332. */
  1333. public function userInfo($info)
  1334. {
  1335. /** @var UserBillServices $userBill */
  1336. $userBill = app()->make(UserBillServices::class);
  1337. $uid = (int)$info['uid'];
  1338. $broken_time = intval(sys_config('extract_time'));
  1339. $search_time = time() - 86400 * $broken_time;
  1340. //改造时间
  1341. $search_time = '1970/01/01' . ' - ' . date('Y/m/d H:i:s', $search_time);
  1342. //可提现佣金
  1343. //返佣 +
  1344. $brokerage_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], $search_time);
  1345. //退款退的佣金 -
  1346. $refund_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 0], $search_time);
  1347. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  1348. if ($info['broken_commission'] < 0)
  1349. $info['broken_commission'] = 0;
  1350. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  1351. if ($info['commissionCount'] < 0)
  1352. $info['commissionCount'] = 0;
  1353. return $info;
  1354. }
  1355. /**
  1356. * 个人中心
  1357. * @param array $user
  1358. */
  1359. public function personalHome(array $user, $tokenData)
  1360. {
  1361. $userInfo = $user;
  1362. $uid = (int)$user['uid'];
  1363. /** @var StoreCouponUserServices $storeCoupon */
  1364. $storeCoupon = app()->make(StoreCouponUserServices::class);
  1365. /** @var UserBillServices $userBill */
  1366. $userBill = app()->make(UserBillServices::class);
  1367. /** @var UserExtractServices $userExtract */
  1368. $userExtract = app()->make(UserExtractServices::class);
  1369. /** @var StoreOrderServices $storeOrder */
  1370. $storeOrder = app()->make(StoreOrderServices::class);
  1371. /** @var UserLevelServices $userLevel */
  1372. $userLevel = app()->make(UserLevelServices::class);
  1373. /** @var StoreServiceServices $storeService */
  1374. $storeService = app()->make(StoreServiceServices::class);
  1375. /** @var WechatUserServices $wechatUser */
  1376. $wechatUser = app()->make(WechatUserServices::class);
  1377. /** @var UserInvoiceServices $userInvoice */
  1378. $userInvoice = app()->make(UserInvoiceServices::class);
  1379. /** @var MemberCardServices $memberCardService */
  1380. $memberCardService = app()->make(MemberCardServices::class);
  1381. /** @var StoreProductRelationServices $collect */
  1382. $collect = app()->make(StoreProductRelationServices::class);
  1383. /** @var MessageSystemServices $messageSystemServices */
  1384. $messageSystemServices = app()->make(MessageSystemServices::class);
  1385. /** @var DiyServices $diyServices */
  1386. $diyServices = app()->make(DiyServices::class);
  1387. /** @var AgentLevelServices $agentLevelServices */
  1388. $agentLevelServices = app()->make(AgentLevelServices::class);
  1389. //看付费会员是否开启
  1390. $isOpenMember = $memberCardService->isOpenMemberCard();
  1391. $user['is_open_member'] = $isOpenMember;
  1392. $user['agent_level_name'] = '';
  1393. if ($user['agent_level']) {
  1394. $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name,status,grade');
  1395. if (!$levelInfo['status']) {
  1396. $levelInfo = $agentLevelServices->get([
  1397. ['grade', '<', $levelInfo['grade']],
  1398. ['is_del', '=', 0],
  1399. ['status', '=', 1]
  1400. ], ['id', 'name', 'status', 'grade']);
  1401. }
  1402. $user['agent_level_name'] = $levelInfo && $levelInfo['name'] && $levelInfo['status'] ? $levelInfo['name'] : '';
  1403. }
  1404. //会员领取优惠券
  1405. // $couponService->sendMemberCoupon($uid);
  1406. //看是否会员过期
  1407. $this->offMemberLevel($uid, $userInfo);
  1408. $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
  1409. $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
  1410. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  1411. $user['like'] = app()->make(StoreProductRelationServices::class)->getUserCollectCount($user['uid']);
  1412. $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
  1413. $user['notice'] = 0;
  1414. /** @var UserMoneyServices $userMoney */
  1415. $userMoney = app()->make(UserMoneyServices::class);
  1416. $user['recharge'] = $userMoney->sum([
  1417. ['uid', '=', $uid], ['pm', '=', 1], ['type', 'in', ['recharge', 'system_add', 'extract']]
  1418. ], 'number');
  1419. $user['orderStatusSum'] = bcsub((string)$user['recharge'], (string)$user['now_money'], 2);
  1420. $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
  1421. $user['extractPrice'] = $user['brokerage_price'];//可提现
  1422. $user['statu'] = (int)sys_config('store_brokerage_statu');
  1423. if (!$user['is_promoter'] && $user['statu'] == 2) {
  1424. $price = $storeOrder->sum(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']], 'pay_price');
  1425. $status = is_brokerage_statu($price);
  1426. if ($status) {
  1427. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  1428. $user['is_promoter'] = 1;
  1429. } else {
  1430. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  1431. $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$price, 2);
  1432. }
  1433. }
  1434. /** @var UserBrokerageServices $frozenPrices */
  1435. $frozenPrices = app()->make(UserBrokerageServices::class);
  1436. $user['broken_commission'] = $frozenPrices->getUserFrozenPrice($uid);
  1437. if ($user['broken_commission'] < 0)
  1438. $user['broken_commission'] = 0;
  1439. $user['commissionCount'] = bcsub((string)$user['brokerage_price'], (string)$user['broken_commission'], 2);
  1440. if ($user['commissionCount'] < 0)
  1441. $user['commissionCount'] = 0;
  1442. if (!sys_config('member_func_status'))
  1443. $user['vip'] = false;
  1444. else {
  1445. $userLevel = $userLevel->getUerLevelInfoByUid($user['uid']);
  1446. $user['vip'] = (bool)$userLevel;
  1447. if ($user['vip']) {
  1448. $user['vip_id'] = $userLevel['id'] ?? 0;
  1449. $user['vip_icon'] = set_file_url($userLevel['icon']) ?? '';
  1450. $user['vip_name'] = $userLevel['name'] ?? '';
  1451. }
  1452. }
  1453. $user['yesterDay'] = $frozenPrices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
  1454. $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
  1455. $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  1456. if ($user['phone'] && $user['user_type'] != 'h5') {
  1457. $user['switchUserInfo'][] = $userInfo;
  1458. $h5UserInfo = $this->dao->getOne(['account' => $user['phone'], 'user_type' => 'h5']);
  1459. if ($h5UserInfo) {
  1460. $user['switchUserInfo'][] = $h5UserInfo;
  1461. }
  1462. } else if ($user['phone'] && $user['user_type'] == 'h5') {
  1463. $wechatUserInfo = $this->getOne([['phone', '=', $user['phone']], ['user_type', '<>', 'h5']]);
  1464. if ($wechatUserInfo) {
  1465. $user['switchUserInfo'][] = $wechatUserInfo;
  1466. }
  1467. $user['switchUserInfo'][] = $userInfo;
  1468. } else if (!$user['phone']) {
  1469. $user['switchUserInfo'][] = $userInfo;
  1470. }
  1471. $user['broken_day'] = (int)sys_config('extract_time');//佣金冻结时间
  1472. $user['balance_func_status'] = (int)sys_config('balance_func_status', 0);
  1473. $invoice_func = $userInvoice->invoiceFuncStatus();
  1474. $user['invioce_func'] = $invoice_func['invoice_func'];
  1475. $user['special_invoice'] = $invoice_func['special_invoice'];
  1476. $user['collectCount'] = $collect->count(['uid' => $uid]);
  1477. $user['spread_status'] = $this->checkUserPromoter($user['uid']);
  1478. $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
  1479. $user['member_style'] = (int)$diyServices->getColorChange('member');
  1480. if ($user['is_ever_level']) {
  1481. $user['vip_status'] = 1;//永久会员
  1482. } else {
  1483. if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
  1484. $user['vip_status'] = -1;//开通过已过期
  1485. } else if (!$user['overdue_time'] && !$user['is_money_level']) {
  1486. $user['vip_status'] = 2;//没有开通过
  1487. } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
  1488. $user['vip_status'] = 3;//开通了,没有到期
  1489. }
  1490. }
  1491. $user['svip_open'] = (bool)sys_config('member_card_status');
  1492. /** @var StoreServiceRecordServices $servicesRecord */
  1493. $servicesRecord = app()->make(StoreServiceRecordServices::class);
  1494. $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
  1495. $message = $messageSystemServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  1496. $user['service_num'] = $service_num + $message;
  1497. /** @var AgentLevelServices $userSpread */
  1498. $agentLevel = app()->make(AgentLevelServices::class);
  1499. $user['spread_level_count'] = $agentLevel->count(['status' => 1, 'is_del' => 0]);
  1500. $user['extract_type'] = sys_config('extract_type');
  1501. $user['integral'] = intval($user['integral']);
  1502. $user['is_agent_level'] = $agentLevelServices->count(['status' => 1, 'is_del' => 0]) > 0 ? 1 : 0;
  1503. $user['division_open'] = (int)sys_config('division_status', 0);
  1504. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  1505. $user['avatar'] = strpos($user['avatar'], '/statics/system_images/') !== false ? set_file_url($user['avatar']) : $user['avatar'];
  1506. return $user;
  1507. }
  1508. /**
  1509. * 用户资金统计
  1510. * @param int $uid
  1511. */
  1512. public function balance(int $uid)
  1513. {
  1514. $userInfo = $this->getUserInfo($uid);
  1515. if (!$userInfo) {
  1516. throw new AdminException(400214);
  1517. }
  1518. /** @var UserBillServices $userBill */
  1519. $userBill = app()->make(UserBillServices::class);
  1520. /** @var StoreOrderServices $storeOrder */
  1521. $storeOrder = app()->make(StoreOrderServices::class);
  1522. $user['now_money'] = $userInfo['now_money'];//当前总资金
  1523. $user['recharge'] = $userBill->getRechargeSum($uid);//累计充值
  1524. $user['orderStatusSum'] = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1525. return $user;
  1526. }
  1527. /**
  1528. * 用户修改信息
  1529. * @param Request $request
  1530. * @return mixed
  1531. */
  1532. public function eidtNickname(int $uid, array $data)
  1533. {
  1534. if (!$this->dao->count(['uid' => $uid])) {
  1535. throw new ApiException(400214);
  1536. }
  1537. if (!$this->dao->update($uid, $data, 'uid')) {
  1538. throw new ApiException(100007);
  1539. }
  1540. return true;
  1541. }
  1542. /**
  1543. * 获取推广人排行
  1544. * @param $data 查询条件
  1545. * @return array
  1546. * @throws \think\db\exception\DataNotFoundException
  1547. * @throws \think\db\exception\ModelNotFoundException
  1548. * @throws \think\exception\DbException
  1549. */
  1550. public function getRankList(array $data)
  1551. {
  1552. $startTime = strtotime('this week Monday');
  1553. $endTime = time();
  1554. switch ($data['type']) {
  1555. case 'week':
  1556. $startTime = strtotime('this week Monday');
  1557. break;
  1558. case 'month':
  1559. $startTime = strtotime('last month');
  1560. break;
  1561. }
  1562. [$page, $limit] = $this->getPageValue();
  1563. $field = 't0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar';
  1564. return $this->dao->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  1565. }
  1566. /**
  1567. * 静默绑定推广人
  1568. * @param int $uid
  1569. * @param int $spreadUid
  1570. * @param $code
  1571. * @return bool
  1572. * @throws \think\db\exception\DataNotFoundException
  1573. * @throws \think\db\exception\DbException
  1574. * @throws \think\db\exception\ModelNotFoundException
  1575. */
  1576. public function spread(int $uid, int $spreadUid, $code)
  1577. {
  1578. $userInfo = $this->dao->getOne(['uid' => $uid], 'uid,spread_uid,spread_time,add_time,last_time');
  1579. if (!$userInfo) {
  1580. throw new ApiException(100026);
  1581. }
  1582. if ($code && !$spreadUid) {
  1583. /** @var QrcodeServices $qrCode */
  1584. $qrCode = app()->make(QrcodeServices::class);
  1585. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  1586. $spreadUid = $info['third_id'];
  1587. }
  1588. }
  1589. if ($spreadUid == 0) return '不绑定';
  1590. $userSpreadUid = $this->dao->value(['uid' => $spreadUid], 'spread_uid');
  1591. //记录好友关系
  1592. if ($spreadUid && $uid && $spreadUid != $uid) {
  1593. /** @var UserFriendsServices $serviceFriend */
  1594. $serviceFriend = app()->make(UserFriendsServices::class);
  1595. $serviceFriend->saveFriend([
  1596. 'uid' => $uid,
  1597. 'friends_uid' => $spreadUid,
  1598. ]);
  1599. }
  1600. $check = false;
  1601. if (sys_config('brokerage_bindind') == 1) {
  1602. if (sys_config('store_brokerage_binding_status') == 1) {
  1603. if (!$userInfo['spread_uid']) {
  1604. $check = true;
  1605. }
  1606. } elseif (sys_config('store_brokerage_binding_status') == 2 && (($userInfo['spread_time'] + (sys_config('store_brokerage_binding_time') * 86400)) < time())) {
  1607. $check = true;
  1608. } elseif (sys_config('store_brokerage_binding_status') == 3) {
  1609. $check = true;
  1610. }
  1611. } elseif (sys_config('brokerage_bindind') == 2) {
  1612. if ($userInfo['add_time'] == $userInfo['last_time']) {
  1613. $check = true;
  1614. }
  1615. }
  1616. if ($userInfo['uid'] == $spreadUid || $userInfo['uid'] == $userSpreadUid) $check = false;
  1617. if ($check) {
  1618. $spreadInfo = $this->dao->get($spreadUid, ['division_id', 'agent_id', 'staff_id']);
  1619. $data = [];
  1620. $data['spread_uid'] = $spreadUid;
  1621. $data['spread_time'] = time();
  1622. $data['division_id'] = $spreadInfo['division_id'];
  1623. $data['agent_id'] = $spreadInfo['agent_id'];
  1624. $data['staff_id'] = $spreadInfo['staff_id'];
  1625. if (!$this->dao->update($uid, $data, 'uid')) {
  1626. throw new ApiException(410288);
  1627. }
  1628. return '绑定上级成功,上级uid为' . $spreadUid;
  1629. } else {
  1630. return '不绑定';
  1631. }
  1632. }
  1633. /**
  1634. * 添加访问记录
  1635. * @param Request $request
  1636. * @return mixed
  1637. */
  1638. public function setVisit(array $data)
  1639. {
  1640. $userInfo = $this->getUserInfo($data['uid'], 'uid,user_type');
  1641. if (!$userInfo) {
  1642. throw new ApiException(100026);
  1643. }
  1644. $data['channel_type'] = $userInfo['user_type'];
  1645. $data['add_time'] = time();
  1646. /** @var WechatUserServices $wechatUserServices */
  1647. $wechatUserServices = app()->make(WechatUserServices::class);
  1648. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid'], 'user_type' => $userInfo['user_type']]);
  1649. if (!$wechatUser) {
  1650. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid']]);
  1651. }
  1652. if ($wechatUser) {
  1653. $data['province'] = $wechatUser['province'];
  1654. }
  1655. /** @var UserVisitServices $userVisit */
  1656. $userVisit = app()->make(UserVisitServices::class);
  1657. if ($userVisit->save($data)) {
  1658. return true;
  1659. } else {
  1660. throw new ApiException(100015);
  1661. }
  1662. }
  1663. /**
  1664. * 获取活动状态
  1665. * @return mixed
  1666. */
  1667. public function activity()
  1668. {
  1669. /** @var StoreBargainServices $storeBragain */
  1670. $storeBragain = app()->make(StoreBargainServices::class);
  1671. /** @var StoreCombinationServices $storeCombinaion */
  1672. $storeCombinaion = app()->make(StoreCombinationServices::class);
  1673. /** @var StoreSeckillServices $storeSeckill */
  1674. $storeSeckill = app()->make(StoreSeckillServices::class);
  1675. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  1676. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  1677. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  1678. return $data;
  1679. }
  1680. /**
  1681. * 获取用户下级推广人
  1682. * @param int $uid 当前用户
  1683. * @param int $grade 等级 0 一级 1 二级
  1684. * @param string $orderBy 排序
  1685. * @param string $keyword
  1686. * @return array|bool
  1687. */
  1688. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '')
  1689. {
  1690. $user = $this->getUserInfo($uid);
  1691. if (!$user) {
  1692. throw new AdminException(400214);
  1693. }
  1694. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  1695. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  1696. $data = [
  1697. 'total' => count($spread_one_ids),
  1698. 'totalLevel' => count($spread_two_ids),
  1699. 'list' => []
  1700. ];
  1701. if (sys_config('brokerage_level', 2) == 1) {
  1702. $data['count'] = $data['total'];
  1703. } else {
  1704. $data['count'] = $data['total'] + $data['totalLevel'];
  1705. }
  1706. /** @var UserStoreOrderServices $userStoreOrder */
  1707. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  1708. $list = [];
  1709. if ($grade == 0) {
  1710. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword);
  1711. } else {
  1712. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword);
  1713. }
  1714. foreach ($list as &$item) {
  1715. if (isset($item['spread_time']) && $item['spread_time']) {
  1716. $item['time'] = date('Y/m/d', $item['spread_time']);
  1717. }
  1718. }
  1719. $data['list'] = $list;
  1720. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  1721. return $data;
  1722. }
  1723. /**
  1724. * 获取推广人uids
  1725. * @param int $uid
  1726. * @param bool $one
  1727. * @return array
  1728. */
  1729. public function getUserSpredadUids(int $uid, int $type = 0)
  1730. {
  1731. $uids = $this->dao->getColumn(['spread_uid' => $uid, 'is_del' => 0], 'uid');
  1732. if ($type === 1) {
  1733. return $uids;
  1734. }
  1735. if ($uids) {
  1736. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids], ['is_del', '=', 0]], 'uid');
  1737. if ($type === 2) {
  1738. return $uidsTwo;
  1739. }
  1740. if ($uidsTwo) {
  1741. $uids = array_merge($uids, $uidsTwo);
  1742. }
  1743. }
  1744. return $uids;
  1745. }
  1746. /**
  1747. * 检测用户是否是推广员
  1748. * @param int $uid
  1749. * @param $user
  1750. * @return bool
  1751. */
  1752. public function checkUserPromoter(int $uid, $user = [])
  1753. {
  1754. if (!$user) {
  1755. $user = $this->getUserInfo($uid, 'spread_open,is_promoter');
  1756. }
  1757. if (!$user) {
  1758. return false;
  1759. }
  1760. //分销是否开启
  1761. if (!sys_config('brokerage_func_status')) {
  1762. return false;
  1763. }
  1764. //用户分校推广资格是否开启4.0.32
  1765. if (isset($user['spread_open']) && !$user['spread_open']) {
  1766. return false;
  1767. }
  1768. /** @var StoreOrderServices $storeOrder */
  1769. $storeOrder = app()->make(StoreOrderServices::class);
  1770. $sumPrice = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1771. $store_brokerage_statu = sys_config('store_brokerage_statu');
  1772. $store_brokerage_price = sys_config('store_brokerage_price');
  1773. if ($user['is_promoter'] || $store_brokerage_statu == 2 || ($store_brokerage_statu == 3 && $sumPrice > $store_brokerage_price)) {
  1774. return true;
  1775. }
  1776. return false;
  1777. }
  1778. /**
  1779. * 同步微信粉丝用户(后台接口)
  1780. * @return bool
  1781. */
  1782. public function syncWechatUsers()
  1783. {
  1784. $appid = sys_config('wechat_appid');
  1785. $appSecret = sys_config('wechat_appsecret');
  1786. if (!$appid || !$appSecret) {
  1787. throw new AdminException(400236);
  1788. }
  1789. $key = md5('sync_wechat_users');
  1790. //一天点击一次
  1791. if (CacheService::get($key)) {
  1792. return true;
  1793. }
  1794. $next_openid = null;
  1795. do {
  1796. $result = WechatService::getUsersList($next_openid);
  1797. $userOpenids = $result['data'];
  1798. //拆分大数组
  1799. $opemidArr = array_chunk($userOpenids, 100);
  1800. foreach ($opemidArr as $openids) {
  1801. //加入同步|更新用户队列
  1802. UserJob::dispatch([$openids]);
  1803. }
  1804. $next_openid = $result['next_openid'];
  1805. } while ($next_openid != null);
  1806. CacheService::set($key, 1, 3600 * 24);
  1807. return true;
  1808. }
  1809. /**
  1810. * 导入微信粉丝用户
  1811. * @param array $openids
  1812. * @return bool
  1813. */
  1814. public function importUser(array $noBeOpenids)
  1815. {
  1816. if (!$noBeOpenids) {
  1817. return true;
  1818. }
  1819. $dataAll = $data = [];
  1820. $time = time();
  1821. foreach ($noBeOpenids as $openid) {
  1822. try {
  1823. $info = WechatService::getUserInfo($openid);
  1824. $info = is_object($info) ? $info->toArray() : $info;
  1825. } catch (\Throwable $e) {
  1826. $info = [];
  1827. }
  1828. if (!$info) continue;
  1829. $data['nickname'] = $info['nickname'] ?? '';
  1830. $data['headimgurl'] = $info['headimgurl'] ?? '';
  1831. $userInfoData = $this->setUserInfo($data);
  1832. if (!$userInfoData) {
  1833. throw new AdminException(400703);
  1834. }
  1835. $data['uid'] = $userInfoData['uid'];
  1836. $data['subscribe'] = $info['subscribe'] ?? 1;
  1837. $data['unionid'] = $info['unionid'] ?? '';
  1838. $data['openid'] = $info['openid'] ?? '';
  1839. $data['sex'] = $info['sex'] ?? 0;
  1840. $data['language'] = $info['language'] ?? '';
  1841. $data['city'] = $info['city'] ?? '';
  1842. $data['province'] = $info['province'] ?? '';
  1843. $data['country'] = $info['country'] ?? '';
  1844. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  1845. $data['groupid'] = $info['groupid'] ?? 0;
  1846. $data['remark'] = $info['remark'] ?? '';
  1847. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  1848. $data['add_time'] = $time;
  1849. $data['is_complete'] = 1;
  1850. $dataAll[] = $data;
  1851. }
  1852. if ($dataAll) {
  1853. /** @var WechatUserServices $wechatUser */
  1854. $wechatUser = app()->make(WechatUserServices::class);
  1855. if (!$wechatUser->saveAll($dataAll)) {
  1856. throw new AdminException(400703);
  1857. }
  1858. }
  1859. return true;
  1860. }
  1861. /** 修改会员的时间及是否会员状态
  1862. * @param int $vip_day 会员天数
  1863. * @param array $user_id 用户id
  1864. * @param int $is_money_level 会员来源途径
  1865. * @param bool $member_type 会员卡类型
  1866. * @return mixed
  1867. * @throws \think\db\exception\DataNotFoundException
  1868. * @throws \think\db\exception\DbException
  1869. * @throws \think\db\exception\ModelNotFoundException
  1870. */
  1871. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  1872. {
  1873. if ($vip_day == 0) throw new ApiException(410289);
  1874. $user_info = $this->getUserInfo($user_id, 'is_money_level,overdue_time');
  1875. if (!$user_info) throw new ApiException(410032);
  1876. if (!$member_type) $member_type = "month";
  1877. if ($member_type == 'ever') {
  1878. $overdue_time = 0;
  1879. $is_ever_level = 1;
  1880. } else {
  1881. if ($user_info['is_money_level'] == 0) {
  1882. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  1883. } else {
  1884. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  1885. }
  1886. $is_ever_level = 0;
  1887. }
  1888. $setData['overdue_time'] = $overdue_time;
  1889. $setData['is_ever_level'] = $is_ever_level;
  1890. $setData['is_money_level'] = $is_money_level ?: 0;
  1891. // if ($user_info['level'] == 0) $setData['level'] = 1;
  1892. return $this->dao->update(['uid' => $user_id], $setData);
  1893. }
  1894. /**
  1895. * 会员过期改变状态,变为普通会员
  1896. * @param $uid
  1897. * @param $userInfo
  1898. * @return bool
  1899. * @throws \think\db\exception\DataNotFoundException
  1900. * @throws \think\db\exception\DbException
  1901. * @throws \think\db\exception\ModelNotFoundException
  1902. */
  1903. public function offMemberLevel($uid, $userInfo = [])
  1904. {
  1905. if (!$uid) return false;
  1906. if (!$userInfo) {
  1907. $userInfo = $this->dao->get($uid, ['is_ever_level', 'is_money_level', 'overdue_time']);
  1908. }
  1909. if (!$userInfo) return false;
  1910. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  1911. $this->dao->update(['uid' => $uid], ['is_money_level' => 0/*, 'overdue_time' => 0*/]);
  1912. return false;
  1913. }
  1914. return true;
  1915. }
  1916. /**
  1917. * @param array $where
  1918. * @return array
  1919. * @throws \think\db\exception\DataNotFoundException
  1920. * @throws \think\db\exception\DbException
  1921. * @throws \think\db\exception\ModelNotFoundException
  1922. */
  1923. public function getUserInfoList(array $where, $field = "*")
  1924. {
  1925. return $this->dao->getUserInfoList($where, $field);
  1926. }
  1927. /**
  1928. * 增加推广用户佣金
  1929. * @param int $uid
  1930. * @param int $spread_uid
  1931. * @param array $userInfo
  1932. * @param array $spread_user
  1933. * @return bool|mixed
  1934. * @throws \think\db\exception\DataNotFoundException
  1935. * @throws \think\db\exception\DbException
  1936. * @throws \think\db\exception\ModelNotFoundException
  1937. */
  1938. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  1939. {
  1940. if (!$uid || !$spread_uid) {
  1941. return false;
  1942. }
  1943. //商城分销功能是否开启 0关闭1开启
  1944. if (!sys_config('brokerage_func_status')) return true;
  1945. if (!sys_config('brokerage_user_status')) return true;
  1946. //获取设置推广佣金单价
  1947. $brokerage_price = sys_config('uni_brokerage_price', 0);
  1948. //获取推广佣金当日限额
  1949. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  1950. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  1951. return true;
  1952. }
  1953. if (!$userInfo) {
  1954. $userInfo = $this->getUserInfo($uid);
  1955. }
  1956. if (!$userInfo) {
  1957. return false;
  1958. }
  1959. if (!$spread_user) {
  1960. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  1961. }
  1962. if (!$spread_user) {
  1963. return false;
  1964. }
  1965. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  1966. return false;
  1967. }
  1968. /** @var UserBrokerageServices $userBrokerageServices */
  1969. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1970. // -1不限制
  1971. if ($day_brokerage_price_upper != -1) {
  1972. if ($day_brokerage_price_upper <= 0) {
  1973. return true;
  1974. } else {
  1975. //获取上级用户今日获取推广用户佣金
  1976. $spread_day_brokerage = $userBrokerageServices->getUserBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  1977. //超过上限
  1978. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  1979. return true;
  1980. }
  1981. }
  1982. }
  1983. $spreadPrice = $spread_user['brokerage_price'];
  1984. // 上级推广员返佣之后的金额
  1985. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  1986. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  1987. // 添加返佣记录
  1988. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  1989. 'nickname' => $userInfo['nickname'],
  1990. 'number' => floatval($brokerage_price)
  1991. ], $balance, $uid);
  1992. // 添加用户余额
  1993. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  1994. //给上级发送获得佣金的模板消息
  1995. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  1996. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  1997. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  1998. return $res1 && $res2;
  1999. });
  2000. }
  2001. /**
  2002. * 获取上级uid
  2003. * @param int $uid
  2004. * @param array $userInfo
  2005. * @param bool $is_spread
  2006. * @return int|mixed
  2007. */
  2008. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  2009. {
  2010. if (!$uid) {
  2011. return 0;
  2012. }
  2013. //商城分销功能是否开启 0关闭1开启
  2014. if (!sys_config('brokerage_func_status')) return -1;
  2015. if (!$userInfo) {
  2016. $userInfo = $this->getUserInfo($uid);
  2017. }
  2018. if (!$userInfo) {
  2019. return 0;
  2020. }
  2021. //上级的上级不需要检测自购
  2022. if ($is_spread) {
  2023. //开启自购
  2024. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  2025. if ($is_self_brokerage && $is_spread) {
  2026. return $uid;
  2027. }
  2028. }
  2029. //绑定类型
  2030. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  2031. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  2032. return $userInfo['spread_uid'];
  2033. }
  2034. //分销绑定类型为时间段且没过期
  2035. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  2036. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  2037. return $userInfo['spread_uid'];
  2038. }
  2039. return -1;
  2040. }
  2041. /**
  2042. * 获取事业部/代理/员工列表
  2043. * @param array $where
  2044. * @param string $field
  2045. * @return array
  2046. * @throws \think\db\exception\DataNotFoundException
  2047. * @throws \think\db\exception\DbException
  2048. * @throws \think\db\exception\ModelNotFoundException
  2049. */
  2050. public function getDivisionList(array $where = [], string $field = '*')
  2051. {
  2052. [$page, $limit] = $this->getPageValue();
  2053. $list = $this->dao->getList($where, $field, $page, $limit);
  2054. $count = $this->dao->count($where);
  2055. return compact('list', 'count');
  2056. }
  2057. /**
  2058. * 添加编辑用户信息时候的信息
  2059. * @param $uid
  2060. * @return array
  2061. * @throws \think\db\exception\DataNotFoundException
  2062. * @throws \think\db\exception\DbException
  2063. * @throws \think\db\exception\ModelNotFoundException
  2064. */
  2065. public function getUserSaveInfo($uid)
  2066. {
  2067. /** @var UserLabelServices $userLabelServices */
  2068. $userLabelServices = app()->make(UserLabelServices::class);
  2069. /** @var UserLabelRelationServices $userLabelRelationServices */
  2070. $userLabelRelationServices = app()->make(UserLabelRelationServices::class);
  2071. /** @var UserLabelCateServices $userLabelCateServices */
  2072. $userLabelCateServices = app()->make(UserLabelCateServices::class);
  2073. /** @var UserGroupServices $userGroupServices */
  2074. $userGroupServices = app()->make(UserGroupServices::class);
  2075. /** @var SystemUserLevelServices $systemUserLevelServices */
  2076. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  2077. $userInfo = $this->dao->get($uid);
  2078. if ($userInfo) {
  2079. $label_ids = $userLabelRelationServices->getUserLabels($uid);
  2080. $userInfo['label_id'] = !empty($label_ids) ? $userLabelServices->getLabelList(['ids' => $label_ids], ['id', 'label_name']) : [];
  2081. $userInfo['birthday'] = date('Y-m-d', (int)$userInfo['birthday']);
  2082. }
  2083. $levelInfo = $systemUserLevelServices->getWhereLevelList([], 'id,name');
  2084. $groupInfo = $userGroupServices->getGroupList();
  2085. $labelInfo = $userLabelCateServices->getUserLabel($uid);
  2086. return compact('userInfo', 'levelInfo', 'groupInfo', 'labelInfo');
  2087. }
  2088. /**
  2089. * 新用户注册奖励
  2090. * @param int $id
  2091. * @return bool
  2092. * @throws Exception
  2093. *
  2094. * @date 2022/09/28
  2095. * @author yyw
  2096. */
  2097. public function rewardNewUser(int $id)
  2098. {
  2099. $user = $this->getUserInfo($id);
  2100. if (!$user) {
  2101. throw new AdminException(100026);
  2102. }
  2103. $res1 = false;
  2104. $res2 = false;
  2105. $reward_money = sys_config('reward_money');
  2106. $reward_integral = sys_config('reward_integral');
  2107. $edit = array();
  2108. if ($reward_money > 0) {//余额增加
  2109. /** @var UserMoneyServices $userMoneyServices */
  2110. $userMoneyServices = app()->make(UserMoneyServices::class);
  2111. $edit['now_money'] = bcadd($user['now_money'], $reward_money, 2);
  2112. $res1 = $userMoneyServices->income('register_system_add', $user['uid'], $reward_money, $edit['now_money'], 1);
  2113. } else {
  2114. $res1 = true;
  2115. }
  2116. if ($reward_integral > 0) {//积分增加
  2117. /** @var UserBillServices $userBill */
  2118. $userBill = app()->make(UserBillServices::class);
  2119. $integral_data = ['link_id' => 1, 'number' => $reward_integral];
  2120. $edit['integral'] = bcadd($user['integral'], $reward_integral, 2);
  2121. $integral_data['balance'] = $edit['integral'];
  2122. $integral_data['title'] = '新用户注册增加积分';
  2123. $integral_data['mark'] = '新用户注册增加了' . floatval($reward_integral) . '积分';
  2124. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  2125. } else {
  2126. $res2 = true;
  2127. }
  2128. if ($edit) $res3 = $this->dao->update($id, $edit);
  2129. else $res3 = true;
  2130. if ($res1 && $res2 && $res3)
  2131. return true;
  2132. else throw new AdminException(100007);
  2133. }
  2134. /**
  2135. * 推送用户信息
  2136. * @param $data
  2137. * @param $pushUrl
  2138. * @return bool
  2139. */
  2140. public function userUpdate($data, $pushUrl)
  2141. {
  2142. return out_push($pushUrl, $data, '更新用户信息');
  2143. }
  2144. }