share.helper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // import { encrypt } from '@/common/crypto.js'
  2. /* 小程序码 */
  3. import store from '@/store/index.js'
  4. import { wxUnlimited } from '@/services/sellse.service.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. 'sellerUserId': 'u'
  31. }
  32. const enQueryKeyOfMap = {
  33. 't': 'type',
  34. 'i': 'inviteUserId',
  35. 'a': 'activityId',
  36. 'd': 'dealerUserId',
  37. 'k': 'keyWord',
  38. 'p': 'productId',
  39. 'j': 'jumpState',
  40. 'c': 'collageId',
  41. 'u': 'sellerUserId'
  42. }
  43. // 创建二维码保存路径
  44. function createQrcodeDir(callback) {
  45. try {
  46. fs.accessSync(qrcodePath)
  47. console.log('已存在文件夹')
  48. callback(qrcodePath)
  49. } catch (e) {
  50. fs.mkdirSync(qrcodePath)
  51. console.log('不存在文件夹')
  52. callback(qrcodePath)
  53. }
  54. }
  55. /* 生成二维码链接 */
  56. export const generateWxUnlimited = async (that, params) => {
  57. try {
  58. // 从服务端获取二维码arrayBuffer
  59. return await that.SellerService.wxUnlimited({
  60. page: params.pagePath,
  61. scene: params.queryStr,
  62. check_path: process.env.NODE_ENV === 'production', // 是否校验页面
  63. env_version: process.env.NODE_ENV === 'production' ? 'release' : 'trial', // 正式版 or 开发版
  64. width: 200, // 二维码宽度
  65. auto_color: false, // 自动颜色
  66. line_color: { 'r': 0, 'g': 0, 'b': 0 }, // 线条颜色
  67. is_hyaline: true // 透明底
  68. })
  69. } catch (e) {
  70. return Promise.reject(e)
  71. }
  72. }
  73. // 编码查询参数
  74. export function codeQueryStr(query = '') {
  75. const keys = Object.keys(queryKeyOfMap)
  76. return query.split('&').map(str => {
  77. return str.split('=').map((substr, index) => {
  78. if (!index) {
  79. return queryKeyOfMap[keys.find(item => substr === item)]
  80. } else {
  81. return substr
  82. }
  83. }).join('=')
  84. }).join('&')
  85. }
  86. // 反编码查询参数
  87. export function enCodeQueryStr(query) {
  88. const keys = Object.keys(enQueryKeyOfMap)
  89. return query.split('&').map(str => {
  90. return str.split('=').map((substr, index) => {
  91. if (!index) {
  92. return enQueryKeyOfMap[keys.find(item => substr === item)]
  93. } else {
  94. return substr
  95. }
  96. }).join('=')
  97. }).join('&')
  98. }