User.php 32 KB

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