| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 | 
							- <?php
 
- namespace app\common\library;
 
- use app\common\model\inspection\InspectionApplication;
 
- use app\common\model\User;
 
- use fast\Random;
 
- use think\Config;
 
- use think\Db;
 
- use think\Exception;
 
- use think\Hook;
 
- use think\Request;
 
- use think\Validate;
 
- class InspectionAuth
 
- {
 
-     protected static $instance = null;
 
-     protected $_error = '';
 
-     protected $_logined = false;
 
-     protected $_application = null;
 
-     protected $_user = null;
 
-     protected $_token = '';
 
-     protected $keeptime = 2592000;
 
-     protected $allowFields = ['id', 'user_id', 'name', 'phone', 'supplier_id', 'audit_status', 'status'];
 
-     public function __construct($options = [])
 
-     {
 
-         // 可扩展配置
 
-     }
 
-     public static function instance($options = [])
 
-     {
 
-         if (is_null(self::$instance)) {
 
-             self::$instance = new static($options);
 
-         }
 
-         return self::$instance;
 
-     }
 
-     /**
 
-      * 初始化验货员身份(通过token)
 
-      * @param string $token
 
-      * @return bool
 
-      */
 
-     public function init($token)
 
-     {
 
-         if ($this->_logined) {
 
-             return true;
 
-         }
 
-         if ($this->_error) {
 
-             return false;
 
-         }
 
-         $application = InspectionApplication::where('token', $token)
 
-             ->where('audit_status', 2)
 
-             ->where('status', 1)
 
-             ->find();
 
-         if (!$application) {
 
-             $this->setError('验货员未登录或未通过审核');
 
-             return false;
 
-         }
 
-         $this->_application = $application;
 
-         $this->_user = User::get($application->user_id);
 
-         $this->_logined = true;
 
-         $this->_token = $token;
 
-         return true;
 
-     }
 
-     /**
 
-      * 直接登录验货员
 
-      * @param int $user_id
 
-      * @return bool
 
-      */
 
-     public function direct($user_id)
 
-     {
 
-         $application = InspectionApplication::where('user_id', $user_id)
 
-             ->where('audit_status', 2)
 
-             ->where('status', 1)
 
-             ->find();
 
-         if (!$application) {
 
-             $this->setError('验货员未通过审核');
 
-             return false;
 
-         }
 
-         $token = Random::uuid();
 
-         $expire = time() + $this->keeptime;
 
-         $application->token = $token;
 
-         $application->token_expiretime = $expire;
 
-         $application->save();
 
-         $this->_application = $application;
 
-         $this->_user = User::get($application->user_id);
 
-         $this->_logined = true;
 
-         $this->_token = $token;
 
-         return true;
 
-     }
 
-     /**
 
-      * 判断是否已登录
 
-      * @return bool
 
-      */
 
-     public function isLogin()
 
-     {
 
-         return $this->_logined;
 
-     }
 
-     /**
 
-      * 获取当前Token
 
-      * @return string
 
-      */
 
-     public function getToken()
 
-     {
 
-         return $this->_token;
 
-     }
 
-     /**
 
-      * 获取验货员申请信息
 
-      * @return InspectionApplication|null
 
-      */
 
-     public function getApplication()
 
-     {
 
-         return $this->_application;
 
-     }
 
-     /**
 
-      * 获取验货员用户信息
 
-      * @return User|null
 
-      */
 
-     public function getUser()
 
-     {
 
-         return $this->_user;
 
-     }
 
-     /**
 
-      * 获取供应商ID
 
-      * @return int
 
-      */
 
-     public function getSupplierId()
 
-     {
 
-         return $this->_application ? $this->_application->supplier_id : 0;
 
-     }
 
-     /**
 
-      * 退出登录
 
-      * @return bool
 
-      */
 
-     public function logout()
 
-     {
 
-         if (!$this->_logined) {
 
-             $this->setError('未登录');
 
-             return false;
 
-         }
 
-         $this->_application->token = null;
 
-         $this->_application->token_expiretime = null;
 
-         $this->_application->save();
 
-         $this->_logined = false;
 
-         $this->_token = '';
 
-         return true;
 
-     }
 
-     /**
 
-      * 设置错误信息
 
-      * @param string $error
 
-      * @return $this
 
-      */
 
-     public function setError($error)
 
-     {
 
-         $this->_error = $error;
 
-         return $this;
 
-     }
 
-     /**
 
-      * 获取错误信息
 
-      * @return string
 
-      */
 
-     public function getError()
 
-     {
 
-         return $this->_error;
 
-     }
 
-     /**
 
-      * 获取允许输出的字段
 
-      * @return array
 
-      */
 
-     public function getAllowFields()
 
-     {
 
-         return $this->allowFields;
 
-     }
 
-     /**
 
-      * 设置允许输出的字段
 
-      * @param array $fields
 
-      */
 
-     public function setAllowFields($fields)
 
-     {
 
-         $this->allowFields = $fields;
 
-     }
 
- } 
 
 
  |