123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?php
- namespace app\api\controller;
- use app\common\model\UserLevelRank;
- use app\common\model\UserMessage;
- use app\common\model\UserVitality;
- 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:hx_username type:string default:-- desc:环信账号用户名
- * @return name:hx_uuid type:string default:-- desc:环信账号uuid
- * @return name:bind_phone type:string default:-- desc:绑定的手机号
- * @return name:bind_email type:string default:-- desc:绑定的邮箱
- * @return name:hx_password type:string default:-- desc:环信密码
- * @return name:is_hx type:string default:-- desc:是否是活动门票核销员【1是0否】
- * @return name:bind_id type:string default:-- desc:被绑定会员id
- * @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
- * @return name:user_level.icons type:string default:-- desc:等级标签
- *
- */
- public function getUserInfo()
- {
- $field = 'id,phone,name,level_id,email,headimg,openid,true_name,sex,invite_code,account_type,group_id,is_first,group_first,hx_username,hx_password,hx_uuid,bind_id,is_hx';
- $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
- $user_level = UserLevelRank::getUserVipInfo($this->user_id);
- $user_info['bind_phone']= $user_info['phone'];
- $user_info['bind_email']= $user_info['email'];
- if($user_info['bind_id']) $user_info['bind_phone'] = User::where('id',$user_info['bind_id'])->value('phone');
- $bind_email = User::where('bind_id',$user_info['id'])->value('email');
- if($bind_email) $user_info['bind_email'] = $bind_email;
- $user_info['level_id'] = !empty($user_level) ? $user_level['level_id'] : 0;
- $this->success('获取成功',['detail'=>$user_info,'user_level'=>$user_level]);
- }
- /**
- * @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= [];
- if($name)$update_data['name'] = $name;
- if($headimg)$update_data['headimg'] = $headimg;
- if(empty($update_data)) $this->error('修改会员信息有误');
- Db::name('store_member')->where('id',$this->user_id)->update($update_data);
- $field = 'phone,name,headimg,email';
- $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
- $user_level = UserLevelRank::getUserVipInfo($this->user_id);
- $user_info['level_id'] = !empty($user_level) ? $user_level['level_id'] : 0;
- $this->success('编辑成功',['detail'=>$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/getUnreadNum
- * @header name:Authorization require:1 desc:Token
- * @param name:num type:int default:0 desc:未读数量
- */
- public function getUnreadNum()
- {
- $num = UserMessage::where(['user_id'=>$this->user_id,'is_deleted'=>0])->where('is_read',0)->count();
- $this->success('ok',['num'=>$num]);
- }
- /**
- * @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,'is_deleted'=>0])
- ->limit($this->off_set,$this->page_num)
- ->order(['id'=>'desc'])
- ->select()->toArray();
- //if(!empty($list)) UserMessage::where('id','in',array_column($list,'id'))->update(['is_read'=>1]);
- $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();
- UserMessage::where('id',input('get.id'))->update(['is_read'=>1]);
- $this->success('ok',['detail'=>$detail]);
- }
- /**
- * @title 用户注销
- * @desc 用户注销
- * @author qc
- * @method GET
- * @url /api/User_center/delUser
- * @header name:Authorization require:1 desc:Token
- */
- public function delUser()
- {
- User::where('id',$this->user_id)->update(['is_deleted' => '1','phone'=>'','email'=>'']);
- User::where('bind_id',$this->user_id)->update(['bind_id' => '0']);
- $this->success('注销成功');
- }
- /**
- * @title 以下接口为二期需求
- * @desc 以下接口为二期需求
- * @author qc
- * @url /api/User_center/secondPhase
- * @method
- * @return name:1 type:string default:-- desc:以下接口为二期需求
- */
- public function secondPhase(){}
- /**
- * @title 获取会员活跃度
- * @desc 获取会员活跃度
- * @author qc
- * @url /api/User_center/getUserVitality
- * @method GET
- * @param name:date_str type:int default:-- desc:时间查询【按年:2023,按月:2023-02,按天:2023-02-22】
- * @return name:type type:int default:-- desc:1:安卓手机,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,pc电脑
- * @return name:num type:int default:-- desc:1:活跃度(登录天数)
- */
- public function getUserVitality()
- {
- $date_str = input('get.date_str');
- $date_arr = explode('-',$date_str);
- $where = [];
- $where['user_id'] = $this->user_id;
- if(isset($date_arr[0])) $where['year'] =$date_arr[0];
- if(isset($date_arr[1])) $where['month'] =$date_arr[1];
- if(isset($date_arr[2])) $where['day'] =$date_arr[2];
- $list = UserVitality::field('type,count(id) num')->where($where)->group('type')->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 获取会员活跃度日志
- * @desc 获取会员活跃度日志
- * @author qc
- * @url /api/User_center/getUserVitalityLog
- * @method GET
- * @param name:page type:int default:0 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:date_str type:int default:-- desc:时间查询【按年:2023,按月:2023-02,按天:2023-02-22】
- * @param name:type type:int default:-- desc:1:安卓手机,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,pc电脑
- * @return name:type type:int default:-- desc:1:安卓手机,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,pc电脑
- * @return name:year type:string default:-- desc:年
- * @return name:month type:string default:-- desc:月
- * @return name:day type:string default:-- desc:日
- * @return name:day_time type:string default:-- desc:日期
- * @return name:create_at type:string default:-- desc:具体时间
- */
- public function getUserVitalityLog()
- {
- $date_str = input('get.date_str');
- $date_arr = explode('-',$date_str);
- $where = [];
- $where['user_id'] = $this->user_id;
- $where['type'] = input('get.type');
- if(isset($date_arr[0])) $where['year'] =$date_arr[0];
- if(isset($date_arr[1])) $where['month'] =$date_arr[1];
- if(isset($date_arr[2])) $where['day'] =$date_arr[2];
- $list = UserVitality::where($where)->order('id desc')->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @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:手机号验证码
- * @param name:phone_pre type:int require:0 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()
- {
- $phone = input('post.phone');
- $code = input('post.code');
- if(empty($phone) || empty($code) ) $this->error('参数错误');
- $field = 'id,phone,name,headimg,bind_id,account_type';
- $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
- if($user_info['bind_id']) $this->error('已绑定其他会员');
- if($user_info['phone']) $this->error('已绑定手机号');
- if($user_info['account_type'] == 2) $this->error('只有企业用户可以更换手机号');
- //if($user_info['phone'] == $phone)$this->error('更换手机号与绑定手机号一致!');
- $check_code = $this->checkPhoneCode($phone,$code,input('post.phone_pre'));
- //if(!$check_code) $this->error('验证码错误');
- //判断手机号是否已注册
- $sel_where = [];
- $sel_where[] = ['phone','=',$phone];
- $sel_where[] = ['is_deleted','=',0];
- if(input('post.phone_pre')) $sel_where[] = ['phone_pre','=',input('post.phone_pre')];
- $member_id = Db::name('store_member')->where($sel_where)->value('id');
- if($member_id) {
- $check_bind = User::where(['bind_id'=>$member_id])->value('id');
- // var_dump($check_bind,$this->user_id,$this->account_id);
- if($check_bind) $this->error('该用户已绑定');
- }else{
- $reg_data = [];
- $reg_data['phone'] = $phone;
- $reg_data['account_type'] = 2;
- $reg_data['name'] = 'G'. substr_replace($phone,'****',3,4);
- $user_info = User::create($reg_data);
- $member_id = $user_info->id;
- }
- Db::name('store_member')->where('id',$this->account_id)->update(['bind_id'=>$member_id]);
- $this->updatePhoneCode($check_code);
- $this->success('绑定成功');
- }
- /**
- * @title 取消绑定
- * @desc 取消绑定
- * @author qc
- * @url /api/User_center/cancelBindPhone
- * @method POST
- * @header name:Authorization require:1 desc:Token
- */
- public function cancelBindPhone()
- {
- Db::name('store_member')->where('id',$this->account_id)->update(['bind_id'=>0]);
- $this->success('解绑成功');
- }
- /**
- * @title 添加用户学习记录
- * @desc 添加用户学习记录
- * @author qc
- * @url /api/User_center/study
- * @method POST
- * @param name:study_duration type:int require:1 default:-- desc:学习时长(单位:分钟)
- * @param name:video_intro_id type:int require:1 default:-- desc:视频系列id
- * @header name:Authorization require:1 desc:Token
- */
- public function study(){
- $uid = $this->user_id;
- $data = input();
- if(!isset($data['video_intro_id'])){
- $this->success('ok1');
- }
- $user = Db::name('store_member')->field('id,phone,email')->where('id',$uid)->find();
- // 设置时区为UTC,可以更改为其他时区
- date_default_timezone_set('UTC');
- // 获取今天开始的时间戳
- $todayStart = strtotime("today");
- // 获取今天结束的时间戳
- $todayEnd = strtotime("tomorrow") - 1;
- // 如果需要将时间戳转换为日期格式,可以使用date函数
- $todayStartDate = date("Y-m-d H:i:s", $todayStart);
- $todayEndDate = date("Y-m-d H:i:s", $todayEnd);
- if($user['phone']){
- $user_phone = $user['phone'];
- }else{
- $user_phone = $user['email'];
- }
- $arr = [
- 'user_id'=>$uid,
- 'user_phone' => $user_phone,
- 'study_duration' => $data['study_duration'],
- 'video_intro_id' => $data['video_intro_id'],
- ];
- $study = Db::name('user_study_log')->where('user_id',$uid)
- ->where('video_intro_id',$data['video_intro_id'])
- ->where('create_at','> time',$todayStartDate)
- ->where('create_at','< time',$todayEndDate)
- ->find();
- if($study){
- $arr['study_duration'] = $arr['study_duration'] + $study['study_duration'];
- Db::name('user_study_log')->where('id',$study['id'])->update($arr);
- }else{
- $arr['create_at'] = date('Y-m-d H:i:s');
- Db::name('user_study_log')->insert($arr);
- }
- $this->success('ok');
- }
- }
|