share.helper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { encrypt } from '@/common/crypto.js'
  2. /* 小程序码 */
  3. import store from '@/store/index.js'
  4. import { wxUnlimited } from '@/services/api/auth.js'
  5. const fs = uni.getFileSystemManager()
  6. const qrcodePath = `${wx.env.USER_DATA_PATH}/qrcodePath`
  7. const defalutOptions = {
  8. title: '护肤上颜选,正品有好货~',
  9. path: '/pages/index/index',
  10. imageUrl: 'https://static.caimei365.com/app/mini-hehe/icon/icon-share.png'
  11. }
  12. export function shareDataResult(shareData, title, coverUrl) {
  13. const state_str = encodeURIComponent(encrypt(shareData))
  14. const result = {
  15. title: title || defalutOptions.title,
  16. path: `${defalutOptions.path}?state_str=${state_str}`,
  17. imageUrl: coverUrl || defalutOptions.imageUrl
  18. }
  19. return result
  20. }
  21. const queryKeyOfMap = {
  22. 'type': 't',
  23. 'inviteUserId': 'i',
  24. 'activityId': 'a',
  25. 'dealerUserId': 'd',
  26. 'keyWord': 'k',
  27. 'productId': 'p',
  28. 'jumpState': 'j',
  29. 'collageId': 'c'
  30. }
  31. const enQueryKeyOfMap = {
  32. 't': 'type',
  33. 'i': 'inviteUserId',
  34. 'a': 'activityId',
  35. 'd': 'dealerUserId',
  36. 'k': 'keyWord',
  37. 'p': 'productId',
  38. 'j': 'jumpState',
  39. 'c': 'collageId'
  40. }
  41. // 创建二维码保存路径
  42. function createQrcodeDir(callback) {
  43. try {
  44. fs.accessSync(qrcodePath)
  45. console.log('已存在文件夹')
  46. callback(qrcodePath)
  47. } catch (e) {
  48. fs.mkdirSync(qrcodePath)
  49. console.log('不存在文件夹')
  50. callback(qrcodePath)
  51. }
  52. }
  53. /* 生成二维码链接 */
  54. export async function generateWxUnlimited(params) {
  55. try {
  56. // 从服务端获取二维码arrayBuffer
  57. return await wxUnlimited({
  58. page: params.pagePath || 'pages/index/index',
  59. scene: codeQueryStr(params.queryStr),
  60. check_path: process.env.NODE_ENV === 'production', // 是否校验页面
  61. env_version: process.env.NODE_ENV === 'production' ? 'release' : 'trial', // 正式版 or 开发版
  62. width: 200, // 二维码宽度
  63. auto_color: false, // 自动颜色
  64. line_color: { 'r': 0, 'g': 0, 'b': 0 }, // 线条颜色
  65. is_hyaline: true // 透明底
  66. })
  67. } catch (e) {
  68. return Promise.reject(e)
  69. }
  70. }
  71. // 编码查询参数
  72. export function codeQueryStr(query = '') {
  73. const keys = Object.keys(queryKeyOfMap)
  74. return query.split('&').map(str => {
  75. return str.split('=').map((substr, index) => {
  76. if (!index) {
  77. return queryKeyOfMap[keys.find(item => substr === item)]
  78. } else {
  79. return substr
  80. }
  81. }).join('=')
  82. }).join('&')
  83. }
  84. // 反编码查询参数
  85. export function enCodeQueryStr(query) {
  86. const keys = Object.keys(enQueryKeyOfMap)
  87. return query.split('&').map(str => {
  88. return str.split('=').map((substr, index) => {
  89. if (!index) {
  90. return enQueryKeyOfMap[keys.find(item => substr === item)]
  91. } else {
  92. return substr
  93. }
  94. }).join('=')
  95. }).join('&')
  96. }