User.php 26 KB

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