微信小程序开发者工具生成预览二维码
<p>由于项目测试人员越来越多,经常需要开发提供二维码测试,所以需要一种动态生成小程序预览二维码的工具。</p><p>开发前需要一个无人使用的开发者账号,用于生成二维码。(使用测试的微信号就行)</p>
<p>具体流程为</p>
<p><img src="https://img2020.cnblogs.com/blog/218565/202010/218565-20201029142752560-1001887647.png"></p>
<p>可以直接使用mpvue taro 等框架直接打包的代码,或者小程序原生未打开增强编译的代码。如果有一下ES新特性的代码,需要编译一下才能打包使用。</p>
<p>第一步是打包 这里使用的是开发工具的源码</p>
<p>./pack.js</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">"use strict";
const a = require("glob"),
b = require("fs"),
c = require("path"),
d = require("crypto");
module.exports = async (e, f, g = {}) => {
const h = [
Buffer.alloc(1),
Buffer.alloc(4),
Buffer.alloc(4),
Buffer.alloc(4),
Buffer.alloc(1),
];
(h = 190), h.writeInt32BE(0, 0), (h = 237);
let i,
j,
k,
l = 0;
const m = [],
n = [];
return new Promise((o, p) => {
const q = Object.assign({ nodir: !0 }, g),
r = {};
let s = 0;
a(`${e}/**`, q, (a, q) => {
if (!a) {
q.forEach((a) => {
const f = b.readFileSync(a),
h = c.relative(e, a);
if (g.needMd5) {
const a = d.createHash("md5");
a.update(f);
const b = a.digest("hex");
if (((r = b), g.ignoreFileMd5 && g.ignoreFileMd5 === b))
return;
}
const i = Buffer.from(`/${h.replace(/\\/g, "/")}`);
l++,
m.push(i),
n.push(f),
/\.js\.map$/.test(a) ||
((s += f.length), (s += i.length), (s += 12));
});
let a = 18 + 12 * l + Buffer.concat(m).length;
k = m.map((b, c) => {
const d = Buffer.alloc(4);
d.writeInt32BE(b.length, 0);
const e = Buffer.alloc(4),
f = n.length,
g = a;
e.writeInt32BE(g, 0), (a += f);
const h = Buffer.alloc(4);
return h.writeInt32BE(f, 0), Buffer.concat();
});
const p = Buffer.alloc(4);
p.writeInt32BE(l, 0),
k.unshift(p),
(i = Buffer.concat(k)),
(j = Buffer.concat(n)),
h.writeInt32BE(i.length, 0),
h.writeInt32BE(j.length, 0);
const t = Buffer.concat(h),
u = Buffer.concat();
(s += 18),
o({ destPath: f, data: u, totalSize: s, fileMd5Info: r });
} else p(a);
});
});
};
</pre>
</div>
<p> 第二步是上传打包数据</p>
<div class="cnblogs_code">
<pre>const zlib = require("zlib"<span style="color: rgba(0, 0, 0, 1)">);
const request </span>= require("request"<span style="color: rgba(0, 0, 0, 1)">);
const readFile </span>= require("./pack"<span style="color: rgba(0, 0, 0, 1)">);
exports.testsource </span>=(src, config) =><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve, reject) =><span style="color: rgba(0, 0, 0, 1)"> {
const params </span>=<span style="color: rgba(0, 0, 0, 1)"> [
</span>"_r=" +<span style="color: rgba(0, 0, 0, 1)"> Math.random(),
</span>"newticket=" +<span style="color: rgba(0, 0, 0, 1)"> config.newticket,
</span>"appid=" +<span style="color: rgba(0, 0, 0, 1)"> config.appid,
</span>"path="+<span style="color: rgba(0, 0, 0, 1)">config.path,
</span>"debug_launch_info=" +<span style="color: rgba(0, 0, 0, 1)"> encodeURIComponent(config.launchInfo),
</span>"platform=0&ext_appid=&os=win&clientversion=1032006090&gzip=1&is_game_engine_app=1&devplugin=0"<span style="color: rgba(0, 0, 0, 1)">,
];
readFile(src).then(({ data }) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
const url </span>=
"https://servicewechat.com/wxa-dev/testsource?" + params.join("&"<span style="color: rgba(0, 0, 0, 1)">);
request(
{ url, body: zlib.gzipSync(data), method: </span>"post"<span style="color: rgba(0, 0, 0, 1)"> },
(error, response, body) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
reject(error);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(response.statusCode != 200<span style="color: rgba(0, 0, 0, 1)">){
reject({statusCode:response.statusCode})
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
resolve(JSON.parse(body));
}
);
});
});
};</span></pre>
</div>
<p>其中src为源代码的根目录的路径。</p>
<p>config为配置项,config.newticket为开发工具的令牌,具体获取和更新查看《微信小程序开发者工具获取和更新newticket》</p>
<div>config.appid:当前小程序的appid</div>
<div>
<div>config.path:小程序入口的路径</div>
<div>
<div>config.launchInfo:入口数据的配置 是一个json字符串 {"scene":1001,"path":"pages/index/index","query":{}} 分别对应的是场景值 入口路径和参数</div>
<div> </div>
<div>最后通过上传,微信服务器会返回一个base64字符串的图片,解析查看即为预览的二维码。</div>
</div>
</div><br><br>
来源:https://www.cnblogs.com/1zero1/p/13896747.html
頁:
[1]