Parcourir la source

师傅端基础,用户完全分离

lizhen_gitee il y a 8 mois
Parent
commit
6e8c58644a

+ 435 - 0
application/api/controller/worker/User.php

@@ -0,0 +1,435 @@
+<?php
+
+namespace app\api\controller\worker;
+
+use app\common\controller\Apiw;
+use app\common\library\Sms;
+use think\Exception;
+use think\Validate;
+
+use think\Db;
+use app\common\library\Wechat;
+
+/**
+ * 会员接口
+ */
+class User extends Apiw
+{
+    protected $noNeedLogin = ['mobilelogin','wechatlogin','bindmobile'];
+    protected $noNeedRight = '*';
+
+    /**
+     * 手机验证码登录
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile  手机号
+     * @param string $captcha 验证码
+     */
+    public function mobilelogin()
+    {
+        $mobile = input('mobile');
+        $captcha = input('captcha');
+        if (!$mobile || !$captcha) {
+            $this->error(__('Invalid parameters'));
+        }
+        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\Worker::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) {
+            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('worker')->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 $mobile   手机号
+     * @param string $captcha 验证码
+     */
+    public function bindmobile()
+    {
+        $mobile  = input('mobile');
+        $captcha = input('captcha');
+        $code    = input('code');
+
+        if (!$mobile || !$captcha || !$code) {
+            $this->error(__('Invalid parameters'));
+        }
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
+            $this->error(__('Captcha is incorrect'));
+        }
+
+        $wechatCodeWhere['code'] = $code;
+        $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
+        if (empty($wechatCode)) {
+            $this->error('请先微信登录');
+        }
+
+        //检查appid绑定的用户
+        $user = Db::name('worker')->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('worker')->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('worker')->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)
+     */
+    public function logout()
+    {
+        if (!$this->request->isPost()) {
+            $this->error(__('Invalid parameters'));
+        }
+        $this->auth->logout();
+        $this->success(__('Logout successful'));
+    }
+
+
+
+    /**
+     * 重置密码
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile      手机号
+     * @param string $captcha     验证码
+     * @param string $newpassword 新密码
+     */
+    /*public function resetpwd()
+    {
+        $mobile      = $this->request->post('mobile');
+        $captcha     = $this->request->post('captcha');
+        $newpassword = $this->request->post("newpassword");
+        if (!$mobile || !$captcha || !$newpassword) {
+            $this->error(__('Invalid parameters'));
+        }
+        //验证Token
+        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
+            $this->error(__('Password must be 6 to 30 characters'));
+        }
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        $user = \app\common\model\CompanyStaff::getByMobile($mobile);
+        if (!$user) {
+            $this->error(__('User not found'));
+        }
+        $ret = Sms::check($mobile, $captcha, 'resetpwd');
+        if (!$ret) {
+            $this->error(__('Captcha is incorrect'));
+        }
+        Sms::flush($mobile, 'resetpwd');
+
+        //模拟一次登录
+        $this->auth->direct($user->id);
+        $ret = $this->auth->resetpwd($newpassword, '', true);
+        if ($ret) {
+            $this->success(__('Reset password successful'));
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }*/
+
+  
+    /**
+     * 修改会员个人信息
+     *
+     * @ApiMethod (POST)
+     * @param string $avatar   头像地址
+     * @param string $username 用户名
+     * @param string $nickname 昵称
+     * @param string $bio      个人简介
+     */
+    public function profile()
+    {
+        $field_array = [
+            'realname',
+            'idcard',
+            'english_status',
+            'idcard_z_image',
+            'idcard_f_image',
+            'doctor_image',
+
+            'avatar','nickname','gender',
+            'keshi_id','hospital','goodat','level_id','info','job_status'
+        ];
+
+        $data = [];
+        foreach($field_array as $key => $field){
+
+            //前端传不了post,改了
+            /*if(!request()->has($field,'post')){
+                continue;
+            }*/
+            if(!input('?'.$field)){
+                continue;
+            }
+
+            $newone = input($field);
+
+            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();
+        }
+
+        if(isset($data['realname']) && isset($data['idcard']) && isset($data['idcard_z_image']) && isset($data['idcard_f_image'])){
+            $data['idcard_status'] = 0;
+        }
+        if(isset($data['doctor_image'])){
+            $data['doctor_status'] = 0;
+        }
+
+        $update_rs = Db::name('worker')->where('id',$this->auth->id)->update($data);
+
+        $this->success();
+    }
+    //问诊设置
+    public function profile_wenzhen()
+    {
+        $field_array = [
+            'typing_switch',
+            'video_switch',
+            'typing_price',
+            'video_price',
+            'notice_switch',
+            'jolt_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('doctor_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('worker')->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());
+        }
+    }
+}

+ 3 - 0
application/api/lang/worker/common.php

@@ -0,0 +1,3 @@
+<?php
+
+return [];

+ 39 - 0
application/api/lang/worker/user.php

@@ -0,0 +1,39 @@
+<?php
+
+return [
+    'User center'                           => '会员中心',
+    'Register'                              => '注册',
+    'Login'                                 => '登录',
+    'Sign up successful'                    => '注册成功',
+    'Username can not be empty'             => '用户名不能为空',
+    'Username must be 3 to 30 characters'   => '用户名必须3-30个字符',
+    'Username must be 6 to 30 characters'   => '用户名必须6-30个字符',
+    'Password can not be empty'             => '密码不能为空',
+    'Password must be 6 to 30 characters'   => '密码必须6-30个字符',
+    'Mobile is incorrect'                   => '手机格式不正确',
+    'Username already exist'                => '用户名已经存在',
+    'Nickname already exist'                => '昵称已经存在',
+    'Email already exist'                   => '邮箱已经存在',
+    'Mobile already exist'                  => '手机号已经存在',
+    'Username is incorrect'                 => '用户名不正确',
+    'Email is incorrect'                    => '邮箱不正确',
+    'Account is locked'                     => '账户已经被锁定',
+    'Password is incorrect'                 => '密码不正确',
+    'Account is incorrect'                  => '账户不正确',
+    'Account not exist'                     => '账户不存在',
+    'Account can not be empty'              => '账户不能为空',
+    'Username or password is incorrect'     => '用户名或密码不正确',
+    'You are not logged in'                 => '你当前还未登录',
+    'You\'ve logged in, do not login again' => '你已经存在,请不要重复登录',
+    'Profile'                               => '个人资料',
+    'Verify email'                          => '邮箱验证',
+    'Change password'                       => '修改密码',
+    'Captcha is incorrect'                  => '验证码不正确',
+    'Logged in successful'                  => '登录成功',
+    'Logout successful'                     => '退出成功',
+    'Operation failed'                      => '操作失败',
+    'Invalid parameters'                    => '参数不正确',
+    'Change password failure'               => '修改密码失败',
+    'Change password successful'            => '修改密码成功',
+    'Reset password successful'             => '重置密码成功',
+];

+ 472 - 0
application/common/controller/Apiw.php

@@ -0,0 +1,472 @@
+<?php
+
+namespace app\common\controller;
+
+use app\common\library\Authworker as Auth;
+use think\Config;
+use think\exception\HttpResponseException;
+use think\exception\ValidateException;
+use think\Hook;
+use think\Lang;
+use think\Loader;
+use think\Request;
+use think\Response;
+use think\Route;
+use think\Validate;
+use Redis;
+
+/**
+ * API控制器基类
+ */
+class Apiw
+{
+
+    /**
+     * @var Request Request 实例
+     */
+    protected $request;
+
+    /**
+     * @var bool 验证失败是否抛出异常
+     */
+    protected $failException = false;
+
+    /**
+     * @var bool 是否批量验证
+     */
+    protected $batchValidate = false;
+
+    /**
+     * @var array 前置操作方法列表
+     */
+    protected $beforeActionList = [];
+
+    /**
+     * 无需登录的方法,同时也就不需要鉴权了
+     * @var array
+     */
+    protected $noNeedLogin = [];
+
+    /**
+     * 无需鉴权的方法,但需要登录
+     * @var array
+     */
+    protected $noNeedRight = [];
+
+    /**
+     * 权限Auth
+     * @var Auth
+     */
+    protected $auth = null;
+
+    /**
+     * 默认响应输出类型,支持json/xml
+     * @var string
+     */
+    protected $responseType = 'json';
+
+    /**
+     * @var int 日志类型 1 文件;2sql
+     */
+    public $logType = 2;
+
+    /**
+     * 构造方法
+     * @access public
+     * @param Request $request Request 对象
+     */
+    public function __construct(Request $request = null)
+    {
+        $this->request = is_null($request) ? Request::instance() : $request;
+
+        if(config('site.apisite_switch') == 0){
+            $controllername = $this->request->controller();
+            $controllername = strtolower($controllername);
+
+            if(!in_array($controllername,['notify','easemob','payios'])){
+                $notice = config('site.apisite_notice') ?: '全站维护中';
+                $this->error($notice);
+            }
+        }
+
+
+        // 控制器初始化
+        $this->_initialize();
+        //日志
+        $this->request_log();
+
+        // 前置操作方法
+        if ($this->beforeActionList) {
+            foreach ($this->beforeActionList as $method => $options) {
+                is_numeric($method) ?
+                    $this->beforeAction($options) :
+                    $this->beforeAction($method, $options);
+            }
+        }
+    }
+
+    /**
+     * 初始化操作
+     * @access protected
+     */
+    protected function _initialize()
+    {
+        //跨域请求检测
+        check_cors_request();
+
+        // 检测IP是否允许
+        check_ip_allowed();
+
+        //移除HTML标签
+        $this->request->filter('trim,strip_tags,htmlspecialchars');
+
+        $this->auth = Auth::instance();
+
+        $modulename = $this->request->module();
+        $controllername = Loader::parseName($this->request->controller());
+        $actionname = strtolower($this->request->action());
+
+        // token
+        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
+
+        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
+        // 设置当前请求的URI
+        $this->auth->setRequestUri($path);
+        // 检测是否需要验证登录
+        if (!$this->auth->match($this->noNeedLogin)) {
+            //初始化
+            $this->auth->init($token);
+            //检测是否登录
+            if (!$this->auth->isLogin()) {
+                $this->error(__('Please login first'), null, 401);
+            }
+            // 判断是否需要验证权限
+            /*if (!$this->auth->match($this->noNeedRight)) {
+                // 判断控制器和方法判断是否有对应权限
+                if (!$this->auth->check($path)) {
+                    $this->error(__('You have no permission'), null, 403);
+                }
+            }*/
+        } else {
+            // 如果有传递token才验证是否登录状态
+            if ($token) {
+                $this->auth->init($token);
+                //传就必须传对
+                if (!$this->auth->isLogin()) {
+                    $this->error(__('Please login first'), null, 401);
+                }
+            }
+        }
+
+        $upload = \app\common\model\Config::upload();
+
+        // 上传信息配置后
+        Hook::listen("upload_config_init", $upload);
+
+        Config::set('upload', array_merge(Config::get('upload'), $upload));
+
+        // 加载当前控制器语言包
+        $this->loadlang($controllername);
+    }
+
+    /**
+     * 加载语言文件
+     * @param string $name
+     */
+    protected function loadlang($name)
+    {
+        $name = Loader::parseName($name);
+        $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
+        $lang = $this->request->langset();
+        $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
+        Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
+    }
+
+
+    /**
+     * 操作成功返回的数据
+     * @param string $msg    提示信息
+     * @param mixed  $data   要返回的数据
+     * @param int    $code   错误码,默认为1
+     * @param string $type   输出类型
+     * @param array  $header 发送的 Header 信息
+     */
+    protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
+    {
+        if($msg == 1){
+            $msg = 'success';
+        }
+        if(empty($msg)){
+            $msg = '操作成功';
+        }
+        $this->result($msg, $data, $code, $type, $header);
+    }
+
+    /**
+     * 操作失败返回的数据
+     * @param string $msg    提示信息
+     * @param mixed  $data   要返回的数据
+     * @param int    $code   错误码,默认为0
+     * @param string $type   输出类型
+     * @param array  $header 发送的 Header 信息
+     */
+    protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
+    {
+        if(empty($msg)){
+            $msg = __('Invalid parameters');
+        }
+        $this->result($msg, $data, $code, $type, $header);
+    }
+
+    /**
+     * 返回封装后的 API 数据到客户端
+     * @access protected
+     * @param mixed  $msg    提示信息
+     * @param mixed  $data   要返回的数据
+     * @param int    $code   错误码,默认为0
+     * @param string $type   输出类型,支持json/xml/jsonp
+     * @param array  $header 发送的 Header 信息
+     * @return void
+     * @throws HttpResponseException
+     */
+    protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
+    {
+        $result = [
+            'code' => $code,
+            'msg'  => __($msg),
+            'time' => Request::instance()->server('REQUEST_TIME'),
+            'data' => $data,
+        ];
+
+        //日志
+        $this->request_log_update($result);
+
+        // 如果未设置类型则使用默认类型判断
+        $type = $type ? : $this->responseType;
+
+        if (isset($header['statuscode'])) {
+            $code = $header['statuscode'];
+            unset($header['statuscode']);
+        } else {
+            //未设置状态码,根据code值判断
+            $code = $code >= 1000 || $code < 200 ? 200 : $code;
+        }
+        $response = Response::create($result, $type, $code)->header($header);
+        throw new HttpResponseException($response);
+    }
+
+    /**
+     * 前置操作
+     * @access protected
+     * @param string $method  前置操作方法名
+     * @param array  $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
+     * @return void
+     */
+    protected function beforeAction($method, $options = [])
+    {
+        if (isset($options['only'])) {
+            if (is_string($options['only'])) {
+                $options['only'] = explode(',', $options['only']);
+            }
+
+            if (!in_array($this->request->action(), $options['only'])) {
+                return;
+            }
+        } elseif (isset($options['except'])) {
+            if (is_string($options['except'])) {
+                $options['except'] = explode(',', $options['except']);
+            }
+
+            if (in_array($this->request->action(), $options['except'])) {
+                return;
+            }
+        }
+
+        call_user_func([$this, $method]);
+    }
+
+    /**
+     * 设置验证失败后是否抛出异常
+     * @access protected
+     * @param bool $fail 是否抛出异常
+     * @return $this
+     */
+    protected function validateFailException($fail = true)
+    {
+        $this->failException = $fail;
+
+        return $this;
+    }
+
+    /**
+     * 验证数据
+     * @access protected
+     * @param array        $data     数据
+     * @param string|array $validate 验证器名或者验证规则数组
+     * @param array        $message  提示信息
+     * @param bool         $batch    是否批量验证
+     * @param mixed        $callback 回调方法(闭包)
+     * @return array|string|true
+     * @throws ValidateException
+     */
+    protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
+    {
+        if (is_array($validate)) {
+            $v = Loader::validate();
+            $v->rule($validate);
+        } else {
+            // 支持场景
+            if (strpos($validate, '.')) {
+                list($validate, $scene) = explode('.', $validate);
+            }
+
+            $v = Loader::validate($validate);
+
+            !empty($scene) && $v->scene($scene);
+        }
+
+        // 批量验证
+        if ($batch || $this->batchValidate) {
+            $v->batch(true);
+        }
+        // 设置错误信息
+        if (is_array($message)) {
+            $v->message($message);
+        }
+        // 使用回调验证
+        if ($callback && is_callable($callback)) {
+            call_user_func_array($callback, [$v, &$data]);
+        }
+
+        if (!$v->check($data)) {
+            if ($this->failException) {
+                throw new ValidateException($v->getError());
+            }
+
+            return $v->getError();
+        }
+
+        return true;
+    }
+
+    /**
+     * 刷新Token
+     */
+    protected function token()
+    {
+        $token = $this->request->param('__token__');
+
+        //验证Token
+        if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
+            $this->error(__('Token verification error'), ['__token__' => $this->request->token()]);
+        }
+
+        //刷新Token
+        $this->request->token();
+    }
+
+    /*
+     * api 请求日志
+     * */
+    protected function request_log(){
+
+        //api_request_log
+        $modulename     = $this->request->module();
+        $controllername = $this->request->controller();
+        $actionname     = $this->request->action();
+
+        if(strtolower($actionname) == 'callback'){
+            return true;
+        }
+
+        defined('API_REQUEST_LOG_TYPE') or define('API_REQUEST_LOG_TYPE', $this->logType);
+        $params = input();
+        if ($this->logType === 1){
+            //日志统一写入
+            register_shutdown_function([new LogUtil, 'close']);
+            LogUtil::getInstance('Api/'); //设置日志存入通道
+
+            LogUtil::info('uid', 'Api-Middleware-Log', 'request_log', $this->auth->id);
+            LogUtil::info('api', 'Api-Middleware-Log', 'request_log', $modulename . '/' . $controllername . '/' . $actionname);
+            LogUtil::info('params', 'Api-Middleware-Log', 'request_log', json_encode($params));
+            LogUtil::info('ip', 'Api-Middleware-Log', 'request_log', request()->ip());
+
+        }else{
+            $data = [
+                'uid'   => $this->auth->id,
+                'api'   => $modulename.'/'.$controllername.'/'.$actionname,
+                'params' => json_encode($params),
+                'addtime'  => time(),
+                'adddatetime'  => date('Y-m-d H:i:s'),
+                'ip'   => request()->ip(),
+            ];
+            $request_id = db('api_request_log')->insertGetId($data);
+            defined('API_REQUEST_ID') or define('API_REQUEST_ID', $request_id);
+        }
+    }
+
+    protected function request_log_update($log_result){
+        $actionname     = $this->request->action();
+        if(strtolower($actionname) == 'givegifttoyou'){
+            //return true;
+        }
+
+        if ($this->logType === 1){
+            if (strlen(json_encode($log_result['data'])) > 1000) {
+                //$log_result['data'] = '数据太多,不记录';
+            }
+            LogUtil::info('result', 'Api-Middleware-Log', 'request_log', $log_result);
+        }else{
+            if(defined('API_REQUEST_ID')) { //记录app正常返回结果
+                if(strlen(json_encode($log_result['data'])) > 1000) {
+                    //$log_result['data'] = '数据太多,不记录';
+                }
+                db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
+            }
+        }
+    }
+
+    /**
+     * 接口请求限制
+     * @param int $apiLimit
+     * @param int $apiLimitTime
+     * @param string $key
+     * @return bool | true:通过 false:拒绝
+     */
+    public function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
+    {
+        $userId = $this->auth->id;
+        $controller = request()->controller();
+        $action = request()->action();
+
+        if (!$key) {
+            $key = strtolower($controller) . '_' . strtolower($action) . '_' . $userId;
+        }
+
+        $redis = new Redis();
+        $redisconfig = config("redis");
+        $redis->connect($redisconfig["host"], $redisconfig["port"]);
+        if ($redisconfig['redis_pwd']) {
+            $redis->auth($redisconfig['redis_pwd']);
+        }
+        if($redisconfig['redis_selectdb'] > 0){
+            $redis->select($redisconfig['redis_selectdb']);
+        }
+
+        //
+        //指定键值新增+1 并获取
+        $count = $redis->incr($key);
+        if ($count > $apiLimit) {
+            return false;
+        }
+
+        //设置过期时间
+        if ($count == 1) {
+            $redis->pExpire($key, $apiLimitTime);
+        }
+
+        return true;
+    }
+
+
+}

+ 642 - 0
application/common/library/Authworker.php

@@ -0,0 +1,642 @@
+<?php
+
+namespace app\common\library;
+
+use app\common\model\Worker;
+use fast\Random;
+use think\Config;
+use think\Db;
+use think\Exception;
+use think\Hook;
+use think\Request;
+use think\Validate;
+
+class Authworker
+{
+    protected static $instance = null;
+    protected $_error = '';
+    protected $_logined = false;
+    protected $_user = null;
+    protected $_token = '';
+    //Token默认有效时长
+    protected $keeptime = 2592000;
+    protected $requestUri = '';
+    protected $rules = [];
+    //默认配置
+    protected $config = [];
+    protected $options = [];
+    protected $allowFields = [
+        'id', 'ruletype', 'nickname', 'avatar', 'gender','mobile',
+        'realname',
+        'idcard',
+        'idcard_z_image',
+        'idcard_f_image',
+        'idcard_status',
+        'worker_image',
+        'worker_status',
+        'english_status',
+        'keshi_id',
+        'hospital',
+        'goodat',
+        'level_id',
+        'info',
+        'job_status'
+    ];
+
+
+    public function __construct($options = [])
+    {
+        if ($config = Config::get('worker')) {
+            $this->config = array_merge($this->config, $config);
+        }
+        $this->options = array_merge($this->config, $options);
+    }
+
+    /**
+     *
+     * @param array $options 参数
+     * @return Auth
+     */
+    public static function instance($options = [])
+    {
+        if (is_null(self::$instance)) {
+            self::$instance = new static($options);
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * 生成不重复的随机数字字母组合
+     */
+    function getUinqueNo($length = 8, $nos = [])
+    {
+        $newid = Random::build("alnum", $length);
+        if (in_array($newid, $nos)) {
+            $newid = $this->getUinqueNo($length, $nos);
+        }
+        return $newid;
+    }
+
+    /**
+     * 获取User模型
+     * @return User
+     */
+    public function getUser()
+    {
+        return $this->_user;
+    }
+
+    /**
+     * 兼容调用user模型的属性
+     *
+     * @param string $name
+     * @return mixed
+     */
+    public function __get($name)
+    {
+        return $this->_user ? $this->_user->$name : null;
+    }
+
+    /**
+     * 兼容调用user模型的属性
+     */
+    public function __isset($name)
+    {
+        return isset($this->_user) ? isset($this->_user->$name) : false;
+    }
+
+    /**
+     * 根据Token初始化
+     *
+     * @param string $token Token
+     * @return boolean
+     */
+    public function init($token)
+    {
+        if ($this->_logined) {
+            return true;
+        }
+        if ($this->_error) {
+            return false;
+        }
+        $data = Tokenworker::get($token);
+        if (!$data) {
+            return false;
+        }
+        $user_id = intval($data['user_id']);
+        if ($user_id > 0) {
+            $user = Worker::get($user_id);
+            if (!$user) {
+                $this->setError('Account not exist');
+                return false;
+            }
+            if ($user->status != 1) {
+                $this->setError('Account is locked');
+                return false;
+            }
+
+
+
+            $this->_user = $user;
+            $this->_logined = true;
+            $this->_token = $token;
+
+            //初始化成功的事件
+//            Hook::listen("company_init_successed", $this->_user);
+
+            return true;
+        } else {
+            $this->setError('You are not logged in');
+            return false;
+        }
+    }
+
+    /**
+     * 注册用户
+     *
+     * @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 && Worker::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'   => config('site.worker_default_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 = Worker::create($params, true);
+
+            $this->_user = Worker::get($user->id);
+            /*$this->_user->username = 'd' . (10000 + $user->id);
+            $this->_user->save();*/
+
+            //设置Token
+            $this->_token = Random::uuid();
+            Tokenworker::set($this->_token, $user->id, $this->keeptime);
+
+            //设置登录状态
+            $this->_logined = true;
+
+            //注册钱包
+            $wallet_id = Db::name('worker_wallet')->insertGetId(['worker_id'=>$user->id]);
+            if(!$wallet_id){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }
+            //注册info
+            $info_id = Db::name('worker_info')->insertGetId(['worker_id'=>$user->id]);
+            if(!$info_id){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }
+
+            //[环信]注册用户。忽略失败
+            /*$easemob = new Easemob();
+            $rs = $easemob->user_create('worker'.$user->id);
+            if($rs === false){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }*/
+
+            //腾讯im注册用户
+            $tenim = new Tenim();
+            $rs = $tenim->register('worker'.$user->id,$params['nickname'],'');
+            if($rs !== true){
+                $this->setError($rs);
+                Db::rollback();
+                return false;
+            }
+
+            //注册成功的事件
+            Db::commit();
+        } catch (Exception $e) {
+            $this->setError($e->getMessage());
+            Db::rollback();
+            return false;
+        }
+        return true;
+    }
+
+
+    /**
+     * 用户登录
+     *
+     * @param string $account  账号,用户名、邮箱、手机号
+     * @param string $password 密码
+     * @return boolean
+     */
+    /*public function login($account, $password)
+    {
+        $field = 'mobile';
+        $user = Worker::get([$field => $account]);
+        if (!$user) {
+            $this->setError('Account is incorrect');
+            return false;
+        }
+
+        if ($user->status != 1) {
+            $this->setError('Account is locked');
+            return false;
+        }
+        if ($user->password != $this->getEncryptPassword($password, $user->salt)) {
+            $this->setError('Password is incorrect');
+            return false;
+        }
+
+        //直接登录员工
+        return $this->direct($user->id);
+    }*/
+
+    /**
+     * 退出
+     *
+     * @return boolean
+     */
+    public function logout()
+    {
+        if (!$this->_logined) {
+            $this->setError('You are not logged in');
+            return false;
+        }
+        //设置登录标识
+        $this->_logined = false;
+        //删除Token
+        Tokenworker::delete($this->_token);
+        //退出成功的事件
+        Hook::listen("user_logout_successed", $this->_user);
+        return true;
+    }
+
+    /**
+     * 修改密码
+     * @param string $newpassword       新密码
+     * @param string $oldpassword       旧密码
+     * @param bool   $ignoreoldpassword 忽略旧密码
+     * @return boolean
+     */
+    public function changepwd($newpassword, $oldpassword = '', $ignoreoldpassword = false)
+    {
+        if (!$this->_logined) {
+            $this->setError('You are not logged in');
+            return false;
+        }
+        //判断旧密码是否正确
+        if ($this->_user->password == $this->getEncryptPassword($oldpassword, $this->_user->salt) || $ignoreoldpassword) {
+            Db::startTrans();
+            try {
+                $salt = Random::alnum();
+                $newpassword = $this->getEncryptPassword($newpassword, $salt);
+                $this->_user->save(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]);
+
+                Token::delete($this->_token);
+                //修改密码成功的事件
+                Hook::listen("user_changepwd_successed", $this->_user);
+                Db::commit();
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->setError($e->getMessage());
+                return false;
+            }
+            return true;
+        } else {
+            $this->setError('Password is incorrect');
+            return false;
+        }
+    }
+
+
+    /**
+     * 直接登录账号
+     * @param int $user_id
+     * @return boolean
+     */
+    public function direct($user_id)
+    {
+        $user = Worker::get($user_id);
+        if ($user) {
+            Db::startTrans();
+            try {
+                $ip = request()->ip();
+                $time = time();
+
+                //判断连续登录和最大连续登录
+                /*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;
+                //记录本次登录的IP和时间
+//                $user->loginip = $ip;
+//                $user->logintime = $time;
+                //重置登录失败次数
+//                $user->loginfailure = 0;
+
+//                $user->save();
+
+                $this->_user = $user;
+
+                $this->_token = Random::uuid();
+                Tokenworker::set($this->_token, $user->id, $this->keeptime);
+
+                $this->_logined = true;
+
+                //登录成功的事件
+                Hook::listen("user_login_successed", $this->_user);
+                Db::commit();
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->setError($e->getMessage());
+                return false;
+            }
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+
+
+    /**
+     * 判断是否登录
+     * @return boolean
+     */
+    public function isLogin()
+    {
+        if ($this->_logined) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 获取当前Token
+     * @return string
+     */
+    public function getToken()
+    {
+        return $this->_token;
+    }
+
+    public function getUserinfo_simple(){
+        $userinfo = Tokenworker::get($this->_token);
+
+        //是否完成用户资料
+        $data = $this->_user->toArray();
+        if(empty($data['idcard'])){
+            $userinfo['finish_profile'] = 0;
+        }else{
+            $userinfo['finish_profile'] = 1;
+        }
+
+        return $userinfo;
+    }
+
+    /**
+     * 获取会员基本信息
+     */
+    public function getUserinfo()
+    {
+        $data = $this->_user->toArray();
+        $allowFields = $this->getAllowFields();
+        $userinfo = array_intersect_key($data, array_flip($allowFields));
+        $userinfo = array_merge($userinfo, Tokenworker::get($this->_token));
+
+        //追加
+        $userinfo['avatar'] = one_domain_image($userinfo['avatar']);
+        $userinfo['keshi_name'] = Db::name('keshi')->where('id',$userinfo['keshi_id'])->value('name');
+        $userinfo['level_name'] = Db::name('worker_level')->where('id',$userinfo['level_id'])->value('name');
+
+        //info
+        $userinfo['worker_info'] = Db::name('worker_info')->where('worker_id',$this->id)->find();
+
+        $userinfo['wallet'] = Db::name('worker_wallet')->where('worker_id',$this->id)->find();
+
+        //是否完成用户资料
+        if(empty($data['idcard'])){
+            $userinfo['finish_profile'] = 0;
+        }else{
+            $userinfo['finish_profile'] = 1;
+        }
+
+        //待接图文订单
+        $where = [
+            'worker_id'=>$this->id,
+            'ordertype'=>1,
+            'status'=>10,
+        ];
+        $userinfo['wenzhen_text_noaccept_num'] = Db::name('wenzhen_order')->where($where)->count();
+
+        //待接视频订单
+        $where = [
+            'worker_id'=>$this->id,
+            'ordertype'=>2,
+            'status'=>10,
+        ];
+        $userinfo['wenzhen_video_noaccept_num'] = Db::name('wenzhen_order')->where($where)->count();
+
+        //待接订单
+        $userinfo['wenzhen_noaccept_num'] = $userinfo['wenzhen_text_noaccept_num'] + $userinfo['wenzhen_video_noaccept_num'];
+
+
+        return $userinfo;
+    }
+
+
+
+    /**
+     * 获取当前请求的URI
+     * @return string
+     */
+    public function getRequestUri()
+    {
+        return $this->requestUri;
+    }
+
+    /**
+     * 设置当前请求的URI
+     * @param string $uri
+     */
+    public function setRequestUri($uri)
+    {
+        $this->requestUri = $uri;
+    }
+
+    /**
+     * 获取允许输出的字段
+     * @return array
+     */
+    public function getAllowFields()
+    {
+        return $this->allowFields;
+    }
+
+    /**
+     * 设置允许输出的字段
+     * @param array $fields
+     */
+    public function setAllowFields($fields)
+    {
+        $this->allowFields = $fields;
+    }
+
+
+
+    /**
+     * 获取密码加密后的字符串
+     * @param string $password 密码
+     * @param string $salt     密码盐
+     * @return string
+     */
+    public function getEncryptPassword($password, $salt = '')
+    {
+        return md5(md5($password) . $salt);
+    }
+
+    /**
+     * 检测当前控制器和方法是否匹配传递的数组
+     *
+     * @param array $arr 需要验证权限的数组
+     * @return boolean
+     */
+    public function match($arr = [])
+    {
+        $request = Request::instance();
+        $arr = is_array($arr) ? $arr : explode(',', $arr);
+        if (!$arr) {
+            return false;
+        }
+        $arr = array_map('strtolower', $arr);
+        // 是否存在
+        if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
+            return true;
+        }
+
+        // 没找到匹配
+        return false;
+    }
+
+    /**
+     * 设置会话有效时间
+     * @param int $keeptime 默认为永久
+     */
+    public function keeptime($keeptime = 0)
+    {
+        $this->keeptime = $keeptime;
+    }
+
+    /**
+     * 渲染用户数据
+     * @param array  $datalist  二维数组
+     * @param mixed  $fields    加载的字段列表
+     * @param string $fieldkey  渲染的字段
+     * @param string $renderkey 结果字段
+     * @return array
+     */
+    /*public function render(&$datalist, $fields = [], $fieldkey = 'user_id', $renderkey = 'userinfo')
+    {
+        $fields = !$fields ? ['id', 'nickname', 'level', 'avatar'] : (is_array($fields) ? $fields : explode(',', $fields));
+        $ids = [];
+        foreach ($datalist as $k => $v) {
+            if (!isset($v[$fieldkey])) {
+                continue;
+            }
+            $ids[] = $v[$fieldkey];
+        }
+        $list = [];
+        if ($ids) {
+            if (!in_array('id', $fields)) {
+                $fields[] = 'id';
+            }
+            $ids = array_unique($ids);
+            $selectlist = User::where('id', 'in', $ids)->column($fields);
+            foreach ($selectlist as $k => $v) {
+                $list[$v['id']] = $v;
+            }
+        }
+        foreach ($datalist as $k => &$v) {
+            $v[$renderkey] = isset($list[$v[$fieldkey]]) ? $list[$v[$fieldkey]] : null;
+        }
+        unset($v);
+        return $datalist;
+    }*/
+
+    /**
+     * 设置错误信息
+     *
+     * @param string $error 错误信息
+     * @return Auth
+     */
+    public function setError($error)
+    {
+        $this->_error = $error;
+        return $this;
+    }
+
+    /**
+     * 获取错误信息
+     * @return string
+     */
+    public function getError()
+    {
+        return $this->_error ? __($this->_error) : '';
+    }
+
+  
+}

+ 161 - 0
application/common/library/Tokenworker.php

@@ -0,0 +1,161 @@
+<?php
+
+namespace app\common\library;
+
+use app\common\library\tokenworker\Driver;
+use think\App;
+use think\Config;
+use think\Log;
+
+/**
+ * Token操作类
+ */
+class Tokenworker
+{
+    /**
+     * @var array Token的实例
+     */
+    public static $instance = [];
+
+    /**
+     * @var object 操作句柄
+     */
+    public static $handler;
+
+    /**
+     * 连接Token驱动
+     * @access public
+     * @param array       $options 配置数组
+     * @param bool|string $name    Token连接标识 true 强制重新连接
+     * @return Driver
+     */
+    public static function connect(array $options = [], $name = false)
+    {
+        $type = !empty($options['type']) ? $options['type'] : 'File';
+
+        if (false === $name) {
+            $name = md5(serialize($options));
+        }
+
+        if (true === $name || !isset(self::$instance[$name])) {
+            $class = false === strpos($type, '\\') ?
+                '\\app\\common\\library\\tokenworker\\driver\\' . ucwords($type) :
+                $type;
+
+            // 记录初始化信息
+            App::$debug && Log::record('[ TOKEN ] INIT ' . $type, 'info');
+
+            if (true === $name) {
+                return new $class($options);
+            }
+
+            self::$instance[$name] = new $class($options);
+        }
+
+        return self::$instance[$name];
+    }
+
+    /**
+     * 自动初始化Token
+     * @access public
+     * @param array $options 配置数组
+     * @return Driver
+     */
+    public static function init(array $options = [])
+    {
+        if (is_null(self::$handler)) {
+            if (empty($options) && 'complex' == Config::get('token.type')) {
+                $default = Config::get('token.default');
+                // 获取默认Token配置,并连接
+                $options = Config::get('token.' . $default['type']) ?: $default;
+            } elseif (empty($options)) {
+                $options = Config::get('token');
+            }
+
+            self::$handler = self::connect($options);
+        }
+
+        return self::$handler;
+    }
+
+    /**
+     * 判断Token是否可用(check别名)
+     * @access public
+     * @param string $token   Token标识
+     * @param int    $user_id 会员ID
+     * @return bool
+     */
+    public static function has($token, $user_id)
+    {
+        return self::check($token, $user_id);
+    }
+
+    /**
+     * 判断Token是否可用
+     * @param string $token   Token标识
+     * @param int    $user_id 会员ID
+     * @return bool
+     */
+    public static function check($token, $user_id)
+    {
+        return self::init()->check($token, $user_id);
+    }
+
+    /**
+     * 读取Token
+     * @access public
+     * @param string $token   Token标识
+     * @param mixed  $default 默认值
+     * @return mixed
+     */
+    public static function get($token, $default = false)
+    {
+        return self::init()->get($token) ?: $default;
+    }
+
+    /**
+     * 写入Token
+     * @access public
+     * @param string   $token   Token标识
+     * @param mixed    $user_id 会员ID
+     * @param int|null $expire  有效时间 0为永久
+     * @return boolean
+     */
+    public static function set($token, $user_id, $expire = null)
+    {
+        return self::init()->set($token, $user_id, $expire);
+    }
+
+    /**
+     * 删除Token(delete别名)
+     * @access public
+     * @param string $token Token标识
+     * @return boolean
+     */
+    public static function rm($token)
+    {
+        return self::delete($token);
+    }
+
+    /**
+     * 删除Token
+     * @param string $token 标签名
+     * @return bool
+     */
+    public static function delete($token)
+    {
+        return self::init()->delete($token);
+    }
+
+    /**
+     * 清除Token
+     * @access public
+     * @param int user_id 会员ID
+     * @return boolean
+     */
+    public static function clear($user_id = null)
+    {
+        return self::init()->clear($user_id);
+    }
+
+}

+ 91 - 0
application/common/library/tokenworker/Driver.php

@@ -0,0 +1,91 @@
+<?php
+// +----------------------------------------------------------------------
+// | ThinkPHP [ WE CAN DO IT JUST THINK ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
+// +----------------------------------------------------------------------
+// | Author: liu21st <liu21st@gmail.com>
+// +----------------------------------------------------------------------
+
+namespace app\common\library\tokenworker;
+
+/**
+ * Token基础类
+ */
+abstract class Driver
+{
+    protected $handler = null;
+    protected $options = [];
+
+    /**
+     * 存储Token
+     * @param   string $token   Token
+     * @param   int    $user_id 会员ID
+     * @param   int    $expire  过期时长,0表示无限,单位秒
+     * @return bool
+     */
+    abstract function set($token, $user_id, $expire = 0);
+
+    /**
+     * 获取Token内的信息
+     * @param   string $token
+     * @return  array
+     */
+    abstract function get($token);
+
+    /**
+     * 判断Token是否可用
+     * @param   string $token   Token
+     * @param   int    $user_id 会员ID
+     * @return  boolean
+     */
+    abstract function check($token, $user_id);
+
+    /**
+     * 删除Token
+     * @param   string $token
+     * @return  boolean
+     */
+    abstract function delete($token);
+
+    /**
+     * 删除指定用户的所有Token
+     * @param   int $user_id
+     * @return  boolean
+     */
+    abstract function clear($user_id);
+
+    /**
+     * 返回句柄对象,可执行其它高级方法
+     *
+     * @access public
+     * @return object
+     */
+    public function handler()
+    {
+        return $this->handler;
+    }
+
+    /**
+     * 获取加密后的Token
+     * @param string $token Token标识
+     * @return string
+     */
+    protected function getEncryptedToken($token)
+    {
+        $config = \think\Config::get('token');
+        return hash_hmac($config['hashalgo'], $token, $config['key']);
+    }
+
+    /**
+     * 获取过期剩余时长
+     * @param $expiretime
+     * @return float|int|mixed
+     */
+    protected function getExpiredIn($expiretime)
+    {
+        return $expiretime ? max(0, $expiretime - time()) : 365 * 86400;
+    }
+}

+ 126 - 0
application/common/library/tokenworker/driver/Mysql.php

@@ -0,0 +1,126 @@
+<?php
+
+namespace app\common\library\tokenworker\driver;
+
+use app\common\library\tokenworker\Driver;
+
+/**
+ * Token操作类
+ */
+class Mysql extends Driver
+{
+
+    /**
+     * 默认配置
+     * @var array
+     */
+    protected $options = [
+        'table'      => 'worker_token',
+        'expire'     => 2592000,
+        'connection' => [],
+    ];
+
+
+    /**
+     * 构造函数
+     * @param array $options 参数
+     * @access public
+     */
+    public function __construct($options = [])
+    {
+        if (!empty($options)) {
+            $this->options = array_merge($this->options, $options);
+        }
+        if ($this->options['connection']) {
+            $this->handler = \think\Db::connect($this->options['connection'])->name($this->options['table']);
+        } else {
+            $this->handler = \think\Db::name($this->options['table']);
+        }
+        $time = time();
+        $tokentime = cache('tokentime');
+        if (!$tokentime || $tokentime < $time - 86400) {
+            cache('tokentime', $time);
+            $this->handler->where('expiretime', '<', $time)->where('expiretime', '>', 0)->delete();
+        }
+    }
+
+    /**
+     * 存储Token
+     * @param string $token   Token
+     * @param int    $user_id 会员ID
+     * @param int    $expire  过期时长,0表示无限,单位秒
+     * @return bool
+     */
+    public function set($token, $user_id, $expire = null)
+    {
+        $expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0;
+        $token = $this->getEncryptedToken($token);
+        $this->handler->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime]);
+        return true;
+    }
+
+    /**
+     * 获取Token内的信息
+     * @param string $token
+     * @return  array
+     */
+    public function get($token)
+    {
+        //方便测试
+        if(strpos($token,'testuid_') !== false && config('api_exception') === true){
+            $uid = substr($token,8);
+            return [
+                'user_id' => intval($uid),
+            ];
+        }
+        //方便测试
+        $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
+        if ($data) {
+            if (!$data['expiretime'] || $data['expiretime'] > time()) {
+                //返回未加密的token给客户端使用
+                $data['token'] = $token;
+                //返回剩余有效时间
+                $data['expires_in'] = $this->getExpiredIn($data['expiretime']);
+                return $data;
+            } else {
+                self::delete($token);
+            }
+        }
+        return [];
+    }
+
+    /**
+     * 判断Token是否可用
+     * @param string $token   Token
+     * @param int    $user_id 会员ID
+     * @return  boolean
+     */
+    public function check($token, $user_id)
+    {
+        $data = $this->get($token);
+        return $data && $data['user_id'] == $user_id ? true : false;
+    }
+
+    /**
+     * 删除Token
+     * @param string $token
+     * @return  boolean
+     */
+    public function delete($token)
+    {
+        $this->handler->where('token', $this->getEncryptedToken($token))->delete();
+        return true;
+    }
+
+    /**
+     * 删除指定用户的所有Token
+     * @param int $user_id
+     * @return  boolean
+     */
+    public function clear($user_id)
+    {
+        $this->handler->where('user_id', $user_id)->delete();
+        return true;
+    }
+
+}

+ 112 - 0
application/common/model/Worker.php

@@ -0,0 +1,112 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 会员模型
+ */
+class Worker extends Model
+{
+
+    // 表名
+    protected $name = 'worker';
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    // 追加属性
+    protected $append = [
+        'gender_text',
+        'idcard_status_text',
+        'worker_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 getWorkerStatusList()
+    {
+        return ['-1' => __('Worker_status -1'), '0' => __('Worker_status 0'), '1' => __('Worker_status 1'), '2' => __('Worker_status 2'), '3' => __('Worker_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 getWorkerStatusTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['worker_status']) ? $data['worker_status'] : '');
+        $list = $this->getWorkerStatusList();
+        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 Workerlevel()
+    {
+        return $this->belongsTo('Workerlevel', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
+    }
+
+
+}