// components/requestMsg/index.js import { wxRequestPost, } from "../../utils/promise-wx-api"; let tempReslove = () => {}; const app = getApp(); interface Tdata { isMsg: Boolean, checked: Boolean, templateList: Array, templateInfo: Object, wxAlwaysVisit: String } function returnData(obj: Tdata) { return obj } Component({ /** * 组件的属性列表 */ options: { // 基础库2.2.3开始支持 addGlobalClass: true }, properties: { behaviorType: { // 触发类型 type: String, value: '' }, behaviorTitle: { // 消息标题 type: String, value: '' }, templateType: { // 模板类型 type: String, value: '' }, }, /** * 组件的初始数据 */ data: returnData({ isMsg: false, checked: false, templateList: [], templateInfo: {}, wxAlwaysVisit: 'no' }), async ready() { await app.doAfterLogin() this.initTemplateList() }, /** * 组件的方法列表 */ methods: { async initTemplateInfo (id) { let hasShow = false const { data: { data: { code, data } }, msg } = await wxRequestPost( "message/wxmpmessage/get-authorizeinfo", { customerId: app.globalData.customerId , clientType: 'resi' , templateId: id }, { // isMock: true, isQuiet: true } ); if (msg === "success" && code === 0) { if (data.alwaysVisit === 'yes' && data.subscribeStatus === 'unsubscribe' && data.wxAlwaysVisit !== 'yes') hasShow = true this.setData({ templateInfo: {...data}, checked: false }) this.triggerEvent('resubscribe', { hasShow, templateId: id}) } }, async initTemplateList () { const extAppid = wx.getStorageSync('extAppid') const { data: { data: { code, data } }, msg } = await wxRequestPost( "message/wxmpmessage/templatelist", { customerId: app.globalData.customerId , appId: extAppid, templateType: this.data.templateType }, { // isMock: true, isQuiet: true } ); console.log('dddddd', data) if (msg === "success" && code === 0) { if (data && data.length > 0) { this.setData({ templateList: data.map(item => item.templateId) }) await this.initTemplateInfo(data[0].templateId) } } }, async saveSysStatus(status: string) { let { checked, wxAlwaysVisit, templateList } = this.data if (wxAlwaysVisit === 'yes') checked = true const { data: { data: { code, data } }, msg } = await wxRequestPost( "message/wxmpmessage/save-sys-authorizeinfo", { customerId: app.globalData.customerId, clientType: 'resi', alwaysVisit: checked ? 'yes' : 'no', // 不再提示 subscribeStatus: status, // 是否订阅, templateId: templateList[0] }, { // isMock: true, isQuiet: true } ); if (msg === "success" && code === 0) { // this.setData({ // templateList: data.data // }) } }, async saveWxStatus(status) { let { templateList, wxAlwaysVisit } = this.data const { data: { data: { code } }, msg } = await wxRequestPost( "message/wxmpmessage/save-wxsubscribe", { customerId: app.globalData.customerId, clientType: 'resi', wxAlwaysVisit: wxAlwaysVisit, // 不再提示 wxSubscribeStatus: status, // 允许,拒绝 behaviorType: this.data.behaviorType, templateId: templateList[0] }, { // isMock: true, isQuiet: true } ); if (msg === "success" && code === 0) { if (status === 'subscribe') { wx.showToast({ title: '订阅成功', icon: 'none', duration: 1500 }) } // this.setData({ // templateList: data.data // }) } }, async handleMsg () { // await this.initTemplateList() if (Object.keys(this.data.templateInfo).length > 0 && this.data.templateInfo.subscribeStatus === 'unsubscribe' && this.data.templateInfo.alwaysVisit === 'yes') return false this.setData({ isMsg: true }) wx.setNavigationBarColor({ frontColor: "#000000", backgroundColor: "#999999", animation: { duration: 270 } }) return new Promise(reslove => { tempReslove = reslove; }); }, wxGetSeting(status) { let { wxAlwaysVisit, templateList, checked } = this.data let subscriptionsSetting = {} let templateId = templateList[0] wx.getSetting({ withSubscriptions: true, success: async (res) => { console.log('subscriptionsSetting', res.subscriptionsSetting); subscriptionsSetting = res.subscriptionsSetting; if (subscriptionsSetting.itemSettings !== undefined) { if (templateId in subscriptionsSetting.itemSettings) { if (subscriptionsSetting.itemSettings[templateId] === 'reject') { wxAlwaysVisit = 'yes' checked = true console.log('itemSettings', subscriptionsSetting.itemSettings) await this.saveSysStatus(status) await this.initTemplateList() } } } this.setData({ wxAlwaysVisit }) await this.saveWxStatus(status) tempReslove(true); } }) }, handleRequestMsg() { this.setData({ isMsg: false }) wx.setNavigationBarColor({ frontColor: "#000000", backgroundColor: "#ffffff", }) wx.requestSubscribeMessage({ tmplIds: this.data.templateList, success: res => { console.log('res', res) if (res.errMsg === 'requestSubscribeMessage:ok') { if (res[this.data.templateList[0]] === 'accept') this.wxGetSeting('subscribe') else if (res[this.data.templateList[0]] === 'reject') this.wxGetSeting('unsubscribe') } }, fail: err => { console.log('err', err) tempReslove(true); } }) this.saveSysStatus('subscribe') }, async handleCancleMsg() { this.setData({ isMsg: false }) wx.setNavigationBarColor({ frontColor: "#000000", backgroundColor: "#ffffff", }) await this.saveSysStatus('unsubscribe') await this.initTemplateList() tempReslove(true); }, handleChecked() { this.setData({ checked: !this.data.checked }) } } })