User.php 29 KB

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