User.php 26 KB

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