| | |
| | | * @returns {boolean} |
| | | */ |
| | | checkLocalSubscribeStatus() { |
| | | return true;// uni.getStorageSync('hasSubscribedTaskNotify') || false |
| | | return uni.getStorageSync('hasSubscribedTaskNotify') || false |
| | | } |
| | | |
| | | /** |
| | |
| | | wx.getSetting({ |
| | | withSubscriptions: true, |
| | | success: (res) => { |
| | | console.log('微信订阅状态查询结果:', res) |
| | | |
| | | // console.log('微信订阅状态查询结果:', res.subscriptionsSetting.mainSwitch) |
| | | |
| | | // 检查subscriptionsSetting中是否有该模板ID的记录 |
| | | if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) { |
| | | const subscribeStatus = res.subscriptionsSetting.mainSwitch; |
| | | resolve(subscribeStatus) |
| | | resolve(false); // 如果用户同意了订阅,则返回false |
| | | // const subscribeStatus = res.subscriptionsSetting.mainSwitch; |
| | | // console.log(res.subscriptionsSetting.itemSettings[templateId]) |
| | | // resolve(subscribeStatus) |
| | | // 'accept' 表示用户同意订阅,'reject' 表示拒绝,'ban' 表示被封禁 |
| | | // const isSubscribed = subscribeStatus === 'accept' |
| | | // console.log(`模板ID ${templateId} 订阅状态:`, subscribeStatus, '是否已订阅:', isSubscribed) |
| | | // resolve(isSubscribed) |
| | | // console.log("发起订阅请求") |
| | | // wx.requestSubscribeMessage({ |
| | | // tmplIds: [templateId], |
| | | // success: (res) => { |
| | | // console.log('订阅消息状态:', res) |
| | | // if (res[templateId] === 'accept') { |
| | | // console.log('用户已订阅') |
| | | // resolve(true) |
| | | // } else if (res[templateId] === 'reject') { |
| | | // console.log('用户已拒绝订阅') |
| | | // resolve(false) |
| | | // } else if (res[templateId] === 'ban') { |
| | | // console.log('用户已封禁订阅') |
| | | // resolve(false) |
| | | // } else { |
| | | // console.log('未知订阅状态') |
| | | // resolve(false) |
| | | // } |
| | | // },fail: (err) => { |
| | | // console.error('订阅消息失败:', err) |
| | | // resolve(false) |
| | | // } |
| | | |
| | | // } |
| | | // ) |
| | | } else { |
| | | console.log('未找到订阅设置信息,视为未订阅') |
| | | resolve(false) |
| | |
| | | async checkSubscribeStatus() { |
| | | const localStatus = this.checkLocalSubscribeStatus() |
| | | const wechatStatus = await this.checkWechatSubscribeStatus() |
| | | |
| | | |
| | | // 如果本地显示已订阅,但微信官方显示未订阅,需要重新订阅 |
| | | const needResubscribe = wechatStatus |
| | | |
| | | const needResubscribe = !localStatus |
| | | |
| | | if (needResubscribe) { |
| | | console.warn('本地状态与微信官方状态不一致,需要重新订阅') |
| | | // console.warn('本地状态与微信官方状态不一致,需要重新订阅') |
| | | // 清除本地记录 |
| | | uni.removeStorageSync('hasSubscribedTaskNotify') |
| | | // uni.removeStorageSync('hasSubscribedTaskNotify') |
| | | } |
| | | |
| | | |
| | | return { |
| | | local: localStatus, |
| | | wechat: wechatStatus, |
| | |
| | | * @returns {Promise} |
| | | */ |
| | | async subscribeTaskNotify(options = {}) { |
| | | const { |
| | | const { |
| | | showConfirm = true, |
| | | onSuccess, |
| | | onReject, |
| | |
| | | success: (res) => { |
| | | console.log('订阅消息授权结果:', res) |
| | | const templateId = this.wechatConfig.taskNotifyTemplateId |
| | | |
| | | |
| | | if (res[templateId] === 'accept') { |
| | | // 记录已订阅 |
| | | uni.setStorageSync('hasSubscribedTaskNotify', true) |
| | |
| | | title: '订阅成功', |
| | | icon: 'success' |
| | | }) |
| | | |
| | | |
| | | if (onSuccess) onSuccess() |
| | | resolve({ success: true, action: 'accept' }) |
| | | } else if (res[templateId] === 'reject') { |
| | |
| | | title: '您拒绝了订阅', |
| | | icon: 'none' |
| | | }) |
| | | |
| | | |
| | | if (onReject) onReject() |
| | | resolve({ success: false, action: 'reject' }) |
| | | } else { |
| | |
| | | title: '订阅失败', |
| | | icon: 'none' |
| | | }) |
| | | |
| | | |
| | | if (onFail) onFail(err) |
| | | reject(err) |
| | | } |
| | |
| | | */ |
| | | async autoSubscribe(options = {}) { |
| | | const { force = false } = options |
| | | |
| | | |
| | | try { |
| | | // 综合检查订阅状态(本地 + 微信官方) |
| | | const status = await this.checkSubscribeStatus() |
| | | |
| | | |
| | | console.log('订阅状态检查结果:', status) |
| | | |
| | | |
| | | // 如果微信官方状态显示已订阅,且不强制订阅 |
| | | if (status.isSubscribed && !force) { |
| | | console.log('用户已订阅过(微信官方状态),跳过自动订阅') |
| | |
| | | console.log('检测到订阅状态失效,触发重新订阅流程') |
| | | } else { |
| | | console.log('用户未订阅,触发自动订阅流程') |
| | | return { success: false, action: 'not_subscribed', skipped: true, status } |
| | | } |
| | | |
| | | |
| | | // 显示确认弹窗并订阅 直接默认订阅 |
| | | const result = await this.subscribeWithConfirm(); |
| | | return { ...result, status } |