wonderwong 2022-05-14 15:58:34 4029次浏览 1条评论 2 1 0
<?php
namespace app\controllers;
use yii\web\Controller;
use Yii;

/**
 * 核心的app类
 * Class AppController
 * @package app\controllers
 */
class AppController extends Controller {

    public $enableCsrfValidation = false;

    public $msg = ['status' => 0,'msg' => '数据请求失败','data'=>[]];

    /**
     * 初始化的数据程序
     */
    public function init()
    {
        //parent::init();

        $this->setHeader();

        if(YII_DEBUG == false){
            $this->checkSign();
            $this->checkLogin();
        }
        
    }

    /**
     * 设置头部信息
     */
    protected function setHeader()
    {
        Yii::$app->response->headers->set('Access-Control-Allow-Origin','*');
        //Yii::$app->response->headers->set("Access-Control-Allow-Headers", "Content-Type,token");
        Yii::$app->response->headers->set("Access-Control-Allow-Methods", "POST,GET");
    }

    /**
     * 检测请求的签名
     * @return \yii\web\Response
     */
    public function checkSign()
    {
        $appid = Yii::$app->request->headers->get('appid');
        $reqtime = Yii::$app->request->headers->get('reqtime');
        $sign = Yii::$app->request->headers->get('sign');

        if(empty($appid)){
            $this->msg['code'] = 10000;
            $this->msg['msg'] = '应用id不能为空';
            Yii::$app->end(0,$this->asJson($this->msg));
        }

        if(empty($reqtime)){
            $this->msg['code'] = 10001;
            $this->msg['msg'] = '当前的请求时间不能为空';
            Yii::$app->end(0,$this->asJson($this->msg));
        }

		$expirsetime = time()- $reqtime;
        if($expirsetime < 0 || $expirsetime >3){
            $this->msg['code'] = 10002;
            $this->msg['msg'] = '请求已过期';
            Yii::$app->end(0,$this->asJson($this->msg));
        }

        if(empty($sign)){
            $this->msg['code'] = 10003;
            $this->msg['msg'] = '应用签名不能为空';
            Yii::$app->end(0,$this->asJson($this->msg));
        }
        $signString = 'appid='.$appid.'&reqtime='.$reqtime.'&key=123456';
        $mySign = md5($signString);
        if($sign !==$mySign){
            $this->msg['code'] = 10004;
            $this->msg['msg'] = '非法请求';
            $this->msg['data'] = ['sign'=>$sign,'mySign'=>$mySign];
            Yii::$app->end(0,$this->asJson($this->msg));
        }

    }

    /**
     * 检查用户是否登陆
     * @throws \yii\base\ExitException
     */
    protected function checkLogin()
    {
        $token = Yii::$app->request->headers->get('token');

        if(empty($token)){
            $this->msg['code'] = 10005;
            $this->msg['msg'] = 'token 不能为空';
            Yii::$app->end(0,$this->asJson($this->msg));
        }

        $userSessionInfo = Yii::$app->session->readSession($token);
        $userSessionInfo = json_decode($userSessionInfo,true);
        if(empty($userSessionInfo)){
            $this->msg['code'] = 10006;
            $this->msg['msg'] = '登录的时间超时,请重新登录';
            Yii::$app->end(0,$this->asJson($this->msg));
        }
    }

    public function getUid()
    {
        $token = Yii::$app->request->headers->get('token');
        $userSessionInfo = Yii::$app->session->readSession($token);
        $userSessionInfo = json_decode($userSessionInfo,true);

        return $userSessionInfo;
    }
}
觉得很赞
  • 评论于 2023-09-20 18:32 举报

    1695205811263.png
    1695205675267.png
    按照你的这个代码写法,返回得是 error Attempt to assign property "format" on string
    是不是需要在main文件里面配置这个呢
    'response' => [

           'format' => yii\web\Response::FORMAT_JSON,
            'charset' => 'UTF-8',
            // ...
        ],
    
    1 条回复
    评论于 2024-04-29 18:16 回复

    现在才看到,你这个返回数据不是数组 ,是字符串。

您需要登录后才可以评论。登录 | 立即注册