User.php 26 KB

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