123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace app\api\controller;
- use app\common\model\UserLevelRank;
- use app\common\model\UserMessage;
- use app\common\validate\UserVali;
- use think\Db;
- use app\common\model\User;
- /**
- * @title 会员个人中心
- * @controller UserCenter
- * @group base
- */
- class UserCenter extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 获取个人信息
- * @desc 个人信息
- * @author qc
- * @url /api/User_center/getUserInfo
- * @method GET
- * @tag 个人信息
- * @header name:Authorization require:1 desc:Token
- * @return name:phone type:int default:-- desc:手机号
- * @return name:name type:string default:-- desc:昵称
- * @return name:headimg type:string default:-- desc:头像地址
- * @return name:sex type:int default:1 desc:性别(1男2女)
- * @return name:true_name type:string default:-- desc:真实姓名
- * @return name:invite_code type:string default:-- desc:邀请码
- * @return name:account_type type:int default:-- desc:账号类型1公司,2个人
- * @return name:is_first type:int default:-- desc:企业组是否是第一个注册(1是0否【第一个注册有会员会员重置权限,公司其他人会员权限读取公司第一个注册的会员权限】)
- * @return name:user_level type:array default:-- desc:开通会员信息[为空就不是vip]
- * @return name:user_level.level_id type:int default:-- desc:等级id
- * @return name:user_level.end_date type:string default:-- desc:过期时间
- * @return name:user_level.name type:string default:-- desc:会员等级名称
- * @return name:user_level.logo type:string default:-- desc:会员等级logo
- *
- */
- public function getUserInfo()
- {
- $field = 'id,phone,name,level_id,headimg,openid,true_name,sex,invite_code,account_type,group_id,is_first,group_first';
- $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
- if($user_info['account_type'] == 2 || ($user_info['account_type']== 1 && $user_info['is_first'])){
- $user_level = UserLevelRank::field('r.id,r.level_id,r.end_time,r.end_date,l.name,l.logo')
- ->alias('r')
- ->leftJoin('UserLevel l','l.id = r.level_id')
- ->where([['r.user_id','=',$this->user_id],['r.end_time','>',time()]])->order('r.level_id desc')->find();
- }else{
- $user_level = UserLevelRank::field('r.id,r.level_id,r.end_time,r.end_date,l.name,l.logo')
- ->alias('r')
- ->leftJoin('UserLevel l','l.id = r.level_id')
- ->where([['r.user_id','=',$user_info['group_first']],['r.end_time','>',time()]])->order('r.level_id desc')->find();
- }
- $this->success('获取成功',['detail'=>$user_info,'user_level'=>$user_level ? $user_level->toArray() :null]);
- }
- /**
- * @title 编辑个人信息
- * @desc 编辑个人信息
- * @author qc
- * @url /api/User_center/updateUserInfo
- * @method POST
- * @tag 编辑信息
- * @header name:Authorization require:1 desc:Token
- * @param name:name type:string default:-- desc:昵称
- * @param name:headimg type:string default:-- desc:头像地址
- */
- public function updateUserInfo()
- {
- $headimg = input('post.headimg');
- $name = trim(input('post.name',''));
- $update_data= [];
- $update_data['name'] = $name;
- $update_data['headimg'] = $headimg;
- Db::name('store_member')->where('id',$this->user_id)->update($update_data);
- $field = 'phone,name,headimg';
- $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
- $this->success('编辑成功',['detail'=>$user_info]);
- }
- /**
- * @title 绑定手机号
- * @desc 绑定手机号
- * @author qc
- * @url /api/User_center/modifyPhone
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:code type:int require:1 default:-- desc:手机号验证码
- * @return name:phone type:int default:-- desc:手机号
- * @return name:name type:string default:-- desc:昵称
- * @return name:headimg type:string default:-- desc:头像地址
- */
- public function modifyPhone()
- {
- $uid = $this->user_id;
- $phone = input('post.phone');
- $code = input('post.code');
- if(empty($phone) || empty($code) ) $this->error('参数错误');
- $field = 'id,phone,name,headimg';
- $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
- if($user_info['phone'] == $phone)$this->error('更换手机号与绑定手机号一致!');
- $check_code = $this->checkPhoneCode($phone,$code);
- if(!$check_code) $this->error('验证码错误');
- //判断手机号是否已注册
- $member_id = Db::name('store_member')
- ->where('phone','=',$phone)->where('is_deleted','=',0)->where('id','<>',$this->user_id)->value('id');
- if($member_id) $this->error('手机号已注册过');
- Db::name('store_member')->where('id',$uid)->update(['phone'=>$phone]);
- $user_info['phone'] = $phone;
- $this->updatePhoneCode($check_code);
- $this->success('绑定成功',$user_info);
- }
- /**
- * @title 更换或设置登录密码
- * @desc 更换或设置登录密码
- * @author qc
- * @url /api/User_center/modifyPassword
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:code type:int require:1 default:-- desc:手机号验证码
- * @param name:password type:string default:-- desc:密码
- */
- public function modifyPassword()
- {
- $uid = $this->user_id;
- $phone = input('post.phone');
- $code = input('post.code');
- $password = input('post.password');
- if(empty($phone) || empty($code) || empty($password) ) $this->error('参数错误');
- $field = 'id,phone';
- $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
- if(!$user_info['phone']) $this->error('请先绑定手机号');
- if($user_info['phone'] != $phone) $this->error('与绑定手机号不一致');
- $check_code = $this->checkPhoneCode($phone,$code);
- if(!$check_code) $this->error('验证码错误');
- $validate = new UserVali();
- if (!$validate->scene('reset_password')->check(['phone'=>$phone,'password'=>$password])) {
- $this->error($validate->getError());
- }
- $encryption_password = encrypt_password($password);
- User::where('id',$uid)->update(['password'=>$password,'encryption_password'=>$encryption_password]);
- $this->updatePhoneCode($check_code);
- $this->success('更换成功');
- }
- /**
- * @title 完善用户openid
- * @desc 完善用户openid
- * @author qc
- * @method POST
- * @tag 编辑信息
- * @url /api/User_center/saveOpenid
- * @header name:Authorization require:1 desc:Token
- * @param name:code type:string require:1 default:-- desc:code
- */
- public function saveOpenid()
- {
- $code = input('post.code');
- if (empty($code)) $this->error('参数错误');
- $res = http_get('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('app.official_account')['appid'].'&secret='.config('app.official_account')['secret'].'&code='.$code.'&grant_type=authorization_code');
- $res = json_decode($res,true);
- if (empty($res['openid'])) $this->error('未获取到openid');
- $member_data = ['openid' => $res['openid']];
- Db::name('store_member')->where('id',$this->user_id)->update($member_data);
- $this->success('已绑定openid');
- }
- /**
- * @title 消息列表【会员消息】
- * @desc 我的点赞
- * @author qc
- * @method GET
- * @url /api/User_center/getMessageList
- * @header name:Authorization require:1 desc:Token
- * @param name:title type:string default:-- desc:标题
- * @param name:page type:int default:-- desc:页数
- * @param name:page_num type:int default:-- desc:每页数
- *
- * @return name:id type:int default:-- desc:记录id
- * @return name:title type:string default:-- desc:标题
- * @return name:content type:string default:-- desc:内容
- * @return name:create_at type:string default:-- desc:时间
- * @return name:is_read type:int default:-- desc:是否已读【0否1是】
- */
- public function getMessageList()
- {
- $list = UserMessage::where(['user_id'=>$this->user_id])
- ->limit($this->off_set,$this->page_num)
- ->order(['id'=>'desc'])
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 消息详情【会员消息】
- * @desc 消息详情
- * @author qc
- * @method GET
- * @url /api/User_center/getMessageInfo
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int default:-- desc:消息记录id
- * @return name:title type:string default:-- desc:标题
- * @return name:content type:string default:-- desc:内容
- * @return name:create_at type:string default:-- desc:时间
- * @return name:is_read type:int default:-- desc:是否已读【0否1是】
- */
- public function getMessageInfo()
- {
- $detail= UserMessage::where(['user_id'=>$this->user_id,'id'=>input('get.id')])->find()->toArray();
- $this->success('ok',['detail'=>$detail]);
- }
- }
|