<?php

namespace app\controller\api;

use app\controller\BaseController;
use think\facade\Config;
use think\response\Json;

/**
 * API控制器基础类
 */
class Base extends BaseController
{
    // 布尔值数字关系
    public const BOOL_FALSE = 0;
    public const BOOL_TRUE  = 1;

    protected function initialize()
    {
        parent::initialize();

        $this->middleware = [
            'jwt',
            'apiLogin' => ['except' => $this->noNeedLogin]
        ];
        Config::load("extra/wechat","wechat");
    }

    public function __call($method, $args)
    {
        return $this->json(4004, 'error request!');
    }

    /**
     * 返回封装后的API数据到客户端(对返回内容进行null转空字符串过滤)
     * 以json格式抛出异常
     * @access protected
     * @param  integer  $code  返回的code
     * @param  mixed  $msg  提示信息
     * @param  mixed  $data  要返回的数据
     * @return Json
     */
    protected function json(int $code = 0, $msg = '操作成功', $data = []): Json
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'data' => arrayNullToString($data)
        ];
        return json($result);
    }

    /**
     * 取消程序运行时间和内存限制
     * max_execution_time PHP程序最大执行时间限制(单位:秒)
     * memory_limit 此次的最大运行内存 (单位:字节Byte)
     * set_time_limit 当前操作最长执行时间限制(单位:秒)
     */
    protected function cancelTimeLimit(int $maxExecTime = 0, int $memory = -1, int $timeOut = 0)
    {
        ini_set('max_execution_time', $maxExecTime);
        ini_set("memory_limit", $memory);
        set_time_limit($timeOut);
    }

}