UserServices.php 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  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. * @return array[]
  516. */
  517. public function typeHead()
  518. {
  519. //全部会员
  520. $all = $this->getCount([]);
  521. /** @var UserWechatuserServices $userWechatUser */
  522. $userWechatUser = app()->make(UserWechatuserServices::class);
  523. //小程序会员
  524. $routine = $userWechatUser->getCount([['w.user_type', '=', 'routine']]);
  525. //公众号会员
  526. $wechat = $userWechatUser->getCount([['w.user_type', '=', 'wechat']]);
  527. //H5会员
  528. $h5 = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'h5']);
  529. //pc会员
  530. $pc = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'pc']);
  531. return [
  532. ['user_type' => '', 'name' => '全部会员', 'count' => $all],
  533. ['user_type' => 'routine', 'name' => '小程序会员', 'count' => $routine],
  534. ['user_type' => 'wechat', 'name' => '公众号会员', 'count' => $wechat],
  535. ['user_type' => 'h5', 'name' => 'H5会员', 'count' => $h5],
  536. ['user_type' => 'pc', 'name' => 'PC会员', 'count' => $pc],
  537. ];
  538. }
  539. /**
  540. * 会员列表
  541. * @param array $where
  542. * @return array
  543. */
  544. public function index(array $where)
  545. {
  546. /** @var UserWechatuserServices $userWechatUser */
  547. $userWechatUser = app()->make(UserWechatuserServices::class);
  548. $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';
  549. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  550. if ($list) {
  551. $uids = array_column($list, 'uid');
  552. $userlabel = $this->getUserLablel($uids);
  553. $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
  554. $userExtract = app()->make(UserExtractServices::class)->getUsersSumList($uids);
  555. $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
  556. $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
  557. $spread_names = $this->dao->getColumn([['uid', 'in', array_unique(array_column($list, 'spread_uid'))]], 'nickname', 'uid');
  558. foreach ($list as &$item) {
  559. if (empty($item['addres'])) {
  560. if (!empty($item['country']) || !empty($item['province']) || !empty($item['city'])) {
  561. $item['addres'] = $item['country'] . $item['province'] . $item['city'];
  562. }
  563. }
  564. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  565. $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
  566. $item['extract_count_price'] = $userExtract[$item['uid']] ?? 0;//累计提现
  567. $item['spread_uid_nickname'] = $item['spread_uid'] ? ($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid'] : '无';
  568. //用户类型
  569. if ($item['user_type'] == 'routine') {
  570. $item['user_type'] = '小程序';
  571. } else if ($item['user_type'] == 'wechat') {
  572. $item['user_type'] = '公众号';
  573. } else if ($item['user_type'] == 'h5') {
  574. $item['user_type'] = 'H5';
  575. } else if ($item['user_type'] == 'pc') {
  576. $item['user_type'] = 'PC';
  577. } else if ($item['user_type'] == 'app' || $item['user_type'] == 'apple') {
  578. $item['user_type'] = 'APP';
  579. } else $item['user_type'] = '其他';
  580. if ($item['sex'] == 1) {
  581. $item['sex'] = '男';
  582. } else if ($item['sex'] == 2) {
  583. $item['sex'] = '女';
  584. } else $item['sex'] = '保密';
  585. //等级名称
  586. $item['level'] = $levelName[$item['level']] ?? '无';
  587. //分组名称
  588. $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
  589. //用户等级
  590. $item['vip_name'] = false;
  591. $levelinfo = $userLevel[$item['uid']] ?? null;
  592. if ($levelinfo) {
  593. if ($levelinfo && ($levelinfo['is_forever'] || time() < $levelinfo['valid_time'])) {
  594. $item['vip_name'] = $item['level'] != '无' ? $item['level'] : false;
  595. }
  596. }
  597. $item['labels'] = $userlabel[$item['uid']] ?? '';
  598. $item['isMember'] = $item['is_money_level'] > 0 ? 1 : 0;
  599. if (strpos($item['avatar'], '/statics/system_images/') !== false) {
  600. $item['avatar'] = set_file_url($item['avatar']);
  601. }
  602. }
  603. }
  604. return compact('count', 'list');
  605. }
  606. /**
  607. * 获取修改页面数据
  608. * @param int $id
  609. * @return array
  610. * @throws \FormBuilder\Exception\FormBuilderException
  611. */
  612. public function edit(int $id)
  613. {
  614. $user = $this->getUserInfo($id);
  615. if (!$user)
  616. throw new AdminException(100026);
  617. $f = array();
  618. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(true);
  619. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
  620. $f[] = Form::input('phone', '手机号码', $user->getData('phone'));
  621. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : '');
  622. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
  623. $f[] = Form::input('addres', '用户地址', $user->getData('addres'));
  624. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  625. $f[] = Form::input('pwd', '登录密码')->type('password')->placeholder('不改密码请留空');
  626. $f[] = Form::input('true_pwd', '确认密码')->type('password')->placeholder('不改密码请留空');
  627. //查询高于当前会员的所有会员等级
  628. // $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  629. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  630. $setOptionLevel = function () use ($systemLevelList) {
  631. $menus = [];
  632. foreach ($systemLevelList as $menu) {
  633. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  634. }
  635. return $menus;
  636. };
  637. $f[] = Form::select('level', '用户等级', (int)$user->getData('level'))->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  638. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  639. $setOptionGroup = function () use ($systemGroupList) {
  640. $menus = [];
  641. foreach ($systemGroupList as $menu) {
  642. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  643. }
  644. return $menus;
  645. };
  646. $f[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  647. $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
  648. $labels = app()->make(UserLabelRelationServices::class)->getUserLabels($user['uid']);
  649. $setOptionLabel = function () use ($systemLabelList) {
  650. $menus = [];
  651. foreach ($systemLabelList as $menu) {
  652. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  653. }
  654. return $menus;
  655. };
  656. $f[] = Form::select('label_id', '用户标签', $labels)->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  657. $f[] = Form::radio('spread_open', '推广资格', $user->getData('spread_open'))->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  658. //分销模式 人人分销
  659. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  660. if ($storeBrokerageStatus == 1) {
  661. $f[] = Form::radio('is_promoter', '推广员权限', $user->getData('is_promoter'))->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  662. }
  663. $f[] = Form::radio('status', '用户状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  664. return create_form('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
  665. }
  666. /**
  667. * 添加用户表单
  668. * @param int $id
  669. * @return array
  670. * @throws \FormBuilder\Exception\FormBuilderException
  671. */
  672. public function saveForm()
  673. {
  674. $f = array();
  675. $f[] = Form::input('real_name', '真实姓名', '')->placeholder('请输入真实姓名');
  676. $f[] = Form::input('phone', '手机号码', '')->placeholder('请输入手机号码')->required();
  677. $f[] = Form::date('birthday', '生日', '')->placeholder('请选择生日');
  678. $f[] = Form::input('card_id', '身份证号', '')->placeholder('请输入身份证号');
  679. $f[] = Form::input('addres', '用户地址', '')->placeholder('请输入用户地址');
  680. $f[] = Form::textarea('mark', '用户备注', '')->placeholder('请输入用户备注');
  681. $f[] = Form::input('pwd', '登录密码')->type('password')->placeholder('请输入登录密码');
  682. $f[] = Form::input('true_pwd', '确认密码')->type('password')->placeholder('请再次确认密码');
  683. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  684. $setOptionLevel = function () use ($systemLevelList) {
  685. $menus = [];
  686. foreach ($systemLevelList as $menu) {
  687. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  688. }
  689. return $menus;
  690. };
  691. $f[] = Form::select('level', '用户等级', '')->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  692. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  693. $setOptionGroup = function () use ($systemGroupList) {
  694. $menus = [];
  695. foreach ($systemGroupList as $menu) {
  696. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  697. }
  698. return $menus;
  699. };
  700. $f[] = Form::select('group_id', '用户分组', '')->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  701. $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
  702. $setOptionLabel = function () use ($systemLabelList) {
  703. $menus = [];
  704. foreach ($systemLabelList as $menu) {
  705. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  706. }
  707. return $menus;
  708. };
  709. $f[] = Form::select('label_id', '用户标签', '')->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  710. $f[] = Form::radio('spread_open', '推广资格', 1)->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  711. //分销模式 人人分销
  712. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  713. if ($storeBrokerageStatus == 1) {
  714. $f[] = Form::radio('is_promoter', '推广员权限', 0)->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  715. }
  716. $f[] = Form::radio('status', '用户状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  717. return create_form('添加用户', $f, $this->url('/user/user'), 'POST');
  718. }
  719. /**
  720. * 修改提交处理
  721. * @param int $id
  722. * @param array $data
  723. * @return bool
  724. * @throws Exception
  725. * @throws \think\db\exception\DataNotFoundException
  726. * @throws \think\db\exception\ModelNotFoundException
  727. */
  728. public function updateInfo(int $id, array $data)
  729. {
  730. $user = $this->getUserInfo($id);
  731. if (!$user) {
  732. throw new AdminException(100026);
  733. }
  734. $res1 = false;
  735. $res2 = false;
  736. $edit = array();
  737. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  738. /** @var UserMoneyServices $userMoneyServices */
  739. $userMoneyServices = app()->make(UserMoneyServices::class);
  740. if ($data['money_status'] == 1) {//增加
  741. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  742. $res1 = $userMoneyServices->income('system_add', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  743. } else if ($data['money_status'] == 2) {//减少
  744. if ($user['now_money'] > $data['money']) {
  745. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  746. } else {
  747. $edit['now_money'] = 0;
  748. $data['money'] = $user['now_money'];
  749. }
  750. $res1 = $userMoneyServices->income('system_sub', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  751. }
  752. event('out.outPush', ['user_update_push', ['uid' => $id, 'type' => 'money', 'value' => $data['money_status'] == 2 ? -floatval($data['money']) : $data['money']]]);
  753. } else {
  754. $res1 = true;
  755. }
  756. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  757. /** @var UserBillServices $userBill */
  758. $userBill = app()->make(UserBillServices::class);
  759. $integral_data = ['link_id' => $data['adminId'] ?? 0, 'number' => $data['integration']];
  760. if ($data['integration_status'] == 1) {//增加
  761. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  762. $integral_data['balance'] = $edit['integral'];
  763. $integral_data['title'] = '系统增加积分';
  764. $integral_data['mark'] = '系统增加了' . floatval($data['integration']) . '积分';
  765. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  766. } else if ($data['integration_status'] == 2) {//减少
  767. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  768. $integral_data['balance'] = $edit['integral'];
  769. $integral_data['title'] = '系统减少积分';
  770. $integral_data['mark'] = '系统扣除了' . floatval($data['integration']) . '积分';
  771. $res2 = $userBill->expendIntegral($user['uid'], 'system_sub', $integral_data);
  772. }
  773. event('out.outPush', ['user_update_push', ['uid' => $id, 'type' => 'point', 'value' => $data['integration_status'] == 2 ? -intval($data['integration']) : $data['integration']]]);
  774. } else {
  775. $res2 = true;
  776. }
  777. //修改基本信息
  778. if (!isset($data['is_other']) || !$data['is_other']) {
  779. app()->make(UserLabelRelationServices::class)->setUserLable([$id], $data['label_id']);
  780. if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
  781. $edit['pwd'] = $data['pwd'];
  782. }
  783. if (isset($data['spread_open'])) {
  784. $edit['spread_open'] = $data['spread_open'];
  785. }
  786. $edit['status'] = $data['status'];
  787. $edit['real_name'] = $data['real_name'];
  788. $edit['card_id'] = $data['card_id'];
  789. $edit['birthday'] = strtotime($data['birthday']);
  790. $edit['mark'] = $data['mark'];
  791. $edit['is_promoter'] = $data['is_promoter'];
  792. $edit['level'] = $data['level'];
  793. $edit['phone'] = $data['phone'];
  794. $edit['addres'] = $data['addres'];
  795. $edit['group_id'] = $data['group_id'];
  796. if ($user['level'] != $data['level']) {
  797. /** @var UserLevelServices $userLevelService */
  798. $userLevelService = app()->make(UserLevelServices::class);
  799. $userLevelService->setUserLevel((int)$user['uid'], (int)$data['level']);
  800. }
  801. }
  802. if ($edit) $res3 = $this->dao->update($id, $edit);
  803. else $res3 = true;
  804. if ($res1 && $res2 && $res3)
  805. return true;
  806. else throw new AdminException(100007);
  807. }
  808. /**
  809. * 编辑其他
  810. * @param $id
  811. * @return mixed
  812. * @throws \FormBuilder\Exception\FormBuilderException
  813. */
  814. public function editOther($id)
  815. {
  816. $user = $this->getUserInfo($id);
  817. if (!$user) {
  818. throw new AdminException(100026);
  819. }
  820. $f = array();
  821. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  822. $f[] = Form::number('money', '余额', 0)->min(0)->max(999999.99);
  823. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  824. $f[] = Form::number('integration', '积分', 0)->min(0)->precision(0)->max(999999);
  825. return create_form('修改其他', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
  826. }
  827. /**
  828. * 设置会员分组
  829. * @param $id
  830. * @return mixed
  831. */
  832. public function setGroup($uids)
  833. {
  834. /** @var UserGroupServices $groupServices */
  835. $groupServices = app()->make(UserGroupServices::class);
  836. $userGroup = $groupServices->getGroupList();
  837. if (count($uids) == 1) {
  838. $user = $this->getUserInfo($uids[0], ['group_id']);
  839. $setOptionUserGroup = function () use ($userGroup) {
  840. $menus = [];
  841. foreach ($userGroup as $menu) {
  842. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  843. }
  844. return $menus;
  845. };
  846. $field[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  847. } else {
  848. $setOptionUserGroup = function () use ($userGroup) {
  849. $menus = [];
  850. foreach ($userGroup as $menu) {
  851. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  852. }
  853. return $menus;
  854. };
  855. $field[] = Form::select('group_id', '用户分组')->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  856. }
  857. $field[] = Form::hidden('uids', implode(',', $uids));
  858. return create_form('设置用户分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
  859. }
  860. /**
  861. * 保存会员分组
  862. * @param $id
  863. * @return mixed
  864. */
  865. public function saveSetGroup($uids, int $group_id)
  866. {
  867. /** @var UserGroupServices $userGroup */
  868. $userGroup = app()->make(UserGroupServices::class);
  869. if (!$userGroup->getGroup($group_id)) {
  870. throw new AdminException(400696);
  871. }
  872. if (!$this->setUserGroup($uids, $group_id)) {
  873. throw new AdminException(400697);
  874. }
  875. return true;
  876. }
  877. /**
  878. * 设置用户标签
  879. * @param $uids
  880. * @return mixed
  881. */
  882. public function setLabel($uids)
  883. {
  884. /** @var UserLabelServices $labelServices */
  885. $labelServices = app()->make(UserLabelServices::class);
  886. $userLabel = $labelServices->getLabelList();
  887. if (count($uids) == 1) {
  888. $lids = app()->make(UserLabelRelationServices::class)->getUserLabels($uids[0]);
  889. $setOptionUserLabel = function () use ($userLabel) {
  890. $menus = [];
  891. foreach ($userLabel as $menu) {
  892. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  893. }
  894. return $menus;
  895. };
  896. $field[] = Form::select('label_id', '用户标签', $lids)->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  897. } else {
  898. $setOptionUserLabel = function () use ($userLabel) {
  899. $menus = [];
  900. foreach ($userLabel as $menu) {
  901. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  902. }
  903. return $menus;
  904. };
  905. $field[] = Form::select('label_id', '用户标签')->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  906. }
  907. $field[] = Form::hidden('uids', implode(',', $uids));
  908. return create_form('设置用户标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
  909. }
  910. /**
  911. * 保存用户标签
  912. * @return mixed
  913. */
  914. public function saveSetLabel($uids, $lable_id)
  915. {
  916. foreach ($lable_id as $id) {
  917. if (!app()->make(UserLabelServices::class)->getLable((int)$id)) {
  918. throw new AdminException(400698);
  919. }
  920. }
  921. /** @var UserLabelRelationServices $services */
  922. $services = app()->make(UserLabelRelationServices::class);
  923. if (!$services->setUserLable($uids, $lable_id)) {
  924. throw new AdminException(400668);
  925. }
  926. return true;
  927. }
  928. /**
  929. * 赠送会员等级
  930. * @param int $uid
  931. * @return mixed
  932. * */
  933. public function giveLevel($id)
  934. {
  935. if (!$this->getUserInfo($id)) {
  936. throw new AdminException(400214);
  937. }
  938. //查询高于当前会员的所有会员等级
  939. $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  940. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', $grade ?? 0], 'id,name');
  941. $setOptionlevel = function () use ($systemLevelList) {
  942. $menus = [];
  943. foreach ($systemLevelList as $menu) {
  944. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  945. }
  946. return $menus;
  947. };
  948. $field[] = Form::select('level_id', '用户等级')->setOptions(FormBuilder::setOptions($setOptionlevel))->filterable(true);
  949. return create_form('赠送等级', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
  950. }
  951. /**
  952. * 执行赠送会员等级
  953. * @param int $uid
  954. * @return mixed
  955. * */
  956. public function saveGiveLevel(int $id, int $level_id)
  957. {
  958. if (!$this->getUserInfo($id)) {
  959. throw new AdminException(400214);
  960. }
  961. /** @var SystemUserLevelServices $systemLevelServices */
  962. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  963. /** @var UserLevelServices $userLevelServices */
  964. $userLevelServices = app()->make(UserLevelServices::class);
  965. //查询当前选择的会员等级
  966. $systemLevel = $systemLevelServices->getLevel($level_id);
  967. if (!$systemLevel) throw new AdminException(400699);
  968. //检查是否拥有此会员等级
  969. $level = $userLevelServices->getWhereLevel(['uid' => $id, 'level_id' => $level_id], 'valid_time,is_forever');
  970. if ($level && $level['status'] == 1 && $level['is_del'] == 0) {
  971. throw new AdminException(400700);
  972. }
  973. //保存会员信息
  974. if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
  975. throw new AdminException(400219);
  976. }
  977. return true;
  978. }
  979. /**
  980. * 赠送付费会员时长
  981. * @param int $uid
  982. * @return mixed
  983. * */
  984. public function giveLevelTime($id)
  985. {
  986. if (!$this->getUserInfo($id)) {
  987. throw new AdminException(400214);
  988. }
  989. $field[] = Form::number('days', '增加时长(天)')->precision(0)->style(['width' => '200px'])->required();
  990. return create_form('赠送付费会员时长', $field, Url::buildUrl('/user/save_give_level_time/' . $id), 'PUT');
  991. }
  992. /**
  993. * 执行赠送付费会员时长
  994. * @param int $uid
  995. * @return mixed
  996. * */
  997. public function saveGiveLevelTime(int $id, int $days)
  998. {
  999. $userInfo = $this->getUserInfo($id);
  1000. if (!$userInfo) {
  1001. throw new AdminException(400214);
  1002. }
  1003. if ($days == 0) throw new AdminException(400701);
  1004. if ($days < -1) throw new AdminException(400702);
  1005. if ($userInfo->is_money_level == 0) {
  1006. $userInfo->is_money_level = 3;
  1007. if ($days == -1) {
  1008. $userInfo->is_ever_level = 1;
  1009. $time = 0;
  1010. } else {
  1011. $userInfo->overdue_time = $time = time() + ($days * 86400);
  1012. }
  1013. } else {
  1014. if ($days == -1) {
  1015. $userInfo->is_ever_level = 1;
  1016. $time = 0;
  1017. } else {
  1018. $userInfo->overdue_time = $time = $userInfo->overdue_time + ($days * 86400);
  1019. }
  1020. }
  1021. $userInfo->save();
  1022. /** @var StoreOrderCreateServices $storeOrderCreateService */
  1023. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  1024. $orderInfo = [
  1025. 'uid' => $id,
  1026. 'order_id' => $storeOrderCreateService->getNewOrderId(),
  1027. 'type' => 3,
  1028. 'member_type' => 0,
  1029. 'pay_type' => 'admin',
  1030. 'paid' => 1,
  1031. 'pay_time' => time(),
  1032. 'is_free' => 1,
  1033. 'overdue_time' => $time,
  1034. 'vip_day' => $days,
  1035. 'add_time' => time()
  1036. ];
  1037. /** @var OtherOrderServices $otherOrder */
  1038. $otherOrder = app()->make(OtherOrderServices::class);
  1039. $otherOrder->save($orderInfo);
  1040. return true;
  1041. }
  1042. /**
  1043. * 清除会员等级
  1044. * @paran int $uid
  1045. * @paran boolean
  1046. * */
  1047. public function cleanUpLevel($uid)
  1048. {
  1049. if (!$this->getUserInfo($uid))
  1050. throw new AdminException(400214);
  1051. /** @var UserLevelServices $services */
  1052. $services = app()->make(UserLevelServices::class);
  1053. return $this->transaction(function () use ($uid, $services) {
  1054. $res = $services->delUserLevel($uid);
  1055. $res1 = $this->dao->update($uid, ['clean_time' => time(), 'level' => 0, 'exp' => 0], 'uid');
  1056. if (!$res && !$res1)
  1057. throw new AdminException(400186);
  1058. return true;
  1059. });
  1060. }
  1061. /**
  1062. * 用户详细信息
  1063. * @param $uid
  1064. */
  1065. public function getUserDetailed(int $uid, $userIfno = [])
  1066. {
  1067. /** @var UserAddressServices $userAddress */
  1068. $userAddress = app()->make(UserAddressServices::class);
  1069. $field = 'real_name,phone,province,city,district,detail,post_code';
  1070. $address = $userAddress->getUserDefaultAddress($uid, $field);
  1071. if (!$address) {
  1072. $address = $userAddress->getUserAddressList($uid, $field);
  1073. $address = $address[0] ?? [];
  1074. }
  1075. $userInfo = $this->getUserInfo($uid);
  1076. return [
  1077. ['name' => '默认收货地址', 'value' => $address ? '收货人:' . $address['real_name'] . '邮编:' . $address['post_code'] . ' 收货人电话:' . $address['phone'] . ' 地址:' . $address['province'] . ' ' . $address['city'] . ' ' . $address['district'] . ' ' . $address['detail'] : ''],
  1078. ['name' => '手机号码', 'value' => $userInfo['phone']],
  1079. ['name' => '姓名', 'value' => ''],
  1080. ['name' => '微信昵称', 'value' => $userInfo['nickname']],
  1081. ['name' => '头像', 'value' => $userInfo['avatar']],
  1082. ['name' => '邮箱', 'value' => ''],
  1083. ['name' => '生日', 'value' => ''],
  1084. ['name' => '积分', 'value' => $userInfo['integral']],
  1085. ['name' => '上级推广人', 'value' => $userInfo['spread_uid'] ? $this->getUserInfo($userInfo['spread_uid'], ['nickname'])['nickname'] ?? '' : ''],
  1086. ['name' => '账户余额', 'value' => $userInfo['now_money']],
  1087. ['name' => '佣金总收入', 'value' => app()->make(UserBillServices::class)->getBrokerageSum($uid)],
  1088. ['name' => '提现总金额', 'value' => app()->make(UserExtractServices::class)->getUserExtract($uid)],
  1089. ];
  1090. }
  1091. /**
  1092. * 获取用户详情里面的用户消费能力和用户余额积分等
  1093. * @param $uid
  1094. * @return array[]
  1095. */
  1096. public function getHeaderList(int $uid, $userInfo = [])
  1097. {
  1098. if (!$userInfo) {
  1099. $userInfo = $this->getUserInfo($uid);
  1100. }
  1101. /** @var StoreOrderServices $orderServices */
  1102. $orderServices = app()->make(StoreOrderServices::class);
  1103. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  1104. return [
  1105. [
  1106. 'title' => '余额',
  1107. 'value' => $userInfo['now_money'] ?? 0,
  1108. 'key' => '元',
  1109. ],
  1110. [
  1111. 'title' => '总计订单',
  1112. 'value' => $orderServices->count($where),
  1113. 'key' => '笔',
  1114. ],
  1115. [
  1116. 'title' => '总消费金额',
  1117. 'value' => $orderServices->together($where, 'pay_price'),
  1118. 'key' => '元',
  1119. ],
  1120. [
  1121. 'title' => '积分',
  1122. 'value' => $userInfo['integral'] ?? 0,
  1123. 'key' => '',
  1124. ],
  1125. [
  1126. 'title' => '本月订单',
  1127. 'value' => $orderServices->count($where + ['time' => 'month']),
  1128. 'key' => '笔',
  1129. ],
  1130. [
  1131. 'title' => '本月消费金额',
  1132. 'value' => $orderServices->together($where + ['time' => 'month'], 'pay_price'),
  1133. 'key' => '元',
  1134. ]
  1135. ];
  1136. }
  1137. /**
  1138. * 获取用户记录里的积分总数和签到总数和余额变动总数
  1139. * @param $uid
  1140. * @return array
  1141. */
  1142. public function getUserBillCountData($uid)
  1143. {
  1144. /** @var UserBillServices $userBill */
  1145. $userBill = app()->make(UserBillServices::class);
  1146. $integral_count = $userBill->getIntegralCount($uid);
  1147. $sign_count = $userBill->getSignCount($uid);
  1148. $balanceChang_count = $userBill->getBrokerageCount($uid);
  1149. return [$integral_count, $sign_count, $balanceChang_count];
  1150. }
  1151. public function read(int $uid)
  1152. {
  1153. $userInfo = $this->getUserInfo($uid);
  1154. if (!$userInfo) {
  1155. throw new AdminException(100026);
  1156. }
  1157. $info = [
  1158. 'uid' => $uid,
  1159. 'userinfo' => $this->getUserDetailed($uid, $userInfo),
  1160. 'headerList' => $this->getHeaderList($uid, $userInfo),
  1161. 'count' => $this->getUserBillCountData($uid),
  1162. 'ps_info' => $userInfo
  1163. ];
  1164. return $info;
  1165. }
  1166. /**
  1167. * 获取好友
  1168. * @param int $id
  1169. * @param string $field
  1170. * @return array
  1171. * @throws \think\db\exception\DataNotFoundException
  1172. * @throws \think\db\exception\DbException
  1173. * @throws \think\db\exception\ModelNotFoundException
  1174. */
  1175. public function getFriendList(int $id, string $field = 'uid,nickname,level,add_time,spread_time')
  1176. {
  1177. [$page, $limit] = $this->getPageValue();
  1178. $list = $this->dao->getList(['spread_uid' => $id], $field, $page, $limit);
  1179. /** @var SystemUserLevelServices $systemLevelServices */
  1180. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  1181. $systemLevelList = $systemLevelServices->getWhereLevelList([], 'id,name');
  1182. if ($systemLevelServices) $systemLevelServices = array_combine(array_column($systemLevelList, 'id'), $systemLevelList);
  1183. foreach ($list as &$item) {
  1184. $item['type'] = $systemLevelServices[$item['level']]['name'] ?? '暂无';
  1185. $item['add_time'] = $item['spread_time'] && is_numeric($item['spread_time']) ? date('Y-m-d H:i:s', $item['spread_time']) : '';
  1186. }
  1187. $count = $this->dao->count(['spread_uid' => $id]);
  1188. return compact('list', 'count');
  1189. }
  1190. /**
  1191. * 获取单个用户信息
  1192. * @param $id 用户id
  1193. * @return mixed
  1194. */
  1195. public function oneUserInfo(int $id, string $type)
  1196. {
  1197. switch ($type) {
  1198. case 'spread':
  1199. // /** @var UserFriendsServices $services */
  1200. // $services = app()->make(UserFriendsServices::class);
  1201. // return $services->getFriendList(['uid' => $id], ['level', 'nickname']);
  1202. return $this->getFriendList($id);
  1203. case 'order':
  1204. /** @var StoreOrderServices $services */
  1205. $services = app()->make(StoreOrderServices::class);
  1206. return $services->getUserOrderList($id);
  1207. case 'integral':
  1208. /** @var UserBillServices $services */
  1209. $services = app()->make(UserBillServices::class);
  1210. return $services->getIntegralList($id, [], 'title,number,balance,mark,add_time,frozen_time');
  1211. case 'sign':
  1212. /** @var UserBillServices $services */
  1213. $services = app()->make(UserBillServices::class);
  1214. return $services->getSignList($id, [], 'title,number,mark,add_time');
  1215. case 'coupon':
  1216. /** @var StoreCouponUserServices $services */
  1217. $services = app()->make(StoreCouponUserServices::class);
  1218. return $services->getUserCouponList($id);
  1219. case 'balance_change':
  1220. /** @var UserMoneyServices $services */
  1221. $services = app()->make(UserMoneyServices::class);
  1222. return $services->balanceList(['uid' => $id]);
  1223. default:
  1224. throw new AdminException(100100);
  1225. }
  1226. }
  1227. /**获取特定时间用户访问量
  1228. * @param $time
  1229. * @param $week
  1230. * @return int
  1231. */
  1232. public function todayLastVisits($time, $week)
  1233. {
  1234. return $this->dao->todayLastVisit($time, $week);
  1235. }
  1236. /**获取特定时间新增用户
  1237. * @param $time
  1238. * @param $week
  1239. * @return int
  1240. */
  1241. public function todayAddVisits($time, $week)
  1242. {
  1243. return $this->dao->todayAddVisit($time, $week);
  1244. }
  1245. /**
  1246. * 用户图表
  1247. */
  1248. public function userChart()
  1249. {
  1250. $starday = date('Y-m-d', strtotime('-30 day'));
  1251. $yesterday = date('Y-m-d', strtotime('+1 day'));
  1252. $user_list = $this->dao->userList($starday, $yesterday);
  1253. $chartdata = [];
  1254. $data = [];
  1255. $chartdata['legend'] = ['用户数'];//分类
  1256. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1257. $chartdata['xAxis'] = [date('m-d')];//X轴值
  1258. $chartdata['series'] = [0];//分类1值
  1259. if (!empty($user_list)) {
  1260. foreach ($user_list as $k => $v) {
  1261. $data['day'][] = $v['day'];
  1262. $data['count'][] = $v['count'];
  1263. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  1264. $chartdata['yAxis']['maxnum'] = $v['count'];
  1265. }
  1266. $chartdata['xAxis'] = $data['day'];//X轴值
  1267. $chartdata['series'] = $data['count'];//分类1值
  1268. }
  1269. $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
  1270. $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  1271. $pay[0] = $this->dao->count(['pay_count' => 0]);
  1272. $pay[1] = $this->dao->count(['pay_count' => 1]);
  1273. $pay[2] = $this->dao->userCount(1);
  1274. $pay[3] = $this->dao->userCount(2);
  1275. foreach ($pay as $key => $item) {
  1276. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  1277. }
  1278. $chartdata['bing_data'] = $bing_data;
  1279. return $chartdata;
  1280. }
  1281. /***********************************************/
  1282. /************ 前端api services *****************/
  1283. /***********************************************/
  1284. /**
  1285. * 用户信息
  1286. * @param $info
  1287. * @return mixed
  1288. */
  1289. public function userInfo($info)
  1290. {
  1291. /** @var UserBillServices $userBill */
  1292. $userBill = app()->make(UserBillServices::class);
  1293. $uid = (int)$info['uid'];
  1294. $broken_time = intval(sys_config('extract_time'));
  1295. $search_time = time() - 86400 * $broken_time;
  1296. //改造时间
  1297. $search_time = '1970/01/01' . ' - ' . date('Y/m/d H:i:s', $search_time);
  1298. //可提现佣金
  1299. //返佣 +
  1300. $brokerage_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], $search_time);
  1301. //退款退的佣金 -
  1302. $refund_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 0], $search_time);
  1303. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  1304. if ($info['broken_commission'] < 0)
  1305. $info['broken_commission'] = 0;
  1306. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  1307. if ($info['commissionCount'] < 0)
  1308. $info['commissionCount'] = 0;
  1309. return $info;
  1310. }
  1311. /**
  1312. * 个人中心
  1313. * @param array $user
  1314. */
  1315. public function personalHome(array $user, $tokenData)
  1316. {
  1317. $userInfo = $user;
  1318. $uid = (int)$user['uid'];
  1319. /** @var StoreCouponUserServices $storeCoupon */
  1320. $storeCoupon = app()->make(StoreCouponUserServices::class);
  1321. /** @var UserBillServices $userBill */
  1322. $userBill = app()->make(UserBillServices::class);
  1323. /** @var UserExtractServices $userExtract */
  1324. $userExtract = app()->make(UserExtractServices::class);
  1325. /** @var StoreOrderServices $storeOrder */
  1326. $storeOrder = app()->make(StoreOrderServices::class);
  1327. /** @var UserLevelServices $userLevel */
  1328. $userLevel = app()->make(UserLevelServices::class);
  1329. /** @var StoreServiceServices $storeService */
  1330. $storeService = app()->make(StoreServiceServices::class);
  1331. /** @var WechatUserServices $wechatUser */
  1332. $wechatUser = app()->make(WechatUserServices::class);
  1333. /** @var UserInvoiceServices $userInvoice */
  1334. $userInvoice = app()->make(UserInvoiceServices::class);
  1335. /** @var MemberCardServices $memberCardService */
  1336. $memberCardService = app()->make(MemberCardServices::class);
  1337. /** @var StoreProductRelationServices $collect */
  1338. $collect = app()->make(StoreProductRelationServices::class);
  1339. /** @var MessageSystemServices $messageSystemServices */
  1340. $messageSystemServices = app()->make(MessageSystemServices::class);
  1341. /** @var DiyServices $diyServices */
  1342. $diyServices = app()->make(DiyServices::class);
  1343. /** @var AgentLevelServices $agentLevelServices */
  1344. $agentLevelServices = app()->make(AgentLevelServices::class);
  1345. //看付费会员是否开启
  1346. $isOpenMember = $memberCardService->isOpenMemberCard();
  1347. $user['is_open_member'] = $isOpenMember;
  1348. $user['agent_level_name'] = '';
  1349. if ($user['agent_level']) {
  1350. $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name,status,grade');
  1351. if (!$levelInfo['status']) {
  1352. $levelInfo = $agentLevelServices->get([
  1353. ['grade', '<', $levelInfo['grade']],
  1354. ['is_del', '=', 0],
  1355. ['status', '=', 1]
  1356. ], ['id', 'name', 'status', 'grade']);
  1357. }
  1358. $user['agent_level_name'] = $levelInfo && $levelInfo['name'] && $levelInfo['status'] ? $levelInfo['name'] : '';
  1359. }
  1360. //会员领取优惠券
  1361. // $couponService->sendMemberCoupon($uid);
  1362. //看是否会员过期
  1363. $this->offMemberLevel($uid, $userInfo);
  1364. $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
  1365. $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
  1366. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  1367. $user['like'] = app()->make(StoreProductRelationServices::class)->getUserCollectCount($user['uid']);
  1368. $user['orderStatusNum'] = $storeOrder->getOrderData($uid, true, true);
  1369. $user['notice'] = 0;
  1370. /** @var UserMoneyServices $userMoney */
  1371. $userMoney = app()->make(UserMoneyServices::class);
  1372. $user['recharge'] = $userMoney->sum([
  1373. ['uid', '=', $uid], ['pm', '=', 1], ['type', 'in', ['recharge', 'system_add', 'extract']]
  1374. ], 'number');
  1375. $user['orderStatusSum'] = bcsub((string)$user['recharge'], (string)$user['now_money'], 2);
  1376. $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
  1377. $user['extractPrice'] = $user['brokerage_price'];//可提现
  1378. $user['statu'] = (int)sys_config('store_brokerage_statu');
  1379. if (!$user['is_promoter'] && $user['statu'] == 2) {
  1380. $price = $storeOrder->sum(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']], 'pay_price');
  1381. $status = is_brokerage_statu($price);
  1382. if ($status) {
  1383. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  1384. $user['is_promoter'] = 1;
  1385. } else {
  1386. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  1387. $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$price, 2);
  1388. }
  1389. }
  1390. /** @var UserBrokerageServices $frozenPrices */
  1391. $frozenPrices = app()->make(UserBrokerageServices::class);
  1392. $user['broken_commission'] = $frozenPrices->getUserFrozenPrice($uid);
  1393. if ($user['broken_commission'] < 0)
  1394. $user['broken_commission'] = 0;
  1395. $user['commissionCount'] = bcsub((string)$user['brokerage_price'], (string)$user['broken_commission'], 2);
  1396. if ($user['commissionCount'] < 0)
  1397. $user['commissionCount'] = 0;
  1398. if (!sys_config('member_func_status'))
  1399. $user['vip'] = false;
  1400. else {
  1401. $userLevel = $userLevel->getUerLevelInfoByUid($user['uid']);
  1402. $user['vip'] = (bool)$userLevel;
  1403. if ($user['vip']) {
  1404. $user['vip_id'] = $userLevel['id'] ?? 0;
  1405. $user['vip_icon'] = $userLevel['icon'] ?? '';
  1406. $user['vip_name'] = $userLevel['name'] ?? '';
  1407. }
  1408. }
  1409. $user['yesterDay'] = $userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
  1410. $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
  1411. $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  1412. if ($user['phone'] && $user['user_type'] != 'h5') {
  1413. $user['switchUserInfo'][] = $userInfo;
  1414. $h5UserInfo = $this->dao->getOne(['account' => $user['phone'], 'user_type' => 'h5']);
  1415. if ($h5UserInfo) {
  1416. $user['switchUserInfo'][] = $h5UserInfo;
  1417. }
  1418. } else if ($user['phone'] && $user['user_type'] == 'h5') {
  1419. $wechatUserInfo = $this->getOne([['phone', '=', $user['phone']], ['user_type', '<>', 'h5']]);
  1420. if ($wechatUserInfo) {
  1421. $user['switchUserInfo'][] = $wechatUserInfo;
  1422. }
  1423. $user['switchUserInfo'][] = $userInfo;
  1424. } else if (!$user['phone']) {
  1425. $user['switchUserInfo'][] = $userInfo;
  1426. }
  1427. $user['broken_day'] = (int)sys_config('extract_time');//佣金冻结时间
  1428. $user['balance_func_status'] = (int)sys_config('balance_func_status', 0);
  1429. $invoice_func = $userInvoice->invoiceFuncStatus();
  1430. $user['invioce_func'] = $invoice_func['invoice_func'];
  1431. $user['special_invoice'] = $invoice_func['special_invoice'];
  1432. $user['collectCount'] = $collect->count(['uid' => $uid]);
  1433. $user['spread_status'] = $this->checkUserPromoter($user['uid']);
  1434. $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
  1435. $user['member_style'] = (int)$diyServices->getColorChange('member');
  1436. if ($user['is_ever_level']) {
  1437. $user['vip_status'] = 1;//永久会员
  1438. } else {
  1439. if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
  1440. $user['vip_status'] = -1;//开通过已过期
  1441. } else if (!$user['overdue_time'] && !$user['is_money_level']) {
  1442. $user['vip_status'] = 2;//没有开通过
  1443. } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
  1444. $user['vip_status'] = 3;//开通了,没有到期
  1445. }
  1446. }
  1447. $user['svip_open'] = (bool)sys_config('member_card_status');
  1448. /** @var StoreServiceRecordServices $servicesRecord */
  1449. $servicesRecord = app()->make(StoreServiceRecordServices::class);
  1450. $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
  1451. $message = $messageSystemServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  1452. $user['service_num'] = $service_num + $message;
  1453. /** @var AgentLevelServices $userSpread */
  1454. $agentLevel = app()->make(AgentLevelServices::class);
  1455. $user['spread_level_count'] = $agentLevel->count(['status' => 1, 'is_del' => 0]);
  1456. $user['extract_type'] = sys_config('extract_type');
  1457. $user['integral'] = intval($user['integral']);
  1458. $user['is_agent_level'] = $agentLevelServices->count(['status' => 1, 'is_del' => 0]) > 0 ? 1 : 0;
  1459. $user['division_open'] = (int)sys_config('division_status', 0);
  1460. $user['avatar'] = strpos($user['avatar'], '/statics/system_images/') !== false ? set_file_url($user['avatar']) : $user['avatar'];
  1461. return $user;
  1462. }
  1463. /**
  1464. * 用户资金统计
  1465. * @param int $uid
  1466. */
  1467. public function balance(int $uid)
  1468. {
  1469. $userInfo = $this->getUserInfo($uid);
  1470. if (!$userInfo) {
  1471. throw new AdminException(400214);
  1472. }
  1473. /** @var UserBillServices $userBill */
  1474. $userBill = app()->make(UserBillServices::class);
  1475. /** @var StoreOrderServices $storeOrder */
  1476. $storeOrder = app()->make(StoreOrderServices::class);
  1477. $user['now_money'] = $userInfo['now_money'];//当前总资金
  1478. $user['recharge'] = $userBill->getRechargeSum($uid);//累计充值
  1479. $user['orderStatusSum'] = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1480. return $user;
  1481. }
  1482. /**
  1483. * 用户修改信息
  1484. * @param Request $request
  1485. * @return mixed
  1486. */
  1487. public function eidtNickname(int $uid, array $data)
  1488. {
  1489. if (!$this->getUserInfo($uid)) {
  1490. throw new ApiException(400214);
  1491. }
  1492. if (!$this->dao->update($uid, $data, 'uid')) {
  1493. throw new ApiException(100007);
  1494. }
  1495. return true;
  1496. }
  1497. /**
  1498. * 获取推广人排行
  1499. * @param $data 查询条件
  1500. * @return array
  1501. * @throws \think\db\exception\DataNotFoundException
  1502. * @throws \think\db\exception\ModelNotFoundException
  1503. * @throws \think\exception\DbException
  1504. */
  1505. public function getRankList(array $data)
  1506. {
  1507. $startTime = strtotime('this week Monday');
  1508. $endTime = time();
  1509. switch ($data['type']) {
  1510. case 'week':
  1511. $startTime = strtotime('this week Monday');
  1512. break;
  1513. case 'month':
  1514. $startTime = strtotime('last month');
  1515. break;
  1516. }
  1517. [$page, $limit] = $this->getPageValue();
  1518. $field = 't0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar';
  1519. return $this->dao->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  1520. }
  1521. /**
  1522. * 静默绑定推广人
  1523. * @param int $uid
  1524. * @param int $spreadUid
  1525. * @param $code
  1526. * @return bool
  1527. * @throws \think\db\exception\DataNotFoundException
  1528. * @throws \think\db\exception\DbException
  1529. * @throws \think\db\exception\ModelNotFoundException
  1530. */
  1531. public function spread(int $uid, int $spreadUid, $code)
  1532. {
  1533. $userInfo = $this->getUserInfo($uid);
  1534. if (!$userInfo) {
  1535. throw new ApiException(100026);
  1536. }
  1537. if ($code && !$spreadUid) {
  1538. /** @var QrcodeServices $qrCode */
  1539. $qrCode = app()->make(QrcodeServices::class);
  1540. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  1541. $spreadUid = $info['third_id'];
  1542. }
  1543. }
  1544. if ($spreadUid == 0) return '不绑定';
  1545. $userSpreadUid = $this->dao->value(['uid' => $spreadUid], 'spread_uid');
  1546. //记录好友关系
  1547. if ($spreadUid && $uid && $spreadUid != $uid) {
  1548. /** @var UserFriendsServices $serviceFriend */
  1549. $serviceFriend = app()->make(UserFriendsServices::class);
  1550. $serviceFriend->saveFriend([
  1551. 'uid' => $uid,
  1552. 'friends_uid' => $spreadUid,
  1553. ]);
  1554. }
  1555. $check = false;
  1556. if (sys_config('brokerage_bindind') == 1) {
  1557. if (sys_config('store_brokerage_binding_status') == 1) {
  1558. if (!$userInfo['spread_uid']) {
  1559. $check = true;
  1560. }
  1561. } elseif (sys_config('store_brokerage_binding_status') == 2 && (($userInfo['spread_time'] + (sys_config('store_brokerage_binding_time') * 86400)) < time())) {
  1562. $check = true;
  1563. } elseif (sys_config('store_brokerage_binding_status') == 3) {
  1564. $check = true;
  1565. }
  1566. } elseif (sys_config('brokerage_bindind') == 2) {
  1567. if ($userInfo['add_time'] == $userInfo['last_time']) {
  1568. $check = true;
  1569. }
  1570. }
  1571. if ($userInfo['uid'] == $userSpreadUid || $userInfo['spread_uid'] == $spreadUid) $check = false;
  1572. if ($check) {
  1573. $spreadInfo = $this->dao->get($spreadUid);
  1574. $data = [];
  1575. $data['spread_uid'] = $spreadUid;
  1576. $data['spread_time'] = time();
  1577. $data['division_id'] = $spreadInfo['division_id'];
  1578. $data['agent_id'] = $spreadInfo['agent_id'];
  1579. $data['staff_id'] = $spreadInfo['staff_id'];
  1580. if (!$this->dao->update($uid, $data, 'uid')) {
  1581. throw new ApiException(410288);
  1582. }
  1583. return '绑定上级成功,上级uid为' . $spreadUid;
  1584. } else {
  1585. return '不绑定';
  1586. }
  1587. }
  1588. /**
  1589. * 添加访问记录
  1590. * @param Request $request
  1591. * @return mixed
  1592. */
  1593. public function setVisit(array $data)
  1594. {
  1595. $userInfo = $this->getUserInfo($data['uid']);
  1596. if (!$userInfo) {
  1597. throw new ApiException(100026);
  1598. }
  1599. $data['channel_type'] = $userInfo['user_type'];
  1600. $data['add_time'] = time();
  1601. /** @var WechatUserServices $wechatUserServices */
  1602. $wechatUserServices = app()->make(WechatUserServices::class);
  1603. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid'], 'user_type' => $userInfo['user_type']]);
  1604. if (!$wechatUser) {
  1605. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid']]);
  1606. }
  1607. if ($wechatUser) {
  1608. $data['province'] = $wechatUser['province'];
  1609. }
  1610. /** @var UserVisitServices $userVisit */
  1611. $userVisit = app()->make(UserVisitServices::class);
  1612. if ($userVisit->save($data)) {
  1613. return true;
  1614. } else {
  1615. throw new ApiException(100015);
  1616. }
  1617. }
  1618. /**
  1619. * 获取活动状态
  1620. * @return mixed
  1621. */
  1622. public function activity()
  1623. {
  1624. /** @var StoreBargainServices $storeBragain */
  1625. $storeBragain = app()->make(StoreBargainServices::class);
  1626. /** @var StoreCombinationServices $storeCombinaion */
  1627. $storeCombinaion = app()->make(StoreCombinationServices::class);
  1628. /** @var StoreSeckillServices $storeSeckill */
  1629. $storeSeckill = app()->make(StoreSeckillServices::class);
  1630. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  1631. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  1632. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  1633. return $data;
  1634. }
  1635. /**
  1636. * 获取用户下级推广人
  1637. * @param int $uid 当前用户
  1638. * @param int $grade 等级 0 一级 1 二级
  1639. * @param string $orderBy 排序
  1640. * @param string $keyword
  1641. * @return array|bool
  1642. */
  1643. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '')
  1644. {
  1645. $user = $this->getUserInfo($uid);
  1646. if (!$user) {
  1647. throw new AdminException(400214);
  1648. }
  1649. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  1650. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  1651. $data = [
  1652. 'total' => count($spread_one_ids),
  1653. 'totalLevel' => count($spread_two_ids),
  1654. 'list' => []
  1655. ];
  1656. if (sys_config('brokerage_level', 2) == 1) {
  1657. $data['count'] = $data['total'];
  1658. } else {
  1659. $data['count'] = $data['total'] + $data['totalLevel'];
  1660. }
  1661. /** @var UserStoreOrderServices $userStoreOrder */
  1662. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  1663. $list = [];
  1664. if ($grade == 0) {
  1665. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword);
  1666. } else {
  1667. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword);
  1668. }
  1669. foreach ($list as &$item) {
  1670. if (isset($item['spread_time']) && $item['spread_time']) {
  1671. $item['time'] = date('Y/m/d', $item['spread_time']);
  1672. }
  1673. }
  1674. $data['list'] = $list;
  1675. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  1676. return $data;
  1677. }
  1678. /**
  1679. * 获取推广人uids
  1680. * @param int $uid
  1681. * @param bool $one
  1682. * @return array
  1683. */
  1684. public function getUserSpredadUids(int $uid, int $type = 0)
  1685. {
  1686. $uids = $this->dao->getColumn(['spread_uid' => $uid, 'is_del' => 0], 'uid');
  1687. if ($type === 1) {
  1688. return $uids;
  1689. }
  1690. if ($uids) {
  1691. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids], ['is_del', '=', 0]], 'uid');
  1692. if ($type === 2) {
  1693. return $uidsTwo;
  1694. }
  1695. if ($uidsTwo) {
  1696. $uids = array_merge($uids, $uidsTwo);
  1697. }
  1698. }
  1699. return $uids;
  1700. }
  1701. /**
  1702. * 检测用户是否是推广员
  1703. * @param int $uid
  1704. * @param $user
  1705. * @return bool
  1706. */
  1707. public function checkUserPromoter(int $uid, $user = [])
  1708. {
  1709. if (!$user) {
  1710. $user = $this->getUserInfo($uid);
  1711. }
  1712. if (!$user) {
  1713. return false;
  1714. }
  1715. //分销是否开启
  1716. if (!sys_config('brokerage_func_status')) {
  1717. return false;
  1718. }
  1719. //用户分校推广资格是否开启4.0.32
  1720. if (isset($user['spread_open']) && !$user['spread_open']) {
  1721. return false;
  1722. }
  1723. /** @var StoreOrderServices $storeOrder */
  1724. $storeOrder = app()->make(StoreOrderServices::class);
  1725. $sumPrice = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1726. $store_brokerage_statu = sys_config('store_brokerage_statu');
  1727. $store_brokerage_price = sys_config('store_brokerage_price');
  1728. if ($user['is_promoter'] || $store_brokerage_statu == 2 || ($store_brokerage_statu == 3 && $sumPrice > $store_brokerage_price)) {
  1729. return true;
  1730. }
  1731. return false;
  1732. }
  1733. /**
  1734. * 同步微信粉丝用户(后台接口)
  1735. * @return bool
  1736. */
  1737. public function syncWechatUsers()
  1738. {
  1739. $appid = sys_config('wechat_appid');
  1740. $appSecret = sys_config('wechat_appsecret');
  1741. if (!$appid || !$appSecret) {
  1742. throw new AdminException(400236);
  1743. }
  1744. $key = md5('sync_wechat_users');
  1745. //一天点击一次
  1746. if (CacheService::get($key)) {
  1747. return true;
  1748. }
  1749. $next_openid = null;
  1750. do {
  1751. $result = WechatService::getUsersList($next_openid);
  1752. $userOpenids = $result['data'];
  1753. //拆分大数组
  1754. $opemidArr = array_chunk($userOpenids, 100);
  1755. foreach ($opemidArr as $openids) {
  1756. //加入同步|更新用户队列
  1757. UserJob::dispatch([$openids]);
  1758. }
  1759. $next_openid = $result['next_openid'];
  1760. } while ($next_openid != null);
  1761. CacheService::set($key, 1, 3600 * 24);
  1762. return true;
  1763. }
  1764. /**
  1765. * 导入微信粉丝用户
  1766. * @param array $openids
  1767. * @return bool
  1768. */
  1769. public function importUser(array $noBeOpenids)
  1770. {
  1771. if (!$noBeOpenids) {
  1772. return true;
  1773. }
  1774. $dataAll = $data = [];
  1775. $time = time();
  1776. foreach ($noBeOpenids as $openid) {
  1777. try {
  1778. $info = WechatService::getUserInfo($openid);
  1779. $info = is_object($info) ? $info->toArray() : $info;
  1780. } catch (\Throwable $e) {
  1781. $info = [];
  1782. }
  1783. if (!$info) continue;
  1784. $data['nickname'] = $info['nickname'] ?? '';
  1785. $data['headimgurl'] = $info['headimgurl'] ?? '';
  1786. $userInfoData = $this->setUserInfo($data);
  1787. if (!$userInfoData) {
  1788. throw new AdminException(400703);
  1789. }
  1790. $data['uid'] = $userInfoData['uid'];
  1791. $data['subscribe'] = $info['subscribe'] ?? 1;
  1792. $data['unionid'] = $info['unionid'] ?? '';
  1793. $data['openid'] = $info['openid'] ?? '';
  1794. $data['sex'] = $info['sex'] ?? 0;
  1795. $data['language'] = $info['language'] ?? '';
  1796. $data['city'] = $info['city'] ?? '';
  1797. $data['province'] = $info['province'] ?? '';
  1798. $data['country'] = $info['country'] ?? '';
  1799. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  1800. $data['groupid'] = $info['groupid'] ?? 0;
  1801. $data['remark'] = $info['remark'] ?? '';
  1802. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  1803. $data['add_time'] = $time;
  1804. $data['is_complete'] = 1;
  1805. $dataAll[] = $data;
  1806. }
  1807. if ($dataAll) {
  1808. /** @var WechatUserServices $wechatUser */
  1809. $wechatUser = app()->make(WechatUserServices::class);
  1810. if (!$wechatUser->saveAll($dataAll)) {
  1811. throw new AdminException(400703);
  1812. }
  1813. }
  1814. return true;
  1815. }
  1816. /** 修改会员的时间及是否会员状态
  1817. * @param int $vip_day 会员天数
  1818. * @param array $user_id 用户id
  1819. * @param int $is_money_level 会员来源途径
  1820. * @param bool $member_type 会员卡类型
  1821. * @return mixed
  1822. * @throws \think\db\exception\DataNotFoundException
  1823. * @throws \think\db\exception\DbException
  1824. * @throws \think\db\exception\ModelNotFoundException
  1825. */
  1826. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  1827. {
  1828. if ($vip_day == 0) throw new ApiException(410289);
  1829. $user_info = $this->getUserInfo($user_id);
  1830. if (!$user_info) throw new ApiException(410032);
  1831. if (!$member_type) $member_type = "month";
  1832. if ($member_type == 'ever') {
  1833. $overdue_time = 0;
  1834. $is_ever_level = 1;
  1835. } else {
  1836. if ($user_info['is_money_level'] == 0) {
  1837. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  1838. } else {
  1839. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  1840. }
  1841. $is_ever_level = 0;
  1842. }
  1843. $setData['overdue_time'] = $overdue_time;
  1844. $setData['is_ever_level'] = $is_ever_level;
  1845. $setData['is_money_level'] = $is_money_level ?: 0;
  1846. // if ($user_info['level'] == 0) $setData['level'] = 1;
  1847. return $this->dao->update(['uid' => $user_id], $setData);
  1848. }
  1849. /**
  1850. * 会员过期改变状态,变为普通会员
  1851. * @param $uid
  1852. * @param $userInfo
  1853. * @return bool
  1854. * @throws \think\db\exception\DataNotFoundException
  1855. * @throws \think\db\exception\DbException
  1856. * @throws \think\db\exception\ModelNotFoundException
  1857. */
  1858. public function offMemberLevel($uid, $userInfo = [])
  1859. {
  1860. if (!$uid) return false;
  1861. if (!$userInfo) {
  1862. $userInfo = $this->dao->get($uid);
  1863. }
  1864. if (!$userInfo) return false;
  1865. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  1866. $this->dao->update(['uid' => $uid], ['is_money_level' => 0/*, 'overdue_time' => 0*/]);
  1867. return false;
  1868. }
  1869. return true;
  1870. }
  1871. /**
  1872. * @param array $where
  1873. * @return array
  1874. * @throws \think\db\exception\DataNotFoundException
  1875. * @throws \think\db\exception\DbException
  1876. * @throws \think\db\exception\ModelNotFoundException
  1877. */
  1878. public function getUserInfoList(array $where, $field = "*")
  1879. {
  1880. return $this->dao->getUserInfoList($where, $field);
  1881. }
  1882. /**
  1883. * 增加推广用户佣金
  1884. * @param int $uid
  1885. * @param int $spread_uid
  1886. * @param array $userInfo
  1887. * @param array $spread_user
  1888. * @return bool|mixed
  1889. * @throws \think\db\exception\DataNotFoundException
  1890. * @throws \think\db\exception\DbException
  1891. * @throws \think\db\exception\ModelNotFoundException
  1892. */
  1893. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  1894. {
  1895. if (!$uid || !$spread_uid) {
  1896. return false;
  1897. }
  1898. //商城分销功能是否开启 0关闭1开启
  1899. if (!sys_config('brokerage_func_status')) return true;
  1900. if (!sys_config('brokerage_user_status')) return true;
  1901. //获取设置推广佣金单价
  1902. $brokerage_price = sys_config('uni_brokerage_price', 0);
  1903. //获取推广佣金当日限额
  1904. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  1905. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  1906. return true;
  1907. }
  1908. if (!$userInfo) {
  1909. $userInfo = $this->getUserInfo($uid);
  1910. }
  1911. if (!$userInfo) {
  1912. return false;
  1913. }
  1914. if (!$spread_user) {
  1915. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  1916. }
  1917. if (!$spread_user) {
  1918. return false;
  1919. }
  1920. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  1921. return false;
  1922. }
  1923. /** @var UserBrokerageServices $userBrokerageServices */
  1924. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1925. // -1不限制
  1926. if ($day_brokerage_price_upper != -1) {
  1927. if ($day_brokerage_price_upper <= 0) {
  1928. return true;
  1929. } else {
  1930. //获取上级用户今日获取推广用户佣金
  1931. $spread_day_brokerage = $userBrokerageServices->getUserBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  1932. //超过上限
  1933. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  1934. return true;
  1935. }
  1936. }
  1937. }
  1938. $spreadPrice = $spread_user['brokerage_price'];
  1939. // 上级推广员返佣之后的金额
  1940. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  1941. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  1942. // 添加返佣记录
  1943. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  1944. 'nickname' => $userInfo['nickname'],
  1945. 'number' => floatval($brokerage_price)
  1946. ], $balance, $uid);
  1947. // 添加用户余额
  1948. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  1949. //给上级发送获得佣金的模板消息
  1950. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  1951. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  1952. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  1953. return $res1 && $res2;
  1954. });
  1955. }
  1956. /**
  1957. * 获取上级uid
  1958. * @param int $uid
  1959. * @param array $userInfo
  1960. * @param bool $is_spread
  1961. * @return int|mixed
  1962. */
  1963. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  1964. {
  1965. if (!$uid) {
  1966. return 0;
  1967. }
  1968. //商城分销功能是否开启 0关闭1开启
  1969. if (!sys_config('brokerage_func_status')) return -1;
  1970. if (!$userInfo) {
  1971. $userInfo = $this->getUserInfo($uid);
  1972. }
  1973. if (!$userInfo) {
  1974. return 0;
  1975. }
  1976. //上级的上级不需要检测自购
  1977. if ($is_spread) {
  1978. //开启自购
  1979. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  1980. if ($is_self_brokerage && $is_spread) {
  1981. return $uid;
  1982. }
  1983. }
  1984. //绑定类型
  1985. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  1986. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  1987. return $userInfo['spread_uid'];
  1988. }
  1989. //分销绑定类型为时间段且没过期
  1990. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  1991. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  1992. return $userInfo['spread_uid'];
  1993. }
  1994. return -1;
  1995. }
  1996. /**
  1997. * 获取事业部/代理/员工列表
  1998. * @param array $where
  1999. * @param string $field
  2000. * @return array
  2001. * @throws \think\db\exception\DataNotFoundException
  2002. * @throws \think\db\exception\DbException
  2003. * @throws \think\db\exception\ModelNotFoundException
  2004. */
  2005. public function getDivisionList(array $where = [], string $field = '*')
  2006. {
  2007. [$page, $limit] = $this->getPageValue();
  2008. $list = $this->dao->getList($where, $field, $page, $limit);
  2009. $count = $this->dao->count($where);
  2010. return compact('list', 'count');
  2011. }
  2012. /**
  2013. * 添加编辑用户信息时候的信息
  2014. * @param $uid
  2015. * @return array
  2016. * @throws \think\db\exception\DataNotFoundException
  2017. * @throws \think\db\exception\DbException
  2018. * @throws \think\db\exception\ModelNotFoundException
  2019. */
  2020. public function getUserSaveInfo($uid)
  2021. {
  2022. /** @var UserLabelServices $userLabelServices */
  2023. $userLabelServices = app()->make(UserLabelServices::class);
  2024. /** @var UserLabelRelationServices $userLabelRelationServices */
  2025. $userLabelRelationServices = app()->make(UserLabelRelationServices::class);
  2026. /** @var UserLabelCateServices $userLabelCateServices */
  2027. $userLabelCateServices = app()->make(UserLabelCateServices::class);
  2028. /** @var UserGroupServices $userGroupServices */
  2029. $userGroupServices = app()->make(UserGroupServices::class);
  2030. /** @var SystemUserLevelServices $systemUserLevelServices */
  2031. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  2032. $userInfo = $this->dao->get($uid);
  2033. if ($userInfo) {
  2034. $label_ids = $userLabelRelationServices->getUserLabels($uid);
  2035. $userInfo['label_id'] = !empty($label_ids) ? $userLabelServices->getLabelList(['ids' => $label_ids], ['id', 'label_name']) : [];
  2036. $userInfo['birthday'] = date('Y-m-d', (int)$userInfo['birthday']);
  2037. }
  2038. $levelInfo = $systemUserLevelServices->getWhereLevelList([], 'id,name');
  2039. $groupInfo = $userGroupServices->getGroupList();
  2040. $labelInfo = $userLabelCateServices->getUserLabel($uid);
  2041. return compact('userInfo', 'levelInfo', 'groupInfo', 'labelInfo');
  2042. }
  2043. /**
  2044. * 新用户注册奖励
  2045. * @param int $id
  2046. * @return bool
  2047. * @throws Exception
  2048. *
  2049. * @date 2022/09/28
  2050. * @author yyw
  2051. */
  2052. public function rewardNewUser(int $id)
  2053. {
  2054. $user = $this->getUserInfo($id);
  2055. if (!$user) {
  2056. throw new AdminException(100026);
  2057. }
  2058. $res1 = false;
  2059. $res2 = false;
  2060. $reward_money = sys_config('reward_money');
  2061. $reward_integral = sys_config('reward_integral');
  2062. $edit = array();
  2063. if ($reward_money > 0) {//余额增加
  2064. /** @var UserMoneyServices $userMoneyServices */
  2065. $userMoneyServices = app()->make(UserMoneyServices::class);
  2066. $edit['now_money'] = bcadd($user['now_money'], $reward_money, 2);
  2067. $res1 = $userMoneyServices->income('register_system_add', $user['uid'], $reward_money, $edit['now_money'], 1);
  2068. } else {
  2069. $res1 = true;
  2070. }
  2071. if ($reward_integral > 0) {//积分增加
  2072. /** @var UserBillServices $userBill */
  2073. $userBill = app()->make(UserBillServices::class);
  2074. $integral_data = ['link_id' => 1, 'number' => $reward_integral];
  2075. $edit['integral'] = bcadd($user['integral'], $reward_integral, 2);
  2076. $integral_data['balance'] = $edit['integral'];
  2077. $integral_data['title'] = '新用户注册增加积分';
  2078. $integral_data['mark'] = '新用户注册增加了' . floatval($reward_integral) . '积分';
  2079. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  2080. } else {
  2081. $res2 = true;
  2082. }
  2083. if ($edit) $res3 = $this->dao->update($id, $edit);
  2084. else $res3 = true;
  2085. if ($res1 && $res2 && $res3)
  2086. return true;
  2087. else throw new AdminException(100007);
  2088. }
  2089. /**
  2090. * 推送用户信息
  2091. * @param $data
  2092. * @param $pushUrl
  2093. * @return bool
  2094. */
  2095. public function userUpdate($data, $pushUrl)
  2096. {
  2097. return out_push($pushUrl, $data, '更新用户信息');
  2098. }
  2099. }