maven:
com.github.binarywangweixin-java-mp4.4.0
java 关键代码:
public String sendTemplateMessage(String templateId, String openId, String miniProgramAppId, String miniProgramPagePath, List data) {
try {
WxMpTemplateMessage message = WxMpTemplateMessage.builder()
.toUser(openId)
.templateId(templateId)
.data(data)
.build();
// message.setMiniProgram(new WxMpTemplateMessage.MiniProgram(
// miniProgramAppId,
// miniProgramPagePath,
// true
// ));
WxMpTemplateMessage.MiniProgram miniProgram = new WxMpTemplateMessage.MiniProgram();
miniProgram.setAppid(miniProgramAppId);
miniProgram.setPagePath(miniProgramPagePath);
miniProgram.setUsePath(true);
message.setMiniProgram(miniProgram);
return templateMsgService.sendTemplateMsg(message);
} catch (WxErrorException e) {
log.error("模板消息发送失败: {}", e.getError().getErrorMsg());
throw new RuntimeException("发送模板消息失败", e);
}
}
调用关键方法:
String msgId = wechatTemplateMessageService.sendTemplateMessage(wxYijiabaoConfig.getTemplateIdNewOrder(),
wechatAccount.getMpOpenid(), wxMaProperties.getConfigs().get(1).getAppid(),
"pages/redirect/redirect?path=pages/order/index", data);
log.info("向社工发送模板消息成功【支付订单】 - {}", msgId);
app.json
{
"pages": [
"pages/home/index",
"pages/customer/index",
"pages/customer/family-avatar",
"pages/order/index",
"pages/me/index",
"pages/customer/detail",
"pages/customer/add-family",
"pages/login/index",
"pages/login/mobile",
"pages/me/me-card",
"pages/order/detail",
"pages/order/comment",
"pages/collect/index",
"pages/chat/item",
"pages/im/index",
"pages/redirect/redirect"
],
"subPackages": [
{
"root": "packageC",
"pages": [
"pages/member/share",
"pages/share/share",
"pages/share/bind-worker",
"pages/service/detail"
]
},
{
"root": "packageA",
"pages": [
"pages/im/chat"
]
}
],
"window": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "益家保",
"navigationBarBackgroundColor": "#F5F5F5",
"backgroundColor": "#F5F5F5"
},
"tabBar": {
"color": "#333333",
"selectedColor": "#FF5D2C",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/home/index",
"iconPath": "/static/image/tabbar/home.png",
"selectedIconPath": "static/image/tabbar/home1.png",
"text": "首页"
},
{
"pagePath": "pages/customer/index",
"iconPath": "/static/image/tabbar/customer.png",
"selectedIconPath": "/static/image/tabbar/customer1.png",
"text": "客户"
},
{
"pagePath": "pages/order/index",
"iconPath": "/static/image/tabbar/order.png",
"selectedIconPath": "/static/image/tabbar/order1.png",
"text": "订单"
},
{
"pagePath": "pages/me/index",
"iconPath": "/static/image/tabbar/me.png",
"selectedIconPath": "/static/image/tabbar/me1.png",
"text": "我的"
}
]
},
"requiredPrivateInfos": [
"getLocation",
"chooseLocation"
],
"permission": {
"scope.userLocation": {
"desc": "用于发送位置消息,为您提供位置分享功能"
}
},
"ignoredFiles": [
"**/*.test.js",
"**/*.spec.js",
"unpackage/**/*",
"node_modules/**/*"
],
"usingComponents": {}
}
由于我真正要跳转的是tabBar(pages/order/index),,所以搞了一个中间页来调整 :
<template>
<view>
<!-- 空白页,仅用于跳转 -->
</view>
</template>
<script>
export default {
data() {
return {
}
},
onLoad(options) {
console.log(' 中间页接收到的参数:', options)
console.log(' 当前页面栈:', getCurrentPages())
const { path, query } = options
// 延迟跳转,确保页面加载完成
setTimeout(() => {
if (path) {
// 如果是tabBar页面,使用switchTab
if (this.isTabBarPage(path)) {
uni.switchTab({
url: `/${path}${query ? '?' + query : ''}`
})
} else {
uni.redirectTo({
url: `/${path}${query ? '?' + query : ''}`
})
}
} else {
// 默认跳转到我的名片
uni.switchTab({
url: '/pages/order/index'
})
}
}, 100)
},
methods: {
isTabBarPage(path) {
const tabBarPages = [
'pages/order/index',
]
return tabBarPages.includes(path)
},
}
}
</script>
<style scoped lang="scss">
.redirect-page {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.loading-text {
font-size: 16px;
color: #666;
}
</style>

你好,只能跳转到首页说明填写的路径是错误的,只有路径错误才会默认跳转首页
使用开发者工具,单独打开路径是能跳转的