<?php
/**
 * Author: luzheng.liu
 * Time: 2020/8/26 16:08
 */

namespace app\common\until;


use think\Response;
use think\Validate;

class Until {

    /**
     * @param array $data
     * @param int $code
     * @param string $message
     */
    public static function output(array $data = [],string $message = 'success',int $code = Enum::SUCCESS_CODE) {
        $re = [
            'code' => $code,
            'message' => $message,
            'data' => $data
        ];
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($re);
        die;
    }

    /**
     * @param string $showMsg
     * @param string $systemErrorMsg
     * @param array $data
     * @param int $code
     */
    public static function outputSystemError(string $showMsg = '',string $systemErrorMsg = '', $data = [], $code = Enum::THROW_ERR_CODE) {
        $output = ['code' => $code, 'msg' => $showMsg, 'systemErrorMsg' => $systemErrorMsg, 'data' => $data];
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($output);
        die;
    }

    public static function modelToArray($data):array {
        if (empty($data)) {
            return [];
        }
        return json_decode(json_encode($data), true);
    }

    public static function check(array $rule, array $data) {
        $validate = new Validate();
        if (!$validate->check($data, $rule)) {
            self::output([],(string)$validate->getError(),Enum::THROW_ERR_CODE);
        }
    }

    public static function getInput(): array {
        $input = file_get_contents("php://input");
        return json_decode($input, true);
    }

}