123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- namespace app\controller\api;
- use app\service\api\UserServiceFacade;
- use app\validate\api\user\Login;
- use app\model\api\Log;
- use app\service\ConfServiceFacade;
- use plugin\ali_sms\service\AliSmsServiceFacade;
- use laytp\controller\Api;
- use laytp\library\Random;
- use laytp\library\Token;
- use laytp\library\Str;
- use think\facade\Db;
- /**
- * 会员相关
- * @ApiWeigh (90)
- */
- class User extends Api
- {
- protected function _initialize()
- {
- $this->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());
- }
- }
- }
|