User.php 30 KB

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