User.php 28 KB

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