|
@@ -4,7 +4,8 @@
|
|
|
<view class="card-title">VIP会员</view>
|
|
<view class="card-title">VIP会员</view>
|
|
|
<view class="card-desc">开通VIP会员专享权益</view>
|
|
<view class="card-desc">开通VIP会员专享权益</view>
|
|
|
<view class="card-status">
|
|
<view class="card-status">
|
|
|
- 已经开通 <button class="card-btn">立即开通</button>
|
|
|
|
|
|
|
+ <template v-if="userInfo && userInfo.type === '1'">已开通</template>
|
|
|
|
|
+ <button class="card-btn" v-else @click="handlePay">立即开通</button>
|
|
|
</view>
|
|
</view>
|
|
|
</view>
|
|
</view>
|
|
|
<view class="list-title">VIP会员专享{{ list.length }}项权益</view>
|
|
<view class="list-title">VIP会员专享{{ list.length }}项权益</view>
|
|
@@ -18,11 +19,15 @@
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
<script>
|
|
|
|
|
+import { getUserInfo } from "@/lib/api/user";
|
|
|
|
|
+import { vipPrePay, vipCompletePay, vipQueryPay } from "@/lib/api/pay";
|
|
|
|
|
+
|
|
|
export default {
|
|
export default {
|
|
|
name: "VipVip",
|
|
name: "VipVip",
|
|
|
data() {
|
|
data() {
|
|
|
return {
|
|
return {
|
|
|
id: 0,
|
|
id: 0,
|
|
|
|
|
+ userInfo: null,
|
|
|
list: [
|
|
list: [
|
|
|
{
|
|
{
|
|
|
name: "10000积分",
|
|
name: "10000积分",
|
|
@@ -51,7 +56,114 @@ export default {
|
|
|
],
|
|
],
|
|
|
};
|
|
};
|
|
|
},
|
|
},
|
|
|
- methods: {},
|
|
|
|
|
|
|
+ onShow() {
|
|
|
|
|
+ const uinfoStr = uni.getStorageSync("userinfo");
|
|
|
|
|
+ if (uinfoStr) {
|
|
|
|
|
+ this.userInfo = JSON.parse(uinfoStr);
|
|
|
|
|
+ this.getUserInfo();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.userInfo = {};
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ methods: {
|
|
|
|
|
+ async getUserInfo() {
|
|
|
|
|
+ const res = await getUserInfo();
|
|
|
|
|
+ this.userInfo = { ...this.userInfo, ...res.data };
|
|
|
|
|
+ uni.setStorageSync("userinfo", JSON.stringify(this.userInfo));
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ async handlePay() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await this.wechatPay();
|
|
|
|
|
+ this.getUserInfo();
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ uni.showToast({
|
|
|
|
|
+ icon: "none",
|
|
|
|
|
+ title: e.message,
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ async wechatPay() {
|
|
|
|
|
+ uni.showLoading({
|
|
|
|
|
+ title: "加载中",
|
|
|
|
|
+ mask: true,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const { errno, errmsg, data } = await vipPrePay();
|
|
|
|
|
+
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+
|
|
|
|
|
+ if (errno !== 10000) {
|
|
|
|
|
+ throw new Error(errmsg);
|
|
|
|
|
+ }
|
|
|
|
|
+ const {
|
|
|
|
|
+ timeStamp,
|
|
|
|
|
+ nonceStr,
|
|
|
|
|
+ package: pkg,
|
|
|
|
|
+ signType,
|
|
|
|
|
+ paySign,
|
|
|
|
|
+ order_id,
|
|
|
|
|
+ } = data;
|
|
|
|
|
+
|
|
|
|
|
+ const type = await new Promise((resolve) => {
|
|
|
|
|
+ uni.requestPayment({
|
|
|
|
|
+ provider: "wxpay",
|
|
|
|
|
+ timeStamp,
|
|
|
|
|
+ nonceStr,
|
|
|
|
|
+ package: pkg,
|
|
|
|
|
+ signType,
|
|
|
|
|
+ paySign,
|
|
|
|
|
+ success: async (res) => {
|
|
|
|
|
+ resolve(
|
|
|
|
|
+ res.errMsg === "requestPayment:ok"
|
|
|
|
|
+ ? "1"
|
|
|
|
|
+ : res.errMsg === "requestPayment:fail cancel"
|
|
|
|
|
+ ? "3"
|
|
|
|
|
+ : "2"
|
|
|
|
|
+ );
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: async (err) => {
|
|
|
|
|
+ resolve(err.errMsg === "requestPayment:fail cancel" ? "3" : "2");
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ await vipCompletePay({
|
|
|
|
|
+ order_id,
|
|
|
|
|
+ type,
|
|
|
|
|
+ }).catch((e) => {
|
|
|
|
|
+ console.log(e);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (type !== "1") throw new Error(type === "3" ? "取消支付" : "支付失败");
|
|
|
|
|
+
|
|
|
|
|
+ return this.loopPayRes("" + order_id);
|
|
|
|
|
+ },
|
|
|
|
|
+ async loopPayRes(order_id) {
|
|
|
|
|
+ uni.showLoading({
|
|
|
|
|
+ title: "加载中",
|
|
|
|
|
+ mask: true,
|
|
|
|
|
+ });
|
|
|
|
|
+ const data = await vipQueryPay({
|
|
|
|
|
+ order_id,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (data.errno == 90000) {
|
|
|
|
|
+ return new Promise((r) => {
|
|
|
|
|
+ setTimeout(() => r(), 1000);
|
|
|
|
|
+ }).then(() => this.loopPayRes(order_id));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+
|
|
|
|
|
+ if (data.errno == 10000) {
|
|
|
|
|
+ return data.data;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ throw new Error("订单查询失败");
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
};
|
|
};
|
|
|
</script>
|
|
</script>
|
|
|
|
|
|