User.php 25 KB

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