我记得前几年小程序是不能给用户推送消息的。须通过公众号给用户推消息。不知道是否是记错了。
但最近项目中有这种小程序给用户推消息的需求。发现小程序确实可以通过wx.requestSubscribeMessage拉起订阅消息的授权弹窗,在用户授权后,直接给用户推微信消息。记录一下。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| import {findCustomerPopTemplate,popSubscribeResult} from '../api/subscriptApi'
export const asyncSubscribeMessage = (type, busNo, extraVariableMap) => { const triggerAct = { 1: '加入购物车', 2: '订单详情', 3: '提交订单', 4: '支付成功', 5: '企业信息', 6: '切换企业身份', 7: '提交售后申请(退货退款)', 9: '提交售后申请(仅退款)', 8: '填写退货信息' }[type]
return new Promise(async (resolve) => { const res = await findCustomerPopTemplate({triggerAct, busNo}) console.log('消息模板tmplIds:', res.data) if(res.data.length === 0){ resolve() return } const tmplIds = res.data wx.requestSubscribeMessage({ tmplIds: tmplIds, success:(res)=>{ const acceptResult = [],rejectResult = [] for(const key in res){ const result = res[key] if(result === 'accept'){ acceptResult.push(key) }else if(result ==='reject'){ rejectResult.push(key) } } if(acceptResult.length > 0 || rejectResult.length > 0){ console.log('用户同意和拒绝的模板ID:',acceptResult,rejectResult) popSubscribeResult({ busNo:busNo, extraVariableMap: extraVariableMap, openId:wx.getStorageSync('openId'), agreeTemplateIds:acceptResult, rejectTemplateIds:rejectResult }) resolve() return } resolve()
}, fail:(err) =>{ console.log(err,'失败 ') resolve() } }) }) }
|