1.修改edit,使得传值到云对象,该条内容的用户id,然后云对象根据用户id查到openid才能进行发送订阅消息
const updataSuccess=()=>{
if(id){
if(formDate.value.status===1){
let {user_id:[{_id:user_id}],_id:soup_id} =formDate.value
soupScore.soupAdd({user_id,soup_id})
}
}
if(id && isAdminRole()){ //就是只有管理员才能审核,但是管理员审核后发给的还是用户
subscribemsg.sendSubscribeMessage({
user_id:formDate.value.user_id[0]._id,
phrase2:useStateFormat.value.text,
time3:formatTimestampToYearMonthDay(Date.now()),
thing4:"点击进入小程序查看"
});
}
}
2.云对象subscribemsg连接数据库查询到openid和username然后进行传值发送订阅消息
const UniSubscribemsg=require('uni-subscribemsg')
module.exports = {
_before: function () { // 通用预处理器
},
async sendSubscribeMessage(params){
const dbJQL=uniCloud.databaseForJQL({
clientInfo:this.getClientInfo()
})
let {data:{username,openid}}=await dbJQL.collection("uni-id-users").where(`_id=="${params.user_id}"`)
.field("username,wx_openid.mp as openid").get({getOne:true});
// 初始化实例
let uniSubscribemsg = new UniSubscribemsg({
dcloudAppid: "__UNI__6A102F8", //在manifest.json里面
provider: "weixin-mp", // 注意,这里是改为weixin-mp就是小程序
});
// 发送模板消息
let res = await uniSubscribemsg.sendSubscribeMessage({
touser: openid, //用户openid
template_id: "nD0V_AkkZwUeJFhTpfZ6TBoHCu6XrScOb82d68QpW6w",//这是订阅信息模板id
page: "pages_self/soup/list", // 用户点击消息后跳转的链接地址
miniprogram_state: "developer", // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
lang: "zh_CN",
data: {
thing1: {
value: username
},
phrase2: {
value: params.phrase2
},
time3: {
value: params.time3
},
thing4: {
value: params.thing4
}
}
});
return {res}
}
}