{"id":33434,"date":"2024-11-25T10:24:24","date_gmt":"2024-11-25T02:24:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33434\/"},"modified":"2024-11-25T10:24:24","modified_gmt":"2024-11-25T02:24:24","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%b0%83%e7%94%a8%e5%9b%be%e7%89%87%e5%ae%89%e5%85%a8api","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33434\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8c03\u7528\u56fe\u7247\u5b89\u5168API"},"content":{"rendered":"<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5ba1\u6838\u62d2\u7edd\uff0c\u62d2\u7edd\u539f\u56e0\u662f\u7528\u6237\u4e0a\u4f20\u56fe\u7247\u53ef\u80fd\u5b58\u5728\u8fdd\u6cd5\u8fdd\u89c4\u95ee\u9898\uff0c\u7a0b\u5e8f\u5fc5\u987b\u6709\u5ba1\u6838\u673a\u5236\u3002<br \/>\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff08\u4e91\u5f00\u53d1\uff09\uff1a<br \/>config.json<\/p>\n<pre>{\n  \"permissions\": {\n    \"openapi\": [\n      \"security.imgSecCheck\"\n    ]\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e91\u51fd\u6570<\/p>\n<pre>const cloud = require('wx-server-sdk')\n\ncloud.init()\n \nexports.main = async (event, context) =&gt; {\n  const { value } = event;\n  try {\n    const res = await cloud.openapi.security.imgSecCheck({\n      media: {\n        header: {\n          'Content-Type': 'application\/octet-stream'},\n        contentType: 'image\/png',\n        value: Buffer.from(value)\n        }\n      })\n    return res;\n  } catch (err) {\n    return err;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>js<\/p>\n<pre>ChooseImage() {\n    wx.chooseImage({\n      count: 1, \n      sizeType: ['original', 'compressed'], \n      sourceType: ['album'], \n      success: (res) =&gt; {\n        if (res.tempFiles[0] &amp;&amp; res.tempFiles[0].size &gt; 1024 * 1024) {\n          wx.showToast({\n            title: '\u56fe\u7247\u4e0d\u80fd\u5927\u4e8e1M',\n            icon: 'none'\n          })\n          return;\n        }\n        \/\/\u6821\u9a8c\u56fe\u7247\n\n        wx.getFileSystemManager().readFile({\n          filePath: res.tempFilePaths[0],\n          success: buffer =&gt; {\n            console.log(buffer.data)\n            wx.cloud.callFunction({\n              name: 'checkImg',\n              data: {\n                value: buffer.data\n              }\n            }).then(\n              imgRes =&gt; {\n                if (imgRes.result.errCode == '87014') {\n                  wx.showToast({\n                    title: '\u56fe\u7247\u542b\u6709\u8fdd\u6cd5\u8fdd\u89c4\u5185\u5bb9',\n                    icon: 'none'\n                  })\n                  return\n                } else {\n                  \/\/\u56fe\u7247\u6b63\u5e38\n\n                  if (this.data.imgList.length != 0) {\n                    this.setData({\n                      imgList: this.data.imgList.concat(res.tempFilePaths)\n                    })\n                  } else {\n                    this.setData({\n                      imgList: res.tempFilePaths\n                    })\n                  }\n\n\n                }\n\n              }\n            )\n          },\n          fail: err =&gt; {\n            console.log(err)\n          }\n        })\n\n      }\n    });\n  },<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a8\u8350\u6559\u7a0b\uff1a\u300a\u300b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8c03\u7528\u56fe\u7247\u5b89\u5168API\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>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5ba1\u6838\u62d2\u7edd\uff0c\u62d2\u7edd\u539f\u56e0\u662f\u7528\u6237\u4e0a\u4f20\u56fe\u7247\u53ef\u80fd\u5b58\u5728\u8fdd\u6cd5\u8fdd\u89c4\u95ee\u9898\uff0c\u7a0b\u5e8f\u5fc5\u987b\u6709\u5ba1\u6838\u673a\u5236\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff08\u4e91\u5f00\u53d1\uff09\uff1aconfig.json { &#8220;permissions&#8221;: { &#8220;openapi&#8221;: [ &#8220;security.imgSecCheck&#8221; ] } } \u767b\u5f55\u540e\u590d\u5236 \u4e91\u51fd\u6570 const cloud = require(&#8216;wx-server-sdk&#8217;) cloud.init() exports.main = async (event, context) =&gt; { const { value } = event; try { const res = await cloud.openapi.security.imgSecCheck({ media: { header: { &#8216;Content-Type&#8217;: &#8216;application\/octet-stream&#8217;}, contentType: &#8216;image\/png&#8217;, value: Buffer.from(value) } }) return res; } catch (err) { [&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-33434","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33434","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=33434"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33434\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}