User.php 30 KB

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