User.php 26 KB

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