User.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. namespace app\models\user;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreProduct;
  5. use crmeb\services\SystemConfigService;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\Exception;
  10. use think\facade\Session;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. use crmeb\traits\JwtAuthModelTrait;
  14. /**
  15. * TODO 用户Model
  16. * Class User
  17. * @package app\models\user
  18. */
  19. class User extends BaseModel
  20. {
  21. use JwtAuthModelTrait;
  22. use ModelTrait;
  23. protected $pk = 'uid';
  24. protected $name = 'user';
  25. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  26. protected $hidden = [
  27. 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
  28. ];
  29. protected function setAddTimeAttr($value)
  30. {
  31. return time();
  32. }
  33. protected function setAddIpAttr($value)
  34. {
  35. return app('request')->ip();
  36. }
  37. protected function setLastTimeAttr($value)
  38. {
  39. return time();
  40. }
  41. protected function setLastIpAttr($value)
  42. {
  43. return app('request')->ip();
  44. }
  45. /**
  46. * @param $wechatUser
  47. * @param int $spread_uid
  48. * @return bool
  49. * @throws DataNotFoundException
  50. * @throws DbException
  51. * @throws ModelNotFoundException
  52. */
  53. public static function setWechatUser($wechatUser, $spread_uid = 0)
  54. {
  55. $res = self::create([
  56. 'account' => 'wx' . $wechatUser['uid'] . time(),
  57. 'pwd' => md5(123456),
  58. 'nickname' => $wechatUser['nickname'] ?: '',
  59. 'avatar' => $wechatUser['headimgurl'] ?: '',
  60. 'add_time' => time(),
  61. 'add_ip' => app('request')->ip(),
  62. 'last_time' => time(),
  63. 'last_ip' => app('request')->ip(),
  64. 'uid' => $wechatUser['uid'],
  65. 'user_type' => 'wechat'
  66. ]);
  67. return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
  68. }
  69. /**
  70. * TODO 获取会员是否被清除过的时间
  71. * @param $uid
  72. * @return int|mixed
  73. * @throws DataNotFoundException
  74. * @throws ModelNotFoundException
  75. * @throws DbException
  76. */
  77. public static function getCleanTime($uid)
  78. {
  79. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  80. if (!$user) return 0;
  81. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  82. }
  83. /**
  84. * 更新用户信息
  85. * @param array $wechatUser 用户信息
  86. * @param int $uid 用户uid
  87. * @return bool|void
  88. * @throws DataNotFoundException
  89. * @throws ModelNotFoundException
  90. * @throws DbException
  91. */
  92. public static function updateWechatUser($wechatUser, $uid)
  93. {
  94. $userInfo = self::where('uid', $uid)->find();
  95. if (!$userInfo) return;
  96. //增加成为分销权限
  97. $status = true;
  98. if ($userInfo->spread_uid) {
  99. return self::edit([
  100. 'nickname' => $wechatUser['nickname'] ?: '',
  101. 'avatar' => $wechatUser['headimgurl'] ?: '',
  102. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  103. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  104. ], $uid, 'uid');
  105. } else {
  106. $data = [
  107. 'nickname' => $wechatUser['nickname'] ?: '',
  108. 'avatar' => $wechatUser['headimgurl'] ?: '',
  109. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  110. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  111. // 'spread_uid' => 0,
  112. // 'spread_time' => 0,
  113. 'last_time' => time(),
  114. 'last_ip' => request()->ip(),
  115. ];
  116. //TODO 获取后台分销类型
  117. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  118. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  119. // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  120. // if ($storeBrokerageStatus == 1) {
  121. // $spreadCount = self::where('uid', $wechatUser['code'])->count();
  122. // if ($spreadCount) {
  123. // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  124. // if ($spreadInfo->is_promoter) {
  125. // //TODO 只有扫码才可以获得推广权限
  126. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  127. // }
  128. // }
  129. // }
  130. // $data['spread_uid'] = $wechatUser['code'];
  131. // $data['spread_time'] = time();
  132. // }
  133. return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
  134. }
  135. }
  136. /**
  137. * 设置推广关系
  138. * @param $spread
  139. * @param $uid
  140. * @return bool
  141. * @throws DataNotFoundException
  142. * @throws ModelNotFoundException
  143. * @throws DbException
  144. */
  145. public static function setSpread($spread, $uid)
  146. {
  147. return UserSpread::setSpread($uid, $spread);
  148. //当前用户信息
  149. // $userInfo = self::where('uid', $uid)->find();
  150. // if (!$userInfo) return true;
  151. // //当前用户有上级直接返回
  152. // if ($userInfo->spread_uid) return true;
  153. // //没有推广编号直接返回
  154. // if (!$spread) return true;
  155. // if ($spread == $uid) return true;
  156. // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  157. // //TODO 获取后台分销类型
  158. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  159. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  160. // if ($storeBrokerageStatus == 1) {
  161. // $spreadCount = self::where('uid', $spread)->count();
  162. // if ($spreadCount) {
  163. // $spreadInfo = self::where('uid', $spread)->find();
  164. // if ($spreadInfo->is_promoter) {
  165. // //TODO 只有扫码才可以获得推广权限
  166. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  167. // }
  168. // }
  169. // }
  170. // $data['spread_uid'] = $spread;
  171. // $data['spread_time'] = time();
  172. // return self::edit($data, $uid, 'uid');
  173. }
  174. /**
  175. * 小程序用户添加
  176. * @param $routineUser
  177. * @param int $spread_uid
  178. * @return object
  179. * @throws DataNotFoundException
  180. * @throws DbException
  181. * @throws ModelNotFoundException
  182. */
  183. public static function setRoutineUser($routineUser, $spread_uid = 0)
  184. {
  185. self::beginTrans();
  186. $res1 = true;
  187. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  188. // $storeBrokerageStatu = sys_config('store_brokerage_statu') ? : 1;//获取后台分销类型
  189. $res2 = self::create([
  190. 'account' => 'rt' . $routineUser['uid'] . time(),
  191. 'pwd' => md5(123456),
  192. 'nickname' => $routineUser['nickname'] ?: '',
  193. 'avatar' => $routineUser['headimgurl'] ?: '',
  194. // 'spread_uid' => $spread_uid,
  195. // 'is_promoter'=>$spread_uid || $storeBrokerageStatu != 1 ? 1: 0,
  196. // 'spread_time' => $spread_uid ? time() : 0,
  197. 'uid' => $routineUser['uid'],
  198. 'add_time' => $routineUser['add_time'],
  199. 'add_ip' => request()->ip(),
  200. 'last_time' => time(),
  201. 'last_ip' => request()->ip(),
  202. 'user_type' => $routineUser['user_type']
  203. ]);
  204. $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
  205. self::checkTrans($res);
  206. return $res2;
  207. }
  208. /**
  209. * 获得当前登陆用户UID
  210. * @return int $uid
  211. */
  212. public static function getActiveUid()
  213. {
  214. $uid = null;
  215. $uid = Session::get('LoginUid');
  216. if ($uid) return $uid;
  217. else return 0;
  218. }
  219. /**
  220. * TODO 查询当前用户信息
  221. * @param $uid $uid 用户编号
  222. * @param string $field $field 查询的字段
  223. * @return array
  224. * @throws DataNotFoundException
  225. * @throws ModelNotFoundException
  226. * @throws DbException
  227. */
  228. public static function getUserInfo($uid, $field = '')
  229. {
  230. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  231. else $userInfo = self::where('uid', $uid)->find();
  232. if (!$userInfo) return [];
  233. return $userInfo->toArray();
  234. }
  235. /**
  236. * 判断当前用户是否推广员
  237. * @param int $uid
  238. * @return bool
  239. */
  240. public static function isUserSpread($uid = 0)
  241. {
  242. if (!$uid) return false;
  243. $status = (int)sys_config('store_brokerage_statu');
  244. $isPromoter = true;
  245. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  246. if ($isPromoter) return true;
  247. else return false;
  248. }
  249. /**
  250. * TODO 一级返佣
  251. * @param $orderInfo
  252. * @return bool
  253. * @throws DataNotFoundException
  254. * @throws ModelNotFoundException
  255. * @throws DbException
  256. */
  257. public static function backOrderBrokerage($orderInfo, bool $open = true)
  258. {
  259. //TODO 营销产品不返佣金
  260. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  261. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  262. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  263. $userInfo = User::getUserInfo($orderInfo['uid']);
  264. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  265. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  266. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  267. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  268. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
  269. //TODO 返佣金额小于等于0 直接返回不返佣金
  270. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  271. //TODO 获取上级推广员信息
  272. // $open && self::beginTrans();
  273. $res = true;
  274. if ($virtualBrokeragePrice > 0) {
  275. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  276. //TODO 上级推广员返佣之后的金额
  277. $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
  278. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  279. //TODO 添加推广记录
  280. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  281. //TODO 添加用户余额
  282. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  283. //TODO 一级返佣成功 跳转二级返佣
  284. $res = $res1 && $res2;
  285. }
  286. if ($realBrokeragePrice > 0) {
  287. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  288. //TODO 上级推广员返佣之后的金额
  289. $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  290. //TODO 添加推广记录
  291. $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfo['brokerage_price'], $mark, 0);
  292. }
  293. $res = $res && self::backOrderBrokerageTwo($orderInfo);
  294. // $open && self::checkTrans($res);
  295. return $res;
  296. }
  297. /**
  298. * TODO 二级推广
  299. * @param $orderInfo
  300. * @return bool
  301. * @throws DataNotFoundException
  302. * @throws ModelNotFoundException
  303. * @throws DbException
  304. */
  305. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  306. {
  307. //TODO 获取购买商品的用户
  308. $userInfo = User::getUserInfo($orderInfo['uid']);
  309. //TODO 获取上推广人
  310. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  311. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  312. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  313. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  314. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  315. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  316. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  317. //TODO 返佣金额小于等于0 直接返回不返佣金
  318. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  319. // $open && self::beginTrans();
  320. $res = true;
  321. if ($virtualBrokeragePrice > 0) {
  322. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  323. //TODO 上级推广员返佣之后的金额
  324. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  325. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  326. //TODO 添加推广记录
  327. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  328. //TODO 添加用户余额
  329. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  330. //TODO 一级返佣成功 跳转二级返佣
  331. $res = $res1 && $res2;
  332. }
  333. if ($realBrokeragePrice > 0) {
  334. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  335. //TODO 上级推广员返佣之后的金额
  336. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  337. //TODO 添加推广记录
  338. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  339. }
  340. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  341. // $open && self::checkTrans($res);
  342. return $res;
  343. }
  344. /**
  345. * 发放实际商品佣金
  346. * @param $orderInfo
  347. * @return bool
  348. * @throws DataNotFoundException
  349. * @throws DbException
  350. * @throws ModelNotFoundException
  351. */
  352. public static function sendBackOrderBrokerage($orderInfo)
  353. {
  354. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  355. if ($list) {
  356. self::beginTrans();
  357. try {
  358. foreach ($list as $v) {
  359. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  360. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  361. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  362. }
  363. self::commitTrans();
  364. return true;
  365. } catch (Exception $e) {
  366. self::rollbackTrans();
  367. return false;
  368. }
  369. }
  370. return true;
  371. }
  372. /**
  373. * 获取推荐人 暂无使用
  374. * @param $uid
  375. * @param $page
  376. * @param $limit
  377. * @return mixed
  378. */
  379. public static function getSpreadList($uid, $page, $limit)
  380. {
  381. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  382. foreach ($list as $k => $user) {
  383. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  384. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  385. }
  386. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  387. $data['count'] = $count;
  388. $data['list'] = $list;
  389. return $data;
  390. }
  391. /**
  392. * 获取某个用户的下级uid
  393. * @param $uid
  394. * @return array
  395. */
  396. public static function getOneSpreadUid($uid)
  397. {
  398. return self::where('spread_uid', $uid)->column('uid');
  399. }
  400. /**
  401. * 修改个人信息
  402. * @param $avatar 头像
  403. * @param $nickname 昵称
  404. * @param $uid 用户uid
  405. * @return bool
  406. */
  407. public static function editUser($avatar, $nickname, $uid)
  408. {
  409. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  410. }
  411. /**
  412. * TODO 获取推广人数 一级
  413. * @param int $uid
  414. * @return bool|int|string
  415. */
  416. public static function getSpreadCount($uid = 0)
  417. {
  418. if (!$uid) return false;
  419. return self::where('spread_uid', $uid)->count();
  420. }
  421. /**
  422. * 修改当前用户的推广人数
  423. * @param $uid
  424. */
  425. public static function setUserSpreadCount($uid)
  426. {
  427. if (!$uid) return true;
  428. if (self::getSpreadCount($uid) > 0) {
  429. self::where('uid', $uid)->update(['spread_count' => 0]);
  430. } else {
  431. self::where('uid', $uid)->update(['spread_count' => 0]);
  432. return true;
  433. }
  434. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  435. }
  436. /**
  437. * TODO 获取推广人数 二级
  438. * @param int $uid
  439. * @return bool|int|string
  440. */
  441. public static function getSpreadLevelCount($uid = 0)
  442. {
  443. if (!$uid) return false;
  444. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  445. if (!count($uidSubordinate)) return 0;
  446. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  447. }
  448. /**
  449. * 获取用户下级推广人
  450. * @param int $uid 当前用户
  451. * @param int $grade 等级 0 一级 1 二级
  452. * @param string $orderBy 排序
  453. * @param string $keyword
  454. * @param int $page
  455. * @param int $limit
  456. * @return array|bool
  457. */
  458. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  459. {
  460. if (!$uid) return [];
  461. $gradeGroup = [0, 1];
  462. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  463. $userStair = self::where('spread_uid', $uid)->column('uid');
  464. if (!count($userStair)) return [];
  465. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  466. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  467. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  468. }
  469. /**
  470. * 获取团队信息
  471. * @param $uid
  472. * @param string $orderBy
  473. * @param string $keyword
  474. * @param int $page
  475. * @param int $limit
  476. * @return array
  477. */
  478. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  479. {
  480. $model = new self;
  481. if ($orderBy === '') $orderBy = 'u.add_time desc';
  482. $model = $model->alias(' u');
  483. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  484. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  485. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  486. $model = $model->where('u.uid', 'IN', $uid);
  487. $model = $model->field("u.uid,u.nickname,u.avatar,from_unixtime(u.add_time,'%Y/%m/%d') as time,u.spread_count as childCount,u.pay_count as orderCount,p.numberCount");
  488. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  489. $model = $model->group('u.uid');
  490. $model = $model->order($orderBy);
  491. $model = $model->page($page, $limit);
  492. $list = $model->select();
  493. if ($list) return $list->toArray();
  494. else return [];
  495. }
  496. /**
  497. * 设置用户的上级关系
  498. * @param $uid 用户uid
  499. * @param $spreadUid 上级用户uid
  500. * @return User|bool
  501. * @throws DataNotFoundException
  502. * @throws ModelNotFoundException
  503. * @throws DbException
  504. */
  505. public static function setSpreadUid($uid, $spreadUid)
  506. {
  507. return UserSpread::setSpread($uid, $spreadUid);
  508. // 自己不能绑定自己为上级
  509. // if ($uid == $spreadUid) return false;
  510. // //TODO 获取后台分销类型
  511. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  512. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  513. // if ($storeBrokerageStatus == 1) {
  514. // $spreadCount = self::where('uid', $spreadUid)->count();
  515. // if ($spreadCount) {
  516. // $spreadInfo = self::where('uid', $spreadUid)->find();
  517. // if ($spreadInfo->is_promoter) {
  518. // //TODO 只有扫码才可以获得推广权限
  519. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  520. // }
  521. // }
  522. // }
  523. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  524. }
  525. /**
  526. * 判断上下级关系是否存在
  527. * @param $uid
  528. * @param $spreadUid
  529. * @return bool|int
  530. */
  531. public static function validSpread($uid, $spreadUid)
  532. {
  533. if (!$uid || !$spreadUid) return false;
  534. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  535. }
  536. /**
  537. * H5用户注册
  538. * @param $account
  539. * @param $password
  540. * @param $spread
  541. * @return bool
  542. * @throws DataNotFoundException
  543. * @throws DbException
  544. * @throws ModelNotFoundException
  545. */
  546. public static function register($account, $password, $spread)
  547. {
  548. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  549. $phone = $account;
  550. $data['account'] = $account;
  551. $data['pwd'] = md5($password);
  552. $data['phone'] = $phone;
  553. // if ($spread) {
  554. // $data['spread_uid'] = $spread;
  555. // $data['spread_time'] = time();
  556. // }
  557. $data['real_name'] = '';
  558. $data['birthday'] = 0;
  559. $data['card_id'] = '';
  560. $data['mark'] = '';
  561. $data['addres'] = '';
  562. $data['user_type'] = 'h5';
  563. $data['add_time'] = time();
  564. $data['add_ip'] = app('request')->ip();
  565. $data['last_time'] = time();
  566. $data['last_ip'] = app('request')->ip();
  567. $data['nickname'] = substr(md5($account . time()), 0, 12);
  568. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  569. $data['city'] = '';
  570. $data['language'] = '';
  571. $data['province'] = '';
  572. $data['country'] = '';
  573. self::beginTrans();
  574. $res2 = WechatUser::create($data);
  575. $data['uid'] = $res2->uid;
  576. $res1 = self::create($data);
  577. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  578. self::checkTrans($res);
  579. return $res;
  580. }
  581. /**
  582. * 密码修改
  583. * @param $account
  584. * @param $password
  585. * @return User|bool
  586. */
  587. public static function reset($account, $password)
  588. {
  589. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  590. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  591. if ($count) return true;
  592. return self::where('account', $account)->update(['pwd' => md5($password)]);
  593. }
  594. /**
  595. * 获取手机号是否注册
  596. * @param $phone
  597. * @return bool
  598. */
  599. public static function checkPhone($phone)
  600. {
  601. return self::be(['account' => $phone]);
  602. }
  603. /**
  604. * 获取推广人
  605. * @param $data 查询条件
  606. * @return array
  607. * @throws DataNotFoundException
  608. * @throws ModelNotFoundException
  609. * @throws \think\exception\DbException
  610. */
  611. public static function getRankList($data)
  612. {
  613. switch ($data['type']) {
  614. case 'week':
  615. $startTime = strtotime('this week');
  616. $endTime = time();
  617. break;
  618. case 'month':
  619. $startTime = strtotime('last month');
  620. $endTime = time();
  621. break;
  622. }
  623. $list = self::alias('t0')
  624. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  625. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  626. ->where('t1.spread_uid', '<>', 0)
  627. ->order('count desc')
  628. ->order('t0.uid desc')
  629. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  630. ->page($data['page'], $data['limit'])
  631. ->group('t0.uid')
  632. ->select();
  633. return count($list) ? $list->toArray() : [];
  634. }
  635. /**
  636. * 获取佣金排行
  637. * @param $data
  638. * @return array
  639. */
  640. public static function brokerageRank($data)
  641. {
  642. $model = self::where('status', 1);
  643. switch ($data['type']) {
  644. case 'week':
  645. $model = $model->whereIn('uid', function ($query) {
  646. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  647. ->whereWeek('add_time')->field('uid');
  648. });
  649. break;
  650. case 'month':
  651. $model = $model->whereIn('uid', function ($query) {
  652. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  653. ->whereMonth('add_time')->field('uid');
  654. });
  655. break;
  656. }
  657. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  658. ->page((int)$data['page'], (int)$data['limit'])->select();
  659. return count($users) ? $users->toArray() : [];
  660. }
  661. /**
  662. * 获取当前用户的佣金排行位置
  663. * @param $uid
  664. * @return int
  665. */
  666. public static function currentUserRank($type, $brokerage_price)
  667. {
  668. $model = self::where('status', 1);
  669. switch ($type) {
  670. case 'week':
  671. $model = $model->whereIn('uid', function ($query) {
  672. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  673. ->whereWeek('add_time')->field('uid');
  674. });
  675. break;
  676. case 'month':
  677. $model = $model->whereIn('uid', function ($query) {
  678. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  679. ->whereMonth('add_time')->field('uid');
  680. });
  681. break;
  682. }
  683. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  684. }
  685. }