UserServices.php 88 KB

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