User.php 25 KB

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