12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?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);
- if(empty($return))
- {
- return [];
- }else{
- 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] ?? '';
- }
- }
|