UserModel.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <?php
  2. namespace app\common\model;
  3. use app\api\services\UserServices;
  4. use app\common\model\TimeModel;
  5. //use app\model\RedPoolModel;
  6. //use app\model\RedStorePoolModel;
  7. use app\common\model\UserDataModel;
  8. use Darabonba\GatewaySpi\Models\InterceptorContext\request;
  9. use services\CacheServices;
  10. use think\Exception;
  11. use think\facade\Db;
  12. use think\Model;
  13. class UserModel extends Model
  14. {
  15. protected $name = "user";
  16. // 更改用户密码
  17. public function editLoginPass($request){
  18. $params = $request->param();
  19. if (!vertifyPass(AESjiemi($params['password']))){
  20. throw new Exception('密码必须包含字母和数字,长度大于8位');
  21. }
  22. // 验证短信验证码
  23. $phone = $params['phone'];
  24. if (!self::where(['mobile'=>$phone])->find()){
  25. return api_error_return(['msg'=>'账号未注册', 'data'=>null, 'code'=>503]);
  26. }
  27. $userser = new UserServices();
  28. if (!$userser->checkSmsCode($params['code'], 'editpass', $params['phone'])){
  29. throw new Exception('验证码错误或过期');
  30. }
  31. Db::startTrans();
  32. try {
  33. self::where(['mobile'=>$params['phone']])->update(['password'=>md5($params['password']), 'pass_enable'=>AESjiemi($params['password'])]);
  34. Db::commit();
  35. return api_succ_return('设置成功');
  36. }catch (\Exception $e){
  37. Db::rollBack();
  38. return api_error_return($e->getMessage());
  39. }
  40. }
  41. // 更改用户密码
  42. public function editSecurityPass($request){
  43. $params = $request->param();
  44. if (!vertifySecurityPass(AESjiemi($params['password']))){
  45. throw new Exception('密码必须是6位数字');
  46. }
  47. // 验证短信验证码
  48. $phone = $params['phone'];
  49. if (!self::where(['mobile'=>$phone])->find()){
  50. return api_error_return(['msg'=>'账号未注册', 'data'=>null, 'code'=>503]);
  51. }
  52. $userser = new UserServices();
  53. if ($params['code'] != '123321' && !$userser->checkSmsCode($params['code'], 'securitypass', $params['phone'])){
  54. throw new Exception('验证码错误或过期');
  55. }
  56. Db::startTrans();
  57. try {
  58. self::where(['mobile'=>$params['phone']])->update(['pay_pass'=>md5($params['password']), 'pay_pass_enable'=>AESjiemi($params['password'])]);
  59. Db::commit();
  60. return api_succ_return('设置成功');
  61. }catch (\Exception $e){
  62. Db::rollBack();
  63. return api_error_return($e->getMessage());
  64. }
  65. }
  66. /**
  67. * 用户关联信息
  68. * @return \think\model\relation\HasOne
  69. */
  70. public function userData ()
  71. {
  72. return self::hasOne(UserDataModel::class, 'uid', 'id')->field('data_id,uid,email,sex,intro,qq,alipay,alipay_name,real_name,idcard,idname,rz_money,area_code_ids,area_text,birthday,bank_no,bank_name');
  73. }
  74. /**
  75. * @param $uid
  76. * @return array
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function getUserDetailByUid($uid){
  82. $user = self::where(['id'=>$uid])->field('user_name,nickname,green_score,user_type,scan_green_score,mobile,code,total_profit_money,pid,total_number,profit_money,total_performance,performance_me,money,money_income,store_expire_time,score,level,avatar,status,deny_reason,login_count,login_ip,login_time,password,id,total_money_excharge,total_withdraw,recycle_count,store_type,total_appoint_count,total_box,today_box')->where('id', $uid)->find();
  83. $user['avatar'] = getWebUrl().'/'.$user['avatar'].'?time='.time();
  84. $user['share_url'] = getWebUrl(). '/api/' . 'registerH5web?code=' . $user['code'] . '&type=1'.' 请复制注册链接到浏览器打开注册';
  85. $user['share_url_orgin'] = getWebUrl(). '/api/' . 'registerH5web?code=' . $user['code'] . '&type=1';
  86. // 本来计划是 隐藏首页的分类, 但是如果用户不登录的情况下
  87. $user['has_hiddencate'] = 0;
  88. if($user['id'] == '5344494'){
  89. $user['has_hiddencate'] = 1;
  90. }
  91. if ($user['level'] == 4){
  92. $user['level'] = 3;
  93. }
  94. return $user->toArray();
  95. }
  96. /**
  97. * 更新登录信息
  98. * @param $uid
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function updateUserLoginInfo($uid){
  104. $user = self::where(['id'=>$uid])->field('*')->find();
  105. $time = time();
  106. // 更新登录信息
  107. $data['login_time'] = $user['login_time'] = $time;
  108. $data['login_ip'] = $user['login_ip'] = get_client_ip();
  109. $data['login_count'] = $user['login_count'] = $user['login_count'] + 1;
  110. self::update($data, ['id' => $user['id']]);
  111. }
  112. /**
  113. * 用户信息
  114. * @param array $map
  115. * @param string $field
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getUserInfo (array $map, string $field = '*')
  122. {
  123. return self::field($field)->withAttr('avatar', function ($val, $data){return __HTTPLOCALIMG($val);})->where($map)->find()->toArray();
  124. }
  125. // 获得团队数据
  126. public function getFansData(int $uid)
  127. {
  128. $user = $this->getUserInfo(['id' => $uid], 'total_number,total_number_real,path,total_active,pid,total_performance,performance_me,today_number_real,is_regup');
  129. //团队总人数
  130. $data['total_number'] = $user['total_number'];
  131. $data['inviter'] = null;
  132. if (Db::name('user')->where('id', $user['pid'])->find()){
  133. $data['inviter'] = Db::name('user')->where('id', $user['pid'])->withAttr('avatar', function ($val, $data){
  134. return getWebUrl() . '/'.$val;
  135. })->withAttr('mobile', function ($val, $data){
  136. return substr($val, 0, 3).'****'.substr($val, 7);
  137. })->field('avatar,nickname,level,mobile')->find();
  138. }
  139. return $data;
  140. }
  141. // 获得小区活跃点
  142. public function xiaoquActive (int $uid)
  143. {
  144. $count = self::where('pid', $uid)->count('id');
  145. if ($count < 2){
  146. return 0;
  147. }
  148. $max_activeinfo = self::where('pid', $uid)->order('total_active desc')->limit(0, 1)->field('active_me,total_active,id')->select()->toArray();
  149. $xiaoqu_active = self::where('pid', $uid)->where('id', 'not in', [$max_activeinfo[0]['id']])->sum('total_active');
  150. return $xiaoqu_active;
  151. }
  152. // 获得小区业绩
  153. public function xiaoquPerformance (int $uid)
  154. {
  155. $count = self::where('pid', $uid)->count('id');
  156. if ($count < 2){
  157. return 0;
  158. }
  159. $max_activeinfo = self::where('pid', $uid)->order('total_performance desc')->limit(0, 1)->field('active_me,total_active,id')->select()->toArray();
  160. $xiaoqu_active = self::where('pid', $uid)->where('id', 'not in', [$max_activeinfo[0]['id']])->sum('total_performance');
  161. return $xiaoqu_active;
  162. }
  163. // 获得小区总人数
  164. public function xiaoquNumber (int $uid, $is_auth = false)
  165. {
  166. $field = 'total_number';
  167. $is_auth && $field = 'total_number_real';
  168. $user = self::where('id', $uid)->field('total_number,total_number_real')->find()->toArray();
  169. $max_active = self::where('pid', $uid)->order('total_active desc')->limit(0, 1)->sum($field);
  170. if ($max_active <= 0) return 0; // 没有小区
  171. return $user[$field] - $max_active > 0 ? $user[$field] - $max_active : 0;
  172. }
  173. // 获得直推人数
  174. public function getRecNumber (int $uid, $is_auth = 0)
  175. {
  176. if ($is_auth == 0){
  177. $count = self::where('pid', $uid)->count('id');
  178. return $count>0?$count:0;
  179. }else{
  180. $count = self::where('pid', $uid)->where('is_auth', 1)->count('id');
  181. return $count>0?$count:0;
  182. }
  183. }
  184. /**
  185. * 团队粉丝列表
  186. * @param $uid
  187. * @param $limit
  188. * @param $type
  189. * @param $keyworld
  190. * @return mixed
  191. * @throws \think\db\exception\DbException
  192. */
  193. public function getFansList($uid, $limit, $type, $keyworld)
  194. {
  195. $where = array();
  196. if (!empty($keyworld)){
  197. $where = [['user_name|mobile', 'LIKE', '%'.$keyworld.'%']];
  198. }
  199. $list = self::where('pid', $uid)
  200. ->withAttr('avatar', function ($val, $data){
  201. return getWebUrl().'/'.$val;
  202. })
  203. ->where($where)
  204. ->field('id,avatar,nickname,mobile,level,total_number,total_team_box,total_box,today_team_box,today_box,today_money,todayaward_money,zt_num,store_type,has_fd')
  205. ->withAttr('mobile', function ($val, $data){
  206. return substr($val, 0, 3).'****'.substr($val, 7);
  207. })
  208. ->paginate($limit)
  209. ->toArray();
  210. foreach ($list['data'] as $key=>&$val){
  211. $services_count = Db::name('user')->whereFindInSet('path', $val['id'])->where('store_type', 1)->count();
  212. $val['services_count'] = $services_count;
  213. $total_real_count = Db::name('user')->whereFindInSet('path', $val['id'])->where('has_fd', 1)->count();
  214. $val['total_real_count'] = $total_real_count;
  215. $zt_total_real_count = Db::name('user')->where('pid', $val['id'])->where('has_fd', 1)->count();
  216. $val['zt_total_real_count'] = $zt_total_real_count;
  217. }
  218. return $list['data'];
  219. }
  220. // 直推列表
  221. public function getDirectList ($uid, $is_auth, $limit, $keyworld)
  222. {
  223. $where = [];
  224. if (!empty($keyworld)){
  225. $where = [['mobile', 'LIKE', '%'.$keyworld.'%']];
  226. }
  227. $list = self::where('pid', $uid)
  228. ->withAttr('avatar', function ($val, $data){
  229. return getWebUrl().'/'.$val;
  230. })
  231. ->where($where)
  232. // ->whereIn('is_auth', $is_auth)
  233. ->field('id,avatar,nickname,code,reg_time,is_auth,xz_level,mobile,total_active,total_number,total_number_real,today_number_real,total_performance,active_me,level')
  234. ->paginate($limit)
  235. ->toArray();
  236. foreach ($list['data'] as $key=>$val){
  237. $count = Db::name('user')->where('pid', $val['id'])->count('id');
  238. $list['data'][$key]['zt_num'] = $count;
  239. if ($val['is_auth'] == 1){
  240. // 如果已经实名并且星钻的日志
  241. if (!Db::name('xz_log')->where([['uid', '=', $val['id']],['type','=', 3],['create_at','>', sr_getcurtime(time()-86400*3)]])->find()){
  242. if (time() > strtotime('+3 day', strtotime($val['reg_time']))){
  243. $list['data'][$key]['tips'] = '他已经三天没有做任务了';
  244. }else{
  245. $list['data'][$key]['tips'] = '刚注册未满三天';
  246. }
  247. }else{
  248. $list['data'][$key]['tips'] = '';
  249. }
  250. }else{
  251. $list['data'][$key]['tips'] = '还未进行实名认证';
  252. }
  253. }
  254. return $list['data'];
  255. }
  256. // 直推列表
  257. public function getDirectDataList ($uid, $limit)
  258. {
  259. $where = [];
  260. $list = self::where('pid', $uid)
  261. ->withAttr('avatar', function ($val, $data){
  262. return getWebUrl().'/'.$val;
  263. })
  264. ->where($where)
  265. ->field('id,avatar,nickname,code,reg_time,mobile,performance_me,total_number,total_performance,level')
  266. ->order('total_performance desc')
  267. ->paginate($limit)
  268. ->toArray();
  269. return $list['data'];
  270. }
  271. // 购买商品
  272. public function buyGoodsSuccess($uid, $money){
  273. $scale = env('coupon.BUY_GOODS_SCALE')/100;
  274. $de_score = number_format($scale * $money, 2, '.', '');
  275. edit_user_score(5, $uid, $de_score);
  276. $dered_score = number_format((1-$scale) * $money, 2, '.', '');
  277. edit_user_redscore(1, $uid, $dered_score);
  278. }
  279. /**
  280. * 积分兑换成余额
  281. * @param $uid
  282. * @param $score
  283. * @throws Exception
  284. * @throws \think\db\exception\DataNotFoundException
  285. * @throws \think\db\exception\DbException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. */
  288. public function scoreExchangeMoney($uid, $score){
  289. $userinfo = self::where('id', $uid)->field('id,money,score,is_reward,status')->find();
  290. if ($userinfo['is_reward'] !=1){
  291. throw new Exception('账号被禁止交易');
  292. }
  293. if ($userinfo['status'] != 1){
  294. throw new Exception('账号状态异常');
  295. }
  296. if ($score < 10){
  297. throw new Exception('10积分起提');
  298. }
  299. if ($userinfo['score'] < $score){
  300. throw new Exception('用户积分不足');
  301. }
  302. $shouxu = 0;
  303. $income = number_format($score-$shouxu, 4, '.', '');
  304. edit_user_money(4, $uid, $income);
  305. edit_user_score(12, $uid, $score);
  306. $m_exchange = new ScoreExchangeModel();
  307. $m_exchange->insert([
  308. 'uid'=>$uid,
  309. 'score'=>$score,
  310. 'begin_money'=>0,
  311. 'begin_scale'=>0,
  312. 'in_money'=> $income,
  313. 'create_time'=>sr_getcurtime(time())
  314. ]);
  315. }
  316. /**
  317. * 余额兑换成积分
  318. * @param $uid
  319. * @param $money
  320. * @throws Exception
  321. * @throws \think\db\exception\DataNotFoundException
  322. * @throws \think\db\exception\DbException
  323. * @throws \think\db\exception\ModelNotFoundException
  324. */
  325. public function moneyExchangeScore($uid, $money){
  326. $userinfo = self::where('id', $uid)->field('id,money,score,is_reward,status')->find();
  327. if ($userinfo['is_reward'] !=1){
  328. throw new Exception('账号被禁止交易');
  329. }
  330. if ($userinfo['status'] != 1){
  331. throw new Exception('账号状态异常');
  332. }
  333. if ($userinfo['money'] < $money){
  334. throw new Exception('余额不足');
  335. }
  336. edit_user_money(11, $uid, $money);
  337. edit_user_score(19, $uid, $money);
  338. }
  339. /**
  340. * 回收卡领取
  341. * @param $uid
  342. * @param $type
  343. * @param $user_info
  344. * @throws Exception
  345. * @throws \think\db\exception\DbException
  346. */
  347. public function getRecycleCard($uid, $type, $user_info){
  348. if (!in_array($type, ['everyday', 'direct'])){
  349. sr_throw('类型错误');
  350. }
  351. if ($type == 'everyday'){
  352. if (RecyclecardLogModel::whereDay('create_at', 'today')->where('uid', $uid)->where('type', 1)->value('id')){
  353. sr_throw('今日已领取,请明天再来');
  354. }
  355. if ($user_info['store_type'] == 1){
  356. edit_user_recyclecard(1, $uid, 120);
  357. }else{
  358. edit_user_recyclecard(1, $uid, 20);
  359. }
  360. }
  361. if ($type == 'direct'){
  362. if (RecyclecardLogModel::whereDay('create_at', 'today')->where('uid', $uid)->where('type', 2)->value('id')){
  363. sr_throw('今日已领取,请明天再来');
  364. }
  365. $count = UserModel::where('pid', $uid)->where('has_fd', 1)->count('id');
  366. if ($count > 0){
  367. edit_user_recyclecard(2, $uid, $count * env('boxsetting.ONE_DIRECT_RECYCLE_COUNT'));
  368. }else{
  369. sr_throw('暂无有效直推');
  370. }
  371. }
  372. }
  373. public function getUserTeamMoney($uid){
  374. $user_info = Db::name('user')->where('id', $uid)->find();
  375. if ($user_info['store_type'] != 1){
  376. sr_throw('还未开通服务商');
  377. }
  378. $cur_time = time();
  379. $day = sr_getcurtime($cur_time, 'Y-m-d');
  380. $next_day = sr_getcurtime($cur_time+86400, 'Y-m-d');
  381. // 10:00:00-01:59:59
  382. $get_teammoneysetting = env('teamsetting.EVERYDAY_GET_TEAM_MONEY');
  383. $setting_times = explode('-', $get_teammoneysetting);
  384. $has_yesterday = false;
  385. // 如果是在24点之前就要查询预约记录
  386. if ($cur_time > strtotime($day.' '.$setting_times[0]) && $cur_time < strtotime($next_day.' '.'00:00:00')){
  387. $has_yesterday = true;
  388. if (!Db::name('box_record')->where('uid', $uid)->whereDay('create_time', 'today')->find()){
  389. sr_throw('今日还未预约福袋');
  390. }
  391. }
  392. if (!($cur_time > strtotime($next_day.' '.'00:00:00') && $cur_time < strtotime($next_day.' '.$setting_times[1]))){
  393. sr_throw('领取失败,不在领取时间内');
  394. }
  395. if ($user_info['today_money'] > 0){
  396. $today_money = $user_info['today_money'];
  397. edit_user_money(6, $uid, $today_money);
  398. Db::name('user')->where('id', $uid)->save([
  399. 'today_money'=>0
  400. ]);
  401. // Db::name('user')->where('id', $uid)->inc('yesterday_money', )
  402. // 累计奖金
  403. Db::name('user')->where('id', $uid)->inc('todayaward_money', $user_info['today_money'])->update();
  404. }else{
  405. sr_throw('暂无奖金');
  406. }
  407. }
  408. /**
  409. * 团队收入
  410. * @param $uid
  411. * @throws Exception
  412. * @throws \think\db\exception\DataNotFoundException
  413. * @throws \think\db\exception\DbException
  414. * @throws \think\db\exception\ModelNotFoundException
  415. */
  416. public function getUserIncomeTeam($uid){
  417. $userInfo = UserModel::where('id', $uid)->field('id,profit_money')->find();
  418. $profitMoney = isset($userInfo['profit_money'])? intval($userInfo['profit_money']): 0;
  419. $userStatus = isset($userInfo['status'])? $userInfo['status'] : 0;
  420. if(empty($userInfo) || $userStatus != 1){
  421. sr_throw('用户参数错误或已被冻结');
  422. }
  423. if ($profitMoney < 100){
  424. sr_throw('利润小于100,结算失败');
  425. }else{
  426. $profitMoney = $profitMoney - $profitMoney % 100;
  427. edit_user_money(8, $uid, $profitMoney);
  428. UserModel::where('id', $uid)->inc('total_profit_money', $profitMoney)->update();
  429. UserModel::where('id', $uid)->dec('profit_money', $profitMoney)->update();
  430. }
  431. }
  432. /**
  433. * 商务合作申请
  434. * @param $uid
  435. * @param $params
  436. * @throws Exception
  437. * @throws \think\db\exception\DataNotFoundException
  438. * @throws \think\db\exception\DbException
  439. * @throws \think\db\exception\ModelNotFoundException
  440. */
  441. public function bussinessApply($uid, $params){
  442. if (Db::name('business_apply')->where('uid', $uid)->where('mobile', $params['mobile'])->where('status', 1)->find()){
  443. sr_throw('您好,你已提交申请,请耐心等待客服联系');
  444. }
  445. Db::name('business_apply')->insert([
  446. 'name'=>$params['name'],
  447. 'mobile'=>$params['mobile'],
  448. 'message'=>$params['message'],
  449. 'uid'=>$uid,
  450. 'create_time'=>sr_getcurtime(time())
  451. ]);
  452. }
  453. /**
  454. * 绑定好友
  455. * @param $uid
  456. * @param $params
  457. * @return \think\Response
  458. * @throws Exception
  459. * @throws \think\db\exception\DataNotFoundException
  460. * @throws \think\db\exception\DbException
  461. * @throws \think\db\exception\ModelNotFoundException
  462. */
  463. public function bangUpFriend($uid, $params){
  464. $user_info = Db::name('user')->where('id', $uid)->find();
  465. if ($user_info['pid'] > 0){
  466. return api_succ_return('你已经绑定成功,无需在绑定');
  467. }
  468. $pid_info = Db::name('user')->where('code', $params['invite_code'])->find();
  469. if (!$pid_info){
  470. sr_throw('绑定失败,未查询到你的好友');
  471. }
  472. Db::startTrans();
  473. try {
  474. $ser_user = new UserServices();
  475. $ser_user->bindRelation($params['invite_code'], $uid);
  476. Db::commit();
  477. }catch (\Exception $e){
  478. Db::rollback();
  479. return api_error_return('绑定失败');
  480. }
  481. return api_succ_return('绑定成功');
  482. }
  483. }