1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace app\common\middleware;
- use app\Request;
- use think\exception\HttpResponseException;
- use think\facade\Config;
- use think\Response;
- class AllowOriginMiddleware extends BaseMiddleware
- {
-
- protected $header = [
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Headers' => 'LatLon, X-Token, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With,Form-type,Referer,Connection,Content-Length,Host,Origin,Authorization,Authori-zation,Accept,Accept-Encoding',
-
- 'Access-Control-Allow-Methods' => 'GET,POST,PATCH,PUT,DELETE,OPTIONS,DELETE',
- 'Access-Control-Max-Age' => '1728000'
- ];
- public function before(Request $request)
- {
- $cookieDomain = Config::get('cookie.domain', '');
- $origin = $request->header('origin');
- $lonlat = trim($request->header('LatLon'));
- $request->macro('lonlat', function () use ($lonlat) {
- return $lonlat;
- });
- if ($origin && $cookieDomain && strpos($origin, $cookieDomain))
- $this->header['Access-Control-Allow-Origin'] = $origin;
- if ($request->method(true) == 'OPTIONS') {
- throw new HttpResponseException(Response::create()->code(200)->header($this->header));
- }
- }
- public function after(Response $response)
- {
- $response->header($this->header);
- }
- }
|