UserServices.php 87 KB

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