User.php 27 KB

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