share.helper.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { encrypt } from '@/common/crypto.js'
  2. /* 小程序码 */
  3. import store from '@/store/index.js'
  4. const fs = uni.getFileSystemManager()
  5. const qrcodePath = `${wx.env.USER_DATA_PATH}/qrcodePath`
  6. const defalutOptions = {
  7. title: '护肤上颜选,正品有好货~',
  8. path: '/pages/index/index',
  9. imageUrl: 'https://static.caimei365.com/app/mini-hehe/icon/icon-share.png'
  10. }
  11. export function shareDataResult(shareData, title, coverUrl) {
  12. const state_str = encodeURIComponent(encrypt(shareData))
  13. const result = {
  14. title: title || defalutOptions.title,
  15. path: `${defalutOptions.path}?state_str=${state_str}`,
  16. imageUrl: coverUrl || defalutOptions.imageUrl
  17. }
  18. return result
  19. }
  20. const queryKeyOfMap = {
  21. 'type': 't',
  22. 'inviteUserId': 'i',
  23. 'activityId': 'a',
  24. 'dealerUserId': 'd',
  25. 'keyWord': 'k',
  26. 'productId': 'p',
  27. 'jumpState': 'j',
  28. }
  29. const enQueryKeyOfMap = {
  30. 't': 'type',
  31. 'i': 'inviteUserId',
  32. 'a': 'activityId',
  33. 'd': 'dealerUserId',
  34. 'k': 'keyWord',
  35. 'p': 'productId',
  36. 'j': 'jumpState',
  37. }
  38. // 创建二维码保存路径
  39. function createQrcodeDir(callback) {
  40. try {
  41. fs.accessSync(qrcodePath)
  42. console.log('已存在文件夹');
  43. callback(qrcodePath)
  44. } catch (e) {
  45. fs.mkdirSync(qrcodePath)
  46. console.log('不存在文件夹');
  47. callback(qrcodePath)
  48. }
  49. }
  50. /* 保存图片为临时路径 */
  51. function saveFileToTempFilePath(options = {}) {
  52. return new Promise((resolve, reject) => {
  53. // 调用创建文件夹Api
  54. createQrcodeDir(qrcodePath => {
  55. // 拼接二维码保存路径
  56. qrcodePath = qrcodePath + '/' + options.fileName
  57. // 写入文件到本地文件夹
  58. fs.writeFile({
  59. filePath: qrcodePath,
  60. data: options.qrcodeArrayBuffer,
  61. encoding: options.encoding || 'base64',
  62. success(res) {
  63. if (qrcodePath.startsWith('wxfile')) {
  64. resolve({
  65. tempFilePath: qrcodePath
  66. })
  67. } else {
  68. // 获取本地文件临时路径
  69. uni.downloadFile({
  70. url: qrcodePath,
  71. success(res) {
  72. console.log('获取本地文件临时路径', qrcodePath);
  73. console.log(res)
  74. resolve(res)
  75. },
  76. fail(error) {
  77. console.log('获取本地文件临时路径', qrcodePath);
  78. console.log(error)
  79. reject(error)
  80. }
  81. })
  82. }
  83. },
  84. fail(error) {
  85. console.log(error)
  86. reject(error)
  87. }
  88. })
  89. })
  90. })
  91. }
  92. // 请求二维码
  93. function requestWxUnlimited(params) {
  94. return new Promise((resolve, reject) => {
  95. // 发起http请求
  96. uni.request({
  97. url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${params.access_token}`,
  98. method: 'POST',
  99. responseType: 'arraybuffer',
  100. data: {
  101. page: params.pagePath || 'pages/index/index',
  102. scene: params.queryStr,
  103. check_path: process.env.NODE_ENV === 'production', // 是否校验页面
  104. env_version: process.env.NODE_ENV === 'production' ? "release" :
  105. 'develop', // 正式版 or 开发版
  106. width: 200, // 二维码宽度
  107. auto_color: false, // 自动颜色
  108. line_color: { "r": 0, "g": 0, "b": 0 }, // 线条颜色
  109. is_hyaline: true // 透明底
  110. },
  111. success(data) {
  112. console.log('二维码');
  113. console.log(data)
  114. resolve(data)
  115. },
  116. fail(error) {
  117. reject(error)
  118. }
  119. })
  120. })
  121. }
  122. /* 生成二维码链接 */
  123. export async function generateWxUnlimited(params) {
  124. try {
  125. // 从服务端获取二维码arrayBuffer
  126. const { data: qrcodeArrayBuffer } = await requestWxUnlimited({
  127. access_token: store.getters.accessToken,
  128. queryStr: codeQueryStr(params.queryStr) // 编码成字符更少的参数
  129. })
  130. // 将arrayBuffer转为文件并返回TempFilePath
  131. const result = await saveFileToTempFilePath({
  132. fileName: 'share-qrcode.png',
  133. qrcodeArrayBuffer
  134. })
  135. return result
  136. } catch (e) {
  137. debugger
  138. return e
  139. }
  140. }
  141. // 编码查询参数
  142. export function codeQueryStr(query = '') {
  143. const keys = Object.keys(queryKeyOfMap)
  144. return query.split('&').map(str => {
  145. return str.split('=').map((substr, index) => {
  146. if (!index) {
  147. return queryKeyOfMap[keys.find(item => substr === item)]
  148. } else {
  149. return substr
  150. }
  151. }).join('=')
  152. }).join('&')
  153. }
  154. // 反编码查询参数
  155. export function enCodeQueryStr(query) {
  156. const keys = Object.keys(enQueryKeyOfMap)
  157. return query.split('&').map(str => {
  158. return str.split('=').map((substr, index) => {
  159. if (!index) {
  160. return enQueryKeyOfMap[keys.find(item => substr === item)]
  161. } else {
  162. return substr
  163. }
  164. }).join('=')
  165. }).join('&')
  166. }