InspectionAuth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\inspection\InspectionApplication;
  4. use app\common\model\User;
  5. use fast\Random;
  6. use think\Config;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Hook;
  10. use think\Request;
  11. use think\Validate;
  12. class InspectionAuth
  13. {
  14. protected static $instance = null;
  15. protected $_error = '';
  16. protected $_logined = false;
  17. protected $_application = null;
  18. protected $_user = null;
  19. protected $_token = '';
  20. protected $keeptime = 2592000;
  21. protected $allowFields = ['id', 'user_id', 'name', 'phone', 'supplier_id', 'audit_status', 'status'];
  22. public function __construct($options = [])
  23. {
  24. // 可扩展配置
  25. }
  26. public static function instance($options = [])
  27. {
  28. if (is_null(self::$instance)) {
  29. self::$instance = new static($options);
  30. }
  31. return self::$instance;
  32. }
  33. /**
  34. * 初始化验货员身份(通过token)
  35. * @param string $token
  36. * @return bool
  37. */
  38. public function init($token)
  39. {
  40. if ($this->_logined) {
  41. return true;
  42. }
  43. if ($this->_error) {
  44. return false;
  45. }
  46. $application = InspectionApplication::where('token', $token)
  47. ->where('audit_status', 2)
  48. ->where('status', 1)
  49. ->find();
  50. if (!$application) {
  51. $this->setError('验货员未登录或未通过审核');
  52. return false;
  53. }
  54. $this->_application = $application;
  55. $this->_user = User::get($application->user_id);
  56. $this->_logined = true;
  57. $this->_token = $token;
  58. return true;
  59. }
  60. /**
  61. * 直接登录验货员
  62. * @param int $user_id
  63. * @return bool
  64. */
  65. public function direct($user_id)
  66. {
  67. $application = InspectionApplication::where('user_id', $user_id)
  68. ->where('audit_status', 2)
  69. ->where('status', 1)
  70. ->find();
  71. if (!$application) {
  72. $this->setError('验货员未通过审核');
  73. return false;
  74. }
  75. $token = Random::uuid();
  76. $expire = time() + $this->keeptime;
  77. $application->token = $token;
  78. $application->token_expiretime = $expire;
  79. $application->save();
  80. $this->_application = $application;
  81. $this->_user = User::get($application->user_id);
  82. $this->_logined = true;
  83. $this->_token = $token;
  84. return true;
  85. }
  86. /**
  87. * 判断是否已登录
  88. * @return bool
  89. */
  90. public function isLogin()
  91. {
  92. return $this->_logined;
  93. }
  94. /**
  95. * 获取当前Token
  96. * @return string
  97. */
  98. public function getToken()
  99. {
  100. return $this->_token;
  101. }
  102. /**
  103. * 获取验货员申请信息
  104. * @return InspectionApplication|null
  105. */
  106. public function getApplication()
  107. {
  108. return $this->_application;
  109. }
  110. /**
  111. * 获取验货员用户信息
  112. * @return User|null
  113. */
  114. public function getUser()
  115. {
  116. return $this->_user;
  117. }
  118. /**
  119. * 获取供应商ID
  120. * @return int
  121. */
  122. public function getSupplierId()
  123. {
  124. return $this->_application ? $this->_application->supplier_id : 0;
  125. }
  126. /**
  127. * 退出登录
  128. * @return bool
  129. */
  130. public function logout()
  131. {
  132. if (!$this->_logined) {
  133. $this->setError('未登录');
  134. return false;
  135. }
  136. $this->_application->token = null;
  137. $this->_application->token_expiretime = null;
  138. $this->_application->save();
  139. $this->_logined = false;
  140. $this->_token = '';
  141. return true;
  142. }
  143. /**
  144. * 设置错误信息
  145. * @param string $error
  146. * @return $this
  147. */
  148. public function setError($error)
  149. {
  150. $this->_error = $error;
  151. return $this;
  152. }
  153. /**
  154. * 获取错误信息
  155. * @return string
  156. */
  157. public function getError()
  158. {
  159. return $this->_error;
  160. }
  161. /**
  162. * 获取允许输出的字段
  163. * @return array
  164. */
  165. public function getAllowFields()
  166. {
  167. return $this->allowFields;
  168. }
  169. /**
  170. * 设置允许输出的字段
  171. * @param array $fields
  172. */
  173. public function setAllowFields($fields)
  174. {
  175. $this->allowFields = $fields;
  176. }
  177. }