<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2019/12/5
 * Time: 17:44
 */

namespace app\api\controller\base;

use app\common\service\WebService;
use think\Controller;

/**
 * 接口父类
 * Class Api
 * @package app\api\controller\base
 */
class Base extends Controller
{
    protected function _initialize()
    {
        (new WebService())->checkInstalled();
        header('Access-Control-Allow-Origin: *');//允许跨域,*星号表示所有的域都可以接受
        header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, x-token');//允许访问的header
    }

    /**
     * api成功的响应
     * 和success方法相比，没有显示跳转页面，直接响应json
     * @param $msg string [提示消息]
     * @param $data null [响应数据]
     * @param int $code
     */
    protected function json_success($msg = "success", $data = null, $code = 1)
    {
        //和success方法返回的code一致，这样后台ajax不用修改就能兼容
        $this->result($data, $code, $msg, 'json');
    }

    /**
     * api失败的响应,可定义错误代码
     * @param $msg [错误消息]
     * @param int $code [自定义错误码,不可为1]
     * @param null $data [响应数据]
     */
    protected function json_error($msg = "error", $data = null, $code = 0)
    {
        $this->result($data, $code, $msg, 'json');
    }

}
