{"id":63,"date":"2015-09-18T12:41:45","date_gmt":"2015-09-18T04:41:45","guid":{"rendered":"http:\/\/blog.inforere.com\/?p=63"},"modified":"2017-06-29T18:23:06","modified_gmt":"2017-06-29T10:23:06","slug":"%e6%9e%81%e7%ae%80%e7%9a%84api%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/blog.inforere.com\/?p=63","title":{"rendered":"\u6781\u7b80\u7684Api\u5904\u7406"},"content":{"rendered":"<p>\u6839\u636e\u8bf7\u6c42\u7684api\u83b7\u53d6class, \u5373\u9700\u8981\u4e00\u4e2aApi class loader, \u00a0api\u683c\u5f0f\u5982 xxx.xxx.xx<\/p>\n<pre class=\"lang:default decode:true\">\/**\r\n     * @param $api\r\n     * @param string $oldApi\r\n     * @return Base\r\n     * @throws Exception\r\n     *\/\r\n    function getClass($api, $oldApi = ''){\r\n        if(isset(self::$classCaches[$api])){\r\n            return self::$classCaches[$api];\r\n        }\r\n\r\n        if(!$oldApi){\r\n            $oldApi = $api;\r\n        }\r\n\r\n        if(isset($this-&gt;apiMap[$api])){\r\n            return new $this-&gt;apiMap[$api];\r\n        }\r\n\r\n        $path = explode('.', $api);\r\n        $className = array_pop($path);\r\n        $apiClass = $this-&gt;nsPrefix . ($path ? (implode('\\\\', $path)) . \"\\\\\" : '') . ucfirst($className);\r\n        if(!class_exists($apiClass)){\r\n            if($path){\r\n                return $this-&gt;getClass(implode('.', $path), $oldApi);\r\n            }else{\r\n                throw new Exception(\"Invalid api({$api})\", Exception::API);\r\n            }\r\n        }\r\n\r\n        if($api != $oldApi)\r\n            $action = str_replace($api . '.', '', $oldApi);\r\n        else {\r\n            $action = '';\r\n        }\r\n\r\n        \/** @var Base $class *\/\r\n        $class = new $apiClass();\r\n        $class-&gt;setAction($action);\r\n        self::$classCaches[$api] = $class;\r\n        return $class;\r\n    }<\/pre>\n<p>\u9700\u8981\u4e00\u4e2aapi \u7684\u57fa\u7c7b\uff0c\u6839\u636e\u8bf7\u6c42\u53c2\u6570\u8fd4\u56de\u7ed3\u679c<\/p>\n<pre class=\"lang:default decode:true \">    public function handle($args, $request = []){\r\n        if($this-&gt;action){\r\n            $method = str_replace('.', '_', $this-&gt;action);\r\n            if($this-&gt;action != 'handle' &amp;&amp; method_exists($this, $method)){\r\n                return call_user_func(array($this, $method), $args);\r\n            }\r\n\r\n            if(method_exists($this, 'getModel')){\r\n                $model = $this-&gt;getModel();\r\n                if(method_exists($model, $method)){\r\n                    return call_user_func_array(array($model, $method), $args);\r\n                }\r\n            }\r\n\r\n        } else if(method_exists($this, 'execute')){\r\n            return call_user_func(array($this, 'execute'), $args);\r\n        }\r\n\r\n        throw new Exception(\"Method not found\");\r\n    }<\/pre>\n<p>\u5f04\u4e2ajs\u5ba2\u6237\u7aef<\/p>\n<pre class=\"lang:default decode:true \">    apiUrl : 'http:\/\/api.' + location.host + '\/index.php?q=%action%&amp;callback=?',\r\n    api : function(api, args){\r\n        if(!args){\r\n            args = {};\r\n        }\r\n        var deferred = $.Deferred();\r\n        var url = this.apiUrl.replace(\/%action%\/, api);\r\n        $.getJSON(url, args, function(response){\r\n                if(jc001.papi.is_succ(response)){\r\n                    deferred.resolve(response.data);\r\n                } else {\r\n                    deferred.reject(response);\r\n                }\r\n            }\r\n        );\r\n        return deferred;\r\n    }<\/pre>\n<pre class=\"lang:default decode:true\">    papi.api('comm.region.name', 2747).done(function(rs){\r\n        console.log(rs);\r\n    }).fail(function(rs){\r\n        console.log(rs);\r\n    });<\/pre>\n<p>\u63a5\u53e3\u6587\u6863\u53caweb\u63a5\u53e3\u6d4b\u8bd5<\/p>\n<p><a href=\"http:\/\/blog.inforere.com\/wp-content\/uploads\/2015\/09\/1233.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-70\" src=\"http:\/\/blog.inforere.com\/wp-content\/uploads\/2015\/09\/1233-1024x303.png\" alt=\"1233\" width=\"660\" height=\"195\" srcset=\"https:\/\/blog.inforere.com\/wp-content\/uploads\/2015\/09\/1233-1024x303.png 1024w, https:\/\/blog.inforere.com\/wp-content\/uploads\/2015\/09\/1233-300x89.png 300w, https:\/\/blog.inforere.com\/wp-content\/uploads\/2015\/09\/1233.png 1059w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><\/p>\n<p>\u52a0\u4e00\u4e2a\u63a5\u53e3\u65e5\u5fd7\u53ca\u7edf\u8ba1\u6536\u5de5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6839\u636e\u8bf7\u6c42\u7684api\u83b7\u53d6class, \u5373\u9700\u8981\u4e00\u4e2aApi class loader, \u00a0api\u683c\u5f0f\u5982 xxx.xxx [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,41],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/63"}],"collection":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63"}],"version-history":[{"count":8,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}