<?php


namespace app\http\middleware;


use app\models\auction\Auction;
use app\models\auction\AuctionOrder;
use app\models\auction\AuctionProduct;
use app\models\auction\AuctionTime;
use app\models\store\StoreOrder;
use app\models\user\User;
use app\Request;
use crmeb\interfaces\MiddlewareInterface;
use think\facade\Config;
use think\facade\Db;
use think\Response;

/**
 * 跨域中间件
 * Class AllowOriginMiddleware
 * @package app\http\middleware
 */
class AllowOriginMiddleware implements MiddlewareInterface
{
    /**
     * header头
     * @var array
     */
    protected $header = [
        'Access-Control-Allow-Origin' => '*',
        'Access-Control-Allow-Headers' => 'Authori-zation,Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
        'Access-Control-Allow-Methods' => 'GET,POST,PATCH,PUT,DELETE,OPTIONS,DELETE',
        'Access-Control-Max-Age' => '1728000'
    ];

    /**
     * 允许跨域的域名
     * @var string
     */
    protected $cookieDomain;

    /**
     * @param Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle(Request $request, \Closure $next)
    {
        $this->cookieDomain = Config::get('cookie.domain', '');
        $header = $this->header;
        $origin = $request->header('origin');

        if ($origin && ('' != $this->cookieDomain && strpos($origin, $this->cookieDomain)))
            $header['Access-Control-Allow-Origin'] = $origin;

        if ($request->method(true) == 'OPTIONS') {
            $response = Response::create('ok')->code(200)->header($header);
        } else {
            $response = $next($request)->header($header);
        }
        $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
        Auction::frequency(); // 更新场次
        AuctionProduct::off_the_shelf(); // 下架未出售商品

        try {
            Db::startTrans();
            AuctionOrder::deduction();//订单一个小时内未上传扣除预约卷
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        try {
            Db::startTrans();
            AuctionOrder::th();//退回预约卷
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        try {
            Db::startTrans();
            AuctionOrder::goods();//自动放货
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        try {
            Db::startTrans();
            User::thaw();//解冻
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        return $response;
    }
}