InspectionAuth.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 $requestUri = '';
  22. protected $rules = [];
  23. //默认配置
  24. protected $config = [];
  25. protected $options = [];
  26. protected $allowFields = ['id', 'user_id', 'name', 'phone', 'supplier_id', 'audit_status', 'status', 'region_full', 'address', 'avatar'];
  27. public function __construct($options = [])
  28. {
  29. if ($config = Config::get('inspection')) {
  30. $this->config = array_merge($this->config, $config);
  31. }
  32. $this->options = array_merge($this->config, $options);
  33. }
  34. public static function instance($options = [])
  35. {
  36. if (is_null(self::$instance)) {
  37. self::$instance = new static($options);
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * 兼容调用application模型的属性
  43. *
  44. * @param string $name
  45. * @return mixed
  46. */
  47. public function __get($name)
  48. {
  49. return $this->_application ? $this->_application->$name : null;
  50. }
  51. /**
  52. * 兼容调用application模型的属性
  53. */
  54. public function __isset($name)
  55. {
  56. return isset($this->_application) ? isset($this->_application->$name) : false;
  57. }
  58. /**
  59. * 根据Token初始化验货员身份
  60. * @param string $token
  61. * @return bool
  62. */
  63. public function init($token)
  64. {
  65. if ($this->_logined) {
  66. return true;
  67. }
  68. if ($this->_error) {
  69. return false;
  70. }
  71. if (!$token) {
  72. return false;
  73. }
  74. $application = InspectionApplication::where([
  75. 'token' => $token,
  76. 'audit_status' => 2,
  77. 'status' => 1
  78. ])->where('token_expire_time', '>', time())->find();
  79. if (!$application) {
  80. $this->setError('验货员未登录或登录已过期');
  81. return false;
  82. }
  83. $user = User::get($application->user_id);
  84. if (!$user) {
  85. $this->setError('用户不存在');
  86. return false;
  87. }
  88. if ($user->status != 1) {
  89. $this->setError('用户已被禁用');
  90. return false;
  91. }
  92. $this->_application = $application;
  93. $this->_user = $user;
  94. $this->_logined = true;
  95. $this->_token = $token;
  96. //初始化成功的事件
  97. Hook::listen("inspection_init_successed", $this->_application);
  98. return true;
  99. }
  100. /**
  101. * 验货员登录
  102. * @param string $phone 手机号
  103. * @param string $password 密码(实际项目中可能需要验证码等)
  104. * @return bool
  105. */
  106. public function login($phone, $password = '')
  107. {
  108. $application = InspectionApplication::where([
  109. 'phone' => $phone,
  110. 'audit_status' => 2,
  111. 'status' => 1
  112. ])->find();
  113. if (!$application) {
  114. $this->setError('验货员不存在或未通过审核');
  115. return false;
  116. }
  117. $user = User::get($application->user_id);
  118. if (!$user) {
  119. $this->setError('关联用户不存在');
  120. return false;
  121. }
  122. if ($user->status != 1) {
  123. $this->setError('用户已被禁用');
  124. return false;
  125. }
  126. // 检查登录失败次数
  127. if ($application->login_failure >= 10 && time() - $application->login_failure_time < 86400) {
  128. $this->setError('登录失败次数过多,请24小时后重试');
  129. return false;
  130. }
  131. // 验证密码
  132. if ($password && $application->password != $this->getEncryptPassword($password, $application->salt)) {
  133. $application->save(['login_failure' => $application->login_failure + 1, 'login_failure_time' => time()]);
  134. $this->setError('密码错误');
  135. return false;
  136. }
  137. //直接登录验货员
  138. return $this->direct($application->user_id);
  139. }
  140. /**
  141. * 直接登录验货员
  142. * @param int $user_id
  143. * @return bool
  144. */
  145. public function direct($id)
  146. {
  147. $application = InspectionApplication::where([
  148. 'id' => $id,
  149. 'audit_status' => 2,
  150. 'status' => 1
  151. ])->find();
  152. if (!$application) {
  153. $this->setError('验货员未通过审核');
  154. return false;
  155. }
  156. $user = User::get($application->user_id);
  157. if (!$user) {
  158. $this->setError('关联用户不存在');
  159. return false;
  160. }
  161. Db::startTrans();
  162. try {
  163. $ip = request()->ip();
  164. $time = time();
  165. $token = Random::uuid();
  166. $expire = $time + $this->keeptime;
  167. //判断连续登录和最大连续登录
  168. if ($application->login_time < \fast\Date::unixtime('day')) {
  169. $application->successions = $application->login_time < \fast\Date::unixtime('day', -1) ? 1 : $application->successions + 1;
  170. $application->max_successions = max($application->successions, $application->max_successions);
  171. }
  172. $application->prev_time = $application->login_time;
  173. //记录本次登录的IP和时间
  174. $application->login_ip = $ip;
  175. $application->login_time = $time;
  176. //重置登录失败次数
  177. $application->login_failure = 0;
  178. //设置token
  179. $application->token = $token;
  180. $application->token_expire_time = $expire;
  181. $application->save();
  182. $this->_application = $application;
  183. $this->_user = $user;
  184. $this->_logined = true;
  185. $this->_token = $token;
  186. //登录成功的事件
  187. Hook::listen("inspection_login_successed", $this->_application);
  188. Db::commit();
  189. } catch (Exception $e) {
  190. Db::rollback();
  191. $this->setError($e->getMessage());
  192. return false;
  193. }
  194. return true;
  195. }
  196. /**
  197. * 退出登录
  198. * @return bool
  199. */
  200. public function logout()
  201. {
  202. if (!$this->_logined) {
  203. $this->setError('未登录');
  204. return false;
  205. }
  206. Db::startTrans();
  207. try {
  208. $this->_application->token = null;
  209. $this->_application->token_expire_time = null;
  210. $this->_application->save();
  211. $this->_logined = false;
  212. $this->_token = '';
  213. //退出成功的事件
  214. Hook::listen("inspection_logout_successed", $this->_application);
  215. Db::commit();
  216. } catch (Exception $e) {
  217. Db::rollback();
  218. $this->setError($e->getMessage());
  219. return false;
  220. }
  221. return true;
  222. }
  223. /**
  224. * 修改密码
  225. * @param string $newpassword 新密码
  226. * @param string $oldpassword 旧密码
  227. * @param bool $ignoreoldpassword 忽略旧密码
  228. * @return boolean
  229. */
  230. public function changepwd($newpassword, $oldpassword = '', $ignoreoldpassword = false)
  231. {
  232. if (!$this->_logined) {
  233. $this->setError('您尚未登录');
  234. return false;
  235. }
  236. //判断旧密码是否正确
  237. if ($this->_application->password == $this->getEncryptPassword($oldpassword, $this->_application->salt) || $ignoreoldpassword) {
  238. Db::startTrans();
  239. try {
  240. $salt = Random::alnum();
  241. $newpassword = $this->getEncryptPassword($newpassword, $salt);
  242. // 更新验货员申请表的密码和salt
  243. $this->_application->password = $newpassword;
  244. $this->_application->salt = $salt;
  245. $this->_application->login_failure = 0;
  246. // 清除当前token,强制重新登录
  247. $this->_application->token = null;
  248. $this->_application->token_expire_time = null;
  249. $this->_application->save();
  250. //修改密码成功的事件
  251. Hook::listen("inspection_changepwd_successed", $this->_application);
  252. Db::commit();
  253. } catch (Exception $e) {
  254. Db::rollback();
  255. $this->setError($e->getMessage());
  256. return false;
  257. }
  258. return true;
  259. } else {
  260. $this->setError('旧密码错误');
  261. return false;
  262. }
  263. }
  264. /**
  265. * 判断是否已登录
  266. * @return bool
  267. */
  268. public function isLogin()
  269. {
  270. return $this->_logined;
  271. }
  272. /**
  273. * 获取当前Token
  274. * @return string
  275. */
  276. public function getToken()
  277. {
  278. return $this->_token;
  279. }
  280. /**
  281. * 获取验货员申请信息
  282. * @return InspectionApplication|null
  283. */
  284. public function getApplication()
  285. {
  286. return $this->_application;
  287. }
  288. /**
  289. * 获取验货员用户信息
  290. * @return User|null
  291. */
  292. public function getUser()
  293. {
  294. return $this->_user;
  295. }
  296. /**
  297. * 获取验货员基本信息
  298. */
  299. public function getInspectorInfo()
  300. {
  301. if (!$this->_application) {
  302. return null;
  303. }
  304. $data = $this->_application->toArray();
  305. $allowFields = $this->getAllowFields();
  306. $inspectorInfo = array_intersect_key($data, array_flip($allowFields));
  307. // 添加token信息
  308. $inspectorInfo['token'] = $this->_token;
  309. $inspectorInfo['token_expire_time'] = $this->_application->token_expire_time;
  310. return $inspectorInfo;
  311. }
  312. /**
  313. * 获取供应商ID
  314. * @return int
  315. */
  316. public function getSupplierId()
  317. {
  318. return $this->_application ? $this->_application->supplier_id : 0;
  319. }
  320. /**
  321. * 获取当前请求的URI
  322. * @return string
  323. */
  324. public function getRequestUri()
  325. {
  326. return $this->requestUri;
  327. }
  328. /**
  329. * 设置当前请求的URI
  330. * @param string $uri
  331. */
  332. public function setRequestUri($uri)
  333. {
  334. $this->requestUri = $uri;
  335. }
  336. /**
  337. * 获取允许输出的字段
  338. * @return array
  339. */
  340. public function getAllowFields()
  341. {
  342. return $this->allowFields;
  343. }
  344. /**
  345. * 设置允许输出的字段
  346. * @param array $fields
  347. */
  348. public function setAllowFields($fields)
  349. {
  350. $this->allowFields = $fields;
  351. }
  352. /**
  353. * 检测当前控制器和方法是否匹配传递的数组
  354. *
  355. * @param array $arr 需要验证权限的数组
  356. * @return boolean
  357. */
  358. public function match($arr = [])
  359. {
  360. $request = Request::instance();
  361. $arr = is_array($arr) ? $arr : explode(',', $arr);
  362. if (!$arr) {
  363. return false;
  364. }
  365. $arr = array_map('strtolower', $arr);
  366. // 是否存在
  367. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
  368. return true;
  369. }
  370. // 没找到匹配
  371. return false;
  372. }
  373. /**
  374. * 设置会话有效时间
  375. * @param int $keeptime 默认为永久
  376. */
  377. public function keeptime($keeptime = 0)
  378. {
  379. $this->keeptime = $keeptime;
  380. }
  381. /**
  382. * 设置初始密码
  383. * @param string $password 密码
  384. * @return boolean
  385. */
  386. public function setPassword($password)
  387. {
  388. if (!$this->_application) {
  389. $this->setError('验货员信息不存在');
  390. return false;
  391. }
  392. try {
  393. $salt = Random::alnum();
  394. $encryptPassword = $this->getEncryptPassword($password, $salt);
  395. $this->_application->password = $encryptPassword;
  396. $this->_application->salt = $salt;
  397. $this->_application->save();
  398. return true;
  399. } catch (Exception $e) {
  400. $this->setError($e->getMessage());
  401. return false;
  402. }
  403. }
  404. /**
  405. * 更新头像
  406. * @param string $avatar 头像路径
  407. * @return boolean
  408. */
  409. public function updateAvatar($avatar)
  410. {
  411. if (!$this->_logined) {
  412. $this->setError('您尚未登录');
  413. return false;
  414. }
  415. try {
  416. $this->_application->avatar = $avatar;
  417. $this->_application->save();
  418. //更新头像成功的事件
  419. Hook::listen("inspection_update_avatar_successed", $this->_application);
  420. return true;
  421. } catch (Exception $e) {
  422. $this->setError($e->getMessage());
  423. return false;
  424. }
  425. }
  426. /**
  427. * 获取头像完整URL
  428. * @return string
  429. */
  430. public function getAvatar()
  431. {
  432. if (!$this->_application || !$this->_application->avatar) {
  433. return '';
  434. }
  435. return cdnurl($this->_application->avatar, true);
  436. }
  437. /**
  438. * 获取密码加密后的字符串
  439. * @param string $password 密码
  440. * @param string $salt 密码盐
  441. * @return string
  442. */
  443. public function getEncryptPassword($password, $salt = '')
  444. {
  445. return md5(md5($password) . $salt);
  446. }
  447. /**
  448. * 设置错误信息
  449. * @param string $error
  450. * @return $this
  451. */
  452. public function setError($error)
  453. {
  454. $this->_error = $error;
  455. return $this;
  456. }
  457. /**
  458. * 获取错误信息
  459. * @return string
  460. */
  461. public function getError()
  462. {
  463. return $this->_error ? __($this->_error) : '';
  464. }
  465. }