{"id":306,"date":"2021-09-26T20:53:10","date_gmt":"2021-09-26T12:53:10","guid":{"rendered":"http:\/\/gjweb.top\/?p=306"},"modified":"2021-09-26T20:53:11","modified_gmt":"2021-09-26T12:53:11","slug":"%e4%b8%83%e7%89%9b%e4%ba%91%e9%9f%b3%e8%a7%86%e9%a2%91%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=306","title":{"rendered":"\u4e03\u725b\u4e91\u97f3\u89c6\u9891\u5b58\u50a8"},"content":{"rendered":"\n<p>\u5728\u4e0a\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u6ce8\u518c\u4e03\u725b\u4e91\u8d26\u53f7\uff0c\u5f00\u901a\u4e03\u725b\u4e91\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1\u3002\u5b98\u65b9\u4f1a\u63d0\u4f9b <strong>accessKey<\/strong> \u548c <strong>secretKey<\/strong> \u4f5c\u4e3a\u6388\u6743token\u7684\u53d1\u653e\uff1b\u5176\u6b21\u8fd8\u9700\u8981\u5728\u540e\u53f0\u7ba1\u7406\u65b0\u5efa\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/z3.ax1x.com\/2021\/09\/26\/4cwBRS.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/z3.ax1x.com\/2021\/09\/26\/4cwBRS.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"4cwBRS.png\"\/><\/div><\/figure>\n\n\n\n<p><a href=\"https:\/\/imgtu.com\/i\/4cwBRS\"><\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528express\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20<\/h2>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install formidable qiniu --save-dev<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\u521b\u5efa <strong>config.js<\/strong> \u6587\u4ef6\u7528\u6765\u901a\u7528\u53c2\u6570\u7684\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let config = {};<br>config.accessKey = \"xxx\";<br>config.secretKey = 'xxx';<br>config.bucket = 'xxx'; &nbsp;\/\/\u5b58\u50a8\u7a7a\u95f4\u7684\u540d\u5b57<br>config.url = 'xxx'; &nbsp;\/\/\u914d\u7f6e\u7684\u57df\u540d<br>module.exports = config;<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa <strong>qn.js<\/strong> \u6587\u4ef6\u5c01\u88c5\u4e03\u725b\u4e0a\u4f20\u53ca\u9a8c\u8bc1API<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let qiniu = require('qiniu');<br>let formidable = require('formidable');<br>let fs = require('fs');<br>let config = require('.\/config');<br>\u200b<br>let qn = {};<br>\/\/ \u8981\u4e0a\u4f20\u7684\u7a7a\u95f4\u53ca\u53c2\u6570<br>let bucket = config.bucket;<br>let accessKey = config.accessKey<br>let secretKey = config.secretKey<br>\/\/ \u83b7\u53d6\u4e03\u725b\u4e91token<br>qn.uptoken = function (bucket) {<br> &nbsp; &nbsp;var putPolicy = new qiniu.rs.PutPolicy({ scope: bucket });<br> &nbsp; &nbsp;var accessKey = accessKey;<br> &nbsp; &nbsp;var secretKey = secretKey;<br> &nbsp; &nbsp;var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);<br> &nbsp; &nbsp;var uploadToken = putPolicy.uploadToken(mac);<br> &nbsp; &nbsp;return uploadToken;<br>}<br>\u200b<br>\/\/ \u6587\u4ef6\u4e0a\u4f20\u914d\u7f6e\u53ca\u6784\u5efa<br>qn.upFlie = function (req, callback) {<br> &nbsp; &nbsp;let callbackObj = {}; &nbsp; \/\/\u56de\u8c03\u51fd\u6570\u8fd4\u56de\u7684\u5bf9\u8c61<br> &nbsp; &nbsp;var form = new formidable.IncomingForm(); &nbsp; \/\/\u521b\u5efa\u4e0a\u4f20\u8868\u5355<br> &nbsp; &nbsp;form.encoding = 'utf-8'; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6e\u7f16\u8f91<br> &nbsp; &nbsp;form.keepExtensions = true; &nbsp; &nbsp; \/\/\u4fdd\u7559\u540e\u7f00<br> &nbsp; &nbsp;form.parse(req, function (err, fields, files) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (err) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.status = 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.msg = err;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('err==', err);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return callback(callbackObj);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u4e0a\u4f20\u5230\u4e03\u725b\u540e\u4fdd\u5b58\u7684\u6587\u4ef6\u540d<br> &nbsp; &nbsp; &nbsp; &nbsp;let key = new Date().getTime();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u751f\u6210\u4e0a\u4f20 Token<br> &nbsp; &nbsp; &nbsp; &nbsp;let token = qn.uptoken(bucket);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8981\u4e0a\u4f20\u6587\u4ef6\u7684\u672c\u5730\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp;let filePath = files.file.path;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5982\u679c\u662f\u8868\u5355\u7684\u6570\u636e\u63d0\u4ea4\u4f1a\u5b58\u50a8\u5728fields\u4e2d\uff0c\u56fe\u7247\u6587\u4ef6\u63d0\u4ea4\u4f1a\u5b58\u50a8\u5728files\u4e2d files.file\u4e2d\u7684file\u6307\u7684\u662f\u524d\u7aef\u8bbe\u5b9ainput\u7684name\u4e00\u822c\u8bbe\u7f6e\u4e3afile<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6784\u9020\u4e0a\u4f20\u51fd\u6570<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6587\u4ef6\u4e0a\u4f20(\u56db\u884c\u4ee3\u7801\u90fd\u662f\u4e03\u725b\u4e91\u4e0a\u4f20\u6587\u4ef6\u7684\u914d\u7f6e\u8bbe\u7f6e)<br> &nbsp; &nbsp; &nbsp; &nbsp;var Config = new qiniu.conf.Config();<br> &nbsp; &nbsp; &nbsp; &nbsp;Config.zone = qiniu.zone.Zone_z1;<br> &nbsp; &nbsp; &nbsp; &nbsp;var formUploader = new qiniu.form_up.FormUploader(Config);<br> &nbsp; &nbsp; &nbsp; &nbsp;var putExtra = new qiniu.form_up.PutExtra();<br> &nbsp; &nbsp; &nbsp; &nbsp;formUploader.putFile(token, key, filePath, putExtra, function (respErr, respBody, respInfo) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (respErr) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.status = 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.msg = respErr;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return callback(callbackObj);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (respInfo.statusCode === 200) { \/\/ \u4e0a\u4f20\u6210\u529f<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8f93\u51fa JSON \u683c\u5f0f <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var response = {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"url\": `${config.url}\/${respBody.key}`<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  };<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.status = 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.data = response;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return callback(callbackObj);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  } else { \/\/ \u4e0a\u4f20\u5931\u8d25<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.status = 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbackObj.msg = respBody;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return callback(callbackObj);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  })<br>}<br>module.exports = qn;<\/code><\/pre>\n\n\n\n<p>\u5728 <strong>index.js<\/strong> \u4f7f\u7528\u5c01\u88c5\u597d\u7684 <strong>qn API<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const express = require(\"express\")<br>let qn = require('.\/qn');<br>const app = express()<br>app.use((req, res, next) =&gt; {<br> &nbsp; &nbsp;\/\/\u8bbe\u7f6e\u8bf7\u6c42\u5934<br> &nbsp; &nbsp;res.set({<br> &nbsp; &nbsp; &nbsp; &nbsp;'Access-Control-Allow-Credentials': true,<br> &nbsp; &nbsp; &nbsp; &nbsp;'Access-Control-Allow-Origin':'*',<br> &nbsp; &nbsp; &nbsp; &nbsp;'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',<br> &nbsp; &nbsp; &nbsp; &nbsp;'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',<br> &nbsp; &nbsp; &nbsp; &nbsp;'Content-Type': 'application\/json; charset=utf-8'<br> &nbsp;  })<br> &nbsp; &nbsp;req.method === 'OPTIONS' ? res.status(204).end() : next()<br>})<br>let result = function (obj,bool){<br> &nbsp; &nbsp;if(bool){<br> &nbsp; &nbsp; &nbsp; &nbsp;return {status:0,data:obj};<br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;return {status:1,data:obj};<br> &nbsp;  }<br>}<br>app.post(\"\/upFlie\",function(req,routerRes){<br> &nbsp; &nbsp;try{<br> &nbsp; &nbsp; &nbsp; &nbsp;qn.upFlie(req,function(res){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"res\",res)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(res.status == 0){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;routerRes.json(result(res.data,true));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;routerRes.json(result(res.msg,false));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  }catch(err){<br> &nbsp; &nbsp; &nbsp; &nbsp;if(err){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"trychtch\u62a5\u9519====\",err)<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>})<br>app.get(\"\/viodeInfo\",function(req,res){<br> &nbsp; &nbsp;let response = qn.parseCode(req.query.code)<br> &nbsp; &nbsp;res.send(JSON.stringify({<br> &nbsp; &nbsp; &nbsp; &nbsp;data:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url:response<br> &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp;message:\"\u67e5\u8be2\u6210\u529f\"<br> &nbsp;  }))<br>})<br>\u200b<br>app.listen(7501,(err)=&gt;{<br> &nbsp; &nbsp;if(err){<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"\u670d\u52a1\u5668\u542f\u52a8\u5931\u8d25\")<br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\")<br> &nbsp;  }<br>})<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f\u5728\u83b7\u53d6\u89c6\u9891\u5730\u5740\u65f6\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u79c1\u7528\u5b58\u50a8\u7a7a\u95f4\uff0c\u4e03\u725b\u4e91\u5bf9\u5730\u5740\u52a0\u5165\u4e86token\u6388\u6743\u7684\u65f6\u5e38\uff0c\u56e0\u6b64\u9700\u8981\u5bf9\u5730\u5740\u8fdb\u884c\u79c1\u6709\u5730\u5740\u89e3\u6790\uff0c\u5728 qn.js \u6dfb\u52a0\u89e3\u6790API<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u89e3\u5bc6\u79c1\u6709\u7a7a\u95f4<br>qn.parseCode = function (keyCode) {<br> &nbsp; &nbsp;\/\/ \u914d\u7f6e\u79c1\u6709\u7a7a\u95f4\u53c2\u6570<br> &nbsp; &nbsp;let key = keyCode<br> &nbsp; &nbsp;var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);<br> &nbsp; &nbsp;var Config = new qiniu.conf.Config();<br> &nbsp; &nbsp;Config.zone = qiniu.zone.Zone_z1;<br> &nbsp; &nbsp;var bucketManager = new qiniu.rs.BucketManager(mac, Config);<br> &nbsp; &nbsp;var privateBucketDomain = config.url;<br> &nbsp; &nbsp;var deadline = parseInt(Date.now() \/ 1000) + 3600; \/\/ 1\u5c0f\u65f6\u8fc7\u671f<br> &nbsp; &nbsp;var privateDownloadUrl = bucketManager.privateDownloadUrl(privateBucketDomain, key, deadline);<br> &nbsp; &nbsp;return privateDownloadUrl<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u7aef\u8c03\u7528-\u4f7f\u7528elementUI \u4e0a\u4f20\u7ec4\u4ef6<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;el-upload<br> &nbsp;class=\"upload-demo\"<br>  :on-success=\"success\"<br>  :on-error=\"error\"<br> &nbsp;action=\"http:\/\/localhost:7501\/upFlie\"&gt;<br> &nbsp;&lt;el-button size=\"small\" type=\"primary\"&gt;\u70b9\u51fb\u4e0a\u4f20&lt;\/el-button&gt;<br>&lt;\/el-upload&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e0a\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u6ce8\u518c\u4e03\u725b\u4e91\u8d26\u53f7\uff0c\u5f00\u901a\u4e03\u725b\u4e91\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1\u3002\u5b98\u65b9\u4f1a\u63d0\u4f9b accessKey \u548c secret [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-38"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}