User.php 25 KB

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