User.php 30 KB

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