UserServices.php 91 KB

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