UserServices.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. //declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\model\user\User;
  14. use app\services\system\config\SystemUserLevelServices;
  15. use app\services\wechat\WechatUserServices;
  16. use qiniu\basic\BaseServices;
  17. use qiniu\exceptions\AdminException;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\Exception;
  22. use think\exception\ValidateException;
  23. use think\facade\Log;
  24. use think\Model;
  25. /**
  26. *
  27. * Class UserServices
  28. * @package app\services\user
  29. * @mixin User
  30. */
  31. class UserServices extends BaseServices
  32. {
  33. /**
  34. * 性别
  35. * @var array
  36. */
  37. public $sex = [
  38. 0 => '保密',//保密
  39. 1 => '男',//男
  40. 2 => '女',//女
  41. ];
  42. /**
  43. * UserServices constructor.
  44. * @param User $model
  45. */
  46. public function __construct(User $model)
  47. {
  48. $this->model = $model;
  49. }
  50. /**
  51. * 获取用户信息
  52. * @param int $uid
  53. * @param string|array $field
  54. * @return mixed
  55. * @throws DataNotFoundException
  56. * @throws DbException
  57. * @throws ModelNotFoundException
  58. */
  59. public function getUserInfo(int $uid, $field = '*')
  60. {
  61. if (is_string($field)) $field = explode(',', $field);
  62. return $this->get($uid, $field);
  63. }
  64. /**
  65. * 是否存在
  66. * @param int $uid
  67. * @return bool
  68. * @throws DbException
  69. */
  70. public function userExist(int $uid)
  71. {
  72. return $this->be($uid);
  73. }
  74. /**
  75. * 获取用户列表
  76. * @param array $where
  77. * @param string $field
  78. * @return array
  79. * @throws DbException
  80. */
  81. public function getUserList(array $where, string $field = '*'): array
  82. {
  83. [$page, $limit] = $this->getPageValue();
  84. $list = $this->getList($where, $field, $page, $limit);
  85. $count = $this->getCount($where);
  86. return compact('list', 'count');
  87. }
  88. /**
  89. * 保存用户信息
  90. * @param $user
  91. * @param int $spreadUid
  92. * @param string $userType
  93. * @return User|Model
  94. * @throws DataNotFoundException
  95. * @throws DbException
  96. * @throws ModelNotFoundException
  97. */
  98. public function setUserInfo($user, int $spreadUid = 0, string $userType = 'wechat')
  99. {
  100. mt_srand();
  101. if (isset($user['pwd']) && $user['pwd']) {
  102. $password = $user['pwd'];
  103. $salt = $user['salt'] ?? '';
  104. } else {
  105. [$password, $salt] = password('123456');
  106. }
  107. $data = [
  108. 'account' => create_account(),
  109. 'pwd' => $password,
  110. 'salt' => $salt,
  111. 'nickname' => $user['nickname'] ?? '',
  112. 'avatar' => !empty($user['headimgurl']) ? $user['headimgurl'] : sys_config('h5_avatar'),
  113. 'phone' => $user['phone'] ?? '',
  114. 'birthday' => $user['birthday'] ?? '',
  115. 'user_type' => $userType
  116. ];
  117. $res = $this->create($data);
  118. if (!$res)
  119. throw new AdminException('保存用户信息失败');
  120. if ($spreadUid && $this->get($spreadUid)) {
  121. /** @var UserSpreadServices $spreadService */
  122. $spreadService = app()->make(UserSpreadServices::class);
  123. $spreadService->setSpread($res->uid, $spreadUid);
  124. }
  125. return $res;
  126. }
  127. /**
  128. * 设置用户登录类型
  129. * @param int $uid
  130. * @param string $type
  131. * @return bool
  132. * @throws Exception
  133. */
  134. public function setLoginType(int $uid, string $type = 'h5')
  135. {
  136. if (!$this->update($uid, ['login_type' => $type]))
  137. throw new Exception('设置登录类型失败');
  138. return true;
  139. }
  140. /**
  141. * 设置用户分组
  142. * @param $uids
  143. * @param int $group_id
  144. * @return Model|null
  145. */
  146. public function setUserGroup($uids, int $group_id)
  147. {
  148. return $this->batchUpdate($uids, ['group_id' => $group_id], 'uid');
  149. }
  150. /**
  151. * 显示资源列表头部
  152. * @return array[]
  153. */
  154. public function typeHead()
  155. {
  156. /** @var UserWechatuserServices $userWechatUser */
  157. $userWechatUser = app()->make(UserWechatuserServices::class);
  158. //全部会员
  159. $all = $this->getCount([]);
  160. //小程序会员
  161. $routine = $userWechatUser->getCount(['w.user_type' => 'routine']);
  162. //公众号会员
  163. $wechat = $userWechatUser->getCount(['w.user_type' => 'wechat']);
  164. //H5会员
  165. $h5 = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'h5']);
  166. //pc会员
  167. $pc = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'pc']);
  168. //app会员
  169. $app = $userWechatUser->getCount(['w.user_type' => 'app']);
  170. return [
  171. ['user_type' => '', 'name' => '全部会员', 'count' => $all],
  172. ['user_type' => 'routine', 'name' => '小程序会员', 'count' => $routine],
  173. ['user_type' => 'wechat', 'name' => '公众号会员', 'count' => $wechat],
  174. ['user_type' => 'h5', 'name' => 'H5会员', 'count' => $h5],
  175. ['user_type' => 'pc', 'name' => 'PC会员', 'count' => $pc],
  176. ['user_type' => 'app', 'name' => 'APP会员', 'count' => $app],
  177. ];
  178. }
  179. /**
  180. * 会员列表
  181. * @param array $where
  182. * @return array
  183. * @throws DataNotFoundException
  184. * @throws DbException
  185. * @throws ModelNotFoundException
  186. */
  187. public function userIndex(array $where)
  188. {
  189. /** @var UserWechatuserServices $userWechatUser */
  190. $userWechatUser = app()->make(UserWechatuserServices::class);
  191. $fields = 'u.*,w.country,w.province,w.city,u.sex,w.unionid,w.openid,w.user_type as w_user_type,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time';
  192. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  193. if ($list) {
  194. /** @var UserGroupServices $userGroup */
  195. $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
  196. $spreadUids = array_unique(array_column($list, 'spread_uid'));
  197. $spread_names = $this->getColumn([['uid', 'in', $spreadUids]], 'nickname', 'uid');
  198. foreach ($list as &$item) {
  199. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  200. $item['delete'] = ($item['delete_time'] != null) ? '已注销' : '正常';
  201. $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
  202. $item['spread_uid_nickname'] = $item['spread_uid'] ? (($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid']) : '无';
  203. //用户类型
  204. if ($item['user_type'] == 'routine') {
  205. $item['user_type'] = '小程序';
  206. } else if ($item['user_type'] == 'wechat') {
  207. $item['user_type'] = '公众号';
  208. } else if ($item['user_type'] == 'h5') {
  209. $item['user_type'] = 'H5';
  210. } else if ($item['user_type'] == 'pc') {
  211. $item['user_type'] = 'PC';
  212. } else if ($item['user_type'] == 'app') {
  213. $item['user_type'] = 'APP';
  214. } else $item['user_type'] = '其他';
  215. if ($item['sex'] == 1) {
  216. $item['sex'] = '男';
  217. } else if ($item['sex'] == 2) {
  218. $item['sex'] = '女';
  219. } else $item['sex'] = '保密';
  220. //等级名称
  221. /** @var UserLevelServices $levelService */
  222. $levelService = app()->make(UserLevelServices::class);
  223. $levelInfo = $levelService->getUserLevelInfo($item['uid']);
  224. //分组名称
  225. $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
  226. //用户等级
  227. $item['vip_name'] = $levelInfo['name'] ?? '无';
  228. }
  229. }
  230. return compact('count', 'list');
  231. }
  232. /**
  233. * 修改提交处理
  234. * @param int $id
  235. * @param array $data
  236. * @return mixed
  237. * @throws DataNotFoundException
  238. * @throws DbException
  239. * @throws ModelNotFoundException
  240. */
  241. public function updateInfo(int $id, array $data)
  242. {
  243. $user = $this->getUserInfo($id);
  244. if (!$user) {
  245. throw new AdminException('数据不存在!');
  246. }
  247. $edit = [];
  248. if ($data['phone']) {
  249. $otherUser = $this->getOne(['phone' => $data['phone']], 'uid,phone');
  250. if ($otherUser && $otherUser['uid'] != $id) {
  251. throw new AdminException('该手机号码已被注册');
  252. }
  253. $otherUser = $this->getOne(['account' => $data['phone']], 'uid,account');
  254. if ($otherUser && $otherUser['uid'] != $id) {
  255. throw new AdminException('该手机号码已被注册');
  256. }
  257. if ($data['phone'] != $user['phone']) {
  258. $edit['phone'] = $data['phone'];
  259. $edit['account'] = $data['phone'];
  260. }
  261. }
  262. if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
  263. $edit['pwd'] = $data['pwd'];
  264. $edit['salt'] = $data['salt'];
  265. }
  266. if (isset($data['trade_pwd']) && $data['trade_pwd'] && $data['trade_pwd'] != $user['trade_pwd']) {
  267. $edit['trade_pwd'] = $data['trade_pwd'];
  268. $edit['trade_salt'] = $data['trade_salt'];
  269. }
  270. $edit['status'] = $data['status'];
  271. $edit['real_name'] = $data['real_name'];
  272. $edit['card_id'] = $data['card_id'];
  273. $edit['birthday'] = strtotime($data['birthday']);
  274. $edit['mark'] = $data['mark'];
  275. $edit['is_promoter'] = (isset($data['is_promoter']) && $data['is_promoter'] >= 0) ? $data['is_promoter'] : $user['is_promoter'];
  276. $edit['group_id'] = $data['group_id'];
  277. if ($data['spread_uid'] >= 0 && $data['spread_uid'] != $user['spread_uid']) {
  278. /** @var UserSpreadServices $spreadService */
  279. $spreadService = app()->make(UserSpreadServices::class);
  280. $spreadService->adminSetSpread($user['uid'], $data['spread_uid']);
  281. }
  282. $edit['sex'] = $data['sex'];
  283. $edit['provincials'] = $data['provincials'];
  284. $edit['province'] = $data['province'];
  285. $edit['city'] = $data['city'];
  286. $edit['area'] = $data['area'];
  287. $edit['street'] = $data['street'];
  288. if ($user['level'] != $data['level']) {
  289. $this->saveGiveLevel((int)$user['uid'], (int)$data['level']);
  290. }
  291. /** @var WechatUserServices $wechatUser */
  292. $wechatUser = app()->make(WechatUserServices::class);
  293. $wechatUser->update(['uid' => $id], ['sex' => $data['sex']]);
  294. $res = $this->update($id, $edit);
  295. if ($res) {
  296. return true;
  297. } else
  298. throw new AdminException('修改失败');
  299. }
  300. public function updateAccount($uid, $money_type, $pm, $num, $mark = '', $admin_id = 0)
  301. {
  302. if ($money_type == 'now_money') {
  303. /** @var UserMoneyServices $service */
  304. $service = app()->make(UserMoneyServices::class);
  305. $type = ($pm == 1 ? 'system_add' : 'system_sub');
  306. } elseif ($money_type == 'brokerage_price') {
  307. /** @var UserBrokerageServices $service */
  308. $service = app()->make(UserBrokerageServices::class);
  309. $type = ($pm == 1 ? 'system_add' : 'system_sub');
  310. } else {
  311. /** @var UserBillServices $service */
  312. $service = app()->make(UserBillServices::class);
  313. $type = ($pm == 1 ? 'system_add_' . $money_type : 'system_sub_' . $money_type);
  314. }
  315. $user = $this->getUserInfo($uid);
  316. if (!isset($user[$money_type])) throw new AdminException('请选择要操作的数据');
  317. return $service->income($type, $uid, ['number' => $num, 'mark' => $mark ? ',' . $mark : ''], $admin_id);
  318. }
  319. /**
  320. * 执行赠送会员等级
  321. * @param int $id
  322. * @param int $level_id
  323. * @return mixed
  324. * @throws DbException
  325. */
  326. public function saveGiveLevel(int $id, int $level_id)
  327. {
  328. if (!$this->userExist($id)) {
  329. throw new AdminException('用户不存在');
  330. }
  331. /** @var SystemUserLevelServices $systemLevelServices */
  332. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  333. /** @var UserLevelServices $userLevelServices */
  334. $userLevelServices = app()->make(UserLevelServices::class);
  335. //查询当前选择的会员等级
  336. $systemLevel = $systemLevelServices->getLevel($level_id);
  337. if (!$systemLevel) throw new AdminException('您选择赠送的用户等级不存在!');
  338. //检查是否拥有此会员等级
  339. $level = $userLevelServices->getOne(['uid' => $id, 'level_id' => $level_id, 'valid' => 1]);
  340. if ($level) {
  341. throw new AdminException('此用户已有该用户等级,无法再次赠送');
  342. }
  343. //保存会员信息
  344. if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
  345. throw new AdminException('赠送失败');
  346. }
  347. $spread_uid = $this->getUserInfo($id)['spread_uid'];
  348. if ($spread_uid > 0) {
  349. /** @var UserLevelServices $level */
  350. $level = app()->make(UserSpreadServices::class);
  351. $level->detection($spread_uid);
  352. }
  353. return true;
  354. }
  355. /**
  356. * 清除会员等级
  357. * @paran int $uid
  358. * @paran boolean
  359. * */
  360. public function cleanUpLevel($uid)
  361. {
  362. if (!$this->userExist($uid))
  363. throw new AdminException('用户不存在');
  364. /** @var UserLevelServices $services */
  365. $services = app()->make(UserLevelServices::class);
  366. return $this->transaction(function () use ($uid, $services) {
  367. $res = $services->delUserLevel($uid);
  368. $res1 = $this->update($uid, ['clean_time' => time(), 'level' => 0], 'uid');
  369. if (!$res && !$res1)
  370. throw new AdminException('清除失败');
  371. return true;
  372. });
  373. }
  374. /**
  375. * 获取单个用户信息
  376. * @param int $id 用户id
  377. * @param string $type
  378. * @param array $where
  379. * @return mixed
  380. * @throws DbException
  381. */
  382. public function oneUserInfo(int $id, string $type, $where = [])
  383. {
  384. switch ($type) {
  385. case 'spread':
  386. [$page, $limit] = $this->getPageValue();
  387. return $this->getList(['spread_uid' => $id], 'uid,avatar,nickname,real_name,spread_uid,spread_time,is_promoter,spread_count,user_type', $page, $limit);
  388. break;
  389. case 'bill_log':
  390. /** @var UserBillServices $services */
  391. $category = $where['category'] ?? 'integral';
  392. $time = $where['time'] ?? [];
  393. $services = app()->make(UserBillServices::class);
  394. return $services->getBillList($id, $category, $time, 'title,category,number,balance,mark,add_time');
  395. break;
  396. case 'balance_log':
  397. /** @var UserMoneyServices $services */
  398. $services = app()->make(UserMoneyServices::class);
  399. $where['uid'] = $id;
  400. return $services->getMoneyList($where, 'title,type,number,balance,mark,pm,status,add_time');
  401. break;
  402. case 'brokerage_log':
  403. /** @var UserBrokerageServices $services */
  404. $services = app()->make(UserBrokerageServices::class);
  405. $where['uid'] = $id;
  406. return $services->getBrokerageList($where, 'title,type,number,balance,mark,pm,status,add_time,frozen_time');
  407. break;
  408. default:
  409. throw new AdminException('type参数错误');
  410. }
  411. }
  412. /***********************************************/
  413. /************ 前端api services *****************/
  414. /***********************************************/
  415. /**
  416. * 用户信息
  417. * @param $info
  418. * @return mixed
  419. */
  420. public function userInfo($info)
  421. {
  422. $uid = (int)$info['uid'];
  423. return $info;
  424. }
  425. /**
  426. * 个人中心
  427. * @param array $user
  428. */
  429. public function personalHome(array $user, $tokenData)
  430. {
  431. $uid = (int)$user['uid'];
  432. event('user.login', [$uid, app('request')->ip()]);
  433. /** @var StoreCouponUserServices $storeCoupon */
  434. $storeCoupon = app()->make(StoreCouponUserServices::class);
  435. /** @var UserMoneyServices $userMoneyServices */
  436. $userMoneyServices = app()->make(UserMoneyServices::class);
  437. /** @var UserExtractServices $userExtract */
  438. $userExtract = app()->make(UserExtractServices::class);
  439. /** @var StoreOrderServices $storeOrder */
  440. $storeOrder = app()->make(StoreOrderServices::class);
  441. /** @var UserLevelServices $userLevel */
  442. $userLevel = app()->make(UserLevelServices::class);
  443. /** @var StoreServiceServices $storeService */
  444. $storeService = app()->make(StoreServiceServices::class);
  445. /** @var WechatUserServices $wechatUser */
  446. $wechatUser = app()->make(WechatUserServices::class);
  447. /** @var UserRelationServices $productRelation */
  448. $productRelation = app()->make(UserRelationServices::class);
  449. /** @var SystemMessageServices $systemMessageServices */
  450. $systemMessageServices = app()->make(SystemMessageServices::class);
  451. /** @var DiyServices $diyServices */
  452. $diyServices = app()->make(DiyServices::class);
  453. /** @var AgentLevelServices $agentLevelServices */
  454. $agentLevelServices = app()->make(AgentLevelServices::class);
  455. /** @var StoreProductLogServices $storeProductLogServices */
  456. $storeProductLogServices = app()->make(StoreProductLogServices::class);
  457. //是否存在核销码
  458. if (!$user['bar_code']) {
  459. $bar_code = $this->getBarCode();
  460. $this->dao->update($uid, ['bar_code' => $bar_code], 'uid');
  461. $user['bar_code'] = $bar_code;
  462. }
  463. //获取配置参数
  464. $configData = SystemConfigService::more([
  465. 'member_card_status',
  466. 'brokerage_func_status',
  467. 'store_brokerage_statu',
  468. 'store_brokerage_price',
  469. 'member_func_status',
  470. 'recharge_switch',
  471. 'extract_time',
  472. 'balance_func_status',
  473. 'invoice_func_status',
  474. 'special_invoice_status',
  475. 'user_extract_bank_status',
  476. 'user_extract_wechat_status',
  477. 'user_extract_alipay_status',
  478. 'level_activate_status'
  479. ]);
  480. //看付费会员是否开启
  481. $user['is_open_member'] = $user['svip_open'] = !!($configData['member_card_status'] ?? 0);
  482. $user['agent_level_name'] = '';
  483. //分销等级信息
  484. if ($user['agent_level']) {
  485. $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name');
  486. $user['agent_level_name'] = $levelInfo && $levelInfo['name'] ? $levelInfo['name'] : '';
  487. }
  488. $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
  489. $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
  490. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  491. $user['like'] = $productRelation->getUserCount($uid, 0, 'like');
  492. $collectCategory = sys_config('video_func_status', 1) ? '' : 'product';
  493. $user['collectCount'] = $productRelation->getUserCount($uid, 0, 'collect', $collectCategory);
  494. $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
  495. $user['notice'] = 0;
  496. $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
  497. $user['orderStatusSum'] = (float)$userMoneyServices->sum(['uid' => $uid, 'pm' => 0, 'status' => 1], 'number', true);
  498. $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
  499. $user['extractPrice'] = $user['brokerage_price'];//可提现
  500. $user['statu'] = (int)($configData['store_brokerage_statu'] ?? 0);
  501. $orderStatusSum = (float)$storeOrder->sum(['pid' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'uid' => $user['uid'], 'is_del' => 0], 'pay_price', true);//累计有效消费
  502. $user['spread_status'] = ($configData['brokerage_func_status'] ?? 1) && $this->checkUserPromoter($user['uid'], $user, $orderStatusSum);
  503. if (!$user['is_promoter'] && $user['spread_status']) {
  504. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  505. $user['is_promoter'] = 1;
  506. }
  507. if ($user['statu'] == 3) {
  508. $storeBrokeragePrice = $configData['store_brokerage_price'] ?? 0;
  509. $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$user['orderStatusSum'], 2);
  510. }
  511. /** @var UserBrokerageServices $userBrokerageServices */
  512. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  513. $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  514. if ($user['broken_commission'] < 0)
  515. $user['broken_commission'] = 0;
  516. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  517. if ($user['commissionCount'] < 0)
  518. $user['commissionCount'] = 0;
  519. //用户等级信息
  520. $userLevelInfo = $userLevel->homeGetUserLevel((int)$user['uid'], $user);
  521. $user = array_merge($user, $userLevelInfo);
  522. $user['yesterDay'] = $userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
  523. $user['recharge_switch'] = (int)($configData['recharge_switch'] ?? 0);//充值开关
  524. $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  525. $user['broken_day'] = (int)($configData['extract_time'] ?? 0);//佣金冻结时间
  526. $user['balance_func_status'] = (int)($configData['balance_func_status'] ?? 0);
  527. $user['invioce_func'] = !!($configData['invoice_func_status'] ?? 0);
  528. $user['special_invoice'] = $user['invioce_func'] && ($configData['special_invoice_status'] ?? 0);
  529. $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
  530. $user['member_style'] = (int)$diyServices->getColorChange('member');
  531. if ($user['is_ever_level']) {
  532. $user['vip_status'] = 1;//永久会员
  533. } else {
  534. if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
  535. $user['vip_status'] = -1;//开通过已过期
  536. } else if (!$user['overdue_time'] && !$user['is_money_level']) {
  537. $user['vip_status'] = 2;//没有开通过
  538. } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
  539. $user['vip_status'] = 3;//开通了,没有到期
  540. }
  541. }
  542. /** @var StoreServiceRecordServices $servicesRecord */
  543. $servicesRecord = app()->make(StoreServiceRecordServices::class);
  544. $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
  545. $message = $systemMessageServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  546. $user['service_num'] = (int)bcadd((string)$service_num, (string)$message);
  547. $user['is_agent_level'] = ($configData['brokerage_func_status'] ?? 1) && $agentLevelServices->count(['status' => 1, 'is_del' => 0]);
  548. $user['visit_num'] = $storeProductLogServices->getDistinctCount(['uid' => $uid, 'type' => 'visit'], 'product_id');
  549. $user['user_extract_bank_status'] = (int)($configData['user_extract_bank_status'] ?? 1);
  550. $user['user_extract_wechat_status'] = (int)($configData['user_extract_wechat_status'] ?? 1);
  551. $user['user_extract_alipay_status'] = (int)($configData['user_extract_alipay_status'] ?? 1);
  552. //是否享受新人专享
  553. /** @var StoreNewcomerServices $newcomerServices */
  554. $newcomerServices = app()->make(StoreNewcomerServices::class);
  555. $user['newcomer_status'] = $newcomerServices->checkUserNewcomer($uid);
  556. $user['level_activate_status'] = $configData['level_activate_status'];
  557. $user['member_func_status'] = $configData['member_func_status'];
  558. $extendInfo = SystemConfigService::get('user_extend_info', []);
  559. $user['register_extend_info'] = [];
  560. if (!$user['level_activate_status']) {//不需要激活,用户激活状态默认为1
  561. $user['level_status'] = 1;
  562. }
  563. if ($extendInfo) {
  564. foreach ($extendInfo as $item) {
  565. if (isset($item['use']) && $item['use'] && isset($item['user_show']) && $item['user_show']) $user['register_extend_info'][] = $item;
  566. }
  567. }
  568. if (isset($user['extend_info']) && $user['extend_info']) {
  569. $default = $this->defaultExtendInfo;
  570. $params = array_column($default, 'param');
  571. $sex = $this->rSex;
  572. foreach ($user['extend_info'] as &$info) {
  573. if (isset($info['param']) && in_array($info['param'], $params)) {
  574. if ($info['param'] == 'sex') {
  575. $info['value'] = $sex[$user['sex']] ?? 0;
  576. } elseif ($info['param'] == 'birthday') {
  577. $info['value'] = ($user['birthday'] ?? '') ? date('Y-m-d', $user['birthday']) : '';
  578. } elseif ($info['param'] == 'address') {
  579. $info['value'] = $user['addres'] ?? '';
  580. } else {
  581. $info['value'] = $user[$info['param']] ?? '';
  582. }
  583. }
  584. }
  585. }
  586. // /** @var UserAwardIntegralServices $award_integral_service */
  587. // $award_integral_service = app()->make(UserAwardIntegralServices::class);
  588. // $award_lack = $award_integral_service->getLake();
  589. // $integral_price = $award_integral_service->getPrice();
  590. // $static_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
  591. // $action_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
  592. // $sum_integral = $award_integral_service->getIntegralSum(['status' => 0]);
  593. $hidden_integral = sys_config('hidden_integral', 0);
  594. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  595. // $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  596. /** @var AwardIntegralServices $award_integral_service */
  597. $award_integral_service = app()->make(AwardIntegralServices::class);
  598. $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  599. $all_integral = $award_integral_service->sum(['uid' => $user['uid']], 'num');
  600. $sent_integral = $award_integral_service->sum(['uid' => $user['uid']], 'sent_num');
  601. $stand_integral = (float)bcsub($all_integral, $sent_integral, 2);
  602. $extractable_integral = $award_integral_service->sum(['uid' => $user['uid']], 'extract_num');
  603. $extracted_integral = $award_integral_service->sum(['uid' => $user['uid']], 'extracted_num');
  604. $shop_integral_trade_commission = sys_config('shop_integral_trade_commission');//微信提现到账方式
  605. $shop_integral_trade_switch = sys_config('shop_integral_trade_switch');//微信提现到账方式
  606. return array_merge($user, compact('shop_integral_trade_commission', 'shop_integral_trade_switch', 'hidden_integral', 'all_integral', 'sent_integral', 'stand_integral', 'extractable_integral', 'extracted_integral'));
  607. }
  608. /**
  609. * 用户资金统计
  610. * @param int $uid `
  611. */
  612. public function balance(int $uid)
  613. {
  614. $userInfo = $this->getUserInfo($uid);
  615. if (!$userInfo) {
  616. throw new ValidateException('数据不存在');
  617. }
  618. /** @var UserMoneyServices $userMoneyServices */
  619. $userMoneyServices = app()->make(UserMoneyServices::class);
  620. /** @var StoreOrderServices $storeOrder */
  621. $storeOrder = app()->make(StoreOrderServices::class);
  622. $user['now_money'] = $userInfo['now_money'];//当前总资金
  623. $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
  624. $user['orderStatusSum'] = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price', true);//累计消费
  625. return $user;
  626. }
  627. /**
  628. * 用户修改信息
  629. * @param Request $request
  630. * @return mixed
  631. */
  632. public function eidtNickname(int $uid, array $data)
  633. {
  634. if (!$this->userExist($uid)) {
  635. throw new ValidateException('用户不存在');
  636. }
  637. if (!$this->dao->update($uid, $data, 'uid')) {
  638. throw new ValidateException('修改失败');
  639. }
  640. return true;
  641. }
  642. /**
  643. * 获取推广人排行
  644. * @param $data 查询条件
  645. * @return array
  646. * @throws DataNotFoundException
  647. * @throws ModelNotFoundException
  648. * @throws \think\exception\DbException
  649. */
  650. public function getRankList(array $data)
  651. {
  652. $startTime = $endTime = 0;
  653. switch ($data['type']) {
  654. case 'week':
  655. $startTime = strtotime('this week Monday');
  656. break;
  657. case 'month':
  658. $startTime = strtotime('first day of ' . date('F Y'));
  659. break;
  660. }
  661. $endTime = time();
  662. [$page, $limit] = $this->getPageValue();
  663. $field = 'spread_uid,count(uid) AS count,spread_time';
  664. /** @var UserSpreadServices $userSpreadServices */
  665. $userSpreadServices = app()->make(UserSpreadServices::class);
  666. $list = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  667. $rank = 0;
  668. foreach ($list as $key => $item) {
  669. if ($item['spread_uid'] == $data['uid']) $rank = $key + 1;
  670. }
  671. $week = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('this week Monday'), time()], 'timeKey' => 'spread_time']);
  672. $month = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('last month'), time()], 'timeKey' => 'spread_time']);
  673. $start = date('Y-m-d H:i', $startTime);
  674. $end = date('Y-m-d H:i', time());
  675. return compact('list', 'rank', 'week', 'month', 'start', 'end');
  676. }
  677. /**
  678. * 静默绑定推广人
  679. * @param Request $request
  680. * @return mixed
  681. */
  682. public function spread(int $uid, int $spreadUid, $code)
  683. {
  684. $userInfo = $this->getUserInfo($uid);
  685. if (!$userInfo) {
  686. throw new ValidateException('数据不存在');
  687. }
  688. if ($code && !$spreadUid) {
  689. /** @var QrcodeServices $qrCode */
  690. $qrCode = app()->make(QrcodeServices::class);
  691. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  692. $spreadUid = $info['third_id'];
  693. }
  694. }
  695. //记录好友关系
  696. if ($spreadUid && $uid && $spreadUid != $uid) {
  697. /** @var UserFriendsServices $serviceFriend */
  698. $serviceFriend = app()->make(UserFriendsServices::class);
  699. $serviceFriend->saveFriend($uid, $spreadUid);
  700. }
  701. $data = [];
  702. //永久绑定
  703. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  704. $spread_uid = isset($user['code']) && $user['code'] && $user['code'] != $userInfo->uid ? $user['code'] : ($userInfo['spread_uid'] ?? 0);
  705. if ($userInfo->spread_uid && $store_brokergae_binding_status == 1) {
  706. $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
  707. } else {
  708. //绑定分销关系 = 所有用户
  709. if (sys_config('brokerage_bindind', 1) == 1) {
  710. //分销绑定类型为时间段且过期 ||临时
  711. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  712. if (!$userInfo['spread_uid'] || $store_brokergae_binding_status == 3 || ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
  713. $spreadUid = $spread_uid;
  714. if ($spreadUid && $userInfo->uid == $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
  715. $spreadUid = 0;
  716. }
  717. if ($spreadUid && $this->dao->get((int)$spreadUid)) {
  718. $data['spread_uid'] = $spreadUid;
  719. $data['spread_time'] = time();
  720. }
  721. }
  722. }
  723. }
  724. if ($data && !$this->dao->update($userInfo['uid'], $data, 'uid')) {
  725. throw new ValidateException('修改信息失败');
  726. }
  727. if (isset($data['spread_uid']) && $data['spread_uid']) {
  728. /** @var UserBillServices $userBill */
  729. $userBill = app()->make(UserBillServices::class);
  730. //邀请新用户增加经验
  731. $userBill->inviteUserIncExp((int)$spreadUid);
  732. }
  733. return true;
  734. }
  735. /**
  736. * 添加访问记录
  737. * @param Request $request
  738. * @return mixed
  739. */
  740. public function setVisit(array $data)
  741. {
  742. $userInfo = $this->getUserInfo($data['uid']);
  743. if (!$userInfo) {
  744. throw new ValidateException('数据不存在');
  745. }
  746. if (isset($data['ip']) && $data['ip']) {
  747. $addressArr = $this->addressHandle($this->convertIp($data['ip']));
  748. $data['province'] = $addressArr['province'] ?? '';
  749. }
  750. $data['channel_type'] = $userInfo['user_type'];
  751. $data['add_time'] = time();
  752. /** @var UserVisitServices $userVisit */
  753. $userVisit = app()->make(UserVisitServices::class);
  754. if ($userVisit->save($data)) {
  755. return true;
  756. } else {
  757. throw new ValidateException('添加访问记录失败');
  758. }
  759. }
  760. /**
  761. * 获取活动状态
  762. * @return mixed
  763. */
  764. public function activity()
  765. {
  766. /** @var StoreBargainServices $storeBragain */
  767. $storeBragain = app()->make(StoreBargainServices::class);
  768. /** @var StoreCombinationServices $storeCombinaion */
  769. $storeCombinaion = app()->make(StoreCombinationServices::class);
  770. /** @var StoreSeckillServices $storeSeckill */
  771. $storeSeckill = app()->make(StoreSeckillServices::class);
  772. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  773. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  774. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  775. return $data;
  776. }
  777. /**
  778. * 获取用户下级推广人
  779. * @param int $uid 当前用户
  780. * @param int $grade 等级 0 一级 1 二级
  781. * @param string $orderBy 排序
  782. * @param string $keyword
  783. * @return array|bool
  784. */
  785. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '', $time = [])
  786. {
  787. $user = $this->getUserInfo($uid);
  788. if (!$user) {
  789. throw new ValidateException('数据不存在');
  790. }
  791. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  792. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  793. $data = [
  794. 'total' => count($spread_one_ids),
  795. 'totalLevel' => count($spread_two_ids),
  796. 'list' => []
  797. ];
  798. /** @var UserStoreOrderServices $userStoreOrder */
  799. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  800. $list = [];
  801. $where = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  802. if ($grade == 0) {
  803. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword, $time);
  804. $where = $where + ['spread_uid' => $uid];
  805. } else {
  806. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword, $time);
  807. $where = $where + ['spread_two_uid' => $uid];
  808. }
  809. foreach ($list as &$item) {
  810. if (isset($item['spread_time']) && $item['spread_time']) {
  811. $item['time'] = date('Y/m/d', $item['spread_time']);
  812. }
  813. }
  814. $data['list'] = $list;
  815. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  816. $data['count'] = 0;
  817. $data['price'] = 0;
  818. if ($list) {
  819. $uids = array_column($list, 'uid');
  820. $data['count'] = count($uids);
  821. /** @var StoreOrderServices $storeOrder */
  822. $storeOrder = app()->make(StoreOrderServices::class);
  823. $data['price'] = $storeOrder->sum($where, $grade == 0 ? 'one_brokerage' : 'two_brokerage');
  824. }
  825. return $data;
  826. }
  827. /**
  828. * 获取推广人uids
  829. * @param int $uid
  830. * @param bool $one
  831. * @return array
  832. */
  833. public function getUserSpredadUids(int $uid, int $type = 0)
  834. {
  835. $uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
  836. if ($type === 1) {
  837. return $uids;
  838. }
  839. if ($uids) {
  840. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids]], 'uid');
  841. if ($type === 2) {
  842. return $uidsTwo;
  843. }
  844. if ($uidsTwo) {
  845. $uids = array_merge($uids, $uidsTwo);
  846. }
  847. }
  848. return $uids;
  849. }
  850. /**
  851. * 检测用户是否是推广员
  852. * @param int $uid
  853. * @param array $user
  854. * @param float $sumPrice
  855. * @return bool
  856. */
  857. public function checkUserPromoter(int $uid, $user = [], float $sumPrice = 0.00)
  858. {
  859. if (!$uid) {
  860. return false;
  861. }
  862. if (!$user) {
  863. $user = $this->getUserInfo($uid);
  864. }
  865. if (!$user) {
  866. return false;
  867. }
  868. //分销是否开启
  869. if (!sys_config('brokerage_func_status')) {
  870. return false;
  871. }
  872. //用户分校推广资格是否开启4.0.32
  873. if (isset($user['spread_open']) && !$user['spread_open']) {
  874. return false;
  875. }
  876. $store_brokerage_statu = sys_config('store_brokerage_statu');
  877. if ($user['is_promoter'] || $store_brokerage_statu == 2) {
  878. return true;
  879. }
  880. if ($store_brokerage_statu == 3) {
  881. if (!$sumPrice) {
  882. /** @var StoreOrderServices $storeOrder */
  883. $storeOrder = app()->make(StoreOrderServices::class);
  884. $sumPrice = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price');//累计消费
  885. }
  886. $store_brokerage_price = sys_config('store_brokerage_price');
  887. if ($sumPrice >= $store_brokerage_price) {
  888. return true;
  889. }
  890. }
  891. return false;
  892. }
  893. /**
  894. * 同步微信粉丝用户(后台接口)
  895. * @return bool
  896. */
  897. public function syncWechatUsers()
  898. {
  899. $key = md5('sync_wechat_users');
  900. //一天点击一次
  901. if (CacheService::get($key)) {
  902. return true;
  903. }
  904. $next_openid = null;
  905. do {
  906. $result = OfficialAccount::userService()->list($next_openid);
  907. $userOpenids = $result['data'];
  908. //拆分大数组
  909. $opemidArr = array_chunk($userOpenids, 100);
  910. foreach ($opemidArr as $openids) {
  911. //加入同步|更新用户队列
  912. UserJob::dispatch([$openids]);
  913. }
  914. $next_openid = $result['next_openid'];
  915. } while ($next_openid != null);
  916. CacheService::set($key, 1, 3600 * 24);
  917. return true;
  918. }
  919. /**
  920. * 导入微信粉丝用户
  921. * @param array $openids
  922. * @return bool
  923. */
  924. public function importUser(array $noBeOpenids)
  925. {
  926. if (!$noBeOpenids) {
  927. return true;
  928. }
  929. $dataAll = $data = [];
  930. $time = time();
  931. foreach ($noBeOpenids as $openid) {
  932. try {
  933. $info = OfficialAccount::userService()->get($openid);
  934. $info = is_object($info) ? $info->toArray() : $info;
  935. } catch (\Throwable $e) {
  936. $info = [];
  937. }
  938. if (!$info) continue;
  939. if (($info['subscribe'] ?? 1) == 1) {
  940. $data['nickname'] = $info['nickname'] ?? '';
  941. $data['headimgurl'] = $info['headimgurl'] ?? '';
  942. $userInfoData = $this->setUserInfo($data);
  943. if (!$userInfoData) {
  944. throw new AdminException('用户信息储存失败!');
  945. }
  946. $data['uid'] = $userInfoData['uid'];
  947. $data['subscribe'] = $info['subscribe'];
  948. $data['unionid'] = $info['unionid'] ?? '';
  949. $data['openid'] = $info['openid'] ?? '';
  950. $data['sex'] = $info['sex'] ?? 0;
  951. $data['language'] = $info['language'] ?? '';
  952. $data['city'] = $info['city'] ?? '';
  953. $data['province'] = $info['province'] ?? '';
  954. $data['country'] = $info['country'] ?? '';
  955. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  956. $data['groupid'] = $info['groupid'] ?? 0;
  957. $data['remark'] = $info['remark'] ?? '';
  958. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  959. $data['add_time'] = $time;
  960. $data['is_complete'] = 1;
  961. $dataAll[] = $data;
  962. }
  963. }
  964. if ($dataAll) {
  965. /** @var WechatUserServices $wechatUser */
  966. $wechatUser = app()->make(WechatUserServices::class);
  967. if (!$wechatUser->saveAll($dataAll)) {
  968. throw new ValidateException('保存用户信息失败');
  969. }
  970. }
  971. return true;
  972. }
  973. /**
  974. * 修改会员的时间及是否会员状态
  975. * @param int $vip_day 会员天数
  976. * @param array $user_id 用户id
  977. * @param int $is_money_level 会员来源途径
  978. * @param bool $member_type 会员卡类型
  979. * @return mixed
  980. * @throws DataNotFoundException
  981. * @throws DbException
  982. * @throws ModelNotFoundException
  983. */
  984. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  985. {
  986. if ($vip_day == 0) throw new ValidateException('天数不能为0');
  987. $user_info = $this->getUserInfo($user_id);
  988. if (!$user_info) throw new ValidateException('用户数据不存在');
  989. if (!$member_type) $member_type = "month";
  990. if ($member_type == 'ever') {
  991. $overdue_time = 0;
  992. $is_ever_level = 1;
  993. } else {
  994. if ($user_info['is_money_level'] == 0) {
  995. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  996. } else {
  997. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  998. }
  999. $is_ever_level = 0;
  1000. }
  1001. $setData['overdue_time'] = $overdue_time;
  1002. $setData['is_ever_level'] = $is_ever_level;
  1003. $setData['is_money_level'] = $is_money_level ? $is_money_level : 0;
  1004. // if ($user_info['level'] == 0) $setData['level'] = 1;
  1005. return $this->dao->update(['uid' => $user_id], $setData);
  1006. }
  1007. /**
  1008. * 清空到期svip(分批加入队列)
  1009. * @return bool
  1010. */
  1011. public function offUserSvip()
  1012. {
  1013. $users = $this->dao->getColumn([['is_ever_level', '=', 0], ['is_money_level', '>', 0], ['overdue_time', '<', time()]], 'uid');
  1014. if ($users) {
  1015. //拆分大数组
  1016. $uidsArr = array_chunk($users, 100);
  1017. foreach ($uidsArr as $uids) {
  1018. //加入同步|更新用户队列
  1019. UserSvipJob::dispatch([$uids]);
  1020. }
  1021. }
  1022. return true;
  1023. }
  1024. /**
  1025. * 会员过期改变状态,变为普通会员
  1026. * @param $uid
  1027. * @param null $userInfo
  1028. * @return bool
  1029. * @throws DataNotFoundException
  1030. * @throws DbException
  1031. * @throws ModelNotFoundException
  1032. */
  1033. public function offMemberLevel($uid, $userInfo = null)
  1034. {
  1035. if (!$uid) return false;
  1036. $userInfo = $userInfo ?: $this->dao->get($uid);
  1037. if (!$userInfo) return false;
  1038. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  1039. $this->dao->update(['uid' => $uid], ['is_money_level' => 0]);
  1040. return false;
  1041. }
  1042. return true;
  1043. }
  1044. /**
  1045. * @param array $where
  1046. * @return array
  1047. * @throws DataNotFoundException
  1048. * @throws DbException
  1049. * @throws ModelNotFoundException
  1050. */
  1051. public function getUserInfoList(array $where, $field = "*")
  1052. {
  1053. return $this->dao->getUserInfoList($where, $field);
  1054. }
  1055. /**
  1056. * 保存用户上级推广人
  1057. * @param int $uid
  1058. * @param int $spread_uid
  1059. * @return bool
  1060. */
  1061. public function saveUserSpreadUid(int $uid, int $spread_uid)
  1062. {
  1063. if (!$uid || !$spread_uid) {
  1064. return false;
  1065. }
  1066. if ($uid == $spread_uid) {
  1067. throw new ValidateException('上级推广人不能为自己');
  1068. }
  1069. $userInfo = $this->getUserInfo($uid);
  1070. if (!$userInfo) {
  1071. throw new ValidateException('用户不存在');
  1072. }
  1073. //上级已经是这个uid
  1074. if ($userInfo['spread_uid'] == $spread_uid) {
  1075. return true;
  1076. }
  1077. $spreadInfo = $this->getUserInfo($spread_uid);
  1078. if (!$spreadInfo) {
  1079. throw new ValidateException('上级用户不存在');
  1080. }
  1081. if ($spreadInfo['spread_uid'] == $uid) {
  1082. throw new ValidateException('上级推广人不能为自己下级');
  1083. }
  1084. $data = ['spread_uid' => $spread_uid, 'spread_time' => time()];
  1085. $this->dao->update($uid, $data);
  1086. //记录推广绑定关系
  1087. UserSpreadJob::dispatch([$uid, $spread_uid]);
  1088. //记录好友关系
  1089. UserFriendsJob::dispatch([$uid, $spread_uid]);
  1090. return true;
  1091. }
  1092. /**
  1093. * 增加推广用户佣金
  1094. * @param int $uid
  1095. * @param int $spread_uid
  1096. * @param array $userInfo
  1097. * @param array $spread_user
  1098. * @return bool|mixed
  1099. * @throws DataNotFoundException
  1100. * @throws DbException
  1101. * @throws ModelNotFoundException
  1102. */
  1103. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  1104. {
  1105. if (!$uid || !$spread_uid) {
  1106. return false;
  1107. }
  1108. //商城分销功能是否开启 0关闭1开启
  1109. if (!sys_config('brokerage_func_status')) return true;
  1110. //获取设置推广佣金单价
  1111. $brokerage_price = sys_config('uni_brokerage_price', 0);
  1112. //推广佣金是否开启
  1113. if (!sys_config('brokerage_user_status', 0)) {
  1114. return true;
  1115. }
  1116. //获取推广佣金当日限额
  1117. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  1118. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  1119. return true;
  1120. }
  1121. if (!$userInfo) {
  1122. $userInfo = $this->getUserInfo($uid);
  1123. }
  1124. if (!$userInfo) {
  1125. return false;
  1126. }
  1127. if (!$spread_user) {
  1128. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  1129. }
  1130. if (!$spread_user) {
  1131. return false;
  1132. }
  1133. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  1134. return false;
  1135. }
  1136. /** @var UserBrokerageServices $userBrokerageServices */
  1137. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  1138. // -1不限制
  1139. if ($day_brokerage_price_upper != -1) {
  1140. if ($day_brokerage_price_upper <= 0) {
  1141. return true;
  1142. } else {
  1143. //获取上级用户今日获取推广用户佣金
  1144. $spread_day_brokerage = $userBrokerageServices->getUserBillBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  1145. //超过上限
  1146. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  1147. return true;
  1148. }
  1149. }
  1150. }
  1151. $spreadPrice = $spread_user['brokerage_price'];
  1152. // 上级推广员返佣之后的金额
  1153. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  1154. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  1155. // 添加返佣记录
  1156. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  1157. 'nickname' => $userInfo['nickname'],
  1158. 'number' => floatval($brokerage_price)
  1159. ], $balance, $uid);
  1160. // 添加用户余额
  1161. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  1162. //给上级发送获得佣金的模板消息
  1163. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  1164. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  1165. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  1166. return $res1 && $res2;
  1167. });
  1168. }
  1169. /**
  1170. * 获取上级uid
  1171. * @param int $uid
  1172. * @param array $userInfo
  1173. * @param bool $is_spread
  1174. * @return int|mixed
  1175. */
  1176. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  1177. {
  1178. if (!$uid) {
  1179. return 0;
  1180. }
  1181. //商城分销功能是否开启 0关闭1开启
  1182. if (!sys_config('brokerage_func_status')) return -1;
  1183. if (!$userInfo) {
  1184. $userInfo = $this->getUserCacheInfo($uid);
  1185. }
  1186. if (!$userInfo) {
  1187. return 0;
  1188. }
  1189. //上级的上级不需要检测自购
  1190. if ($is_spread) {
  1191. //开启自购
  1192. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  1193. if ($is_self_brokerage && $is_spread) {
  1194. return $uid;
  1195. }
  1196. }
  1197. //绑定类型
  1198. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  1199. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  1200. return $userInfo['spread_uid'];
  1201. }
  1202. //分销绑定类型为时间段且没过期
  1203. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  1204. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  1205. return $userInfo['spread_uid'];
  1206. }
  1207. return -1;
  1208. }
  1209. /**
  1210. * 用户付款code
  1211. * @param int $uid
  1212. * @return bool|mixed|null
  1213. */
  1214. public function getRandCode(int $uid)
  1215. {
  1216. $key = 'user_rand_code' . $uid;
  1217. return CacheService::redisHandler()->remember($key, function () {
  1218. return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 9), 1))), 0, 3) . str_pad((string)mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
  1219. }, 600);
  1220. }
  1221. /**
  1222. * 获取barcode
  1223. * @return bool|int|mixed|null
  1224. */
  1225. public function getBarCode()
  1226. {
  1227. mt_srand();
  1228. $code = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 9), 1))), 0, 4) . str_pad((string)mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  1229. if (!$this->dao->count(['bar_code' => $code])) {
  1230. return $code;
  1231. } else {
  1232. return $this->getBarCode();
  1233. }
  1234. }
  1235. /**
  1236. * 获取用户推广用户列表
  1237. * @param $uid
  1238. * @param $type
  1239. * @return array
  1240. * @throws DataNotFoundException
  1241. * @throws DbException
  1242. * @throws ModelNotFoundException
  1243. */
  1244. public function agentUserList($uid, $type)
  1245. {
  1246. $where['spread_uid'] = $uid;
  1247. if ($type == 1) {
  1248. $where['pay_count'] = -1;
  1249. }
  1250. [$page, $limit] = $this->getPageValue();
  1251. $list = $this->dao->getList($where, 'uid,nickname,avatar,FROM_UNIXTIME(spread_time, \'%Y.%m.%d %H:%m\') as spread_time', $page, $limit);
  1252. $count = $this->dao->count($where);
  1253. return compact('list', 'count');
  1254. }
  1255. /**
  1256. * 个人中心
  1257. * @param $uid
  1258. * @return array|mixed
  1259. */
  1260. public function bankInfo($uid)
  1261. {
  1262. $userService = app()->make(UserServices::class);
  1263. $user = $userService->getUserInfo($uid);
  1264. if (!$user) {
  1265. throw new ValidateException('数据不存在');
  1266. }
  1267. if (!$user['enterprise_professional_facilitator_id']) {
  1268. return [];
  1269. }
  1270. return WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  1271. }
  1272. public function cidUser($rname, $cid, $uid)
  1273. {
  1274. //找出出生年月日
  1275. $time = substr($cid, 6, 4) . '-' . substr($cid, 10, 2) . '-' . substr($cid, 12, 2);
  1276. $brithday = strtotime($time);
  1277. $rust = $this->update($uid, ['real_name' => $rname, 'card_id' => $cid, 'is_auth' => 1, 'off' => '', 'birthday' => $brithday], 'uid');
  1278. if ($rust)
  1279. return true;
  1280. else
  1281. return false;
  1282. }
  1283. /**
  1284. * TODO 获取推广人数 一级
  1285. * @param int $uid
  1286. * @return bool|int|string
  1287. */
  1288. public function getSpreadCount($uid = 0)
  1289. {
  1290. if (!$uid) return false;
  1291. return $this->search()->where('spread_uid', $uid)->count();
  1292. }
  1293. /**
  1294. * 修改当前用户的推广人数
  1295. * @param $uid
  1296. * @return bool
  1297. */
  1298. public function setUserSpreadCount($uid)
  1299. {
  1300. if (!$uid) return true;
  1301. if ($this->getSpreadCount($uid) > 0) {
  1302. $this->update($uid, ['spread_count' => 0]);
  1303. } else {
  1304. $this->update($uid, ['spread_count' => 0]);
  1305. return true;
  1306. }
  1307. return $this->update($uid, ['spread_count' => self::getSpreadCount($uid)]);
  1308. }
  1309. public function cancelUser(int $uid)
  1310. {
  1311. try {
  1312. $user = $this->get($uid);
  1313. /** @var WechatUserServices $wechatUserServices */
  1314. $wechatUserServices = app()->make(WechatUserServices::class);
  1315. $this->update(['spread_uid' => $uid], ['spread_uid' => 0]);// 清除用户与下级的关系
  1316. $this->update($uid, ['spread_uid' => 0, 'integral' => 0, 'now_money' => 0, 'brokerage_price' => 0, 'status' => 0]);//清除
  1317. $this->bcDec($user['spread_uid'], 'spread_count', 1, 'uid');
  1318. $this->destroy($uid);// 软删除用户
  1319. $wechatUserServices->update(['uid' => $uid], ['unionid' => '', 'openid' => time() . rand(1000, 9999)]);// 删除微信用户
  1320. $wechatUserServices->destroy(['uid' => $uid]);// 删除微信用户
  1321. } catch (\Throwable $e) {
  1322. Log::error('注销用户失败,失败原因:' . $e->getMessage());
  1323. }
  1324. }
  1325. }