import {getJsConfig, sendOneTimeMsgApi} from "@/views/pageApi";
import wx from "weixin-js-sdk";
export default {
name: "WxSubscribe",
data() {
return {
wxReady:false,
templateId: "CpT9B7YACSEjhghjghgS-zqMXHqunGwXXz38e8",
open_id: "oDPkZ5pSI-8fhghjkdfRvE41ywas"
};
},
async mounted() {
alert("111111");
const pageUrl = window.location.href.split('#')[0];
console.log("99999999",pageUrl)
const that = this;
try {
getJsConfig(pageUrl).then(res=>{
wx.config({
debug: true,
appId: res.appId,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: ["showModal", "openSubscribeMessage"],
openTagList: ["wx-open-subscribe"]
});
wx.ready(() => {
console.log("微信配置成功,标签已激活");
alert("微信配置成功,标签已激活");
that.wxReady = true;
});
wx.error((err) => {
console.error("微信配置失败:", err);
alert("微信配置失败");
});
})
} catch (err) {
console.error("获取微信签名失败:", err);
}
},
methods: {
handleSubscribeSuccess(e) {
const detail = e.detail;
const ticket = detail.subscribeTicket;
const tempId = detail.templateId;
const scene = detail.scene;
alert("一次性订阅:"+JSON.stringify(detail));
console.log("一次性订阅成功", ticket, tempId, scene);
if (!ticket) {
wx.showModal({ title: "异常", content: "未获取到订阅凭证" });
return;
}
sendOneTimeMsgApi({
openId: this.open_id,
templateId: this.templateId,
subscribeTicket: ticket
}).then(res => {
wx.showModal({ title: "成功", content: "通知已下发,请注意查收服务号消息" });
}).catch(err => {
wx.showModal({ title: "推送失败", content: "消息下发失败,请稍后重试" });
console.error("一次性消息推送接口报错", err);
});
},
handleSubscribeError(e) {
const errCode = e.detail?.errCode;
alert("订阅失败:"+e.detail);
if (errCode === 10001) {
wx.showToast({ title: "已取消订阅", icon: "none" });
} else {
wx.showModal({ title: "订阅失败", content: `错误码:${errCode}` });
}
}
}
};
你好,提供下完整的请求包