首页 各类教程

首先

application目录下创建单独文件夹exception
在exception目录下创建controller
在controller下创建ApiHandleException.php


之后

把下面代码仍ApiHandleException.php里面


?php
 
namespace appexceptioncontroller;
 
use appSendWxtempMsg;
use thinkexceptionHandle;
 
Class ApiHandleException extends Handle
{
 
    /**
     * http状态码
     */
    public $httpCode = 500;
 
    public function render(Exception $e)
    {
        return $this->show($e,$e->getMessage(), $this->httpCode);
    }
 
 
    /**
    通用化API接口数据输出
    @param int $status 业务状态码
    @param string $message 信息提示
    @param int $httpCode http状态码
     */
    public function show($e,$message ,$httpCode = 200)
    {
        $data =  [
            'message' => $message,
        ];
        return json($data, $httpCode);
         //下边代码为微信公众帐号模板消息推送 
        /*$ip = request()->ip();
 
        $url = 'http://apis.juhe.cn/ip/ipNew?key=xxxxxxxx&ip='.$ip;
 
        $http = $this->httpGet($url);
 
        $result = json_decode($http,true);
 
        $template = array(
            "touser" => 'xxxx',
            'url'=> 'http://www.xxx.com/exception/errors/index?error='.$e,
            "template_id" => "xxxx",
            'topcolor' => '#ccc',
            'data' => array('first' => array('value' => "xxx报错了",
                'color' => "#101BF4",),
                'keyword1' => array('value' => 'xxx系统',
                    'color' => '#743A3A'
                ),
                'keyword2' => array('value' => $message ,
                    'color' => '#743A3A'
                ),
                'keyword3' => array('value' => '',
                    'color' => '#743A3A'
                ),
                'keyword4' => array('value' => date('Y-m-d H:i:s',time()),
                    'color' => '#743A3A'
                ),
                'remark' => array('value' =>$result['result']['Country'].'|'.$result'result'.'|'.$result['result']['City'].'|'.$result'result',
                    'color' => '#3A16EE'
                ),
            )
        );
          $SendWxtempMsg = new SendWxtempMsg();
 
          $SendWxtempMsg->api_notice_increment($template);*/
 
        
    }
    public function httpGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}

最终

在config里配置appexceptioncontrollerApiHandleException
路径



本站已关闭评论
如有事请联系QQ:24677102
微信:phpxuan