share-mixin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * share-mixin — 统一分享逻辑
  3. * 处理:邀请码获取、分享配置、自动绑定邀请码
  4. *
  5. * 使用方式:
  6. * mixins: [shareMixin],
  7. * mounted/after data load:
  8. * this.setShareInfo('推荐阅读: ' + title, '/pages/article-center/article-detail?id=' + id)
  9. */
  10. import { getReferralCode, bindReferral } from '../utils/api.js'
  11. export default {
  12. data() {
  13. return {
  14. referralCode: '',
  15. shareTitle: '',
  16. sharePath: ''
  17. }
  18. },
  19. onLoad(options) {
  20. // 自动绑定:从分享链接进入时,绑定邀请码
  21. if (options && options.inviteCode) {
  22. this.autoBindInviteCode(options.inviteCode)
  23. }
  24. },
  25. methods: {
  26. /**
  27. * 设置分享信息(由各页面在数据加载完成后调用)
  28. */
  29. setShareInfo(title, path) {
  30. this.shareTitle = title
  31. this.sharePath = path
  32. this.loadReferralCode()
  33. },
  34. /**
  35. * 获取自己的邀请码
  36. */
  37. async loadReferralCode() {
  38. if (this.referralCode) return
  39. // 未登录时不请求邀请码(避免 500 报错弹窗和 401 跳转)
  40. if (!uni.getStorageSync('token')) return
  41. try {
  42. var res = await getReferralCode()
  43. if (res && res.code === 200 && res.data) {
  44. this.referralCode = res.data.referralCode || res.data.code || ''
  45. }
  46. } catch (e) {
  47. console.log('获取邀请码失败', e)
  48. }
  49. },
  50. /**
  51. * 自动绑定分享者的邀请码
  52. * 规则:如果用户已有邀请人,则忽略此邀请码
  53. */
  54. async autoBindInviteCode(code) {
  55. // 未登录时存 storage,登录后由 login 页面处理
  56. if (!uni.getStorageSync('token')) {
  57. uni.setStorageSync('inviteCode', code)
  58. return
  59. }
  60. // 已绑定过则跳过
  61. var bound = uni.getStorageSync('boundInviteCode')
  62. if (bound === code) return
  63. try {
  64. var res = await bindReferral(code)
  65. if (res && res.code === 200) {
  66. uni.setStorageSync('boundInviteCode', code)
  67. console.log('邀请码绑定成功', code)
  68. } else if (res && res.code === 400) {
  69. // 后端返回 400 表示已有邀请人,记录此 code 避免重复请求
  70. uni.setStorageSync('boundInviteCode', code)
  71. console.log('用户已有邀请人,跳过绑定', code)
  72. }
  73. } catch (e) {
  74. console.log('邀请码绑定失败', e)
  75. }
  76. },
  77. /**
  78. * 拼接完整分享路径
  79. */
  80. getSharePath() {
  81. if (!this.sharePath) return ''
  82. if (this.referralCode) {
  83. var sep = this.sharePath.indexOf('?') === -1 ? '?' : '&'
  84. return this.sharePath + sep + 'inviteCode=' + this.referralCode
  85. }
  86. return this.sharePath
  87. },
  88. /**
  89. * 分享按钮点击(用于统计等)
  90. */
  91. onShareTap() {
  92. console.log('分享按钮点击')
  93. }
  94. },
  95. onShareAppMessage() {
  96. if (!this.shareTitle || !this.sharePath) {
  97. return {
  98. title: '浠艾福 — 给全家的一站式幸福提案',
  99. path: '/pages/index/index'
  100. }
  101. }
  102. return {
  103. title: this.shareTitle,
  104. path: this.getSharePath(),
  105. imageUrl: ''
  106. }
  107. }
  108. }