User.php 25 KB

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