| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /**
- * share-mixin — 统一分享逻辑
- * 处理:邀请码获取、分享配置、自动绑定邀请码
- *
- * 使用方式:
- * mixins: [shareMixin],
- * mounted/after data load:
- * this.setShareInfo('推荐阅读: ' + title, '/pages/article-center/article-detail?id=' + id)
- */
- import { getReferralCode, bindReferralIfNotSelf, extractReferralCode } from '../utils/api.js'
- export default {
- data() {
- return {
- referralCode: '',
- shareTitle: '',
- sharePath: ''
- }
- },
- onLoad(options) {
- // 自动绑定:从分享链接进入时,绑定邀请码
- if (options && options.inviteCode) {
- this.autoBindInviteCode(options.inviteCode)
- }
- },
- methods: {
- /**
- * 设置分享信息(由各页面在数据加载完成后调用)
- */
- setShareInfo(title, path) {
- this.shareTitle = title
- this.sharePath = path
- this.loadReferralCode()
- },
- /**
- * 获取自己的邀请码
- */
- async loadReferralCode() {
- if (this.referralCode) return
- // 未登录时不请求邀请码(避免 500 报错弹窗和 401 跳转)
- if (!uni.getStorageSync('token')) return
- try {
- var res = await getReferralCode()
- this.referralCode = extractReferralCode(res)
- } catch (e) {
- console.log('获取邀请码失败', e)
- }
- },
- /**
- * 自动绑定分享者的邀请码
- * 规则:如果用户已有邀请人,则忽略此邀请码
- */
- async autoBindInviteCode(code) {
- // 未登录时存 storage,登录后由 login 页面处理
- if (!uni.getStorageSync('token')) {
- uni.setStorageSync('inviteCode', code)
- return
- }
- // 已绑定过则跳过
- var bound = uni.getStorageSync('boundInviteCode')
- if (bound === code) return
- try {
- var res = await bindReferralIfNotSelf(code)
- if (res && res.code === 200) {
- uni.setStorageSync('boundInviteCode', code)
- console.log('邀请码绑定成功', code)
- }
- } catch (e) {
- // 后端已绑定(code=500 message=已绑定推荐人)→ 记录防重,避免每次进入重复请求/重复提示
- if (e && e.code !== undefined && e.code !== 401) {
- uni.setStorageSync('boundInviteCode', code)
- console.log('邀请码绑定失败(业务拒绝,已防重)', code, e.message || '')
- } else {
- console.log('邀请码绑定失败(网络/登录态)', e)
- }
- }
- },
- /**
- * 拼接完整分享路径
- */
- getSharePath() {
- if (!this.sharePath) return ''
- if (this.referralCode) {
- var sep = this.sharePath.indexOf('?') === -1 ? '?' : '&'
- return this.sharePath + sep + 'inviteCode=' + this.referralCode
- }
- return this.sharePath
- },
- /**
- * 分享按钮点击(用于统计等)
- */
- onShareTap() {
- console.log('分享按钮点击')
- }
- },
- onShareAppMessage() {
- if (!this.shareTitle || !this.sharePath) {
- return {
- title: '浠艾福 — 给全家的一站式幸福提案',
- path: '/pages/index-home/index'
- }
- }
- return {
- title: this.shareTitle,
- path: this.getSharePath(),
- imageUrl: ''
- }
- },
- /**
- * 分享到朋友圈(微信基础库 2.11.3+,仅非游戏类小程序支持)
- * 前提:该页面已定义 onShareAppMessage(已满足)
- * 注意:单页模式下页面无登录态,详情接口需在 JwtInterceptor 的公开路径中
- */
- onShareTimeline() {
- var path = this.getSharePath()
- var query = ''
- var qi = path.indexOf('?')
- if (qi !== -1) query = path.substring(qi + 1)
- return {
- title: this.shareTitle || '浠艾福 — 给全家的一站式幸福提案',
- query: query
- }
- }
- }
|