123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace app\api\controller;
- use think\Controller;
- /**
- * 接口父类
- */
- class Api extends Controller{
- protected function _initialize(){
- $this->header();
- $this->init();
- }
- protected function header(){
- header('Access-Control-Allow-Origin:*');
- header('Content-type: application/json;chartset=uft-8');
- if( $_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
- exit;
- }
- }
- protected function init(){
-
- }
- public function output($code,$msg,$data = [],$count = 0){
- try {
- // 返回JSON数据格式到客户端 包含状态信息
- $count = $count == 0?count($data):$count;
- $data = json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data,'count'=>$count], JSON_UNESCAPED_UNICODE);
- if ($data === false) {
- throw new \InvalidArgumentException(json_last_error_msg());
- }
- header('Content-type: application/json');
- echo $data;exit();
- } catch (\Exception $e) {
- if ($e->getPrevious()) {
- throw $e->getPrevious();
- }
- throw $e;
- }
- }
- }
|