猫妈狗爸伴宠师小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.2 KiB

2 weeks ago
  1. import { useStore } from 'vuex'
  2. import { computed } from 'vue'
  3. export default {
  4. data() {
  5. return {
  6. shareConfig: {
  7. // 默认分享配置
  8. title: '',
  9. desc: '',
  10. imageUrl: '',
  11. path: '',
  12. query: ''
  13. }
  14. }
  15. },
  16. async onLoad(options) {
  17. // 构建当前页面路径,包含参数
  18. const pages = getCurrentPages()
  19. const currentPage = pages[pages.length - 1]
  20. let path = currentPage.route
  21. // 构建查询参数
  22. if (options && Object.keys(options).length > 0) {
  23. const queryString = Object.keys(options)
  24. .map(key => `${key}=${options[key]}`)
  25. .join('&')
  26. path = `${path}?${queryString}`
  27. }
  28. this.shareConfig.path = path
  29. this.shareConfig.query = options || {}
  30. // 设置默认分享内容
  31. this.setDefaultShareConfig()
  32. },
  33. methods: {
  34. // 设置默认分享配置
  35. setDefaultShareConfig() {
  36. try {
  37. const store = useStore()
  38. const configList = computed(() => store.getters.configList)
  39. // 从全局配置中获取默认分享信息
  40. this.shareConfig.title = configList.value?.applet_info?.paramValueText || '猫妈狗爸伴宠师'
  41. this.shareConfig.imageUrl = configList.value?.applet_info?.paramValueImage || ''
  42. } catch (error) {
  43. console.log('获取分享配置失败:', error)
  44. // 设置默认值
  45. this.shareConfig.title = '猫妈狗爸伴宠师'
  46. this.shareConfig.imageUrl = ''
  47. }
  48. },
  49. // 自定义分享配置
  50. setShareConfig(config = {}) {
  51. this.shareConfig = {
  52. ...this.shareConfig,
  53. ...config
  54. }
  55. },
  56. // 获取分享配置
  57. getShareConfig() {
  58. return {
  59. title: this.shareConfig.title,
  60. desc: this.shareConfig.desc,
  61. imageUrl: this.shareConfig.imageUrl,
  62. path: this.shareConfig.path,
  63. query: this.shareConfig.query
  64. }
  65. },
  66. // 显示分享菜单
  67. showShareMenu() {
  68. // #ifdef MP-WEIXIN
  69. uni.showShareMenu({
  70. withShareTicket: true,
  71. success: () => {
  72. console.log('显示分享菜单成功')
  73. },
  74. fail: (error) => {
  75. console.error('显示分享菜单失败:', error)
  76. }
  77. })
  78. // #endif
  79. },
  80. },
  81. // 微信小程序分享给朋友
  82. onShareAppMessage(res) {
  83. return this.getShareConfig()
  84. },
  85. // 微信小程序分享到朋友圈
  86. onShareTimeline(res) {
  87. return this.getShareConfig()
  88. },
  89. }