1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace app\common\controller;
- use app\common\library\BlurUtils;
- use app\common\library\Gm;
- use think\facade\Cache;
- use think\facade\Config;
- class ApiController extends Api
- {
-
- /**
- * 无需解码的方法
- * 访问本控制器的此方法,无需解码
- * @var array
- */
- protected array $noAuth = [];
- protected $tokenData = null;
-
- protected function needAuthToken(): bool
- {
- return !action_in_arr($this->noAuth);
- }
- protected function initialize(): void
- {
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
- header("Access-Control-Allow-Headers: X-Requested-With");
- if($this->needAuthToken()) {
- $token = $this->getHeaderToken();
- $data = Cache::get($token);
- $this->tokenData = $data;
- if(empty($data))
- {
- $this->error('无效的token');
- }
- }
- BlurUtils::setNotNeedBlur();
- }
- /**
- * $params $data
- */
- public function makeModelData($data,$mapping): array
- {
- $info = [];
- foreach ($data as $k=>$v)
- {
- if($info[$mapping[$k]] ?? '')
- {
- continue;
- }
- $info[$mapping[$k]] = $v;
- }
- return $info;
- }
- public function getDecryptData($data): array
- {
- $key = 'zLxapoeqWYpoeqWY';
- $info = Gm::decrypt($key,$data);
- return json_decode(base64_decode($info),true);
- }
- public function makeEncryptData($data)
- {
- $key = Config::get('gm.key');
- $info = Gm::encrypt($key,(base64_encode(json_encode($data))));
- return $info;
- }
- public function getHeaderToken(): string
- {
- $header = getallheaders();
- $auth = $header['Authorization'];
- $data = explode(' ',$auth);
- return $data[1];
- }
- }
|