|
@@ -0,0 +1,194 @@
|
|
|
|
+<?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;
|
|
|
|
+ }
|
|
|
|
+}
|