Browse Source

医生用户接口

lizhen_gitee 10 months ago
parent
commit
dea5fd4921

+ 300 - 41
application/api/controller/doctor/User.php

@@ -8,65 +8,208 @@ use think\Exception;
 use think\Validate;
 
 use think\Db;
+use app\common\library\Wechat;
 
 /**
  * 会员接口
  */
 class User extends Apic
 {
-    protected $noNeedLogin = ['login','resetpwd'];
+    protected $noNeedLogin = ['mobilelogin','wechatlogin','bindmobile'];
     protected $noNeedRight = '*';
 
-
-    //员工手机+密码登录
-    public function login()
+    /**
+     * 手机验证码登录
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile  手机号
+     * @param string $captcha 验证码
+     */
+    public function mobilelogin()
     {
         $mobile = input('mobile');
-        $password = input('password');
-        if (!$mobile || !$password) {
+        $captcha = input('captcha');
+        if (!$mobile || !$captcha) {
             $this->error(__('Invalid parameters'));
         }
-        $ret = $this->auth->login($mobile, $password);
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
+            $this->error(__('Captcha is incorrect'));
+        }
+        $user = \app\common\model\Doctor::getByMobile($mobile);
+        if ($user) {
+            if ($user->status == -1) {
+                $this->error('账号已注销');
+            }
+            if ($user->status != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user->id);
+        } else {
+            $ret = $this->auth->register('', '', '', $mobile, []);
+        }
         if ($ret) {
-            $data = $this->auth->getUserinfo();
-            $this->success(__('Logged in successful'), $data);
+            Sms::flush($mobile, 'mobilelogin');
+            $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
         } else {
             $this->error($this->auth->getError());
         }
     }
 
+    //微信登录,预先假注册
+    public function wechatlogin(){
+        $code = input('code','');
+        if(!$code){
+            $this->error();
+        }
+        //微信
+        $wechat = new Wechat();
+        $wxuserinfo = $wechat->getAccessToken($code);
+
+        if(!$wxuserinfo){
+            $this->error('openid获取失败');
+        }
+        if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
+            $this->error('openid获取失败');
+        }
+
+        $openid = $wxuserinfo['openid'];
+
+        //检查用户
+        $user = Db::name('doctor')->where('wechat_openid',$openid)->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已注销');
+            }
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user['id']);
+
+            if ($ret) {
+                $userInfo = $this->auth->getUserinfo_simple();
+                $userInfo['is_register'] = 0;
+                $userInfo['code'] = $code;
+                $this->success(__('Logged in successful'), $userInfo);
+            } else {
+                $this->error($this->auth->getError());
+            }
+
+        } else {
+            //记录code和openid,绑定手机号的时候更新openid
+            $wechatCodeData = [
+                'code' => $code,
+                'openid' => $openid,
+                'createtime' => time(),
+            ];
+            $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
+            if (empty($wechatCode)) {
+                Db::name('wechat_code')->insertGetId($wechatCodeData);
+            } else {
+                Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
+            }
+
+            //直接返回
+            $userInfo = [];
+            $userInfo['is_register'] = 1;
+            $userInfo['code'] = $code;
+            $this->success('获取信息成功', $userInfo);
+        }
+
+    }
+
     /**
-     * 修改密码
+     * 微信注册来的,绑定手机号
      *
      * @ApiMethod (POST)
-     * @param string $newpassword 新密码
-     * @param string $oldpassword 旧密码
+     * @param string $mobile   手机号
+     * @param string $captcha 验证
      */
-    public function changepwd(){
-        $newpassword = input('newpassword');
-        $oldpassword = input('oldpassword','');
+    public function bindmobile()
+    {
+        $mobile  = input('mobile');
+        $captcha = input('captcha');
+        $code    = input('code');
 
-        if (!$newpassword) {
-            $this->error('请输入新密码');
+        if (!$mobile || !$captcha || !$code) {
+            $this->error(__('Invalid parameters'));
         }
-        if($this->auth->password && empty($oldpassword)){
-            $this->error('旧密码必填');
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
+            $this->error(__('Captcha is incorrect'));
         }
 
-        if(empty($this->auth->password)){
-            $ret = $this->auth->changepwd($newpassword, '', true);
-        }else{
-            $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
+        $wechatCodeWhere['code'] = $code;
+        $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
+        if (empty($wechatCode)) {
+            $this->error('请先微信登录');
         }
 
-        if ($ret) {
-            $this->success(__('Reset password successful'));
+        //检查appid绑定的用户
+        $user = Db::name('doctor')->where('wechat_openid',$wechatCode['openid'])->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已注销');
+            }
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user['id']);
+            $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
+        }
+
+        //新的openid用户
+        $where = [];
+        $where['mobile'] = $mobile;
+        $userData = Db::name('doctor')->where($where)->find();//老用户
+        if (!empty($userData)) {
+            if ($userData['status'] == -1) {
+                $this->error('账户已注销');
+            }
+            if ($userData['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+
+            if (empty($userData['wechat_openid'])) {
+                Db::name('doctor')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
+            } else {
+                if ($userData['wechat_openid'] != $wechatCode['openid']) {
+                    $this->error('该手机号已被其他用户绑定');
+                }
+            }
+            $ret = $this->auth->direct($userData['id']);
         } else {
+            $extend = [
+                'wechat_openid' => $wechatCode['openid'],
+            ];
+            $ret = $this->auth->register('', '','', $mobile, $extend);
+        }
+        if (!$ret) {
             $this->error($this->auth->getError());
         }
+
+        $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
+
+    }
+
+
+    //用户详细资料
+    public function userInfo(){
+        $info = $this->auth->getUserinfo();
+
+        $this->success(__('success'),$info);
     }
 
 
+
+
     /**
      * 退出登录
      * @ApiMethod (POST)
@@ -82,11 +225,9 @@ class User extends Apic
 
 
     //用户详细资料
-    public function getUserinfo($type = 1){
+    public function getUserinfo(){
         $info = $this->auth->getUserinfo();
-        if($type == 'return'){
-            return $info;
-        }
+
         $this->success(__('success'),$info);
     }
 
@@ -145,29 +286,147 @@ class User extends Apic
      */
     public function profile()
     {
-        $field = [
-            'mobile',
-            'avatar',
-            'firstname',
-            'lastname',
-            'lang',
+        $field_array = [
+            'realname',
+            'idcard',
+            'english_status',
+            'idcard_z_image',
+            'idcard_f_image',
+            'doctor_image',
+
+            'avatar','nickname','gender',
+            'keshi_id','hospital','goodat','level_id','info'
         ];
 
-        $data = request_post_hub($field);
+        $data = [];
+        foreach($field_array as $key => $field){
 
+            //前端传不了post,改了
+            /*if(!request()->has($field,'post')){
+                continue;
+            }*/
+            if(!input('?'.$field)){
+                continue;
+            }
 
+            $newone = input($field);
 
-        if(isset($data['mobile'])){
-            $check_mobile = Db::name('doctor')->where('mobile',$data['mobile'])->where('id','neq',$this->auth->id)->find();
-            if($check_mobile){
-                $this->error('手机号已被其他人使用');
+            if($field == 'avatar'){
+                $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
             }
+
+            $data[$field] = $newone;
         }
 
+        //
+        /*if(isset($data['birthday'])){
+            $data['birthday'] = strtotime($data['birthday']);
+        }*/
+
+        if(empty($data)){
+            $this->success();
+        }
         $update_rs = Db::name('doctor')->where('id',$this->auth->id)->update($data);
 
-        $this->success('资料更新完成');
+        $this->success();
+    }
+    //问诊设置
+    public function profile_wenzhen()
+    {
+        $field_array = [
+            'typing_switch',
+            'video_switch',
+            'typing_price',
+            'video_price',
+            'notice_switch',
+        ];
+
+        $data = [];
+        foreach($field_array as $key => $field){
+
+            if(!input('?'.$field)){
+                continue;
+            }
+
+            $newone = input($field);
+
+            $data[$field] = $newone;
+        }
+        if(empty($data)){
+            $this->success();
+        }
+        $update_rs = Db::name('doctor_info')->where('id',$this->auth->id)->update($data);
+
+        $this->success();
     }
 
+    //假注销
+    public function cancleUser(){
+        /*$captcha = input('captcha','');
 
+        if (!$captcha) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
+            $this->error(__('Captcha is incorrect'));
+        }*/
+
+        Db::name('doctor')->where('id',$this->auth->id)->update(['status'=>-1]);
+
+        $this->auth->logout();
+        $this->success('注销成功');
+    }
+
+//////////////////////////////////////////////////////
+
+
+    //员工手机+密码登录
+    public function login()
+    {
+        $mobile = input('mobile');
+        $password = input('password');
+        if (!$mobile || !$password) {
+            $this->error(__('Invalid parameters'));
+        }
+        $ret = $this->auth->login($mobile, $password);
+        if ($ret) {
+            $data = $this->auth->getUserinfo();
+            $this->success(__('Logged in successful'), $data);
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
+
+
+    /**
+     * 修改密码
+     *
+     * @ApiMethod (POST)
+     * @param string $newpassword 新密码
+     * @param string $oldpassword 旧密码
+     */
+    public function changepwd(){
+        $newpassword = input('newpassword');
+        $oldpassword = input('oldpassword','');
+
+        if (!$newpassword) {
+            $this->error('请输入新密码');
+        }
+        if($this->auth->password && empty($oldpassword)){
+            $this->error('旧密码必填');
+        }
+
+        if(empty($this->auth->password)){
+            $ret = $this->auth->changepwd($newpassword, '', true);
+        }else{
+            $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
+        }
+
+        if ($ret) {
+            $this->success(__('Reset password successful'));
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
 }

+ 149 - 10
application/common/library/Authdoctor.php

@@ -25,7 +25,21 @@ class Authdoctor
     //默认配置
     protected $config = [];
     protected $options = [];
-    protected $allowFields = ['id', 'username','nickname', 'mobile', 'avatar'];
+    protected $allowFields = [
+        'id', 'ruletype', 'nickname', 'avatar', 'gender','mobile',
+        'realname',
+        'idcard',
+        'idcard_z_image',
+        'idcard_f_image',
+        'idcard_status',
+        'doctor_image',
+        'doctor_status',
+        'english_status',
+        'keshi_id',
+        'hospital',
+        'goodat',
+        'level_id',
+    ];
 
 
     public function __construct($options = [])
@@ -136,6 +150,118 @@ class Authdoctor
         }
     }
 
+    /**
+     * 注册用户
+     *
+     * @param string $username 用户名
+     * @param string $password 密码
+     * @param string $email    邮箱
+     * @param string $mobile   手机号
+     * @param array  $extend   扩展参数
+     * @return boolean
+     */
+    public function register($username, $password, $email = '', $mobile = '', $extend = [])
+    {
+        // 检测用户名、昵称、邮箱、手机号是否存在
+        /*if (User::getByUsername($username)) {
+            $this->setError('Username already exist');
+            return false;
+        }
+        if (User::getByNickname($username)) {
+            $this->setError('Nickname already exist');
+            return false;
+        }
+        if ($email && User::getByEmail($email)) {
+            $this->setError('Email already exist');
+            return false;
+        }*/
+
+        if(empty($mobile)){
+            $this->setError('手机号必填');
+            return false;
+        }
+
+        if ($mobile && Doctor::getByMobile($mobile)) {
+            $this->setError('Mobile already exist');
+            return false;
+        }
+
+        $ip = request()->ip();
+        $time = time();
+
+        $data = [
+//            'username' => $username,
+//            'password' => $password,
+//            'email'    => $email,
+            'mobile'   => $mobile,
+//            'level'    => 1,
+//            'score'    => 0,
+            'avatar'   => '',
+        ];
+        $params = array_merge($data, [
+//            'nickname'  => preg_match("/^1[3-9]{1}\d{9}$/", $username) ? substr_replace($username, '****', 3, 4) : $username,
+            'nickname'  => get_rand_nick_name(),
+//            'salt'      => Random::alnum(),
+//            'jointime'  => $time,
+//            'joinip'    => $ip,
+//            'logintime' => $time,
+//            'loginip'   => $ip,
+//            'prevtime'  => $time,
+            'status'    => 1
+        ]);
+//        $params['password'] = $this->getEncryptPassword($password, $params['salt']);
+        $params = array_merge($params, $extend);
+
+        //账号注册时需要开启事务,避免出现垃圾数据
+        Db::startTrans();
+        try {
+            $user = Doctor::create($params, true);
+
+            $this->_user = Doctor::get($user->id);
+            /*$this->_user->username = 'd' . (10000 + $user->id);
+            $this->_user->save();*/
+
+            //设置Token
+            $this->_token = Random::uuid();
+            Tokendoctor::set($this->_token, $user->id, $this->keeptime);
+
+            //设置登录状态
+            $this->_logined = true;
+
+            //注册钱包
+            $wallet_id = Db::name('doctor_wallet')->insertGetId(['doctor_id'=>$user->id]);
+            if(!$wallet_id){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }
+            //注册info
+            $info_id = Db::name('doctor_info')->insertGetId(['doctor_id'=>$user->id]);
+            if(!$info_id){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }
+
+            //[环信]注册用户。忽略失败
+            $easemob = new Easemob();
+            $rs = $easemob->user_create('doctor'.$user->id);
+            if($rs === false){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }
+
+            //注册成功的事件
+            Db::commit();
+        } catch (Exception $e) {
+            $this->setError($e->getMessage());
+            Db::rollback();
+            return false;
+        }
+        return true;
+    }
+
 
     /**
      * 用户登录
@@ -144,7 +270,7 @@ class Authdoctor
      * @param string $password 密码
      * @return boolean
      */
-    public function login($account, $password)
+    /*public function login($account, $password)
     {
         $field = 'mobile';
         $user = Doctor::get([$field => $account]);
@@ -164,7 +290,7 @@ class Authdoctor
 
         //直接登录员工
         return $this->direct($user->id);
-    }
+    }*/
 
     /**
      * 退出
@@ -239,19 +365,19 @@ class Authdoctor
                 $time = time();
 
                 //判断连续登录和最大连续登录
-                if ($user->logintime < \fast\Date::unixtime('day')) {
+                /*if ($user->logintime < \fast\Date::unixtime('day')) {
                     $user->successions = $user->logintime < \fast\Date::unixtime('day', -1) ? 1 : $user->successions + 1;
                     $user->maxsuccessions = max($user->successions, $user->maxsuccessions);
-                }
+                }*/
 
-                $user->prevtime = $user->logintime;
+//                $user->prevtime = $user->logintime;
                 //记录本次登录的IP和时间
-                $user->loginip = $ip;
-                $user->logintime = $time;
+//                $user->loginip = $ip;
+//                $user->logintime = $time;
                 //重置登录失败次数
-                $user->loginfailure = 0;
+//                $user->loginfailure = 0;
 
-                $user->save();
+//                $user->save();
 
                 $this->_user = $user;
 
@@ -297,6 +423,12 @@ class Authdoctor
         return $this->_token;
     }
 
+    public function getUserinfo_simple(){
+        $userinfo = Tokendoctor::get($this->_token);
+
+        return $userinfo;
+    }
+
     /**
      * 获取会员基本信息
      */
@@ -309,6 +441,13 @@ class Authdoctor
 
         //追加
         $userinfo['avatar'] = one_domain_image($userinfo['avatar']);
+        $userinfo['level_name'] = Db::name('keshi')->where('id',$userinfo['keshi_id'])->value('name');
+        $userinfo['keshi_name'] = Db::name('doctor_level')->where('id',$userinfo['level_id'])->value('name');
+
+        //info
+        $userinfo['doctor_info'] = Db::name('doctor_info')->where('doctor_id',$this->id)->find();
+
+        $userinfo['wallet'] = Db::name('doctor_wallet')->where('doctor_id',$this->id)->find();
 
         return $userinfo;
     }

+ 91 - 4
application/common/model/Doctor.php

@@ -14,12 +14,99 @@ class Doctor extends Model
     // 表名
     protected $name = 'doctor';
     // 开启自动写入时间戳字段
-    protected $autoWriteTimestamp = 'int';
+    protected $autoWriteTimestamp = false;
     // 定义时间戳字段名
-    protected $createTime = 'createtime';
-    protected $updateTime = 'updatetime';
+    protected $createTime = false;
+    protected $updateTime = false;
     // 追加属性
-    protected $append = [];
+    protected $append = [
+        'gender_text',
+        'idcard_status_text',
+        'doctor_status_text',
+        'english_status_text',
+        'status_text'
+    ];
+
+
+
+    public function getGenderList()
+    {
+        return ['1' => __('Gender 1'), '0' => __('Gender 0')];
+    }
+
+    public function getIdcardStatusList()
+    {
+        return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2'), '3' => __('Idcard_status 3')];
+    }
+
+    public function getDoctorStatusList()
+    {
+        return ['-1' => __('Doctor_status -1'), '0' => __('Doctor_status 0'), '1' => __('Doctor_status 1'), '2' => __('Doctor_status 2'), '3' => __('Doctor_status 3')];
+    }
+
+    public function getEnglishStatusList()
+    {
+        return ['1' => __('English_status 1'), '0' => __('English_status 0')];
+    }
+
+    public function getStatusList()
+    {
+        return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
+    }
+
+
+    public function getGenderTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
+        $list = $this->getGenderList();
+        return isset($list[$value]) ? $list[$value] : '';
+    }
+
+
+    public function getIdcardStatusTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
+        $list = $this->getIdcardStatusList();
+        return isset($list[$value]) ? $list[$value] : '';
+    }
+
+
+    public function getDoctorStatusTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['doctor_status']) ? $data['doctor_status'] : '');
+        $list = $this->getDoctorStatusList();
+        return isset($list[$value]) ? $list[$value] : '';
+    }
+
+
+    public function getEnglishStatusTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['english_status']) ? $data['english_status'] : '');
+        $list = $this->getEnglishStatusList();
+        return isset($list[$value]) ? $list[$value] : '';
+    }
+
+
+    public function getStatusTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
+        $list = $this->getStatusList();
+        return isset($list[$value]) ? $list[$value] : '';
+    }
+
+
+
+
+    public function keshi()
+    {
+        return $this->belongsTo('Keshi', 'keshi_id', 'id', [], 'LEFT')->setEagerlyType(0);
+    }
+
+
+    public function doctorlevel()
+    {
+        return $this->belongsTo('doctorlevel', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
+    }
 
 
 }