UserServices.php 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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']]
  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['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  1506. $user['avatar'] = strpos($user['avatar'], '/statics/system_images/') !== false ? set_file_url($user['avatar']) : $user['avatar'];
  1507. return $user;
  1508. }
  1509. /**
  1510. * 用户资金统计
  1511. * @param int $uid
  1512. */
  1513. public function balance(int $uid)
  1514. {
  1515. $userInfo = $this->getUserInfo($uid);
  1516. if (!$userInfo) {
  1517. throw new AdminException(400214);
  1518. }
  1519. /** @var UserBillServices $userBill */
  1520. $userBill = app()->make(UserBillServices::class);
  1521. /** @var StoreOrderServices $storeOrder */
  1522. $storeOrder = app()->make(StoreOrderServices::class);
  1523. $user['now_money'] = $userInfo['now_money'];//当前总资金
  1524. $user['recharge'] = $userBill->getRechargeSum($uid);//累计充值
  1525. $user['orderStatusSum'] = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1526. return $user;
  1527. }
  1528. /**
  1529. * 用户修改信息
  1530. * @param Request $request
  1531. * @return mixed
  1532. */
  1533. public function eidtNickname(int $uid, array $data)
  1534. {
  1535. if (!$this->dao->count(['uid' => $uid])) {
  1536. throw new ApiException(400214);
  1537. }
  1538. if (!$this->dao->update($uid, $data, 'uid')) {
  1539. throw new ApiException(100007);
  1540. }
  1541. return true;
  1542. }
  1543. /**
  1544. * 获取推广人排行
  1545. * @param $data 查询条件
  1546. * @return array
  1547. * @throws \think\db\exception\DataNotFoundException
  1548. * @throws \think\db\exception\ModelNotFoundException
  1549. * @throws \think\exception\DbException
  1550. */
  1551. public function getRankList(array $data)
  1552. {
  1553. $startTime = strtotime('this week Monday');
  1554. $endTime = time();
  1555. switch ($data['type']) {
  1556. case 'week':
  1557. $startTime = strtotime('this week Monday');
  1558. break;
  1559. case 'month':
  1560. $startTime = strtotime('last month');
  1561. break;
  1562. }
  1563. [$page, $limit] = $this->getPageValue();
  1564. $field = 't0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar';
  1565. return $this->dao->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  1566. }
  1567. /**
  1568. * 静默绑定推广人
  1569. * @param int $uid
  1570. * @param int $spreadUid
  1571. * @param $code
  1572. * @return bool
  1573. * @throws \think\db\exception\DataNotFoundException
  1574. * @throws \think\db\exception\DbException
  1575. * @throws \think\db\exception\ModelNotFoundException
  1576. */
  1577. public function spread(int $uid, int $spreadUid, $code)
  1578. {
  1579. $userInfo = $this->dao->getOne(['uid' => $uid], 'uid,spread_uid,spread_time,add_time,last_time');
  1580. if (!$userInfo) {
  1581. throw new ApiException(100026);
  1582. }
  1583. if ($code && !$spreadUid) {
  1584. /** @var QrcodeServices $qrCode */
  1585. $qrCode = app()->make(QrcodeServices::class);
  1586. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  1587. $spreadUid = $info['third_id'];
  1588. }
  1589. }
  1590. if ($spreadUid == 0) return '不绑定';
  1591. $userSpreadUid = $this->dao->value(['uid' => $spreadUid], 'spread_uid');
  1592. //记录好友关系
  1593. if ($spreadUid && $uid && $spreadUid != $uid) {
  1594. /** @var UserFriendsServices $serviceFriend */
  1595. $serviceFriend = app()->make(UserFriendsServices::class);
  1596. $serviceFriend->saveFriend([
  1597. 'uid' => $uid,
  1598. 'friends_uid' => $spreadUid,
  1599. ]);
  1600. }
  1601. $check = false;
  1602. if (sys_config('brokerage_bindind') == 1) {
  1603. if (sys_config('store_brokerage_binding_status') == 1) {
  1604. if (!$userInfo['spread_uid']) {
  1605. $check = true;
  1606. }
  1607. } elseif (sys_config('store_brokerage_binding_status') == 2 && (($userInfo['spread_time'] + (sys_config('store_brokerage_binding_time') * 86400)) < time())) {
  1608. $check = true;
  1609. } elseif (sys_config('store_brokerage_binding_status') == 3) {
  1610. $check = true;
  1611. }
  1612. } elseif (sys_config('brokerage_bindind') == 2) {
  1613. if ($userInfo['add_time'] == $userInfo['last_time']) {
  1614. $check = true;
  1615. }
  1616. }
  1617. if ($userInfo['uid'] == $spreadUid || $userInfo['uid'] == $userSpreadUid) $check = false;
  1618. if ($check) {
  1619. $spreadInfo = $this->dao->get($spreadUid, ['division_id', 'agent_id', 'staff_id']);
  1620. $data = [];
  1621. $data['spread_uid'] = $spreadUid;
  1622. $data['spread_time'] = time();
  1623. $data['division_id'] = $spreadInfo['division_id'];
  1624. $data['agent_id'] = $spreadInfo['agent_id'];
  1625. $data['staff_id'] = $spreadInfo['staff_id'];
  1626. if (!$this->dao->update($uid, $data, 'uid')) {
  1627. throw new ApiException(410288);
  1628. }
  1629. return '绑定上级成功,上级uid为' . $spreadUid;
  1630. } else {
  1631. return '不绑定';
  1632. }
  1633. }
  1634. /**
  1635. * 添加访问记录
  1636. * @param Request $request
  1637. * @return mixed
  1638. */
  1639. public function setVisit(array $data)
  1640. {
  1641. $userInfo = $this->getUserInfo($data['uid'], 'uid,user_type');
  1642. if (!$userInfo) {
  1643. throw new ApiException(100026);
  1644. }
  1645. $data['channel_type'] = $userInfo['user_type'];
  1646. $data['add_time'] = time();
  1647. /** @var WechatUserServices $wechatUserServices */
  1648. $wechatUserServices = app()->make(WechatUserServices::class);
  1649. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid'], 'user_type' => $userInfo['user_type']]);
  1650. if (!$wechatUser) {
  1651. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid']]);
  1652. }
  1653. if ($wechatUser) {
  1654. $data['province'] = $wechatUser['province'];
  1655. }
  1656. /** @var UserVisitServices $userVisit */
  1657. $userVisit = app()->make(UserVisitServices::class);
  1658. if ($userVisit->save($data)) {
  1659. return true;
  1660. } else {
  1661. throw new ApiException(100015);
  1662. }
  1663. }
  1664. /**
  1665. * 获取活动状态
  1666. * @return mixed
  1667. */
  1668. public function activity()
  1669. {
  1670. /** @var StoreBargainServices $storeBragain */
  1671. $storeBragain = app()->make(StoreBargainServices::class);
  1672. /** @var StoreCombinationServices $storeCombinaion */
  1673. $storeCombinaion = app()->make(StoreCombinationServices::class);
  1674. /** @var StoreSeckillServices $storeSeckill */
  1675. $storeSeckill = app()->make(StoreSeckillServices::class);
  1676. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  1677. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  1678. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  1679. return $data;
  1680. }
  1681. /**
  1682. * 获取用户下级推广人
  1683. * @param int $uid 当前用户
  1684. * @param int $grade 等级 0 一级 1 二级
  1685. * @param string $orderBy 排序
  1686. * @param string $keyword
  1687. * @return array|bool
  1688. */
  1689. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '')
  1690. {
  1691. $user = $this->getUserInfo($uid);
  1692. if (!$user) {
  1693. throw new AdminException(400214);
  1694. }
  1695. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  1696. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  1697. $data = [
  1698. 'total' => count($spread_one_ids),
  1699. 'totalLevel' => count($spread_two_ids),
  1700. 'list' => []
  1701. ];
  1702. if (sys_config('brokerage_level', 2) == 1) {
  1703. $data['count'] = $data['total'];
  1704. } else {
  1705. $data['count'] = $data['total'] + $data['totalLevel'];
  1706. }
  1707. /** @var UserStoreOrderServices $userStoreOrder */
  1708. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  1709. $list = [];
  1710. if ($grade == 0) {
  1711. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword);
  1712. } else {
  1713. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword);
  1714. }
  1715. foreach ($list as &$item) {
  1716. if (isset($item['spread_time']) && $item['spread_time']) {
  1717. $item['time'] = date('Y/m/d', $item['spread_time']);
  1718. }
  1719. }
  1720. $data['list'] = $list;
  1721. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  1722. return $data;
  1723. }
  1724. /**
  1725. * 获取推广人uids
  1726. * @param int $uid
  1727. * @param bool $one
  1728. * @return array
  1729. */
  1730. public function getUserSpredadUids(int $uid, int $type = 0)
  1731. {
  1732. $uids = $this->dao->getColumn(['spread_uid' => $uid, 'is_del' => 0], 'uid');
  1733. if ($type === 1) {
  1734. return $uids;
  1735. }
  1736. if ($uids) {
  1737. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids], ['is_del', '=', 0]], 'uid');
  1738. if ($type === 2) {
  1739. return $uidsTwo;
  1740. }
  1741. if ($uidsTwo) {
  1742. $uids = array_merge($uids, $uidsTwo);
  1743. }
  1744. }
  1745. return $uids;
  1746. }
  1747. /**
  1748. * 检测用户是否是推广员
  1749. * @param int $uid
  1750. * @param $user
  1751. * @return bool
  1752. */
  1753. public function checkUserPromoter(int $uid, $user = [])
  1754. {
  1755. if (!$user) {
  1756. $user = $this->getUserInfo($uid, 'spread_open,is_promoter');
  1757. }
  1758. if (!$user) {
  1759. return false;
  1760. }
  1761. //分销是否开启
  1762. if (!sys_config('brokerage_func_status')) {
  1763. return false;
  1764. }
  1765. //用户分校推广资格是否开启4.0.32
  1766. if (isset($user['spread_open']) && !$user['spread_open']) {
  1767. return false;
  1768. }
  1769. /** @var StoreOrderServices $storeOrder */
  1770. $storeOrder = app()->make(StoreOrderServices::class);
  1771. $sumPrice = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1772. $store_brokerage_statu = sys_config('store_brokerage_statu');
  1773. $store_brokerage_price = sys_config('store_brokerage_price');
  1774. if ($user['is_promoter'] || $store_brokerage_statu == 2 || ($store_brokerage_statu == 3 && $sumPrice > $store_brokerage_price)) {
  1775. return true;
  1776. }
  1777. return false;
  1778. }
  1779. /**
  1780. * 同步微信粉丝用户(后台接口)
  1781. * @return bool
  1782. */
  1783. public function syncWechatUsers()
  1784. {
  1785. $appid = sys_config('wechat_appid');
  1786. $appSecret = sys_config('wechat_appsecret');
  1787. if (!$appid || !$appSecret) {
  1788. throw new AdminException(400236);
  1789. }
  1790. $key = md5('sync_wechat_users');
  1791. //一天点击一次
  1792. if (CacheService::get($key)) {
  1793. return true;
  1794. }
  1795. $next_openid = null;
  1796. do {
  1797. $result = WechatService::getUsersList($next_openid);
  1798. $userOpenids = $result['data'];
  1799. //拆分大数组
  1800. $opemidArr = array_chunk($userOpenids, 100);
  1801. foreach ($opemidArr as $openids) {
  1802. //加入同步|更新用户队列
  1803. UserJob::dispatch([$openids]);
  1804. }
  1805. $next_openid = $result['next_openid'];
  1806. } while ($next_openid != null);
  1807. CacheService::set($key, 1, 3600 * 24);
  1808. return true;
  1809. }
  1810. /**
  1811. * 导入微信粉丝用户
  1812. * @param array $openids
  1813. * @return bool
  1814. */
  1815. public function importUser(array $noBeOpenids)
  1816. {
  1817. if (!$noBeOpenids) {
  1818. return true;
  1819. }
  1820. $dataAll = $data = [];
  1821. $time = time();
  1822. foreach ($noBeOpenids as $openid) {
  1823. try {
  1824. $info = WechatService::getUserInfo($openid);
  1825. $info = is_object($info) ? $info->toArray() : $info;
  1826. } catch (\Throwable $e) {
  1827. $info = [];
  1828. }
  1829. if (!$info) continue;
  1830. $data['nickname'] = $info['nickname'] ?? '';
  1831. $data['headimgurl'] = $info['headimgurl'] ?? '';
  1832. $userInfoData = $this->setUserInfo($data);
  1833. if (!$userInfoData) {
  1834. throw new AdminException(400703);
  1835. }
  1836. $data['uid'] = $userInfoData['uid'];
  1837. $data['subscribe'] = $info['subscribe'] ?? 1;
  1838. $data['unionid'] = $info['unionid'] ?? '';
  1839. $data['openid'] = $info['openid'] ?? '';
  1840. $data['sex'] = $info['sex'] ?? 0;
  1841. $data['language'] = $info['language'] ?? '';
  1842. $data['city'] = $info['city'] ?? '';
  1843. $data['province'] = $info['province'] ?? '';
  1844. $data['country'] = $info['country'] ?? '';
  1845. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  1846. $data['groupid'] = $info['groupid'] ?? 0;
  1847. $data['remark'] = $info['remark'] ?? '';
  1848. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  1849. $data['add_time'] = $time;
  1850. $data['is_complete'] = 1;
  1851. $dataAll[] = $data;
  1852. }
  1853. if ($dataAll) {
  1854. /** @var WechatUserServices $wechatUser */
  1855. $wechatUser = app()->make(WechatUserServices::class);
  1856. if (!$wechatUser->saveAll($dataAll)) {
  1857. throw new AdminException(400703);
  1858. }
  1859. }
  1860. return true;
  1861. }
  1862. /** 修改会员的时间及是否会员状态
  1863. * @param int $vip_day 会员天数
  1864. * @param array $user_id 用户id
  1865. * @param int $is_money_level 会员来源途径
  1866. * @param bool $member_type 会员卡类型
  1867. * @return mixed
  1868. * @throws \think\db\exception\DataNotFoundException
  1869. * @throws \think\db\exception\DbException
  1870. * @throws \think\db\exception\ModelNotFoundException
  1871. */
  1872. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  1873. {
  1874. if ($vip_day == 0) throw new ApiException(410289);
  1875. $user_info = $this->getUserInfo($user_id, 'is_money_level,overdue_time');
  1876. if (!$user_info) throw new ApiException(410032);
  1877. if (!$member_type) $member_type = "month";
  1878. if ($member_type == 'ever') {
  1879. $overdue_time = 0;
  1880. $is_ever_level = 1;
  1881. } else {
  1882. if ($user_info['is_money_level'] == 0) {
  1883. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  1884. } else {
  1885. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  1886. }
  1887. $is_ever_level = 0;
  1888. }
  1889. $setData['overdue_time'] = $overdue_time;
  1890. $setData['is_ever_level'] = $is_ever_level;
  1891. $setData['is_money_level'] = $is_money_level ?: 0;
  1892. // if ($user_info['level'] == 0) $setData['level'] = 1;
  1893. return $this->dao->update(['uid' => $user_id], $setData);
  1894. }
  1895. /**
  1896. * 会员过期改变状态,变为普通会员
  1897. * @param $uid
  1898. * @param $userInfo
  1899. * @return bool
  1900. * @throws \think\db\exception\DataNotFoundException
  1901. * @throws \think\db\exception\DbException
  1902. * @throws \think\db\exception\ModelNotFoundException
  1903. */
  1904. public function offMemberLevel($uid, $userInfo = [])
  1905. {
  1906. if (!$uid) return false;
  1907. if (!$userInfo) {
  1908. $userInfo = $this->dao->get($uid, ['is_ever_level', 'is_money_level', 'overdue_time']);
  1909. }
  1910. if (!$userInfo) return false;
  1911. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  1912. $this->dao->update(['uid' => $uid], ['is_money_level' => 0/*, 'overdue_time' => 0*/]);
  1913. return false;
  1914. }
  1915. return true;
  1916. }
  1917. /**
  1918. * @param array $where
  1919. * @return array
  1920. * @throws \think\db\exception\DataNotFoundException
  1921. * @throws \think\db\exception\DbException
  1922. * @throws \think\db\exception\ModelNotFoundException
  1923. */
  1924. public function getUserInfoList(array $where, $field = "*")
  1925. {
  1926. return $this->dao->getUserInfoList($where, $field);
  1927. }
  1928. /**
  1929. * 增加推广用户佣金
  1930. * @param int $uid
  1931. * @param int $spread_uid
  1932. * @param array $userInfo
  1933. * @param array $spread_user
  1934. * @return bool|mixed
  1935. * @throws \think\db\exception\DataNotFoundException
  1936. * @throws \think\db\exception\DbException
  1937. * @throws \think\db\exception\ModelNotFoundException
  1938. */
  1939. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  1940. {
  1941. if (!$uid || !$spread_uid) {
  1942. return false;
  1943. }
  1944. //商城分销功能是否开启 0关闭1开启
  1945. if (!sys_config('brokerage_func_status')) return true;
  1946. if (!sys_config('brokerage_user_status')) return true;
  1947. //获取设置推广佣金单价
  1948. $brokerage_price = sys_config('uni_brokerage_price', 0);
  1949. //获取推广佣金当日限额
  1950. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  1951. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  1952. return true;
  1953. }
  1954. if (!$userInfo) {
  1955. $userInfo = $this->getUserInfo($uid);
  1956. }
  1957. if (!$userInfo) {
  1958. return false;
  1959. }
  1960. if (!$spread_user) {
  1961. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  1962. }
  1963. if (!$spread_user) {
  1964. return false;
  1965. }
  1966. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  1967. return false;
  1968. }
  1969. /** @var UserBrokerageServices $userBrokerageServices */
  1970. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1971. // -1不限制
  1972. if ($day_brokerage_price_upper != -1) {
  1973. if ($day_brokerage_price_upper <= 0) {
  1974. return true;
  1975. } else {
  1976. //获取上级用户今日获取推广用户佣金
  1977. $spread_day_brokerage = $userBrokerageServices->getUserBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  1978. //超过上限
  1979. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  1980. return true;
  1981. }
  1982. }
  1983. }
  1984. $spreadPrice = $spread_user['brokerage_price'];
  1985. // 上级推广员返佣之后的金额
  1986. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  1987. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  1988. // 添加返佣记录
  1989. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  1990. 'nickname' => $userInfo['nickname'],
  1991. 'number' => floatval($brokerage_price)
  1992. ], $balance, $uid);
  1993. // 添加用户余额
  1994. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  1995. //给上级发送获得佣金的模板消息
  1996. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  1997. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  1998. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  1999. return $res1 && $res2;
  2000. });
  2001. }
  2002. /**
  2003. * 获取上级uid
  2004. * @param int $uid
  2005. * @param array $userInfo
  2006. * @param bool $is_spread
  2007. * @return int|mixed
  2008. */
  2009. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  2010. {
  2011. if (!$uid) {
  2012. return 0;
  2013. }
  2014. //商城分销功能是否开启 0关闭1开启
  2015. if (!sys_config('brokerage_func_status')) return -1;
  2016. if (!$userInfo) {
  2017. $userInfo = $this->getUserInfo($uid);
  2018. }
  2019. if (!$userInfo) {
  2020. return 0;
  2021. }
  2022. //上级的上级不需要检测自购
  2023. if ($is_spread) {
  2024. //开启自购
  2025. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  2026. if ($is_self_brokerage && $is_spread) {
  2027. return $uid;
  2028. }
  2029. }
  2030. //绑定类型
  2031. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  2032. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  2033. return $userInfo['spread_uid'];
  2034. }
  2035. //分销绑定类型为时间段且没过期
  2036. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  2037. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  2038. return $userInfo['spread_uid'];
  2039. }
  2040. return -1;
  2041. }
  2042. /**
  2043. * 获取事业部/代理/员工列表
  2044. * @param array $where
  2045. * @param string $field
  2046. * @return array
  2047. * @throws \think\db\exception\DataNotFoundException
  2048. * @throws \think\db\exception\DbException
  2049. * @throws \think\db\exception\ModelNotFoundException
  2050. */
  2051. public function getDivisionList(array $where = [], string $field = '*')
  2052. {
  2053. [$page, $limit] = $this->getPageValue();
  2054. $list = $this->dao->getList($where, $field, $page, $limit);
  2055. $count = $this->dao->count($where);
  2056. return compact('list', 'count');
  2057. }
  2058. /**
  2059. * 添加编辑用户信息时候的信息
  2060. * @param $uid
  2061. * @return array
  2062. * @throws \think\db\exception\DataNotFoundException
  2063. * @throws \think\db\exception\DbException
  2064. * @throws \think\db\exception\ModelNotFoundException
  2065. */
  2066. public function getUserSaveInfo($uid)
  2067. {
  2068. /** @var UserLabelServices $userLabelServices */
  2069. $userLabelServices = app()->make(UserLabelServices::class);
  2070. /** @var UserLabelRelationServices $userLabelRelationServices */
  2071. $userLabelRelationServices = app()->make(UserLabelRelationServices::class);
  2072. /** @var UserLabelCateServices $userLabelCateServices */
  2073. $userLabelCateServices = app()->make(UserLabelCateServices::class);
  2074. /** @var UserGroupServices $userGroupServices */
  2075. $userGroupServices = app()->make(UserGroupServices::class);
  2076. /** @var SystemUserLevelServices $systemUserLevelServices */
  2077. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  2078. $userInfo = $this->dao->get($uid);
  2079. if ($userInfo) {
  2080. $label_ids = $userLabelRelationServices->getUserLabels($uid);
  2081. $userInfo['label_id'] = !empty($label_ids) ? $userLabelServices->getLabelList(['ids' => $label_ids], ['id', 'label_name']) : [];
  2082. $userInfo['birthday'] = date('Y-m-d', (int)$userInfo['birthday']);
  2083. }
  2084. $levelInfo = $systemUserLevelServices->getWhereLevelList([], 'id,name');
  2085. $groupInfo = $userGroupServices->getGroupList();
  2086. $labelInfo = $userLabelCateServices->getUserLabel($uid);
  2087. return compact('userInfo', 'levelInfo', 'groupInfo', 'labelInfo');
  2088. }
  2089. /**
  2090. * 新用户注册奖励
  2091. * @param int $id
  2092. * @return bool
  2093. * @throws Exception
  2094. *
  2095. * @date 2022/09/28
  2096. * @author yyw
  2097. */
  2098. public function rewardNewUser(int $id)
  2099. {
  2100. $user = $this->getUserInfo($id);
  2101. if (!$user) {
  2102. throw new AdminException(100026);
  2103. }
  2104. $res1 = false;
  2105. $res2 = false;
  2106. $reward_money = sys_config('reward_money');
  2107. $reward_integral = sys_config('reward_integral');
  2108. $edit = array();
  2109. if ($reward_money > 0) {//余额增加
  2110. /** @var UserMoneyServices $userMoneyServices */
  2111. $userMoneyServices = app()->make(UserMoneyServices::class);
  2112. $edit['now_money'] = bcadd($user['now_money'], $reward_money, 2);
  2113. $res1 = $userMoneyServices->income('register_system_add', $user['uid'], $reward_money, $edit['now_money'], 1);
  2114. } else {
  2115. $res1 = true;
  2116. }
  2117. if ($reward_integral > 0) {//积分增加
  2118. /** @var UserBillServices $userBill */
  2119. $userBill = app()->make(UserBillServices::class);
  2120. $integral_data = ['link_id' => 1, 'number' => $reward_integral];
  2121. $edit['integral'] = bcadd($user['integral'], $reward_integral, 2);
  2122. $integral_data['balance'] = $edit['integral'];
  2123. $integral_data['title'] = '新用户注册增加积分';
  2124. $integral_data['mark'] = '新用户注册增加了' . floatval($reward_integral) . '积分';
  2125. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  2126. } else {
  2127. $res2 = true;
  2128. }
  2129. if ($edit) $res3 = $this->dao->update($id, $edit);
  2130. else $res3 = true;
  2131. if ($res1 && $res2 && $res3)
  2132. return true;
  2133. else throw new AdminException(100007);
  2134. }
  2135. /**
  2136. * 推送用户信息
  2137. * @param $data
  2138. * @param $pushUrl
  2139. * @return bool
  2140. */
  2141. public function userUpdate($data, $pushUrl)
  2142. {
  2143. return out_push($pushUrl, $data, '更新用户信息');
  2144. }
  2145. }