{"id":31481,"date":"2024-11-25T14:34:00","date_gmt":"2024-11-25T06:34:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31481\/"},"modified":"2024-11-25T14:34:00","modified_gmt":"2024-11-25T06:34:00","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8koa2%e5%bc%80%e5%8f%91%e5%be%ae%e4%bf%a1%e4%ba%8c%e7%bb%b4%e7%a0%81%e6%89%ab%e7%a0%81%e6%94%af%e4%bb%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31481\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\uff0c\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u524d\u6bb5\u65f6\u95f4\u5728\u5f00\u53d1\u4e00\u4e2a\u529f\u80fd\uff0c\u8981\u6c42\u662f\u901a\u8fc7\u5fae\u4fe1\u4e8c\u7ef4\u7801\u8fdb\u884c\u626b\u7801\u652f\u4ed8\u3002\u8fd9\u4e2a\u60c5\u666f\u6211\u4eec\u5c61\u89c1\u4e0d\u9c9c\u4e86\uff0c\u5404\u79cd\u7535\u5b50\u5546\u57ce\u3001\u7ebf\u4e0b\u7684\u81ea\u52a8\u8d29\u5356\u673a\u7b49\u7b49\u90fd\u4f1a\u6709\u8fd9\u4e2a\u529f\u80fd\u3002\u5e73\u65f6\u53ea\u662f\u4f7f\u7528\u8005\uff0c\u5982\u4eca\u53d8\u4e3a\u5f00\u53d1\u8005\uff0c\u4e5f\u662f\u6709\u4e0d\u5c0f\u7684\u5751\u3002\u6240\u4ee5\u7279\u6b64\u5199\u4e00\u7bc7\u535a\u5ba2\u8bb0\u5f55\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u6ce8\uff1a \u8981\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u652f\u4ed8\uff0c\u4f60\u5fc5\u987b\u8981\u6709\u76f8\u5e94\u7684\u5546\u6237\u53f7\u7684\u6743\u9650\uff0c\u5426\u5219\u4f60\u662f\u65e0\u6cd5\u5f00\u53d1\u7684\u3002\u82e5\u65e0\u76f8\u5e94\u6743\u9650\uff0c\u672c\u6587\u4e0d\u63a8\u8350\u9605\u8bfb\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u4e24\u79cd\u6a21\u5f0f<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u6253\u5f00\u5fae\u4fe1\u652f\u4ed8\u7684\u6587\u6863\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e24\u79cd\u652f\u4ed8\u6a21\u5f0f\uff1a\u6a21\u5f0f\u4e00\u548c\u6a21\u5f0f\u4e8c\u3002\u8fd9\u4e8c\u8005\u7684\u6d41\u7a0b\u56fe\u5fae\u4fe1\u7684\u6587\u6863\u91cc\u90fd\u7ed9\u51fa\u4e86\uff08\u4e0d\u8fc7\u8bf4\u5b9e\u8bdd\u753b\u5f97\u771f\u7684\u6709\u70b9\u4e11\uff09\u3002<\/p>\n<p style=\"text-align: left\">\u6587\u6863\u91cc\u6307\u51fa\u4e86\u4e8c\u8005\u7684\u533a\u522b\uff1a<\/p>\n<p style=\"text-align: left\">\u6a21\u5f0f\u4e00\u5f00\u53d1\u524d\uff0c\u5546\u6237\u5fc5\u987b\u5728\u516c\u4f17\u5e73\u53f0\u540e\u53f0\u8bbe\u7f6e\u652f\u4ed8\u56de\u8c03URL\u3002URL\u5b9e\u73b0\u7684\u529f\u80fd\uff1a\u63a5\u6536\u7528\u6237\u626b\u7801\u540e\u5fae\u4fe1\u652f\u4ed8\u7cfb\u7edf\u56de\u8c03\u7684productid\u548copenid\u3002<\/p>\n<p style=\"text-align: left\">\u6a21\u5f0f\u4e8c\u4e0e\u6a21\u5f0f\u4e00\u76f8\u6bd4\uff0c\u6d41\u7a0b\u66f4\u4e3a\u7b80\u5355\uff0c\u4e0d\u4f9d\u8d56\u8bbe\u7f6e\u7684\u56de\u8c03\u652f\u4ed8URL\u3002\u5546\u6237\u540e\u53f0\u7cfb\u7edf\u5148\u8c03\u7528\u5fae\u4fe1\u652f\u4ed8\u7684\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\uff0c\u5fae\u4fe1\u540e\u53f0\u7cfb\u7edf\u8fd4\u56de\u94fe\u63a5\u53c2\u6570code_url\uff0c\u5546\u6237\u540e\u53f0\u7cfb\u7edf\u5c06code_url\u503c\u751f\u6210\u4e8c\u7ef4\u7801\u56fe\u7247\uff0c\u7528\u6237\u4f7f\u7528\u5fae\u4fe1\u5ba2\u6237\u7aef\u626b\u7801\u540e\u53d1\u8d77\u652f\u4ed8\u3002\u6ce8\u610f\uff1acode_url\u6709\u6548\u671f\u4e3a2\u5c0f\u65f6\uff0c\u8fc7\u671f\u540e\u626b\u7801\u4e0d\u80fd\u518d\u53d1\u8d77\u652f\u4ed8\u3002<\/p>\n<p style=\"text-align: left\">\u6a21\u5f0f\u4e00\u662f\u6211\u4eec\u5e73\u65f6\u5728\u7f51\u8d2d\u7684\u65f6\u5019\u6bd4\u8f83\u5e38\u89c1\u7684\uff0c\u4f1a\u5f39\u51fa\u4e00\u4e2a\u4e13\u95e8\u7684\u9875\u9762\u7528\u4e8e\u626b\u7801\u652f\u4ed8\uff0c\u7136\u540e\u652f\u4ed8\u6210\u529f\u540e\u8fd9\u4e2a\u9875\u9762\u4f1a\u518d\u6b21\u8df3\u8f6c\u56de\u56de\u8c03\u9875\u9762\uff0c\u901a\u77e5\u4f60\u652f\u4ed8\u6210\u529f\u3002\u7b2c\u4e8c\u79cd\u7684\u8bdd\u60f3\u5bf9\u5c11\u4e00\u4e9b\uff0c\u4e0d\u8fc7\u7b2c\u4e8c\u79cd\u5f00\u53d1\u8d77\u6765\u76f8\u5bf9\u7b80\u5355\u70b9\u3002 <strong>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u6a21\u5f0f\u4e8c\u7684\u5f00\u53d1<\/strong> \u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u642d\u5efaKoa2\u7684\u7b80\u5355\u5f00\u53d1\u73af\u5883<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u5feb\u901f\u642d\u5efaKoa2\u7684\u5f00\u53d1\u73af\u5883\u6211\u63a8\u8350\u53ef\u4ee5\u4f7f\u7528koa-generator \u3002\u811a\u624b\u67b6\u80fd\u5e2e\u6211\u4eec\u7701\u53bbKoa\u9879\u76ee\u4e00\u5f00\u59cb\u7684\u4e00\u4e9b\u57fa\u672c\u4e2d\u95f4\u4ef6\u7684\u4e66\u5199\u6b65\u9aa4\u3002\uff08\u5982\u679c\u4f60\u60f3\u5b66\u4e60Koa\u6700\u597d\u81ea\u5df1\u642d\u5efa\u4e00\u4e2a\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u4f1aKoa\u4e86\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5feb\u901f\u811a\u624b\u67b6\u4e86\u3002\uff09<\/p>\n<p style=\"text-align: left\">\u9996\u5148\u5168\u5c40\u5b89\u88c5 koa-generator \uff1a<\/p>\n<pre>npm install -g koa-generator\r\n#or\r\nyarn global add koa-generator<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u627e\u4e00\u4e2a\u76ee\u5f55\u7528\u6765\u5b58\u653eKoa\u9879\u76ee\uff0c\u6211\u4eec\u6253\u7b97\u7ed9\u8fd9\u4e2a\u9879\u76ee\u53d6\u4e2a\u540d\u5b57\u53eb\u505a koa-wechatpay \uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8f93\u5165 koa2 koa-wechatpay \u3002\u7136\u540e\u811a\u624b\u67b6\u4f1a\u81ea\u52a8\u521b\u5efa\u76f8\u5e94\u6587\u4ef6\u5939 koa-wechatpay \uff0c\u5e76\u751f\u6210\u57fa\u672c\u9aa8\u67b6\u3002\u8fdb\u5165\u8fd9\u4e2a\u6587\u4ef6\u5939\uff0c\u5b89\u88c5\u76f8\u5e94\u7684\u63d2\u4ef6\u3002\u8f93\u5165\uff1a<\/p>\n<pre>npm install\r\n#or\r\nyarn<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u63a5\u7740\u4f60\u53ef\u4ee5\u8f93\u5165 npm start \u6216\u8005 yarn start \u6765\u8fd0\u884c\u9879\u76ee\uff08\u9ed8\u8ba4\u76d1\u542c\u57283000\u7aef\u53e3\uff09\u3002<\/p>\n<p style=\"text-align: left\">\u5982\u679c\u4e0d\u51fa\u610f\u5916\uff0c\u4f60\u7684\u9879\u76ee\u8dd1\u8d77\u6765\u4e86\uff0c\u7136\u540e\u6211\u4eec\u7528postman\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n<p style=\"text-align: left\">\u8fd9\u6761\u8def\u7531\u662f\u5728 routes\/index.js \u91cc\u3002<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/30fcbc0e3b0a31f23a02e98a38e11ae0-0.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u63d2\u56fe\" \/><\/p>\n<p style=\"text-align: left\">\u5982\u679c\u4f60\u770b\u5230\u4e86<\/p>\n<pre>{\r\n \"title\": \"koa2 json\"\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5c31\u8bf4\u660e\u6ca1\u95ee\u9898\u3002\uff08\u5982\u679c\u6709\u95ee\u9898\uff0c\u68c0\u67e5\u4e00\u4e0b\u662f\u4e0d\u662f\u7aef\u53e3\u88ab\u5360\u7528\u4e86\u7b49\u7b49\u3002\uff09<\/p>\n<p style=\"text-align: left\">\u63a5\u4e0b\u6765\u5728 routes \u6587\u4ef6\u5939\u91cc\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a wechatpay.js \u7684\u6587\u4ef6\u7528\u6765\u4e66\u5199\u6211\u4eec\u7684\u6d41\u7a0b\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u7b7e\u540d<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u8ddf\u5fae\u4fe1\u7684\u670d\u52a1\u5668\u4ea4\u6d41\u5f88\u5173\u952e\u7684\u4e00\u73af\u662f\u7b7e\u540d\u5fc5\u987b\u6b63\u786e\uff0c\u5982\u679c\u7b7e\u540d\u4e0d\u6b63\u786e\uff0c\u90a3\u4e48\u4e00\u5207\u90fd\u767d\u642d\u3002<\/p>\n<p style=\"text-align: left\">\u9996\u5148\u6211\u4eec\u9700\u8981\u53bb\u516c\u4f17\u53f7\u7684\u540e\u53f0\u83b7\u53d6\u6211\u4eec\u6240\u9700\u8981\u7684\u5982\u4e0b\u76f8\u5e94\u7684id\u6216\u8005key\u7684\u4fe1\u606f\u3002\u5176\u4e2d notify_url \u548c server_ip \u662f\u7528\u4e8e\u5f53\u6211\u4eec\u652f\u4ed8\u6210\u529f\u540e\uff0c\u5fae\u4fe1\u4f1a\u4e3b\u52a8\u5f80\u8fd9\u4e2aurl post \u652f\u4ed8\u6210\u529f\u7684\u4fe1\u606f\u3002<\/p>\n<p style=\"text-align: left\">\u7b7e\u540d\u7b97\u6cd5\u5982\u4e0b\uff1apay.weixin.qq.com\/wiki\/doc\/api\/native.php?chapter=4_3<\/p>\n<p style=\"text-align: left\">\u4e3a\u4e86\u7b7e\u540d\u6b63\u786e\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e0b md5 \u3002<\/p>\n<pre>npm install md5 --save\r\n#or\r\nyarn add md5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>const md5 = require('md5')\r\nconst appid = 'xxx'\r\nconst mch_id = 'yyy'\r\nconst mch_api_key = 'zzz'\r\nconst notify_url = 'http:\/\/xxx\/api\/notify' \/\/ \u670d\u52a1\u7aef\u53ef\u8bbf\u95ee\u7684\u57df\u540d\u548c\u63a5\u53e3\r\nconst server_ip = 'xx.xx.xx.xx' \/\/ \u670d\u52a1\u7aef\u7684ip\u5730\u5740\r\nconst trade_type = 'NATIVE' \/\/ NATIVE\u5bf9\u5e94\u7684\u662f\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\r\nlet body = 'XXX\u7684\u5145\u503c\u652f\u4ed8' \/\/ \u7528\u4e8e\u663e\u793a\u5728\u652f\u4ed8\u754c\u9762\u7684\u63d0\u793a\u8bcd<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u5f00\u59cb\u5199\u7b7e\u540d\u51fd\u6570\uff1a<\/p>\n<pre>const signString = (fee, ip, nonce) =&gt; {\r\n let tempString = `appid=${appid}&amp;body=${body}&amp;mch_id=${mch_id}&amp;nonce_str=${nonce}&amp;notify_url=${notify_url}&amp;out_trade_no=${nonce}&amp;spbill_create_ip=${ip}&amp;total_fee=${fee}&amp;trade_type=${trade_type}&amp;key=${mch_api_key}`\r\n return md5(tempString).toUpperCase()\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5176\u4e2d fee \u662f\u8981\u5145\u503c\u7684\u8d39\u7528\uff0c\u4ee5\u5206\u4e3a\u5355\u4f4d\u3002\u6bd4\u5982\u8981\u5145\u503c1\u5757\u94b1\uff0c fee \u5c31\u662f100\u3002ip\u662f\u4e2a\u6bd4\u8f83\u968f\u610f\u7684\u9009\u9879\uff0c\u53ea\u8981\u7b26\u5408\u89c4\u5219\u7684ip\u7ecf\u8fc7\u6d4b\u8bd5\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u4e0b\u6587\u91cc\u6211\u7528\u7684\u662f server_ip \u3002 nonce \u5c31\u662f\u5fae\u4fe1\u8981\u6c42\u7684\u4e0d\u91cd\u590d\u768432\u4f4d\u4ee5\u5185\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u8ba2\u5355\u53f7\u7b49\u552f\u4e00\u6807\u8bc6\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p style=\"text-align: left\">\u7531\u4e8e\u8ddf\u5fae\u4fe1\u7684\u670d\u52a1\u5668\u4ea4\u6d41\u90fd\u662f\u7528xml\u6765\u4ea4\u6d41\uff0c\u6240\u4ee5\u73b0\u5728\u6211\u4eec\u8981\u624b\u52a8\u7ec4\u88c5\u4e00\u4e0b\u7684 xml :<\/p>\n<pre>const xmlBody = (fee, nonce_str) =&gt; {\r\n const xml = `\r\n &lt;xml&gt;\r\n &lt;appid&gt;${appid}&lt;\/appid&gt;\r\n &lt;body&gt;${body}&lt;\/body&gt;\r\n &lt;mch_id&gt;${mch_id}&lt;\/mch_id&gt;\r\n &lt;nonce_str&gt;${nonce_str}&lt;\/nonce_str&gt;\r\n &lt;notify_url&gt;${notify_url}&lt;\/notify_url&gt;\r\n &lt;out_trade_no&gt;${nonce_str}&lt;\/out_trade_no&gt;\r\n &lt;total_fee&gt;${fee}&lt;\/total_fee&gt;\r\n &lt;spbill_create_ip&gt;${server_ip}&lt;\/spbill_create_ip&gt;\r\n &lt;trade_type&gt;NATIVE&lt;\/trade_type&gt;\r\n &lt;sign&gt;${signString(fee, server_ip, nonce_str)}&lt;\/sign&gt;\r\n &lt;\/xml&gt;\r\n `\r\n return {\r\n xml,\r\n out_trade_no: nonce_str\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5982\u679c\u4f60\u6015\u81ea\u5df1\u7684\u7b7e\u540d\u7684 xml \u4e32\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u63d0\u524d\u5728\u5fae\u4fe1\u63d0\u4f9b\u7684\u7b7e\u540d\u6821\u9a8c\u5de5\u5177\u91cc\u5148\u6821\u9a8c\u4e00\u904d\uff0c\u770b\u770b\u662f\u5426\u80fd\u901a\u8fc7\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u56e0\u4e3a\u9700\u8981\u8ddf\u5fae\u4fe1\u670d\u52a1\u7aef\u53d1\u8bf7\u6c42\uff0c\u6240\u4ee5\u6211\u9009\u62e9\u4e86 axios \u8fd9\u4e2a\u5728\u6d4f\u89c8\u5668\u7aef\u548cnode\u7aef\u90fd\u80fd\u53d1\u8d77ajax\u8bf7\u6c42\u7684\u5e93\u3002<\/p>\n<p style=\"text-align: left\">\u5b89\u88c5\u8fc7\u7a0b\u4e0d\u518d\u8d58\u8ff0\u3002\u7ee7\u7eed\u5728 wechatpay.js \u5199\u53d1\u8bf7\u6c42\u7684\u903b\u8f91\u3002<\/p>\n<p style=\"text-align: left\">\u7531\u4e8e\u5fae\u4fe1\u7ed9\u6211\u4eec\u8fd4\u56de\u7684\u4e5f\u5c06\u662f\u4e00\u4e2axml\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u9884\u5148\u5199\u597d\u89e3\u6790\u51fd\u6570\uff0c\u5c06xml\u89e3\u6790\u6210js\u5bf9\u8c61\u3002\u4e3a\u6b64\u4f60\u53ef\u4ee5\u5b89\u88c5\u4e00\u4e2a xml2js \u3002\u5b89\u88c5\u8fc7\u7a0b\u8ddf\u4e0a\u9762\u7684\u7c7b\u4f3c\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n<p style=\"text-align: left\">\u5fae\u4fe1\u4f1a\u7ed9\u6211\u4eec\u8fd4\u56de\u4e00\u4e2a\u8bf8\u5982\u4e0b\u9762\u683c\u5f0f\u7684 xml \u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>&lt;xml&gt;&lt;return_code&gt;&lt;![CDATA[SUCCESS]]&gt;&lt;\/return_code&gt;\r\n&lt;return_msg&gt;&lt;![CDATA[OK]]&gt;&lt;\/return_msg&gt;\r\n&lt;appid&gt;&lt;![CDATA[wx742xxxxxxxxxxxxx]]&gt;&lt;\/appid&gt;\r\n&lt;mch_id&gt;&lt;![CDATA[14899xxxxx]]&gt;&lt;\/mch_id&gt;\r\n&lt;nonce_str&gt;&lt;![CDATA[R69QXXXXXXXX6O]]&gt;&lt;\/nonce_str&gt;\r\n&lt;sign&gt;&lt;![CDATA[79F0891XXXXXX189507A184XXXXXXXXX]]&gt;&lt;\/sign&gt;\r\n&lt;result_code&gt;&lt;![CDATA[SUCCESS]]&gt;&lt;\/result_code&gt;\r\n&lt;prepay_id&gt;&lt;![CDATA[wx152316xxxxxxxxxxxxxxxxxxxxxxxxxxx]]&gt;&lt;\/prepay_id&gt;\r\n&lt;trade_type&gt;&lt;![CDATA[NATIVE]]&gt;&lt;\/trade_type&gt;\r\n&lt;code_url&gt;&lt;![CDATA[weixin:\/\/wxpay\/xxxurl?pr=dQNakHH]]&gt;&lt;\/code_url&gt;\r\n&lt;\/xml&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u6211\u4eec\u7684\u76ee\u6807\u662f\u8f6c\u4e3a\u5982\u4e0b\u7684js\u5bf9\u8c61\uff0c\u597d\u8ba9\u6211\u4eec\u7528js\u6765\u64cd\u4f5c\u6570\u636e\uff1a<\/p>\n<pre>{\r\n return_code: 'SUCCESS', \/\/ SUCCESS \u6216\u8005 FAIL\r\n return_msg: 'OK',\r\n appid: 'wx742xxxxxxxxxxxxx',\r\n mch_id: '14899xxxxx',\r\n nonce_str: 'R69QXXXXXXXX6O',\r\n sign: '79F0891XXXXXX189507A184XXXXXXXXX',\r\n result_code: 'SUCCESS',\r\n prepay_id: 'wx152316xxxxxxxxxxxxxxxxxxxxxxxxxxx',\r\n trade_type: 'NATIVE',\r\n code_url: 'weixin:\/\/wxpay\/xxxurl?pr=dQNakHH' \/\/ \u7528\u4e8e\u751f\u6210\u652f\u4ed8\u4e8c\u7ef4\u7801\u7684\u94fe\u63a5\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4e8e\u662f\u6211\u4eec\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8c03\u7528 xml2js \u6765\u89e3\u6790xml\uff1a<\/p>\n<pre>\/\/ \u5c06XML\u8f6c\u4e3aJS\u5bf9\u8c61\r\nconst parseXML = (xml) =&gt; {\r\n return new Promise((res, rej) =&gt; {\r\n xml2js.parseString(xml, {trim: true, explicitArray: false}, (err, json) =&gt; {\r\n if (err) {\r\n rej(err)\r\n } else {\r\n res(json.xml)\r\n }\r\n })\r\n })\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4e0a\u9762\u7684\u4ee3\u7801\u8fd4\u56de\u4e86\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u56e0\u4e3a xml2js \u7684\u64cd\u4f5c\u662f\u5728\u91cc\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u4e3a\u4e86\u914d\u5408Koa2\u7684 async \u3001 await \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5c01\u88c5\u6210\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5c06\u89e3\u6790\u5b8c\u7684\u7ed3\u679c\u901a\u8fc7 resolve \u8fd4\u56de\u56de\u53bb\u3002\u8fd9\u6837\u5c31\u80fd\u7528 await \u6765\u53d6\u6570\u636e\u4e86\uff1a<\/p>\n<pre>const axios = require('axios')\r\nconst url = 'https:\/\/api.mch.weixin.qq.com\/pay\/unifiedorder' \/\/ \u5fae\u4fe1\u670d\u52a1\u7aef\u5730\u5740\r\nconst pay = async (ctx) =&gt; {\r\n const form = ctx.request.body \/\/ \u901a\u8fc7\u524d\u7aef\u4f20\u6765\u7684\u6570\u636e\r\n const orderNo = 'XXXXXXXXXXXXXXXX' \/\/ \u4e0d\u91cd\u590d\u7684\u8ba2\u5355\u53f7\r\n const fee = form.fee \/\/ \u901a\u8fc7\u524d\u7aef\u4f20\u6765\u7684\u8d39\u7528\u503c\r\n const data = xmlBody(fee, orderNo) \/\/ fee\u662f\u8d39\u7528\uff0corderNo\u662f\u8ba2\u5355\u53f7\uff08\u552f\u4e00\uff09\r\n const res = await axios.post(url, {\r\n data: data.xml\r\n }).then(async res =&gt; {\r\n const resJson = await parseXML(res.data)\r\n return resJson \/\/ \u62ff\u5230\u8fd4\u56de\u7684\u6570\u636e\r\n }).catch(err =&gt; {\r\n console.log(err)\r\n })\r\n if (res.return_code === 'SUCCESS') { \/\/ \u5982\u679c\u8fd4\u56de\u7684\r\n return ctx.body = {\r\n success: true,\r\n message: '\u8bf7\u6c42\u6210\u529f',\r\n code_url: res.code_url, \/\/ code_url\u5c31\u662f\u7528\u4e8e\u751f\u6210\u652f\u4ed8\u4e8c\u7ef4\u7801\u7684\u94fe\u63a5\r\n order_no: orderNo \/\/ \u8ba2\u5355\u53f7\r\n }\r\n }\r\n ctx.body = {\r\n success: false,\r\n message: '\u8bf7\u6c42\u5931\u8d25'\r\n }\r\n}\r\nrouter.post('\/api\/pay', pay)\r\nmodule.exports = router<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u6211\u4eec\u8981\u5c06\u8fd9\u4e2arouter\u6302\u8f7d\u5230\u6839\u76ee\u5f55\u7684 app.js \u91cc\u53bb\u3002<\/p>\n<p style=\"text-align: left\">\u627e\u5230\u4e4b\u524d\u9ed8\u8ba4\u7684\u4e24\u4e2a\u8def\u7531\uff0c\u4e00\u4e2a index \uff0c\u4e00\u4e2a user \uff1a<\/p>\n<pre>const index = require('.\/routes\/index')\r\nconst users = require('.\/routes\/users')\r\nconst wechatpay = require('.\/routes\/wechatpay') \/\/ \u52a0\u5728\u8fd9\u91cc<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u5230\u9875\u9762\u5e95\u4e0b\u6302\u8f7d\u8fd9\u4e2a\u8def\u7531\uff1a<\/p>\n<pre>\/\/ routes\r\napp.use(index.routes(), index.allowedMethods())\r\napp.use(users.routes(), users.allowedMethods())\r\napp.use(wechatpay.routes(), users.allowedMethods()) \/\/ \u52a0\u5728\u8fd9\u91cc<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4e8e\u662f\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001 \/api\/pay \u6765\u8bf7\u6c42\u4e8c\u7ef4\u7801\u6570\u636e\u5566\u3002\uff08\u5982\u679c\u6709\u8de8\u57df\u9700\u8981\u81ea\u5df1\u8003\u8651\u89e3\u51b3\u8de8\u57df\u65b9\u6848\uff0c\u53ef\u4ee5\u8ddfKoa\u653e\u5728\u540c\u57df\u91cc\uff0c\u4e5f\u53ef\u4ee5\u5f00\u4e00\u5c42proxy\u6765\u8f6c\u53d1\uff0c\u4e5f\u53ef\u4ee5\u5f00CORS\u5934\u7b49\u7b49\uff09<\/p>\n<p style=\"text-align: left\">\u6ce8\u610f\uff0c \u672c\u4f8b\u91cc\u662f\u7528\u524d\u7aef\u6765\u751f\u6210\u4e8c\u7ef4\u7801\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u901a\u8fc7\u540e\u7aef\u751f\u6210\u4e8c\u7ef4\u7801\uff0c\u7136\u540e\u518d\u8fd4\u56de\u7ed9\u524d\u7aef\u3002\u4e0d\u8fc7\u4e3a\u4e86\u7b80\u6613\u6f14\u793a\uff0c\u672c\u4f8b\u91c7\u7528\u524d\u7aef\u901a\u8fc7\u83b7\u53d6 code_url \u540e\uff0c\u5728\u524d\u7aef\u751f\u6210\u4e8c\u7ef4\u7801\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u5c55\u793a\u652f\u4ed8\u4e8c\u7ef4\u7801<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u524d\u7aef\u6211\u7528\u7684\u662f Vue \uff0c\u5f53\u7136\u4f60\u53ef\u4ee5\u9009\u62e9\u4f60\u559c\u6b22\u7684\u524d\u7aef\u6846\u67b6\u3002\u8fd9\u91cc\u5173\u6ce8\u70b9\u5728\u4e8e\u901a\u8fc7\u62ff\u5230\u521a\u624d\u540e\u7aef\u4f20\u8fc7\u6765\u7684 code_url \u6765\u751f\u6210\u4e8c\u7ef4\u7801\u3002<\/p>\n<p style=\"text-align: left\">\u5728\u524d\u7aef\uff0c\u6211\u4f7f\u7528\u7684\u662f @xkeshi\/vue-qrcode \u8fd9\u4e2a\u5e93\u6765\u751f\u6210\u4e8c\u7ef4\u7801\u3002\u5b83\u8c03\u7528\u7279\u522b\u7b80\u5355\uff1a<\/p>\n<pre>import VueQrcode from '@xkeshi\/vue-qrcode'\r\nexport default {\r\n components: {\r\n VueQrcode\r\n },\r\n \/\/ ...\u5176\u4ed6\u4ee3\u7801\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u5c31\u53ef\u4ee5\u5728\u524d\u7aef\u91cc\u7528 \u7684\u7ec4\u4ef6\u6765\u751f\u6210\u4e8c\u7ef4\u7801\u4e86\uff1a <\/p>\n<pre>&lt;vue-qrcode :value=\"codeUrl\" :options=\"{ size: 200 }\"&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u653e\u5230Dialog\u91cc\u5c31\u662f\u8fd9\u6837\u7684\u6548\u679c\uff1a<\/p>\n<p style=\"text-align: left\">\u6587\u672c\u662f\u6211\u81ea\u5df1\u6dfb\u52a0\u7684<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/13b45249225f5f5be5a8b2b3db907692-1.png\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u63d2\u56fe1\" alt=\"\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u63d2\u56fe1\" \/>&nbsp;<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u4ed8\u6b3e\u6210\u529f\u81ea\u52a8\u5237\u65b0\u9875\u9762<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u6709\u4e24\u79cd\u5c06\u652f\u4ed8\u6210\u529f\u5199\u5165\u6570\u636e\u5e93\u7684\u529e\u6cd5\u3002<\/p>\n<p style=\"text-align: left\">\u4e00\u79cd\u662f\u5728\u6253\u5f00\u4e86\u626b\u7801\u5bf9\u8bdd\u6846\u540e\uff0c\u4e0d\u505c\u5411\u5fae\u4fe1\u670d\u52a1\u7aef\u8f6e\u8be2\u652f\u4ed8\u7ed3\u679c\uff0c\u5982\u679c\u652f\u4ed8\u6210\u529f\uff0c\u90a3\u4e48\u5c31\u5411\u540e\u7aef\u53d1\u8d77\u8bf7\u6c42\uff0c\u544a\u8bc9\u540e\u7aef\u652f\u4ed8\u6210\u529f\uff0c\u8ba9\u540e\u7aef\u5199\u5165\u6570\u636e\u5e93\u3002<\/p>\n<p style=\"text-align: left\">\u4e00\u79cd\u662f\u540e\u7aef\u4e00\u76f4\u5f00\u7740\u63a5\u53e3\uff0c\u7b49\u5fae\u4fe1\u4e3b\u52a8\u7ed9\u540e\u7aef\u7684 notify_url \u53d1\u8d77post\u8bf7\u6c42\uff0c\u544a\u8bc9\u540e\u7aef\u652f\u4ed8\u7ed3\u679c\uff0c\u8ba9\u540e\u7aef\u5199\u5165\u6570\u636e\u5e93\u3002\u7136\u540e\u6b64\u65f6\u524d\u7aef\u5411\u540e\u7aef\u8f6e\u8be2\u7684\u65f6\u5019\u5e94\u8be5\u662f\u53bb\u6570\u636e\u5e93\u53d6\u8f6e\u8be2\u8be5\u8ba2\u5355\u7684\u652f\u4ed8\u7ed3\u679c\uff0c\u5982\u679c\u652f\u4ed8\u6210\u529f\u5c31\u5173\u95edDialog\u3002<\/p>\n<p style=\"text-align: left\">\u7b2c\u4e00\u79cd\u6bd4\u8f83\u7b80\u5355\u4f46\u662f\u4e0d\u5b89\u5168\uff1a\u8bd5\u60f3\u4e07\u4e00\u7528\u6237\u652f\u4ed8\u6210\u529f\u7684\u540c\u65f6\u5173\u95ed\u4e86\u9875\u9762\uff0c\u6216\u8005\u7528\u6237\u652f\u4ed8\u6210\u529f\u4e86\uff0c\u4f46\u662f\u7f51\u7edc\u6709\u95ee\u9898\u5bfc\u81f4\u524d\u7aef\u6ca1\u6cd5\u5f80\u540e\u7aef\u53d1\u652f\u4ed8\u6210\u529f\u7684\u7ed3\u679c\uff0c\u90a3\u4e48\u540e\u7aef\u5c31\u4e00\u76f4\u6ca1\u529e\u6cd5\u5199\u5165\u652f\u4ed8\u6210\u529f\u7684\u6570\u636e\u3002<\/p>\n<p style=\"text-align: left\">\u7b2c\u4e8c\u79cd\u867d\u7136\u9ebb\u70e6\uff0c\u4f46\u662f\u4fdd\u8bc1\u4e86\u5b89\u5168\u3002\u6240\u6709\u7684\u652f\u4ed8\u7ed3\u679c\u90fd\u5fc5\u987b\u7b49\u5fae\u4fe1\u4e3b\u52a8\u5411\u540e\u7aef\u901a\u77e5\uff0c\u540e\u7aef\u5b58\u5b8c\u6570\u636e\u5e93\u540e\u518d\u8fd4\u56de\u7ed9\u524d\u7aef\u6d88\u606f\u3002\u8fd9\u6837\u54ea\u6015\u7528\u6237\u652f\u4ed8\u6210\u529f\u7684\u540c\u65f6\u5173\u95ed\u4e86\u9875\u9762\uff0c\u4e0b\u6b21\u518d\u6253\u5f00\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u6570\u636e\u5e93\u5df2\u7ecf\u5199\u5165\u4e86\uff0c\u6240\u4ee5\u62ff\u5230\u7684\u4e5f\u662f\u652f\u4ed8\u6210\u529f\u7684\u7ed3\u679c\u3002<\/p>\n<p style=\"text-align: left\">\u6240\u4ee5 \u4ed8\u6b3e\u6210\u529f\u81ea\u52a8\u5237\u65b0\u9875\u9762 \u8fd9\u4e2a\u90e8\u5206\u6211\u4eec\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\u6765\u8bf4\uff1a<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u524d\u7aef\u90e8\u5206<\/strong><\/span><\/p>\n<p style=\"text-align: left\">Vue\u7684data\u90e8\u5206<\/p>\n<pre>data: {\r\n payStatus: false, \/\/ \u672a\u652f\u4ed8\u6210\u529f\r\n retryCount: 0, \/\/ \u8f6e\u8be2\u6b21\u6570\uff0c\u4ece0-200\r\n orderNo: 'xxx', \/\/ \u4ece\u540e\u7aef\u4f20\u6765\u7684order_no\r\n codeUrl: 'xxx' \/\/ \u4ece\u540e\u7aef\u4f20\u6765\u7684code_url\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5728methods\u91cc\u5199\u4e00\u4e2a\u67e5\u8be2\u8ba2\u5355\u4fe1\u606f\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ ...\r\nhandleCheckBill () {\r\n return setTimeout(() =&gt; {\r\n if (!this.payStatus &amp;&amp; this.retryCount &lt; 120) {\r\n this.retryCount += 1\r\n axios.post('\/api\/check-bill', { \/\/ \u5411\u540e\u7aef\u8bf7\u6c42\u8ba2\u5355\u652f\u4ed8\u4fe1\u606f\r\n orderNo: this.orderNo\r\n })\r\n .then(res =&gt; {\r\n if (res.data.success) {\r\n this.payStatus = true\r\n location.reload() \/\/ \u5077\u61d2\u5c31\u7528reload\u91cd\u65b0\u5237\u65b0\u9875\u9762\r\n } else {\r\n this.handleCheckBill()\r\n }\r\n }).catch(err =&gt; {\r\n console.log(err)\r\n })\r\n } else {\r\n location.reload()\r\n }\r\n }, 1000)\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5728\u6253\u5f00\u4e8c\u7ef4\u7801Dialog\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u542f\u7528\u4e86\u3002\u7136\u540e\u5c31\u5f00\u59cb\u8f6e\u8be2\u3002\u6211\u8ba2\u4e86\u4e00\u4e2a\u65f6\u95f4\uff0c200s\u540e\u5982\u679c\u8fd8\u662f\u6ca1\u6709\u4ed8\u6b3e\u4fe1\u606f\u4e5f\u81ea\u52a8\u5237\u65b0\u9875\u9762\u3002\u5b9e\u9645\u4e0a\u4f60\u53ef\u4ee5\u81ea\u5df1\u6839\u636e\u9879\u76ee\u7684\u9700\u8981\u6765\u5b9a\u4e49\u8fd9\u4e2a\u65f6\u95f4\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u540e\u7aef\u90e8\u5206<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u524d\u7aef\u5230\u540e\u7aef\u53ea\u6709\u4e00\u4e2a\u63a5\u53e3\uff0c\u4f46\u662f\u540e\u7aef\u6709\u4e24\u4e2a\u63a5\u53e3\u3002\u4e00\u4e2a\u662f\u7528\u6765\u63a5\u6536\u5fae\u4fe1\u7684\u63a8\u9001\uff0c\u4e00\u4e2a\u662f\u7528\u6765\u63a5\u6536\u524d\u7aef\u7684\u67e5\u8be2\u8bf7\u6c42\u3002<\/p>\n<p style=\"text-align: left\">\u5148\u6765\u5199\u6700\u5173\u952e\u7684\u5fae\u4fe1\u7684\u63a8\u9001\u8bf7\u6c42\u5904\u7406\u3002\u7531\u4e8e\u6211\u4eec\u63a5\u6536\u5fae\u4fe1\u7684\u8bf7\u6c42\u662f\u5728Koa\u7684\u8def\u7531\u91cc\uff0c\u5e76\u4e14\u662f\u4ee5\u6d41\u7684\u5f62\u5f0f\u4f20\u8f93\u7684\u3002\u9700\u8981\u8ba9Koa\u652f\u6301\u89e3\u6790xml\u683c\u5f0f\u7684body\uff0c\u6240\u4ee5\u9700\u8981\u5b89\u88c5\u4e00\u4e2arawbody \u6765\u83b7\u53d6xml\u683c\u5f0f\u7684body\u3002<\/p>\n<pre>\/\/ \u5904\u7406\u5fae\u4fe1\u652f\u4ed8\u56de\u4f20notify\r\n\/\/ \u5982\u679c\u6536\u5230\u6d88\u606f\u8981\u8ddf\u5fae\u4fe1\u56de\u4f20\u662f\u5426\u63a5\u6536\u5230\r\nconst handleNotify = async (ctx) =&gt; {\r\n const xml = await rawbody(ctx.req, {\r\n length: ctx.request.length,\r\n limit: '1mb',\r\n encoding: ctx.request.charset || 'utf-8'\r\n })\r\n const res = await parseXML(xml) \/\/ \u89e3\u6790xml\r\n if (res.return_code === 'SUCCESS') {\r\n if (res.result_code === 'SUCCESS') { \/\/ \u5982\u679c\u90fd\u4e3aSUCCESS\u4ee3\u8868\u652f\u4ed8\u6210\u529f\r\n \/\/ ... \u8fd9\u91cc\u662f\u5199\u5165\u6570\u636e\u5e93\u7684\u76f8\u5173\u64cd\u4f5c\r\n \/\/ \u5f00\u59cb\u56de\u4f20\u5fae\u4fe1\r\n ctx.type = 'application\/xml' \/\/ \u6307\u5b9a\u53d1\u9001\u7684\u8bf7\u6c42\u7c7b\u578b\u662fxml\r\n \/\/ \u56de\u4f20\u5fae\u4fe1\uff0c\u544a\u8bc9\u5df2\u7ecf\u6536\u5230\r\n return ctx.body = `&lt;xml&gt;\r\n &lt;return_code&gt;&lt;![CDATA[SUCCESS]]&gt;&lt;\/return_code&gt;\r\n &lt;return_msg&gt;&lt;![CDATA[OK]]&gt;&lt;\/return_msg&gt;\r\n &lt;\/xml&gt;\r\n `\r\n }\r\n }\r\n \/\/ \u5982\u679c\u652f\u4ed8\u5931\u8d25\uff0c\u4e5f\u56de\u4f20\u5fae\u4fe1\r\n ctx.status = 400\r\n ctx.type = 'application\/xml'\r\n ctx.body = `&lt;xml&gt;\r\n &lt;return_code&gt;&lt;![CDATA[FAIL]]&gt;&lt;\/return_code&gt;\r\n &lt;return_msg&gt;&lt;![CDATA[OK]]&gt;&lt;\/return_msg&gt;\r\n &lt;\/xml&gt;\r\n `\r\n}\r\nrouter.post('\/api\/notify', handleNotify)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u8fd9\u91cc\u7684\u5751\u5c31\u662fKoa\u5904\u7406\u5fae\u4fe1\u56de\u4f20\u7684xml\u3002\u5982\u679c\u4e0d\u77e5\u9053\u662f\u4ee5 raw-body \u7684\u5f62\u5f0f\u56de\u4f20\u7684\uff0c\u4f1a\u8c03\u8bd5\u534a\u5929\u3002\u3002<\/p>\n<p style=\"text-align: left\">\u63a5\u4e0b\u6765\u8fd9\u4e2a\u5c31\u662f\u6bd4\u8f83\u7b80\u5355\u7684\u7ed9\u524d\u7aef\u56de\u4f20\u7684\u4e86\u3002<\/p>\n<pre>const checkBill = async (ctx) =&gt; {\r\n const form = ctx.request.body\r\n const orderNo = form.orderNo\r\n const result = await \u6570\u636e\u5e93\u64cd\u4f5c\r\n if (result) { \/\/ \u5982\u679c\u8ba2\u5355\u652f\u4ed8\u6210\u529f\r\n return ctx.body = {\r\n success: true\r\n }\r\n }\r\n ctx.status = 400\r\n ctx.body = {\r\n success: false\r\n }\r\n}\r\nrouter.post('\/api\/check-bill', checkBill)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5982\u4f55\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\uff0c\u4f7f\u7528Koa2\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u626b\u7801\u652f\u4ed8\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u524d\u6bb5\u65f6\u95f4\u5728\u5f00\u53d1\u4e00\u4e2a\u529f\u80fd\uff0c\u8981\u6c42\u662f\u901a\u8fc7\u5fae\u4fe1\u4e8c\u7ef4\u7801\u8fdb\u884c\u626b\u7801\u652f\u4ed8\u3002\u8fd9\u4e2a\u60c5\u666f\u6211\u4eec\u5c61\u89c1\u4e0d\u9c9c\u4e86\uff0c\u5404\u79cd\u7535\u5b50\u5546\u57ce\u3001\u7ebf\u4e0b\u7684\u81ea\u52a8\u8d29\u5356\u673a\u7b49\u7b49\u90fd\u4f1a\u6709\u8fd9\u4e2a\u529f\u80fd\u3002\u5e73\u65f6\u53ea\u662f\u4f7f\u7528\u8005\uff0c\u5982\u4eca\u53d8\u4e3a\u5f00\u53d1\u8005\uff0c\u4e5f\u662f\u6709\u4e0d\u5c0f\u7684\u5751\u3002\u6240\u4ee5\u7279\u6b64\u5199\u4e00\u7bc7\u535a\u5ba2\u8bb0\u5f55\u4e00\u4e0b\u3002 \u6ce8\uff1a \u8981\u5f00\u53d1\u5fae\u4fe1\u4e8c\u7ef4\u7801\u652f\u4ed8\uff0c\u4f60\u5fc5\u987b\u8981\u6709\u76f8\u5e94\u7684\u5546\u6237\u53f7\u7684\u6743\u9650\uff0c\u5426\u5219\u4f60\u662f\u65e0\u6cd5\u5f00\u53d1\u7684\u3002\u82e5\u65e0\u76f8\u5e94\u6743\u9650\uff0c\u672c\u6587\u4e0d\u63a8\u8350\u9605\u8bfb\u3002 \u4e24\u79cd\u6a21\u5f0f \u6253\u5f00\u5fae\u4fe1\u652f\u4ed8\u7684\u6587\u6863\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e24\u79cd\u652f\u4ed8\u6a21\u5f0f\uff1a\u6a21\u5f0f\u4e00\u548c\u6a21\u5f0f\u4e8c\u3002\u8fd9\u4e8c\u8005\u7684\u6d41\u7a0b\u56fe\u5fae\u4fe1\u7684\u6587\u6863\u91cc\u90fd\u7ed9\u51fa\u4e86\uff08\u4e0d\u8fc7\u8bf4\u5b9e\u8bdd\u753b\u5f97\u771f\u7684\u6709\u70b9\u4e11\uff09\u3002 \u6587\u6863\u91cc\u6307\u51fa\u4e86\u4e8c\u8005\u7684\u533a\u522b\uff1a \u6a21\u5f0f\u4e00\u5f00\u53d1\u524d\uff0c\u5546\u6237\u5fc5\u987b\u5728\u516c\u4f17\u5e73\u53f0\u540e\u53f0\u8bbe\u7f6e\u652f\u4ed8\u56de\u8c03URL\u3002URL\u5b9e\u73b0\u7684\u529f\u80fd\uff1a\u63a5\u6536\u7528\u6237\u626b\u7801\u540e\u5fae\u4fe1\u652f\u4ed8\u7cfb\u7edf\u56de\u8c03\u7684productid\u548copenid\u3002 \u6a21\u5f0f\u4e8c\u4e0e\u6a21\u5f0f\u4e00\u76f8\u6bd4\uff0c\u6d41\u7a0b\u66f4\u4e3a\u7b80\u5355\uff0c\u4e0d\u4f9d\u8d56\u8bbe\u7f6e\u7684\u56de\u8c03\u652f\u4ed8URL\u3002\u5546\u6237\u540e\u53f0\u7cfb\u7edf\u5148\u8c03\u7528\u5fae\u4fe1\u652f\u4ed8\u7684\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\uff0c\u5fae\u4fe1\u540e\u53f0\u7cfb\u7edf\u8fd4\u56de\u94fe\u63a5\u53c2\u6570code_url\uff0c\u5546\u6237\u540e\u53f0\u7cfb\u7edf\u5c06code_url\u503c\u751f\u6210\u4e8c\u7ef4\u7801\u56fe\u7247\uff0c\u7528\u6237\u4f7f\u7528\u5fae\u4fe1\u5ba2\u6237\u7aef\u626b\u7801\u540e\u53d1\u8d77\u652f\u4ed8\u3002\u6ce8\u610f\uff1acode_url\u6709\u6548\u671f\u4e3a2\u5c0f\u65f6\uff0c\u8fc7\u671f\u540e\u626b\u7801\u4e0d\u80fd\u518d\u53d1\u8d77\u652f\u4ed8\u3002 \u6a21\u5f0f\u4e00\u662f\u6211\u4eec\u5e73\u65f6\u5728\u7f51\u8d2d\u7684\u65f6\u5019\u6bd4\u8f83\u5e38\u89c1\u7684\uff0c\u4f1a\u5f39\u51fa\u4e00\u4e2a\u4e13\u95e8\u7684\u9875\u9762\u7528\u4e8e\u626b\u7801\u652f\u4ed8\uff0c\u7136\u540e\u652f\u4ed8\u6210\u529f\u540e\u8fd9\u4e2a\u9875\u9762\u4f1a\u518d\u6b21\u8df3\u8f6c\u56de\u56de\u8c03\u9875\u9762\uff0c\u901a\u77e5\u4f60\u652f\u4ed8\u6210\u529f\u3002\u7b2c\u4e8c\u79cd\u7684\u8bdd\u60f3\u5bf9\u5c11\u4e00\u4e9b\uff0c\u4e0d\u8fc7\u7b2c\u4e8c\u79cd\u5f00\u53d1\u8d77\u6765\u76f8\u5bf9\u7b80\u5355\u70b9\u3002 \u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u6a21\u5f0f\u4e8c\u7684\u5f00\u53d1 \u3002 \u642d\u5efaKoa2\u7684\u7b80\u5355\u5f00\u53d1\u73af\u5883 \u5feb\u901f\u642d\u5efaKoa2\u7684\u5f00\u53d1\u73af\u5883\u6211\u63a8\u8350\u53ef\u4ee5\u4f7f\u7528koa-generator \u3002\u811a\u624b\u67b6\u80fd\u5e2e\u6211\u4eec\u7701\u53bbKoa\u9879\u76ee\u4e00\u5f00\u59cb\u7684\u4e00\u4e9b\u57fa\u672c\u4e2d\u95f4\u4ef6\u7684\u4e66\u5199\u6b65\u9aa4\u3002\uff08\u5982\u679c\u4f60\u60f3\u5b66\u4e60Koa\u6700\u597d\u81ea\u5df1\u642d\u5efa\u4e00\u4e2a\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u4f1aKoa\u4e86\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5feb\u901f\u811a\u624b\u67b6\u4e86\u3002\uff09 \u9996\u5148\u5168\u5c40\u5b89\u88c5 koa-generator \uff1a npm install -g koa-generator #or yarn global add koa-generator \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u627e\u4e00\u4e2a\u76ee\u5f55\u7528\u6765\u5b58\u653eKoa\u9879\u76ee\uff0c\u6211\u4eec\u6253\u7b97\u7ed9\u8fd9\u4e2a\u9879\u76ee\u53d6\u4e2a\u540d\u5b57\u53eb\u505a koa-wechatpay \uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8f93\u5165 koa2 koa-wechatpay \u3002\u7136\u540e\u811a\u624b\u67b6\u4f1a\u81ea\u52a8\u521b\u5efa\u76f8\u5e94\u6587\u4ef6\u5939 koa-wechatpay \uff0c\u5e76\u751f\u6210\u57fa\u672c\u9aa8\u67b6\u3002\u8fdb\u5165\u8fd9\u4e2a\u6587\u4ef6\u5939\uff0c\u5b89\u88c5\u76f8\u5e94\u7684\u63d2\u4ef6\u3002\u8f93\u5165\uff1a npm install #or yarn \u767b\u5f55\u540e\u590d\u5236 \u63a5\u7740\u4f60\u53ef\u4ee5\u8f93\u5165 npm start \u6216\u8005 yarn start \u6765\u8fd0\u884c\u9879\u76ee\uff08\u9ed8\u8ba4\u76d1\u542c\u57283000\u7aef\u53e3\uff09\u3002 \u5982\u679c\u4e0d\u51fa\u610f\u5916\uff0c\u4f60\u7684\u9879\u76ee\u8dd1\u8d77\u6765\u4e86\uff0c\u7136\u540e\u6211\u4eec\u7528postman\u6d4b\u8bd5\u4e00\u4e0b\uff1a \u8fd9\u6761\u8def\u7531\u662f\u5728 routes\/index.js \u91cc\u3002 \u5982\u679c\u4f60\u770b\u5230\u4e86 { &#8220;title&#8221;: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31481","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31481"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31481\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}