123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import { encrypt } from '@/common/crypto.js'
- /* 小程序码 */
- import store from '@/store/index.js'
- const fs = uni.getFileSystemManager()
- const qrcodePath = `${wx.env.USER_DATA_PATH}/qrcodePath`
- const defalutOptions = {
- title: '护肤上颜选,正品有好货~',
- path: '/pages/index/index',
- imageUrl: 'https://static.caimei365.com/app/mini-hehe/icon/icon-share.png'
- }
- export function shareDataResult(shareData, title, coverUrl) {
- const state_str = encodeURIComponent(encrypt(shareData))
- const result = {
- title: title || defalutOptions.title,
- path: `${defalutOptions.path}?state_str=${state_str}`,
- imageUrl: coverUrl || defalutOptions.imageUrl
- }
- return result
- }
- const queryKeyOfMap = {
- 'type': 't',
- 'inviteUserId': 'i',
- 'activityId': 'a',
- 'dealerUserId': 'd',
- 'keyWord': 'k',
- 'productId': 'p',
- 'jumpState': 'j',
- }
- const enQueryKeyOfMap = {
- 't': 'type',
- 'i': 'inviteUserId',
- 'a': 'activityId',
- 'd': 'dealerUserId',
- 'k': 'keyWord',
- 'p': 'productId',
- 'j': 'jumpState',
- }
- // 创建二维码保存路径
- function createQrcodeDir(callback) {
- try {
- fs.accessSync(qrcodePath)
- console.log('已存在文件夹');
- callback(qrcodePath)
- } catch (e) {
- fs.mkdirSync(qrcodePath)
- console.log('不存在文件夹');
- callback(qrcodePath)
- }
- }
- /* 保存图片为临时路径 */
- function saveFileToTempFilePath(options = {}) {
- return new Promise((resolve, reject) => {
- // 调用创建文件夹Api
- createQrcodeDir(qrcodePath => {
- // 拼接二维码保存路径
- qrcodePath = qrcodePath + '/' + options.fileName
- // 写入文件到本地文件夹
- fs.writeFile({
- filePath: qrcodePath,
- data: options.qrcodeArrayBuffer,
- encoding: options.encoding || 'base64',
- success(res) {
- if (qrcodePath.startsWith('wxfile')) {
- resolve({
- tempFilePath: qrcodePath
- })
- } else {
- // 获取本地文件临时路径
- uni.downloadFile({
- url: qrcodePath,
- success(res) {
- console.log('获取本地文件临时路径', qrcodePath);
- console.log(res)
- resolve(res)
- },
- fail(error) {
- console.log('获取本地文件临时路径', qrcodePath);
- console.log(error)
- reject(error)
- }
- })
- }
- },
- fail(error) {
- console.log(error)
- reject(error)
- }
- })
- })
- })
- }
- // 请求二维码
- function requestWxUnlimited(params) {
- return new Promise((resolve, reject) => {
- // 发起http请求
- uni.request({
- url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${params.access_token}`,
- method: 'POST',
- responseType: 'arraybuffer',
- data: {
- page: params.pagePath || 'pages/index/index',
- scene: params.queryStr,
- check_path: process.env.NODE_ENV === 'production', // 是否校验页面
- env_version: process.env.NODE_ENV === 'production' ? "release" :
- 'develop', // 正式版 or 开发版
- width: 200, // 二维码宽度
- auto_color: false, // 自动颜色
- line_color: { "r": 0, "g": 0, "b": 0 }, // 线条颜色
- is_hyaline: true // 透明底
- },
- success(data) {
- console.log('二维码');
- console.log(data)
- resolve(data)
- },
- fail(error) {
- reject(error)
- }
- })
- })
- }
- /* 生成二维码链接 */
- export async function generateWxUnlimited(params) {
- try {
- // 从服务端获取二维码arrayBuffer
- const { data: qrcodeArrayBuffer } = await requestWxUnlimited({
- access_token: store.getters.accessToken,
- queryStr: codeQueryStr(params.queryStr) // 编码成字符更少的参数
- })
- // 将arrayBuffer转为文件并返回TempFilePath
- const result = await saveFileToTempFilePath({
- fileName: 'share-qrcode.png',
- qrcodeArrayBuffer
- })
- return result
- } catch (e) {
- debugger
- return e
- }
- }
- // 编码查询参数
- export function codeQueryStr(query = '') {
- const keys = Object.keys(queryKeyOfMap)
- return query.split('&').map(str => {
- return str.split('=').map((substr, index) => {
- if (!index) {
- return queryKeyOfMap[keys.find(item => substr === item)]
- } else {
- return substr
- }
- }).join('=')
- }).join('&')
- }
- // 反编码查询参数
- export function enCodeQueryStr(query) {
- const keys = Object.keys(enQueryKeyOfMap)
- return query.split('&').map(str => {
- return str.split('=').map((substr, index) => {
- if (!index) {
- return enQueryKeyOfMap[keys.find(item => substr === item)]
- } else {
- return substr
- }
- }).join('=')
- }).join('&')
- }
|