{"id":597,"date":"2025-12-07T10:11:57","date_gmt":"2025-12-07T02:11:57","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=597"},"modified":"2025-12-07T10:11:57","modified_gmt":"2025-12-07T02:11:57","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%94%af%e4%bb%98%e5%8c%85%e5%90%abuniapp%e6%96%b9%e6%b3%95%e8%b6%85%e8%af%a6%e7%bb%86%e8%ae%b2%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%94%af%e4%bb%98%e5%8c%85%e5%90%abuniapp%e6%96%b9%e6%b3%95%e8%b6%85%e8%af%a6%e7%bb%86%e8%ae%b2%e8%a7%a3\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u4ed8\u5305\u542buniapp\u65b9\u6cd5\u8d85\u8be6\u7ec6\u8bb2\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u4ed8\uff08\u542b uni-app\uff09\u6700\u5168\u3001\u6700\u8be6\u7ec6\u3001\u4ece\u96f6\u5230\u6210\u54c1\u7684\u8bb2\u89e3<\/strong>\uff0c\u6db5\u76d6 <strong>\u5c0f\u7a0b\u5e8f\u7aef\u3001\u4e91\u51fd\u6570\/\u670d\u52a1\u7aef\u3001uni-app \u4e24\u79cd\u5199\u6cd5\u3001\u6ce8\u610f\u4e8b\u9879\u3001\u5e38\u89c1\u62a5\u9519\u3001\u56de\u8c03\u901a\u77e5\u3001\u9000\u6b3e<\/strong> \u7b49\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u4ed8\uff08\u542b uni-app\uff09\u8d85\u8be6\u7ec6\u6559\u7a0b<\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6838\u5fc3\u6d41\u7a0b<\/strong>\uff1a<br>\u5c0f\u7a0b\u5e8f\u7aef\u4e0b\u5355 \u2192 \u540e\u7aef\u7edf\u4e00\u4e0b\u5355\uff08\u751f\u6210\u9884\u652f\u4ed8\u5355\uff09\u2192 \u5c0f\u7a0b\u5e8f\u62c9\u8d77\u652f\u4ed8 \u2192 \u652f\u4ed8\u6210\u529f \u2192 \u670d\u52a1\u7aef\u652f\u4ed8\u56de\u8c03\u9a8c\u8bc1 \u2192 \u66f4\u65b0\u8ba2\u5355\u72b6\u6001<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u76ee\u5f55<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5fc5\u5907\u524d\u7f6e\u914d\u7f6e<\/li>\n\n\n\n<li>\u5c0f\u7a0b\u5e8f\u652f\u4ed8\u6d41\u7a0b\u56fe<\/li>\n\n\n\n<li>\u5c0f\u7a0b\u5e8f\u7aef JS\uff08\u539f\u751f\u5199\u6cd5\uff09<\/li>\n\n\n\n<li>uni-app \u7aef\u4ee3\u7801\uff08APP + \u5c0f\u7a0b\u5e8f\u540c\u5199\u6cd5\uff09<\/li>\n\n\n\n<li>Node.js \u670d\u52a1\u7aef\u7edf\u4e00\u4e0b\u5355\u4ee3\u7801<\/li>\n\n\n\n<li>\u652f\u4ed8\u7ed3\u679c\u56de\u8c03\uff08\u670d\u52a1\u7aef\uff09<\/li>\n\n\n\n<li>\u5e38\u89c1\u5751\u4e0e\u9519\u8bef<\/li>\n\n\n\n<li>\u9000\u6b3e\u793a\u4f8b<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1. \ud83d\udccc \u5fc5\u5907\u524d\u7f6e\u914d\u7f6e<\/h1>\n\n\n\n<p>\u4f60\u9700\u8981\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u5df2\u5f00\u901a\u5fae\u4fe1\u652f\u4ed8\u5546\u6237\u53f7<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5546\u6237\u53f7 MchID<\/li>\n\n\n\n<li>APIv3 \u5bc6\u94a5<\/li>\n\n\n\n<li>API \u8bc1\u4e66\uff08\u79c1\u94a5 + cert\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u5c0f\u7a0b\u5e8f AppID<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u914d\u7f6e\u5230\u5546\u6237\u5e73\u53f0\uff1a<br>\u8def\u5f84\uff1a\u5546\u6237\u5e73\u53f0 \u2192 \u4ea7\u54c1\u4e2d\u5fc3 \u2192 APPID \u7ed1\u5b9a<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u540e\u7aef\u670d\u52a1\uff08Node.js \/ Java \/ PHP\uff09<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u793a\u4f8b Node.js\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2. \ud83d\udccc \u5c0f\u7a0b\u5e8f\u652f\u4ed8\u6d41\u7a0b\u56fe<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u7528\u6237\u70b9\u51fb\u652f\u4ed8  \n    \u2193  \n\u5c0f\u7a0b\u5e8f\u8bf7\u6c42\u540e\u7aef\u4e0b\u5355  \n    \u2193  \n\u540e\u7aef\u8c03\u7528\u5fae\u4fe1\u652f\u4ed8\u7edf\u4e00\u4e0b\u5355 API  \n    \u2193  \n\u5fae\u4fe1\u8fd4\u56de prepay_id  \n    \u2193  \n\u5c0f\u7a0b\u5e8f wx.requestPayment \u8c03\u8d77\u652f\u4ed8  \n    \u2193  \n\u652f\u4ed8\u6210\u529f  \n    \u2193  \n\u5fae\u4fe1\u670d\u52a1\u5668\u53d1\u9001\u652f\u4ed8\u56de\u8c03 \u2192 \u540e\u7aef  \n    \u2193  \n\u540e\u7aef\u6821\u9a8c\u7b7e\u540d\u5e76\u4fee\u6539\u8ba2\u5355\u72b6\u6001\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3. \ud83d\udccc \u5c0f\u7a0b\u5e8f\u7aef\u4ee3\u7801\uff08\u539f\u751f WeChat Mini-program\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2460 \u524d\u7aef\u53d1\u8d77\u8ba2\u5355\u8bf7\u6c42<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nwx.request({\n  url: &#039;https:\/\/your-server.com\/pay\/unifiedorder&#039;,\n  method: &#039;POST&#039;,\n  data: { orderId: &#039;12345&#039; },\n  success(res) {\n    const pay = res.data;\n\n    wx.requestPayment({\n      timeStamp: pay.timeStamp,\n      nonceStr: pay.nonceStr,\n      package: pay.package,\n      signType: pay.signType,\n      paySign: pay.paySign,\n      success(res) {\n        console.log(&#039;\u652f\u4ed8\u6210\u529f&#039;, res);\n      },\n      fail(err) {\n        console.log(&#039;\u652f\u4ed8\u5931\u8d25&#039;, err);\n      }\n    });\n  }\n});\n\n<\/pre><\/div>\n\n\n<p>\u8fd4\u56de\u7684 <code>pay<\/code> \u4fe1\u606f\u6765\u81ea\u540e\u7aef\u7b7e\u540d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4. \ud83d\udccc uni-app \u7aef\u5b8c\u6574\u652f\u4ed8\u5199\u6cd5\uff08\u5c0f\u7a0b\u5e8f + APP \u901a\u7528\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">uni.request + uni.requestPayment<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npayOrder(orderId) {\n  uni.request({\n    url: &#039;https:\/\/your-server.com\/pay\/unifiedorder&#039;,\n    method: &#039;POST&#039;,\n    data: { orderId },\n    success: (res) =&gt; {\n      const pay = res.data;\n\n      uni.requestPayment({\n        provider: &#039;wxpay&#039;,\n        timeStamp: pay.timeStamp,\n        nonceStr: pay.nonceStr,\n        package: pay.package,\n        signType: &#039;RSA&#039;,\n        paySign: pay.paySign,\n        success: res =&gt; {\n          console.log(&#039;\u652f\u4ed8\u6210\u529f&#039;, res);\n        },\n        fail: err =&gt; {\n          console.log(&#039;\u652f\u4ed8\u53d6\u6d88\/\u5931\u8d25&#039;, err);\n        }\n      });\n    }\n  });\n}\n\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728 <strong>App \u7aef<\/strong> \u652f\u4ed8\u65f6 provider \u5fc5\u987b\u5199 <code>\"wxpay\"<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5. \ud83d\udccc Node.js \u540e\u7aef\u7edf\u4e00\u4e0b\u5355\uff08\u6700\u5168\u5b9e\u73b0\uff09<\/h1>\n\n\n\n<p>\u4f7f\u7528\u5b98\u65b9 <strong>\u5fae\u4fe1\u652f\u4ed8 v3 API<\/strong>\uff1a<\/p>\n\n\n\n<p>\u5b89\u88c5\u5e93\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm i axios crypto\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\u4ee3\u7801\uff08Node.js Koa\/Express \u90fd\u53ef\u7528\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst axios = require(&#039;axios&#039;);\nconst crypto = require(&#039;crypto&#039;);\nconst fs = require(&#039;fs&#039;);\n\nconst appId = &quot;\u4f60\u7684appid&quot;;\nconst mchId = &quot;\u4f60\u7684\u5546\u6237\u53f7&quot;;\nconst apiKey = &quot;\u4f60\u7684APIv3\u5bc6\u94a5&quot;;\n\n\/\/ \u79c1\u94a5\u6587\u4ef6\uff08apiclient_key.pem\uff09\nconst privateKey = fs.readFileSync(&#039;.\/cert\/apiclient_key.pem&#039;);\n\n\/\/ \u751f\u6210\u7b7e\u540d\u51fd\u6570\nfunction sign(method, url, timestamp, nonceStr, body = &#039;&#039;) {\n  const message = `${method}\\n${url}\\n${timestamp}\\n${nonceStr}\\n${body}\\n`;\n\n  const signer = crypto.createSign(&#039;RSA-SHA256&#039;);\n  signer.update(message);\n  return signer.sign(privateKey, &#039;base64&#039;);\n}\n\nexports.unifiedorder = async (req, res) =&gt; {\n  const { orderId } = req.body;\n\n  const url = &#039;\/v3\/pay\/transactions\/jsapi&#039;;\n  const fullUrl = &#039;https:\/\/api.mch.weixin.qq.com&#039; + url;\n\n  const data = {\n    appid: appId,\n    mchid: mchId,\n    description: &quot;\u5546\u54c1\u63cf\u8ff0&quot;,\n    out_trade_no: orderId,\n    notify_url: &quot;https:\/\/your-server.com\/pay\/notify&quot;,\n    amount: {\n      total: 100, \/\/ \u5355\u4f4d\uff1a\u5206\n    },\n    payer: {\n      openid: req.body.openid,\n    }\n  };\n\n  const timestamp = Math.floor(Date.now() \/ 1000).toString();\n  const nonceStr = crypto.randomBytes(16).toString(&#039;hex&#039;);\n  const bodyStr = JSON.stringify(data);\n\n  const signature = sign(&#039;POST&#039;, url, timestamp, nonceStr, bodyStr);\n\n  const auth = `WECHATPAY2-SHA256-RSA2048 mchid=&quot;${mchId}&quot;,nonce_str=&quot;${nonceStr}&quot;,signature=&quot;${signature}&quot;,timestamp=&quot;${timestamp}&quot;,serial_no=&quot;\u4f60\u7684\u8bc1\u4e66\u5e8f\u5217\u53f7&quot;`;\n\n  const result = await axios.post(fullUrl, data, {\n    headers: {\n      &#039;Authorization&#039;: auth,\n      &#039;Content-Type&#039;: &#039;application\/json&#039;\n    }\n  });\n\n  const prepayId = result.data.prepay_id;\n\n  \/\/ \u751f\u6210\u524d\u7aef\u9700\u8981\u7684\u7b7e\u540d\n  const payTimestamp = timestamp;\n  const payNonceStr = nonceStr;\n  const payPackage = `prepay_id=${prepayId}`;\n  const paySignString = `${appId}\\n${payTimestamp}\\n${payNonceStr}\\n${payPackage}\\n`;\n\n  const paySign = crypto\n    .createSign(&#039;RSA-SHA256&#039;)\n    .update(paySignString)\n    .sign(privateKey, &#039;base64&#039;);\n\n  res.json({\n    timeStamp: payTimestamp,\n    nonceStr: payNonceStr,\n    package: payPackage,\n    signType: &#039;RSA&#039;,\n    paySign,\n  });\n};\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">6. \ud83d\udccc \u5fae\u4fe1\u652f\u4ed8\u56de\u8c03\u901a\u77e5\uff08\u5fc5\u987b\u5904\u7406\uff09<\/h1>\n\n\n\n<p>\u5fae\u4fe1\u652f\u4ed8\u6210\u529f\u540e\u4f1a POST \u5230\u4f60\u7684\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttps:\/\/your-server.com\/pay\/notify\n\n<\/pre><\/div>\n\n\n<p>Node.js \u4ee3\u7801\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexports.notify = async (req, res) =&gt; {\n  const { resource, summary } = req.body;\n\n  console.log(&quot;\u652f\u4ed8\u56de\u8c03\u5185\u5bb9\uff1a&quot;, req.body);\n\n  \/\/ \u89e3\u5bc6 resource.ciphertext\n  const decipher = crypto.createDecipheriv(\n    &#039;aes-256-gcm&#039;,\n    apiKey,\n    Buffer.from(resource.nonce, &#039;hex&#039;)\n  );\n  decipher.setAuthTag(Buffer.from(resource.tag, &#039;hex&#039;));\n\n  const decrypted = JSON.parse(\n    decipher.update(resource.ciphertext, &#039;base64&#039;, &#039;utf8&#039;) + decipher.final(&#039;utf8&#039;)\n  );\n\n  console.log(&quot;\u5df2\u89e3\u5bc6\u7684\u652f\u4ed8\u4fe1\u606f\uff1a&quot;, decrypted);\n\n  \/\/ \u4fee\u6539\u8ba2\u5355\u72b6\u6001\u2026\n  \/\/ updateOrder(decrypted.out_trade_no)\n\n  res.status(200).json({ code: &#039;SUCCESS&#039; });\n};\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">7. \ud83d\udccc \u5e38\u89c1\u62a5\u9519\u4e0e\u89e3\u51b3\u65b9\u6cd5<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9519\u8bef<\/th><th>\u539f\u56e0<\/th><th>\u89e3\u51b3<\/th><\/tr><\/thead><tbody><tr><td><code>INVALID_REQUEST<\/code><\/td><td>\u53c2\u6570\u4e0d\u5b8c\u6574<\/td><td>\u68c0\u67e5 appid, mchid, notify_url<\/td><\/tr><tr><td><code>appid \u548c openid \u4e0d\u5339\u914d<\/code><\/td><td>openid \u975e\u5f53\u524d\u5c0f\u7a0b\u5e8f<\/td><td>\u5fc5\u987b\u7528 wx.login \u83b7\u53d6<\/td><\/tr><tr><td>\u652f\u4ed8\u8c03\u8d77\u540e\u65e0\u53cd\u5e94<\/td><td>\u524d\u7aef\u7b7e\u540d\u9519\u8bef<\/td><td>\u91cd\u70b9\u68c0\u67e5 paySign<\/td><\/tr><tr><td><code>prepay_id \u5df2\u5931\u6548<\/code><\/td><td>5 \u5206\u949f\u672a\u4f7f\u7528<\/td><td>\u91cd\u7b7e\u540d\u5e76\u91cd\u65b0\u4e0b\u5355<\/td><\/tr><tr><td>\u5c0f\u7a0b\u5e8f\u663e\u793a &#8220;\u5546\u6237\u53f7\u672a\u5f00\u901a\u5c0f\u7a0b\u5e8f\u652f\u4ed8&#8221;<\/td><td>\u5546\u6237\u53f7\u672a\u7ed1\u5b9a appid<\/td><td>\u53bb\u5546\u6237\u5e73\u53f0\u7ed1\u5b9a<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">8. \ud83d\udccc \u9000\u6b3e\u793a\u4f8b\uff08Node.js\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexports.refund = async (req, res) =&gt; {\n  const url = &#039;\/v3\/refund\/domestic\/refunds&#039;;\n  const data = {\n    out_trade_no: req.body.orderId,\n    out_refund_no: &#039;refund_&#039; + Date.now(),\n    amount: {\n      refund: 100,\n      total: 100,\n      currency: &#039;CNY&#039;\n    }\n  };\n\n  \/\/ \u4e0e\u7edf\u4e00\u4e0b\u5355\u4e00\u6837\uff1a\u751f\u6210\u7b7e\u540d \u2192 \u8c03\u7528\u5fae\u4fe1 API\n};\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u4ed8\uff08\u542b uni-app\uff09\u6700\u5168\u3001\u6700\u8be6\u7ec6\u3001\u4ece\u96f6\u5230\u6210\u54c1\u7684\u8bb2\u89e3&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%94%af%e4%bb%98%e5%8c%85%e5%90%abuniapp%e6%96%b9%e6%b3%95%e8%b6%85%e8%af%a6%e7%bb%86%e8%ae%b2%e8%a7%a3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-597","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":598,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/597\/revisions\/598"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}