-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support script to generate qrcode (#917)
* fix: update demo * feat: support script to generate qrcode * feat: update qrcode
- Loading branch information
Showing
68 changed files
with
208 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { get, post } = require('./httpRequest'); | ||
|
||
/** | ||
* 获取access_token, 网页调试工具:https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E5%9F%BA%E7%A1%80%E6%94%AF%E6%8C%81&form=%E8%8E%B7%E5%8F%96access_token%E6%8E%A5%E5%8F%A3%20/token&token=&lang=zh_CN | ||
* @api https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET | ||
* @method GET | ||
* @parameter grant_type | ||
* @parameter appid | ||
* @parameter secret | ||
* @return Object | ||
*/ | ||
const getAccessToken = (appId, appSecret) => { | ||
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`; | ||
return get(url); | ||
}; | ||
|
||
/** | ||
* 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 | ||
* @api https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN | ||
* @method POST | ||
* @parameter access_token | ||
* @parameter body | ||
* @parameter scene //跳转带参 | ||
* @parameter path //跳转页面 | ||
* @return 二成功时返回的是 Buffer ,失败时返回 JSON | ||
*/ | ||
const getUnlimitedQRCode = (token, parameter, config) => { | ||
const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`; | ||
return post(url, parameter, config); | ||
}; | ||
|
||
module.exports = { | ||
getAccessToken, | ||
getUnlimitedQRCode, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** axios封装 | ||
* 请求拦截、响应拦截、错误统一处理 | ||
*/ | ||
const axios = require('axios'); | ||
|
||
// axios.defaults.timeout = 10000; // 超时抛出异常 | ||
// axios.defaults.withCredentials = true; | ||
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; // post请求头 | ||
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; // 默认异步请求 | ||
|
||
// 请求拦截 | ||
axios.interceptors.request.use( | ||
(config) => config, | ||
(error) => Promise.error(error), | ||
); | ||
// 响应拦截 | ||
axios.interceptors.response.use( | ||
(response) => { | ||
if (response.status === 200) { | ||
return Promise.resolve(response); | ||
} | ||
return Promise.reject(response); | ||
}, | ||
// 服务器状态码不是200的情况 | ||
(error) => { | ||
if (error.response.status) { | ||
return Promise.reject(error.response); | ||
} | ||
}, | ||
); | ||
|
||
/** | ||
* @description get请求 | ||
* @parameter {String} url [请求的url地址] | ||
* @parameter {Object} parameter [请求时携带的参数] | ||
* @parameter {Object} config [其他配置信息] | ||
*/ | ||
const get = (url, parameter, config) => { | ||
return new Promise((resolve, reject) => { | ||
axios | ||
.get(url, parameter, config) | ||
.then((res) => { | ||
resolve(res.data); | ||
}) | ||
.catch((err) => { | ||
reject(err.data); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* @description post请求 | ||
* @parameter {String} url [请求的url地址] | ||
* @parameter {Object} parameter [请求时携带的参数] | ||
* @parameter {Object} config [其他配置信息] | ||
*/ | ||
|
||
const post = (url, parameter, config) => { | ||
return new Promise((resolve, reject) => { | ||
axios | ||
.post(url, parameter, config) | ||
.then((res) => { | ||
resolve(res.data); | ||
}) | ||
.catch((err) => { | ||
reject(err.data); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
get, | ||
post, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { getAccessToken, getUnlimitedQRCode } = require('./api'); | ||
|
||
// 去读 app.json 中 pages 字段 | ||
const { pages } = require('../../example/app.json'); | ||
|
||
const APP_ID = process.argv[process.argv.indexOf('--APP_ID') + 1]; // 在 --APP_ID 后面 | ||
const APP_SECRET = process.argv[process.argv.indexOf('--APP_SECRET') + 1]; // --APP_SECRET 后面 | ||
|
||
const getUnlimitedQRCodeImage = (appid, appSecret) => { | ||
getAccessToken(appid, appSecret).then((e) => { | ||
if (e.access_token) { | ||
const token = e.access_token; | ||
// eslint-disable-next-line no-console | ||
console.log('==access_token 2h内有效=', token); | ||
const baseParameter = { | ||
width: 280, // 小程序码大小 | ||
// check_path: false, | ||
}; | ||
const baseConfig = { | ||
responseType: 'arraybuffer', | ||
}; | ||
|
||
// 循环 pages, 获取相应小程序码 | ||
pages.forEach((item, index) => { | ||
const temp = [...new Set(item.split('/').slice(1))]; | ||
const fileName = temp.join('-'); | ||
|
||
const specialParameter = { | ||
page: item, // 扫码进入的小程序页面路径 | ||
scene: `name=${temp[0]}`, // 标识 | ||
}; | ||
getUnlimitedQRCode(token, { ...specialParameter, ...baseParameter }, { ...baseConfig }).then((res) => { | ||
// 因为微信接口 getwxacodeunlimit 成功时返回的是 Buffer ,失败时返回 JSON 结构。这里把返回数据全部当成 Buffer 处理,所以 res.length < 200, 则表示获取失败。 | ||
if (res.length < 200) { | ||
const { errcode, errmsg } = JSON.parse(res.toString()); | ||
// eslint-disable-next-line no-console | ||
console.log('===小程序码获取失败===', item, { errcode, errmsg }); | ||
return; | ||
} | ||
|
||
const buffer = Buffer.from(res, 'base64'); | ||
const destPath = path.resolve(__dirname, `../../site/public/assets/qrcode/${fileName}.png`); | ||
|
||
fs.writeFile( | ||
destPath, | ||
buffer, | ||
{ | ||
encoding: 'binary', | ||
flag: 'w+', | ||
}, | ||
(err) => { | ||
if (err) { | ||
// eslint-disable-next-line no-console | ||
console.log('===小程序码图片存储错误===', err); | ||
} | ||
}, | ||
); | ||
}); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* @description 命令行生成小程序码 npm run qrcode -- --APP_ID xxx --APP_SECRET xxx | ||
*/ | ||
getUnlimitedQRCodeImage(APP_ID, APP_SECRET); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Component({}); | ||
Page({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": { | ||
"base": "./base", | ||
"multiple": "./multiple", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Page({ | ||
goToProgress() { | ||
wx.redirectTo({ | ||
url: '/pages/loading/loadingProgress/loadingProgress', | ||
url: '/pages/loading/loadingProgress/index', | ||
}); | ||
}, | ||
}); |