User.php 25 KB

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