{"id":185395,"date":"2023-08-07T14:30:14","date_gmt":"2023-08-07T06:30:14","guid":{"rendered":"http:\/\/www.idc.net\/help\/185395\/"},"modified":"2023-08-07T14:30:14","modified_gmt":"2023-08-07T06:30:14","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%95%99%e7%a8%8b%ef%bc%9a%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f-api","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/185395\/","title":{"rendered":"\u540e\u6d6a\u4e91\u5c0f\u7a0b\u5e8f\u6559\u7a0b\uff1a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f API"},"content":{"rendered":"<h2>API<\/h2>\n<p>\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\u63d0\u4f9b\u4e30\u5bcc\u7684\u5fae\u4fe1\u539f\u751f API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u8c03\u8d77\u5fae\u4fe1\u63d0\u4f9b\u7684\u80fd\u529b\uff0c\u5982\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u672c\u5730\u5b58\u50a8\uff0c\u652f\u4ed8\u529f\u80fd\u7b49\u3002\u8be6\u7ec6\u4ecb\u7ecd\u8bf7\u53c2\u8003&nbsp;API \u6587\u6863\u3002<\/p>\n<p>\u901a\u5e38\uff0c\u5728\u5c0f\u7a0b\u5e8f API \u6709\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\uff1a<\/p>\n<h2>\u4e8b\u4ef6\u76d1\u542c API<\/h2>\n<p>\u6211\u4eec\u7ea6\u5b9a\uff0c\u4ee5&nbsp;on&nbsp;\u5f00\u5934\u7684 API \u7528\u6765\u76d1\u542c\u67d0\u4e2a\u4e8b\u4ef6\u662f\u5426\u89e6\u53d1\uff0c\u5982\uff1awx.onSocketOpen\uff0cwx.onCompassChange&nbsp;\u7b49\u3002<\/p>\n<p>\u8fd9\u7c7b API \u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\u4f1a\u8c03\u7528\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5c06\u76f8\u5173\u6570\u636e\u4ee5\u53c2\u6570\u5f62\u5f0f\u4f20\u5165\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b<\/p>\n<pre><code>wx.onCompassChange(function (res) {\n  console.log(res.direction)\n})\n<\/code><\/pre>\n<h2>\u540c\u6b65 API<\/h2>\n<p>\u6211\u4eec\u7ea6\u5b9a\uff0c\u4ee5&nbsp;Sync&nbsp;\u7ed3\u5c3e\u7684 API \u90fd\u662f\u540c\u6b65 API\uff0c \u5982&nbsp;wx.setStorageSync\uff0cwx.getSystemInfoSync&nbsp;\u7b49\u3002\u6b64\u5916\uff0c\u4e5f\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u540c\u6b65 API\uff0c\u5982&nbsp;wx.createWorker\uff0cwx.getBackgroundAudioManager&nbsp;\u7b49\uff0c\u8be6\u60c5\u53c2\u89c1 API \u6587\u6863\u4e2d\u7684\u8bf4\u660e\u3002<\/p>\n<p>\u540c\u6b65 API \u7684\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u8fd4\u56de\u503c\u76f4\u63a5\u83b7\u53d6\uff0c\u5982\u679c\u6267\u884c\u51fa\u9519\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b<\/p>\n<pre><code>try {\n  wx.setStorageSync('key', 'value')\n} catch (e) {\n  console.error(e)\n}\n<\/code><\/pre>\n<h2>\u5f02\u6b65 API<\/h2>\n<p>\u5927\u591a\u6570 API \u90fd\u662f\u5f02\u6b65 API\uff0c\u5982&nbsp;wx.request\uff0cwx.login&nbsp;\u7b49\u3002\u8fd9\u7c7b API \u63a5\u53e3\u901a\u5e38\u90fd\u63a5\u53d7\u4e00\u4e2a&nbsp;Object&nbsp;\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u90fd\u652f\u6301\u6309\u9700\u6307\u5b9a\u4ee5\u4e0b\u5b57\u6bb5\u6765\u63a5\u6536\u63a5\u53e3\u8c03\u7528\u7ed3\u679c\uff1a<\/p>\n<p>Object \u53c2\u6570\u8bf4\u660e<\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570\u540d<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>success<\/td>\n<td>function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>fail<\/td>\n<td>function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>complete<\/td>\n<td>function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u5176\u4ed6<\/td>\n<td>Any<\/td>\n<td>-<\/td>\n<td>\u63a5\u53e3\u5b9a\u4e49\u7684\u5176\u4ed6\u53c2\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570<\/p>\n<p>success\uff0cfail\uff0ccomplete&nbsp;\u51fd\u6570\u8c03\u7528\u65f6\u4f1a\u4f20\u5165\u4e00\u4e2a&nbsp;Object&nbsp;\u7c7b\u578b\u53c2\u6570\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5c5e\u6027<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>errMsg<\/td>\n<td>string<\/td>\n<td>\u9519\u8bef\u4fe1\u606f\uff0c\u5982\u679c\u8c03\u7528\u6210\u529f\u8fd4\u56de&nbsp;<code>${apiName}:ok<\/code> <\/td>\n<\/tr>\n<tr>\n<td>errCode<\/td>\n<td>number<\/td>\n<td>\u9519\u8bef\u7801\uff0c\u4ec5\u90e8\u5206 API \u652f\u6301\uff0c\u5177\u4f53\u542b\u4e49\u8bf7\u53c2\u8003\u5bf9\u5e94 API \u6587\u6863\uff0c\u6210\u529f\u65f6\u4e3a&nbsp;<code>0<\/code>\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5176\u4ed6<\/td>\n<td>Any<\/td>\n<td>\u63a5\u53e3\u8fd4\u56de\u7684\u5176\u4ed6\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f02\u6b65 API \u7684\u6267\u884c\u7ed3\u679c\u9700\u8981\u901a\u8fc7&nbsp;Object&nbsp;\u7c7b\u578b\u7684\u53c2\u6570\u4e2d\u4f20\u5165\u7684\u5bf9\u5e94\u56de\u8c03\u51fd\u6570\u83b7\u53d6\u3002\u90e8\u5206\u5f02\u6b65 API \u4e5f\u4f1a\u6709\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5982&nbsp;wx.request\uff0cwx.connectSocket&nbsp;\u7b49\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b<\/p>\n<pre><code>wx.login({\n  success(res) {\n    console.log(res.code)\n  }\n})\n<\/code><\/pre>\n<h2>\u5f02\u6b65 API \u8fd4\u56de Promise<\/h2>\n<p>\u57fa\u7840\u5e93&nbsp;2.10.2&nbsp;\u7248\u672c\u8d77\uff0c\u5f02\u6b65 API \u652f\u6301 callback &amp; promise \u4e24\u79cd\u8c03\u7528\u65b9\u5f0f\u3002\u5f53\u63a5\u53e3\u53c2\u6570 Object \u5bf9\u8c61\u4e2d\u4e0d\u5305\u542b success\/fail\/complete \u65f6\u5c06\u9ed8\u8ba4\u8fd4\u56de promise\uff0c\u5426\u5219\u4ecd\u6309\u56de\u8c03\u65b9\u5f0f\u6267\u884c\uff0c\u65e0\u8fd4\u56de\u503c\u3002<\/p>\n<h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<ol>\n<li>\u90e8\u5206\u63a5\u53e3\u5982&nbsp;downloadFile,&nbsp;request,&nbsp;uploadFile,&nbsp;connectSocket,&nbsp;createCamera\uff08\u5c0f\u6e38\u620f\uff09\u672c\u8eab\u5c31\u6709\u8fd4\u56de\u503c\uff0c \u5b83\u4eec\u7684 promisify \u9700\u8981\u5f00\u53d1\u8005\u81ea\u884c\u5c01\u88c5\u3002<\/li>\n<li>\u5f53\u6ca1\u6709\u56de\u8c03\u53c2\u6570\u65f6\uff0c\u5f02\u6b65\u63a5\u53e3\u8fd4\u56de promise\u3002\u6b64\u65f6\u82e5\u51fd\u6570\u8c03\u7528\u5931\u8d25\u8fdb\u5165 fail \u903b\u8f91\uff0c \u4f1a\u62a5\u9519\u63d0\u793a&nbsp;Uncaught (in promise)\uff0c\u5f00\u53d1\u8005\u53ef\u901a\u8fc7 catch \u6765\u8fdb\u884c\u6355\u83b7\u3002<\/li>\n<li>wx.onUnhandledRejection&nbsp;\u53ef\u4ee5\u76d1\u542c\u672a\u5904\u7406\u7684 Promise \u62d2\u7edd\u4e8b\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u793a\u4f8b<\/p>\n<pre><code>\/\/ callback \u5f62\u5f0f\u8c03\u7528\nwx.chooseImage({\n  success(res) {\n    console.log('res:', res)\n  }\n})\n\n\/\/ promise \u5f62\u5f0f\u8c03\u7528\nwx.chooseImage().then(res =&gt; console.log('res: ', res))<\/code><\/pre>\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>API \u5c0f\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\u63d0\u4f9b\u4e30\u5bcc\u7684\u5fae\u4fe1\u539f\u751f API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u8c03\u8d77\u5fae\u4fe1\u63d0\u4f9b\u7684\u80fd\u529b\uff0c\u5982\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u672c\u5730\u5b58\u50a8\uff0c\u652f\u4ed8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-185395","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=185395"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185395\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=185395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=185395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=185395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}