UserServices.php 90 KB

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