User.php 30 KB

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