appserve接口优化

bug问题 · xi_qing · 于 6年前 发布 · 2293 次阅读

近期在编写接口的时候发现会有出现跨域问题,查看了一下源码,做了一下2个修改: 1、\vendor\fancyecommerce\fecshop\app\appserver\modules\AppserverController.php中增加一下代码:

public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors["corsFilter"] = [
            "class" => \yii\filters\Cors::className()
        ];
        return $behaviors;
    }

2、在使用access-token验证并获取信息时,若token过期,使用ajax请求会出现跨域问题,修改如下: 在vendor\fancyecommerce\fecshop\yii\filters\auth\QueryParamAuth.php中 原代码:

public function authenticate($user, $request, $response)
    {   
        $identity = Yii::$service->customer->loginByAccessToken(get_class($this));
        if($identity){
            return $identity;
        }else{
            $result = ['status' => 'ERROR', 'code' => 401,'message' => 'token is time out'];
            echo json_encode($result);
            exit;
        }
    }

修改后为:

public function authenticate($user, $request, $response)
    {   
        $identity = Yii::$service->customer->loginByAccessToken(get_class($this));
        if($identity){
            return $identity;
        }else{
            $result = ['status' => 'ERROR', 'code' => 401,'message' => 'token is time out'];
            Yii::$app->response->data=json_encode($result);
            Yii::$app->response->send();
            Yii::$app->end();
        }
    }

以上是在写接口的时候发现的问题以及我这边的修改方式,供大家参考。若有不对,也请大家纠正,谢谢! 另外在写接口时在Controller中使用echo和exit也会出现跨域问题,建议使用return。

共收到 1 条回复
water#16年前 0 个赞

多谢,有点忙,明天细看一下

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics