User.php 30 KB

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