一段没有多大意义的操作
直接上代码
<?php namespace api\controllers; use Yii; use yii\web\Controller; use yii\rest\ActiveController; /** * ActivityPaperCouponController implements the CRUD actions for ActivityPaperCoupon model. */ class BaseController extends Controller { private $secret = 'acec7ebb2b38baa272e2ae102db78fa3';//md5('your key') private $key = 'yourKey'; /** * @var int * @desc 接口延时 */ private $delayed = 5; /** * @desc 返回状态码 */ const FORBIDDEN = 43; const TIME_OUT = 44; const PARAM_MISS = 45; const OK = 200; const NOT_FOUND = 404; const NULL = 400; const UNKNOWN = 500; public function init(){ header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: x-requested-with,content-type"); header('Access-Control-Allow-Methods:POST,GET'); \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $this->checkTimeOut(); } /** * @desc 检测接口时间戳是否过期 */ private function checkTimeOut(){ $timeStamp = Yii::$app->request->get('timestamp'); if(empty($timeStamp)){ echo json_encode([ 'message' => '缺少timeStamp参数', 'code' => self::PARAM_MISS, ]); exit; } //使用当前时间戳最后一位作为延时 $this->delayed = intval(substr(time(), -1, 1)); $timeDifference = time() - $timeStamp; if($timeDifference > $this->delayed || $timeDifference < 0){ echo json_encode([ 'message' => '超时', 'code' => self::TIME_OUT, ]); exit; } } /** * @desc 暂时没写 */ public function valid(){ $secret = Yii::$app->request->get('secret'); $key = Yii::$app->request->get('key'); $sign = Yii::$app->request->get('sign'); $data = array($secret, $key, $sign); sort($data, SORT_STRING); $validString = ''; foreach ($data as $v) { $validString .= $v ; } return $validString; } } <?php namespace api\controllers; use Yii; use api\models\StudyCourse; use yii\web\Controller; use yii\rest\ActiveController; use api\controllers\BaseController; /** * StudyCourseController implements the CRUD actions for StudyCourse model. */ class StudyCourseController extends BaseController { /** * Lists all StudyCourse models. * @return mixed */ public function actionGetCourseTypes() { $types = StudyCourse::allCourseType(); return [ 'message' => $types, 'code' => self::OK, ]; } public function actionCourseList(){ $type = Yii::$app->request->get('type'); $list = StudyCourse::getCourseList($type, StudyCourse::PAGESIZE); return [ 'message' => $list, 'code' => self::OK, ]; } } #js var myDate = new Date(); var timestamp = myDate.getTime();//时间戳 var timestamp = timestamp.toString().substring(0,10); let self = this; let type = self.$route.params.type //获取文章的类型 axios.get('http://backtest.jumoshen.cn/study-course/course-list.html', { params: { type: type, page: p, timestamp: timestamp } })