User.php 25 KB

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