User.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. 'channel' => $routineUser['channel'],
  212. 'user_type' => $routineUser['user_type']
  213. ]);
  214. $res = $res1 && $res2;
  215. self::checkTrans($res);
  216. if ($res) {
  217. event('UserRegistered', ['user' => $res2->toArray()]);
  218. }
  219. return $res2;
  220. }
  221. /**
  222. * 获得当前登陆用户UID
  223. * @return int $uid
  224. */
  225. public static function getActiveUid()
  226. {
  227. $uid = null;
  228. $uid = Session::get('LoginUid');
  229. if ($uid) return $uid;
  230. else return 0;
  231. }
  232. /**
  233. * TODO 查询当前用户信息
  234. * @param $uid $uid 用户编号
  235. * @param string $field $field 查询的字段
  236. * @return array
  237. * @throws \think\Exception
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. * @throws \think\exception\DbException
  241. */
  242. public static function getUserInfo($uid, $field = '')
  243. {
  244. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  245. else $userInfo = self::where('uid', $uid)->find();
  246. if (!$userInfo) return [];
  247. return $userInfo->toArray();
  248. }
  249. /**
  250. * 判断当前用户是否推广员
  251. * @param int $uid
  252. * @return bool
  253. */
  254. public static function isUserSpread($uid = 0)
  255. {
  256. if (!$uid) return false;
  257. $status = (int)sys_config('store_brokerage_statu');
  258. $isPromoter = true;
  259. if ($status == DISTRIBUTE_SPECIFIED) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  260. if ($isPromoter) return true;
  261. else return false;
  262. }
  263. /**
  264. * TODO 一级返佣
  265. * @param $orderInfo
  266. * @return bool
  267. * @throws \think\Exception
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\ModelNotFoundException
  270. * @throws \think\exception\DbException
  271. */
  272. public static function backOrderBrokerage($orderInfo, bool $open = true)
  273. {
  274. //TODO 营销产品不返佣金
  275. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  276. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  277. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  278. $userInfo = User::getUserInfo($orderInfo['uid']);
  279. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  280. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  281. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  282. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  283. $brokeragePrice = StoreProduct::getProductBrokerage($cartId);
  284. //TODO 返佣金额小于等于0 直接返回不返佣金
  285. if ($brokeragePrice <= 0) return true;
  286. //TODO 获取上级推广员信息
  287. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  288. //TODO 上级推广员返佣之后的金额
  289. $balance = bcadd($spreadUserInfo['brokerage_price'], $brokeragePrice, 2);
  290. $mark = $userInfo['nickname'] . '成功消费' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokeragePrice);
  291. $open && self::beginTrans();
  292. //TODO 添加推广记录
  293. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $balance, $mark);
  294. //TODO 添加用户余额
  295. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  296. //TODO 一级返佣成功 跳转二级返佣
  297. $res = $res1 && $res2 && self::backOrderBrokerageTwo($orderInfo, $open);
  298. $open && self::checkTrans($res);
  299. // if($res) return self::backOrderBrokerageTwo($orderInfo);
  300. return $res;
  301. }
  302. /**
  303. * TODO 二级推广
  304. * @param $orderInfo
  305. * @return bool
  306. * @throws \think\Exception
  307. * @throws \think\db\exception\DataNotFoundException
  308. * @throws \think\db\exception\ModelNotFoundException
  309. * @throws \think\exception\DbException
  310. */
  311. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  312. {
  313. //TODO 获取购买商品的用户
  314. $userInfo = User::getUserInfo($orderInfo['uid']);
  315. //TODO 获取上推广人
  316. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  317. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  318. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  319. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  320. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  321. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  322. $brokeragePrice = StoreProduct::getProductBrokerage($cartId, false);
  323. //TODO 返佣金额小于等于0 直接返回不返佣金
  324. if ($brokeragePrice <= 0) return true;
  325. //TODO 获取上上级推广员信息
  326. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  327. //TODO 获取上上级推广员返佣之后余额
  328. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $brokeragePrice, 2);
  329. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokeragePrice);
  330. $open && self::beginTrans();
  331. //TODO 添加返佣记录
  332. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $balance, $mark);
  333. //TODO 添加用户余额
  334. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  335. $res = $res1 && $res2;
  336. $open && self::checkTrans($res);
  337. return $res;
  338. }
  339. /**
  340. * 获取推荐人 暂无使用
  341. * @param $uid
  342. * @param $page
  343. * @param $limit
  344. * @return mixed
  345. */
  346. public static function getSpreadList($uid, $page, $limit)
  347. {
  348. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  349. foreach ($list as $k => $user) {
  350. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  351. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  352. }
  353. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  354. $data['count'] = $count;
  355. $data['list'] = $list;
  356. return $data;
  357. }
  358. /**
  359. * 获取某个用户的下级uid
  360. * @param $uid
  361. * @return array
  362. */
  363. public static function getOneSpreadUid($uid)
  364. {
  365. return self::where('spread_uid', $uid)->column('uid');
  366. }
  367. /**
  368. * 修改个人信息
  369. * @param $avatar 头像
  370. * @param $nickname 昵称
  371. * @param $uid 用户uid
  372. * @return bool
  373. */
  374. public static function editUser($avatar, $nickname, $uid)
  375. {
  376. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  377. }
  378. /**
  379. * TODO 获取推广人数 一级
  380. * @param int $uid
  381. * @return bool|int|string
  382. */
  383. public static function getSpreadCount($uid = 0)
  384. {
  385. if (!$uid) return false;
  386. return self::where('spread_uid', $uid)->count('uid');
  387. }
  388. /**
  389. * 修改当前用户的推广人数
  390. * @param $uid
  391. */
  392. public static function setUserSpreadCount($uid)
  393. {
  394. self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  395. }
  396. /**
  397. * TODO 获取推广人数 二级
  398. * @param int $uid
  399. * @return bool|int|string
  400. */
  401. public static function getSpreadLevelCount($uid = 0)
  402. {
  403. if (!$uid) return false;
  404. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  405. if (!count($uidSubordinate)) return 0;
  406. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count('uid');
  407. }
  408. /**
  409. * 获取用户下级推广人
  410. * @param int $uid 当前用户
  411. * @param int $grade 等级 0 一级 1 二级
  412. * @param string $orderBy 排序
  413. * @param string $keyword
  414. * @param int $page
  415. * @param int $limit
  416. * @return array|bool
  417. */
  418. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  419. {
  420. if (!$uid) return [];
  421. $gradeGroup = [0, 1];
  422. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  423. $userStair = self::where('spread_uid', $uid)->column('uid');
  424. if (!count($userStair)) return [];
  425. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  426. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  427. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  428. }
  429. /**
  430. * 获取团队信息
  431. * @param $uid
  432. * @param string $orderBy
  433. * @param string $keyword
  434. * @param int $page
  435. * @param int $limit
  436. * @return array
  437. */
  438. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  439. {
  440. $model = new self;
  441. if ($orderBy === '') $orderBy = 'u.add_time desc';
  442. $model = $model->alias(' u');
  443. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  444. ->where('o.is_del', 0)->where('o.refund_status', 'in', [0,1])->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  445. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  446. $model = $model->where('u.uid', 'IN', $uid);
  447. $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");
  448. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  449. $model = $model->group('u.uid');
  450. $model = $model->order($orderBy);
  451. $model = $model->page($page, $limit);
  452. $list = $model->select();
  453. if ($list) return $list->toArray();
  454. else return [];
  455. }
  456. /**
  457. * 设置用户的上级关系
  458. * @param $uid 用户uid
  459. * @param $spreadUid 上级用户uid
  460. * @return User|bool
  461. * @throws \think\db\exception\DataNotFoundException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. * @throws \think\exception\DbException
  464. */
  465. public static function setSpreadUid($uid, $spreadUid)
  466. {
  467. // 自己不能绑定自己为上级
  468. if ($uid == $spreadUid) return false;
  469. //TODO 获取后台分销类型
  470. $storeBrokerageStatus = sys_config('store_brokerage_statu');
  471. $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : DISTRIBUTE_SPECIFIED;
  472. if ($storeBrokerageStatus == DISTRIBUTE_SPECIFIED) {
  473. $spreadCount = self::where('uid', $spreadUid)->count();
  474. if ($spreadCount) {
  475. $spreadInfo = self::where('uid', $spreadUid)->find();
  476. if ($spreadInfo->is_promoter) {
  477. //TODO 只有扫码才可以获得推广权限
  478. if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  479. }
  480. }
  481. }
  482. return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  483. }
  484. /**
  485. * 判断上下级关系是否存在
  486. * @param $uid
  487. * @param $spreadUid
  488. * @return bool|int
  489. */
  490. public static function validSpread($uid, $spreadUid)
  491. {
  492. if (!$uid || !$spreadUid) return false;
  493. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  494. }
  495. /**
  496. * H5用户注册
  497. * @param $account
  498. * @param $password
  499. * @param $spread
  500. * @return User|\think\Model
  501. */
  502. public static function register($account, $password, $spread)
  503. {
  504. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  505. $phone = $account;
  506. $data['account'] = $account;
  507. $data['pwd'] = md5($password);
  508. $data['phone'] = $phone;
  509. if ($spread) {
  510. $data['spread_uid'] = $spread;
  511. $data['spread_time'] = time();
  512. }
  513. $data['real_name'] = '';
  514. $data['birthday'] = 0;
  515. $data['card_id'] = '';
  516. $data['mark'] = '';
  517. $data['addres'] = '';
  518. $data['user_type'] = 'h5';
  519. $data['add_time'] = time();
  520. $data['add_ip'] = app('request')->ip();
  521. $data['last_time'] = time();
  522. $data['last_ip'] = app('request')->ip();
  523. $data['nickname'] = substr(md5($account . time()), 0, 12);
  524. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  525. $data['city'] = '';
  526. $data['language'] = '';
  527. $data['province'] = '';
  528. $data['country'] = '';
  529. self::beginTrans();
  530. $res2 = WechatUser::create($data);
  531. $data['uid'] = $res2->uid;
  532. $res1 = self::create($data);
  533. $res = $res1 && $res2;
  534. self::checkTrans($res);
  535. return $res;
  536. }
  537. /**
  538. * 密码修改
  539. * @param $account
  540. * @param $password
  541. * @return User|bool
  542. */
  543. public static function reset($account, $password)
  544. {
  545. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  546. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  547. if ($count) return true;
  548. return self::where('account', $account)->update(['pwd' => md5($password)]);
  549. }
  550. /**
  551. * 获取手机号是否注册
  552. * @param $phone
  553. * @return bool
  554. */
  555. public static function checkPhone($phone)
  556. {
  557. return self::be(['account' => $phone]);
  558. }
  559. /**
  560. * 获取推广人
  561. * @param $data 查询条件
  562. * @return array
  563. * @throws \think\db\exception\DataNotFoundException
  564. * @throws \think\db\exception\ModelNotFoundException
  565. * @throws \think\exception\DbException
  566. */
  567. public static function getRankList($data)
  568. {
  569. switch ($data['type']) {
  570. case 'week':
  571. $startTime = strtotime('this week');
  572. $endTime = time();
  573. break;
  574. case 'month':
  575. $startTime = strtotime('last month');
  576. $endTime = time();
  577. break;
  578. }
  579. $list = self::alias('t0')
  580. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  581. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  582. ->where('t1.spread_uid', '<>', 0)
  583. ->order('count desc')
  584. ->order('t0.uid desc')
  585. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  586. ->page($data['page'], $data['limit'])
  587. ->group('t0.uid')
  588. ->select();
  589. return count($list) ? $list->toArray() : [];
  590. }
  591. /**
  592. * 获取佣金排行
  593. * @param $data
  594. * @return array
  595. */
  596. public static function brokerageRank($data)
  597. {
  598. $model = UserBill::alias('b')->join('user u', 'b.uid = u.uid');
  599. $model = $model->where('b.category', 'now_money')->where('b.type', 'brokerage');
  600. switch ($data['type']) {
  601. case 'week':
  602. $model = $model->whereWeek('b.add_time');
  603. break;
  604. case 'month':
  605. $model = $model->whereMonth('b.add_time');
  606. break;
  607. }
  608. $users = $model->group('b.uid')
  609. ->field('b.uid,u.nickname,u.avatar,SUM(IF(pm=1,`number`,-`number`)) as brokerage_price')
  610. ->order('brokerage_price desc')
  611. ->page((int)$data['page'], (int)$data['limit'])
  612. ->select();
  613. return count($users) ? $users->toArray() : [];
  614. }
  615. /**
  616. * 获取当前用户的佣金排行位置
  617. * @param $uid
  618. * @return int
  619. */
  620. public static function currentUserRank($type, $brokerage_price)
  621. {
  622. $model = self::where('status', 1);
  623. switch ($type) {
  624. case 'week':
  625. $model = $model->whereIn('uid', function ($query) {
  626. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  627. ->whereWeek('add_time')->field('uid');
  628. });
  629. break;
  630. case 'month':
  631. $model = $model->whereIn('uid', function ($query) {
  632. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  633. ->whereMonth('add_time')->field('uid');
  634. });
  635. break;
  636. }
  637. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  638. }
  639. }