UserServices.php 87 KB

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