<?php
namespace app\hander;

class HelpHander
{

    public static function Response($code, $msg, $data = [])
    {
        $type = gettype($data);
        if($type == 'object'){
            $data = json_decode(json_encode($data),true);
        }
        if(is_array($data)){
            $data = array_change_line_to_hump($data);
        }
        $json = [
            'code' => $code,
            'message' => $msg,
            'data' => $data
        ];
        throw new \think\exception\HttpResponseException(json($json));
        return;
    }

    public static function success($data = [], $msg = '')
    {
        $type = gettype($data);
        if($type == 'object'){
            $data = json_decode(json_encode($data),true);
        }
        if(is_array($data)){
            $data = array_change_line_to_hump($data);
        }
        $json = [
            'code' => 0,
            'message' => $msg,
            'data' => $data
        ];
        throw new \think\exception\HttpResponseException(json($json));
        return;
    }

    public static function error($msg, $code = 1, $data =null)
    {
        $code = $code == 0 ? 1 : $code; // code不能等于0
        $type = gettype($data);
        if($type == 'object'){
            $data = json_decode(json_encode($data),true);
        }
        if(is_array($data)){
            $data = array_change_line_to_hump($data);
        }
        $json = [
            'code' => $code,
            'message' => $msg,
            'data' => $data
        ];
        throw new \think\exception\HttpResponseException(json($json));
        return;
    }
}