|
@@ -0,0 +1,51 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm
|
|
|
+ * User:林志杰
|
|
|
+ * Email:[email protected]
|
|
|
+ * Motto:纵有疾风起,人生不言弃!
|
|
|
+ * Time:2020/2/8 23:10
|
|
|
+ */
|
|
|
+
|
|
|
+namespace app\api\controller;
|
|
|
+
|
|
|
+/**
|
|
|
+ * API模块下的公共控制器
|
|
|
+ * 如果不需要登录的场景继承ApiBase这个控制器
|
|
|
+ * 如果需要登录的场景继承AuthBase这个控制器
|
|
|
+ * Class ApiBase
|
|
|
+ * @package app\api\controller
|
|
|
+ */
|
|
|
+class AuthBase extends ApiBase
|
|
|
+{
|
|
|
+ public $userId = 0;
|
|
|
+ public $username = '';
|
|
|
+ public $accessToken = '';
|
|
|
+
|
|
|
+ public function initialize()
|
|
|
+ {
|
|
|
+ parent::initialize(); // TODO: Change the autogenerated stub
|
|
|
+ $this->accessToken = $this->request->header('access-token');
|
|
|
+ if (!$this->accessToken || !$this->isLogin()) {
|
|
|
+ return $this->show(config('status.not_login'), '没有登录');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断用户是否登录
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function isLogin(): bool
|
|
|
+ {
|
|
|
+ $userInfo = cache(config('redis.token_pre') . $this->accessToken);
|
|
|
+ if (!$userInfo) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!empty($userInfo['id']) && !empty($userInfo['username'])) {
|
|
|
+ $this->userId = $userInfo['id'];
|
|
|
+ $this->username = $userInfo['username'];
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|