User.php 25 KB

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