User.php 28 KB

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