model = new \app\model\User(); } public $noNeedLogin = [ 'login', 'login_code', 'register' ]; // 用户名密码登录 public function login(){ global $_GPC; //获取表单提交数据 $post = $this->request->post(); //验证表单提交 if(empty($post['account'])){ return $this->error('登录失败,请填写账号'); } if(empty($post['password'])){ return $this->error('登录失败,请填写密码'); } $loginUserInfo = $this->model->where(['account' => $post['account'],'uniacid'=>$_GPC['uniacid'] ])->find(); if(!$loginUserInfo){ return $this->error('登录失败,此账号无注册信息,请注册后再登录'); } $passwordHash = $loginUserInfo->password; if (!Str::checkPassword($post['password'], $passwordHash)) { return $this->error('用户名或密码错误'); } $status = $loginUserInfo->status; if ($status == 2) { return $this->error('用户已被禁用,请联系管理员'); } $token = Random::uuid(); $loginUserInfo['token'] = $token; Token::set($token, $loginUserInfo['id'], 24 * 60 * 60 * 365); // // $post['password'] = '******';//登录成功不记录用户密码 // Log::create([ // 'login_status' => 1, // 'admin_id' => $loginUserInfo['id'], // 'request_body' => json_encode($post), // 'request_header' => json_encode($this->request->header()), // 'ip' => $this->request->ip(), // 'create_time' => date('Y-m-d H:i:s'), // 'uniacid' => $_GPC['uniacid'], // ]); return $this->success('登录成功', [ 'user'=>$loginUserInfo ]); } // 验证码登录 public function login_code(){ //获取表单提交数据 $post = $this->request->post(); if(!$post['phone'] || !$post['code']){ return $this->error('注册失败,请上传手机号或验证码!'); } $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']); if(!$check){ return $this->error('验证失败,'.AliSmsServiceFacade::getError()); } $loginUserInfo = $this->model->where('phone', '=', $post['phone'])->find(); if(!$loginUserInfo){ return $this->error('登录失败,此手机号无注册信息,请注册后再登录'); } $token = Random::uuid(); $res['token'] = $token; Token::set($token, $loginUserInfo['id'], 24 * 60 * 60 * 3); return $this->success('获取结果', $res); } // 绑定手机号 public function phone_binding(){ //获取表单提交数据 $post = $this->request->post(); if(!$post['phone'] || !$post['code']){ return $this->error('注册失败,请上传手机号或验证码!'); } $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']); if(!$check){ return $this->error('验证失败,'.AliSmsServiceFacade::getError()); } $loginUserInfo = $this->model->where('phone', '=', $post['phone'])->find(); // print_r($loginUserInfo); if($loginUserInfo){ return $this->error('绑定失败,此手机号已有注册信息'); } // $token = Random::uuid(); // $res['token'] = $token; // Token::set($token, $loginUserInfo['id'], 24 * 60 * 60 * 3); // return $this->success('获取结果', $res); } // 通过手机验证码修改密码 public function reset() { $post = $this->request->post(); if(!$post['phone'] || !$post['code'] || !$post['password']){ return $this->error('注册失败,请上传正确的手机号格式或验证码密码等信息!'); } $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']); if(!$check){ return $this->error('验证失败,'.AliSmsServiceFacade::getError()); } $loginUserInfo = $this->model->where('phone', '=', $post['phone'])->find(); if(!$loginUserInfo){ return $this->error('修改失败,此手机号无注册信息'); } $post['password'] = Str::createPassword($post['password']); $data = array('password' => $post['password']); $updateRes = $this->model->where('id', '=', $loginUserInfo['id'])->update($data); if (!$updateRes) throw new \Exception('保存基本信息失败'); return $this->success('获取结果', $loginUserInfo); } // 通过手机验证码修改密码 public function profile() { $post = $this->request->post(); if(!($post['nickname'] || $post['avatar'])){ return $this->error('修改失败,请上传要修改的头像或昵称等信息'); } if (!empty($post['password'])){ $post['password'] = Str::createPassword($post['password']); } $loginUserInfo = UserServiceFacade::getUserInfo(); $updateRes = $this->model->where('id', '=', $loginUserInfo['id'])->update($post); if (!$updateRes) throw new \Exception('保存基本信息失败'); if($post['nickname']){ $loginUserInfo['nickname'] = $post['nickname']; } if($post['avatar']){ $loginUserInfo['avatar'] = $post['avatar']; } return $this->success('获取结果', $loginUserInfo); } // 注册即登录 public function register() { global $_GPC; $conf = ConfServiceFacade::groupGet('system.plan', true); // $validate = new Login(); Db::startTrans(); try { $post = $this->request->post(); $post['uniacid'] = $_GPC['uniacid']; if(!$post['account']){ return $this->error('注册失败,请填写账号!'); } if(!$post['password']){ return $this->error('注册失败,请填写密码!'); } $post['password'] = Str::createPassword($post['password']); $loginUserInfo = $this->model->where('account', '=', $post['account'])->value('id'); if($loginUserInfo){ return $this->error('注册失败,此账号已被绑定!'); } $post['login_time'] = date('Y-m-d H:i:s'); $post['id_number'] = Random::numeric(10); $post['coin'] = !empty($conf['register'])?$conf['register']:0; $post['nickname'] = '默认用户'; $saveRes = $this->model->save($post); if (!$saveRes) throw new \Exception('保存基础信息失败'); Db::commit(); $token = Random::uuid(); $post['token'] = $token; Token::set($token, $this->model->id, 24 * 60 * 60 * 3); return $this->success('操作成功',$post); } catch (\Exception $e) { Db::rollback(); return $this->error('数据库异常,操作失败'); } } /*@formatter:off*/ /** * @ApiTitle (根据token获取用户信息) * @ApiSummary (根据token获取用户信息) * @ApiMethod (GET) * @ApiHeaders (name="token", type="string", required="true", description="用户登录后得到的Token") * @ApiReturnParams (name="code", type="integer", description="接口返回码.0=常规正确码,表示常规操作成功;1=常规错误码,客户端仅需提示msg;其他返回码与具体业务相关。框架实现了的唯一其他返回码:10401,前端需要跳转至登录界面。在一个复杂的交互过程中,你可能需要自行定义其他返回码") * @ApiReturnParams (name="msg", type="string", description="返回描述") * @ApiReturnParams (name="time", type="integer", description="请求时间,Unix时间戳,单位秒") * @ApiReturnParams (name="data.id", type="integer", description="用户主键ID") * @ApiReturnParams (name="data.phone", type="string", description="手机号") * @ApiReturnParams (name="data.username", type="string", description="用户名") * @ApiReturnParams (name="data.nickname", type="string", description="昵称") * @ApiReturnParams (name="data.avatar", type="string", description="头像") * @ApiReturnParams (name="data.token", type="string", description="用户登录凭证,Token") * @ApiReturn ({ "code": 0, "msg": "获取成功", "time": 1591149171, "data": { "id": 4, "mobile": "17603005414", "username": "", "nickname": "", "avatar": "http://local.laytp.com/static/index/image/default.png", "token": "d32e5210-050d-4902-b4b2-0173da12e191" } }) */ /*@formatter:on*/ public function info() { global $_GPC; $loginUserInfo = UserServiceFacade::getUserInfo(); $modelVipinfo =new \app\model\Vipinfo(); if($loginUserInfo['vip_time'] > time() || $loginUserInfo['vip_time']==9999){ $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find(); if(empty($vipinfo)){ $save =[ 'uid'=>$loginUserInfo['id'], 'gpt35_times'=>-1, 'uniacid'=>$_GPC['uniacid'] ]; $res = $modelVipinfo->save($save); } } return $this->success('获取成功', $loginUserInfo); } // 用户数据 public function userData() { global $_GPC; $modelVipinfo =new \app\model\Vipinfo(); $today = strtotime(date("Y-m-d"),time()); $loginUserInfo = UserServiceFacade::getUserInfo(); $data['coin'] = $loginUserInfo['coin']; $data['vip_time'] = 0; $data['countdown'] = 0; //倒计时多少天 $data['is_validity'] = 0;//有效期 $data['vip_info'] = ''; if(!empty($loginUserInfo['vip_time'])){ if($loginUserInfo['vip_time'] === 9999){ $data['is_validity'] = 1; $data['countdown'] = 9999; }else{ if($loginUserInfo['vip_time'] < time()){ $data['is_validity'] = 0; }else{ $data['is_validity'] = 1; $data['countdown'] = round(($loginUserInfo['vip_time']-time())/86400); } } $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find(); if(!empty($vipinfo)){ $data['vip_info'] = $vipinfo; if($vipinfo['gpt35_type'] == 'time'){ // 每天 $data['vip_info']['gpt35_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt35'],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); }else{ // 总数 $data['vip_info']['gpt35_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt35'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count(); } if($vipinfo['gpt4_type'] == 'time'){ // 每天 $data['vip_info']['gpt4_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt4'],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); }else{ // 总数 $data['vip_info']['gpt4_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt4'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count(); } if($vipinfo['sd_type'] == 'time'){ // 每天 $data['vip_info']['sd_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['is_time','=','1'],['type','=','sd'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); }else{ // 总数 $data['vip_info']['sd_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','sd'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count(); } if($vipinfo['mj_type'] == 'time'){ // 每天 $data['vip_info']['mj_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['is_time','=','1'],['type','=','mj'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); }else{ // 总数 $data['vip_info']['mj_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','mj'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count(); } } } $data['vip_time'] = !empty($loginUserInfo['vip_time'])?$loginUserInfo['vip_time']:0; $data['share_count_today'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','share'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); $data['video_count_today'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','video'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); $data['sign_count_today'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','sign'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count(); return $this->success('获取成功', $data); } /*@formatter:off*/ /** * @ApiTitle (注销登录) * @ApiSummary (注销登录信息) * @ApiMethod (GET) * @ApiRoute (/api.user/logout) * @ApiHeaders (name="token", type="string", required="true", description="用户登录后得到的Token") * @ApiReturnParams (name="code", type="integer", description="接口返回码.0=常规正确码,表示常规操作成功;1=常规错误码,客户端仅需提示msg;其他返回码与具体业务相关。框架实现了的唯一其他返回码:10401,前端需要跳转至登录界面。在一个复杂的交互过程中,你可能需要自行定义其他返回码") * @ApiReturnParams (name="msg", type="string", description="返回描述") * @ApiReturnParams (name="time", type="integer", description="请求时间,Unix时间戳,单位秒") * @ApiReturnParams (name="data", type="null", description="只会返回null") * @ApiReturn ({ "code": 0, "msg": "注销成功", "time": 1584513627, "data": null }) */ /*@formatter:on*/ public function logout() { if (UserServiceFacade::logout()) { return $this->success('注销成功'); } else { return $this->error(UserServiceFacade::getError()); } } }