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

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 { initiatePay } from "@/api/trade";
import { getWeChatMpMessage } from "@/api/message.js";
class LiLiWXPay { class LiLiWXPay {
constructor(...payList) { constructor(...payList) {
this.data = payList[0]; this.data = payList[0];
console.log(payList);
// 调用支付 // 调用支付
this.pay = () => { this.pay = () => {
uni.showLoading({ uni.showLoading({
@ -32,13 +43,7 @@ class LiLiWXPay {
icon: "none", icon: "none",
title: "支付成功!", title: "支付成功!",
}); });
// 之后成功后跳转到支付成功页面 sendMessage(payList[0].price);
uni.redirectTo({
url:
"/pages/cart/payment/success?paymentMethod=WECHAT" +
"&payPrice=" +
this.data.price,
});
}, },
fail: (e) => { fail: (e) => {
this.exception = 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; export default LiLiWXPay;

View File

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

View File

@ -7,36 +7,24 @@
<div class="pay-btns"> <div class="pay-btns">
<div v-show="!from" @click="checkOrder">{{this.orderType == "RECHARGE" ? '' : ''}}</div> <div v-show="!from" @click="checkOrder">{{this.orderType == "RECHARGE" ? '' : ''}}</div>
<div @click="navigateTo('/pages/tabbar/home/index', 'switch')">回到首页</div> <div @click="navigateTo('/pages/tabbar/home/index', 'switch')">回到首页</div>
</div> </div>
</div> </div>
<div class="pay-box"> <div class="pay-box">
<div class="pay-tag-box"> <div class="pay-tag-box">
<h2>订单支付成功!</h2> <h2>订单支付成功!</h2>
<div class="pay-item"> <div class="pay-item">
<div> <div>
支付方式 支付方式
</div> </div>
<div>{{paymentMethod | paymentTypeFilter}}</div> <div>{{paymentMethod | paymentTypeFilter}}</div>
</div> </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> </div>
<goodsRecommend /> <goodsRecommend />
</div> </div>
</template> </template>
<script> <script>
import { getWeChatMpMessage } from "@/api/message.js";
import goodsRecommend from "@/components/m-goods-recommend"; import goodsRecommend from "@/components/m-goods-recommend";
export default { export default {
data() { data() {
@ -48,7 +36,6 @@ export default {
payPrice: 0, payPrice: 0,
goodsList: [], goodsList: [],
activeColor: this.$mainColor, activeColor: this.$mainColor,
}; };
}, },
components: { components: {
@ -73,6 +60,7 @@ export default {
this.from = options.from || ""; this.from = options.from || "";
this.payPrice = options.payPrice || 0; this.payPrice = options.payPrice || 0;
this.orderType = options.orderType; this.orderType = options.orderType;
// this.sendMessage()
}, },
methods: { methods: {
checkOrder() { 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) { navigateTo(url, type) {
if (type === "switch") { if (type === "switch") {
uni.switchTab({ uni.switchTab({