微信支付成功订阅进行优化

master
lemon橪 2021-08-23 10:09:12 +08:00
parent 224eb05703
commit 915aee9143
3 changed files with 64 additions and 46 deletions

View File

@ -1,7 +1,18 @@
/**
* 微信小程序支付
* 此处针对于微信小程序开发的支付插件
* 第一次支付成功后会跳出订阅的消息 如果用户拒绝或同意都会跳转到支付成功页面
* 如果点击订阅 会将状态写进缓存 之后不再提醒
*
* @param {sn,price}
*/
import { initiatePay } from "@/api/trade";
import { getWeChatMpMessage } from "@/api/message.js";
class LiLiWXPay {
constructor(...payList) {
this.data = payList[0];
console.log(payList);
// 调用支付
this.pay = () => {
uni.showLoading({
@ -32,13 +43,7 @@ class LiLiWXPay {
icon: "none",
title: "支付成功!",
});
// 之后成功后跳转到支付成功页面
uni.redirectTo({
url:
"/pages/cart/payment/success?paymentMethod=WECHAT" +
"&payPrice=" +
this.data.price,
});
sendMessage(payList[0].price);
},
fail: (e) => {
this.exception = e;
@ -59,4 +64,53 @@ class LiLiWXPay {
}
}
function sendMessage(price) {
//判断用户是否已经进行了订阅
if (!uni.getStorageSync("acceptSubscribeMessage")) {
//订阅消息
getWeChatMpMessage().then((res) => {
var message = res.data.result;
var templateid = message.map((item) => item.code);
uni.requestSubscribeMessage({
tmplIds: templateid,
success: (res) => {
for (let key in res) {
// 表示用户拒绝订阅该信息
if (res[key] == "reject") {
this.checked = false;
} else {
uni.setStorageSync("acceptSubscribeMessage", res);
}
}
},
fail: (res) => {
uni.removeStorageSync("acceptSubscribeMessage");
this.checked = false;
},
complete: () => {
/**
* 已经支付成功
*/
uni.redirectTo({
url:
"/pages/cart/payment/success?paymentMethod=WECHAT" +
"&payPrice=" +
price,
});
},
});
});
} else {
/**
* 已经支付成功
*/
uni.redirectTo({
url:
"/pages/cart/payment/success?paymentMethod=WECHAT" +
"&payPrice=" +
price,
});
}
}
export default LiLiWXPay;

View File

@ -2,8 +2,8 @@
"name" : "lili商城",
"appid" : "__UNI__C100675",
"description" : "",
"versionName" : "4.0.30",
"versionCode" : 4000030,
"versionName" : "4.0.31",
"versionCode" : 4000031,
"transformPx" : false,
"app-plus" : {
"compatible" : {

View File

@ -7,36 +7,24 @@
<div class="pay-btns">
<div v-show="!from" @click="checkOrder">{{this.orderType == "RECHARGE" ? '' : ''}}</div>
<div @click="navigateTo('/pages/tabbar/home/index', 'switch')">回到首页</div>
</div>
</div>
<div class="pay-box">
<div class="pay-tag-box">
<h2>订单支付成功!</h2>
<div class="pay-item">
<div>
支付方式
</div>
<div>{{paymentMethod | paymentTypeFilter}}</div>
</div>
</div>
<!-- #ifdef MP-WEIXIN -->
<div class="subscribe flex">
<div>订阅订单状态</div>
<div>
<u-switch size="50" :disabled="checked" :active-color="activeColor" @change="changeStatus" v-model="checked"></u-switch>
</div>
</div>
<!-- #endif -->
</div>
<goodsRecommend />
</div>
</template>
<script>
import { getWeChatMpMessage } from "@/api/message.js";
import goodsRecommend from "@/components/m-goods-recommend";
export default {
data() {
@ -48,7 +36,6 @@ export default {
payPrice: 0,
goodsList: [],
activeColor: this.$mainColor,
};
},
components: {
@ -73,6 +60,7 @@ export default {
this.from = options.from || "";
this.payPrice = options.payPrice || 0;
this.orderType = options.orderType;
// this.sendMessage()
},
methods: {
checkOrder() {
@ -95,30 +83,6 @@ export default {
}
},
sendMessage() {
//
//#ifdef MP-WEIXIN
getWeChatMpMessage().then((res) => {
var message = res.data.result;
var templateid = message.map((item) => item.code);
uni.requestSubscribeMessage({
tmplIds: templateid,
success: (res) => {
for (let key in res) {
if (res[key] == "reject") {
this.checked = false;
}
}
},
fail: (res) => {
uni.removeStorageSync("acceptSubscribeMessage");
this.checked = false;
},
});
});
//#endif
},
navigateTo(url, type) {
if (type === "switch") {
uni.switchTab({