{"id":32499,"date":"2024-11-25T13:17:18","date_gmt":"2024-11-25T05:17:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32499\/"},"modified":"2024-11-25T13:17:18","modified_gmt":"2024-11-25T05:17:18","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%b0%86base64%e5%9b%be%e7%89%87%e4%bf%9d%e5%ad%98%e5%88%b0%e7%9b%b8%e5%86%8c%e4%b8%ad%e7%9a%84%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32499\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u4e2d\u5c06base64\u56fe\u7247\u4fdd\u5b58\u5230\u76f8\u518c\u4e2d\u7684\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u4e2d\u5c06base64\u56fe\u7247\u4fdd\u5b58\u5230\u76f8\u518c\u4e2d\u7684\u65b9\u6cd5\u4ecb\u7ecd\uff0c\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4e00\u3001\u6388\u6743\u83b7\u53d6<\/p>\n<p>1\u3001\u76f8\u5173api<br \/>wx.getSetting<br \/>wx.authorize<\/p>\n<p>2\u3001\u6388\u6743\u83b7\u53d6\u6d41\u7a0b\u4e00\u822c\u4e3a<br \/> \u662f\u5426\u6709\u8be5\u6743\u9650 \u2014\u2014&gt; \u82e5\u65e0 \u2014\u2014&gt; \u8c03\u8d77\u6388\u6743\u5f39\u7a97 \u2014\u2014&gt; \u540c\u610f \u2014\u2014&gt; \u4f7f\u7528\u76f8\u5173api<br \/> \uff08\u5982\u679c\u7528\u6237\u62d2\u7edd\u6388\u6743\u5219\u53ef\u4f7f\u7528wx.opensetting\u5f15\u5bfc\u7528\u6237\u524d\u5f80\u6388\u6743\u8bbe\u7f6e\u9875\u6388\u6743\uff09<\/p>\n<p>3\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<pre>static async weAuthCheck(type = 'address') {\n    let resGetting = await new Promise((resolve, reject) =&gt; {\n        wepy.getSetting({\n            success: res =&gt; {\n                \/\/ console.log(res, 'getsetting')\n                if (res.authSetting.hasOwnProperty(`scope.${type}`) &amp;&amp; res.authSetting[`scope.${type}`]) {\n                    resolve({\n                        succeeded: true\n                    })\n                } else {\n                    wepy.authorize({\n                        scope: `scope.${type}`,\n                        success: () =&gt; {\n                            resolve({\n                                succeeded: true\n                            })\n                        },\n                        fail: err =&gt; {\n                            \/\/ console.log(err, 'errrrr')\n                            resolve({\n                                succeeded: false,\n                                err: err\n                            })\n                        }\n                    })\n                }\n            },\n            fail: err =&gt; {\n                resolve({\n                    succeeded: false,\n                    err: err\n                })\n            }\n        })\n    })\n    console.log('getSetting res: \n', resGetting)\n    return resGetting\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4e8c\u3001\u5199\u5165\u4e34\u65f6\u6587\u4ef6<\/strong><\/p>\n<p>1\u3001\u76f8\u5173api<br \/>\u6587\u4ef6\u7cfb\u7edf<br \/>writeFile<\/p>\n<p>2\u3001\u53c2\u6570encoding \u7528\u4e8e\u8bf4\u660e \u5199\u5165\u7684\u53c2\u6570data\u7684\u683c\u5f0f\u662f\u4ec0\u4e48\uff0c\u5e76\u975e\u662f\u5c06data\u6309encoding\u7684\u5f62\u5f0f\u5199\u5165\u3002\u5728\u8fd9\u91cc\u6211\u4eec\u5e94\u8be5\u6307\u5b9aencoding\u4e3abase64<\/p>\n<p>3\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<pre>\/\/ \u5148\u83b7\u5f97\u4e00\u4e2a\u5b9e\u4f8b  this.fileManager = wx.getFileSystemManager()\nthis.fileManager.writeFile({\n    filePath: `${wx.env.USER_DATA_PATH}\/qrcode_${timestamp}.png`,\n    data: data,\n    encoding: 'base64',\n    success: res =&gt; {\n        console.log('res: \n:', res)\n    },\n    fail: res =&gt; {\n        console.log(res)\n    }\n})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4e09\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>1\u3001base64\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\uff1a &#8220;data:image\/png;base64,&#8230;&#8230;&#8230;..&#8221;\uff0c\u9017\u53f7\u524d\u9762\u8fd9\u4e00\u6bb5\u4e3a\u683c\u5f0f\u8bf4\u660e\uff0c\u7528\u4e8e\u8bf4\u660e\u540e\u7eed\u7684\u5185\u5bb9\u683c\u5f0f\u662f\u56fe\u7247\u683c\u5f0f\u4e3apng\u7684base64\u683c\u5f0f\u3002<\/p>\n<p>2\u3001\u5982\u679c\u76f4\u63a5\u5c06\u6574\u4e00\u4e32\u5b57\u7b26\u4f20\u5165\uff0c\u867d\u7136\u53ef\u4ee5\u4fdd\u5b58\u6210\u529f\uff0c\u4f46\u662f\u5c06\u5bfc\u81f4\u56fe\u7247\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\u3002\u56e0\u6b64\u518d\u505a\u4e00\u6b65\u5207\u5272\u64cd\u4f5c<\/p>\n<pre>let&nbsp;startIdx&nbsp;=&nbsp;this.qrcode.indexOf('base64,')&nbsp;+&nbsp;7<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u56db\u3001\u5b8c\u6574\u5b9e\u73b0<\/strong><\/p>\n<pre>async onTapSaveQrcode() {\n    let startIdx = this.qrcode.indexOf('base64,') + 7\n    let resCheck = await this.$weAuthCheck('writePhotosAlbum')\n    let timestamp = new Date().getTime()\n    let self = this\n    if (resCheck.succeeded) {\n        wepy.showLoading()\n        this.fileManager.writeFile({\n            filePath: `${wx.env.USER_DATA_PATH}\/qrcode_${timestamp}.png`,\n            data: this.qrcode.slice(startIdx),\n            encoding: 'base64',\n            success: res =&gt; {\n                console.log('res: \n:', res)\n                wx.saveImageToPhotosAlbum({\n                    filePath: `${wx.env.USER_DATA_PATH}\/qrcode_${timestamp}.png`,\n                    success: res =&gt; {\n                        self.$emit('save-qrcode-success')\n                        wepy.showToast({\n                            title: '\u4fdd\u5b58\u6210\u529f'\n                        })\n                    },\n                    fail: err =&gt; {\n                        console.log(err)\n                        if (!err.errMsg.includes('cancel')) {\n                            wepy.showToast({\n                                title: err.errMsg,\n                                icon: 'none'\n                            })\n                        }\n                    },\n                    complete: () =&gt; {\n                        wepy.hideLoading()\n                    }\n                })\n            },\n            fail: res =&gt; {\n                wepy.hideLoading()\n                console.log(res)\n            }\n        })\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u4e2d\u5c06base64\u56fe\u7247\u4fdd\u5b58\u5230\u76f8\u518c\u4e2d\u7684\u65b9\u6cd5\u4ecb\u7ecd\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>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u4e2d\u5c06base64\u56fe\u7247\u4fdd\u5b58\u5230\u76f8\u518c\u4e2d\u7684\u65b9\u6cd5\u4ecb\u7ecd\uff0c\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002 \u4e00\u3001\u6388\u6743\u83b7\u53d6 1\u3001\u76f8\u5173apiwx.getSettingwx.authorize 2\u3001\u6388\u6743\u83b7\u53d6\u6d41\u7a0b\u4e00\u822c\u4e3a \u662f\u5426\u6709\u8be5\u6743\u9650 \u2014\u2014&gt; \u82e5\u65e0 \u2014\u2014&gt; \u8c03\u8d77\u6388\u6743\u5f39\u7a97 \u2014\u2014&gt; \u540c\u610f \u2014\u2014&gt; \u4f7f\u7528\u76f8\u5173api \uff08\u5982\u679c\u7528\u6237\u62d2\u7edd\u6388\u6743\u5219\u53ef\u4f7f\u7528wx.opensetting\u5f15\u5bfc\u7528\u6237\u524d\u5f80\u6388\u6743\u8bbe\u7f6e\u9875\u6388\u6743\uff09 3\u3001\u4ee3\u7801\u5b9e\u73b0 static async weAuthCheck(type = &#8216;address&#8217;) { let resGetting = await new Promise((resolve, reject) =&gt; { wepy.getSetting({ success: res =&gt; { \/\/ console.log(res, &#8216;getsetting&#8217;) if (res.authSetting.hasOwnProperty(`scope.${type}`) &amp;&amp; res.authSetting[`scope.${type}`]) { resolve({ succeeded: true }) } else { wepy.authorize({ scope: `scope.${type}`, success: () [&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-32499","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32499","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=32499"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32499\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}