import {
get,
post
} from '../../utils/http'
import {
formatActivity
} from '../../utils/time'
var WxParse = require('../../components/local/wxParse/wxParse.js')
Page({
/**
* 页面的初始数据
*/
data: {
list: [],
visible: false,
userInfo: null,
current: 0,
activity: {},
wxParseMemberRules: false, // 是否已解析
pay_methon: 1,
payNow: false,
},
pay_methonw() {
this.setData({
pay_methon: 1
})
},
pay_methonc() {
this.setData({
pay_methon: 2
})
},
onClosePay() {
this.setData({
payNow: false
})
},
/**
* 获取我的储值
* api/user
*/
getAmount() {
get('api/user/amount', {}, (res) => {
if (res.data) {
this.setData({
inserllAmount: res.data.amount
})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// if(options.userInfo) {
// let userInfo = decodeURIComponent((options.userInfo));
// this.setData({ userInfo: JSON.parse(userInfo) })
// }
this.setData({
userInfo: wx.getStorageSync('userInfo'),
})
this.getCard()
this.getUserActivity()
console.log(getApp().globalData)
let that = this
// 获取会员守则
this.getmemberRules()
.then((memberRules) => {
WxParse.wxParse('article', 'html', memberRules, that, 5)
that.setData({
wxParseMemberRules: true,
})
})
.catch(() => {})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.getAmount()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
if (this.timer_) {
clearInterval(this.timer_)
}
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {},
onPay: function () {
this.setData({
payNow: true,
})
},
/**
* 去支付
*/
payNow() {
let {
list,
current
} = this.data
let that = this
this.checkFirst()
.then((res) => {
post(
'api/card', {
card_id: list[current].id,
},
(res) => {
this.setData({
order_id: res.data.order_id,
})
post(
'api/pay/card', {
pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
order_id: res.data.order_id,
},
(res) => {
that.setData({
payNow: false
})
if (that.data.pay_methon == 2) {
wx.showToast({
title: '支付成功',
icon: 'none',
})
setTimeout(() => {
that.setData({
visible: true
})
}, 1000)
return
}
wx.requestPayment({
timeStamp: res.data.pay_data.timeStamp,
nonceStr: res.data.pay_data.nonceStr,
package: res.data.pay_data.package,
signType: res.data.pay_data.signType,
paySign: res.data.pay_data.paySign,
success(res) {
wx.showToast({
title: '支付成功',
icon: 'none',
})
setTimeout(() => {
that.setData({
visible: true
})
}, 1000)
},
fail(res) {
wx.showToast({
title: '支付失败',
icon: 'error',
})
},
})
}
)
}
)
})
.catch((rej) => {
wx.navigateTo({
url: '/pages/disclaimer/disclaimer',
})
})
},
/**
* 暂不激活
*/
onCancelUse() {
this.onClose('visible')
wx.redirectTo({
url: '/pages/vipCard/vipCard',
})
},
/**
* 确认激活
*/
onConfirmUse() {
post(
'api/card/active', {
order_id: this.data.order_id,
},
(res) => {
console.log(res)
this.onClose('visible')
wx.showToast({
title: '激活成功',
icon: 'none',
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
}
)
},
/**
* 关闭弹框
*/
onClose(key) {
this.setData({
[key]: false,
})
},
/**
* 选择vip
*/
onChangeVip(e) {
this.setData({
current: e.currentTarget.dataset.index,
})
},
/**
* 获取会员卡列表
* /api/card
*/
getCard() {
get('api/card', {}, (res) => {
this.setData({
list: res.data,
})
console.log(res)
})
},
/**
* 是否首次购买
* /api/order/check_first
*/
checkFirst() {
return new Promise((resolve, reject) => {
if (wx.getStorageSync('agree')) {
resolve()
} else {
get('api/order/check_first', {}, (res) => {
if (res.data.order_status == 1) {
resolve()
} else {
reject()
}
})
}
})
},
/**
* 获取首次开卡优惠
* api/user/activity
*/
getUserActivity() {
if (this.timer) {
clearInterval(this.timer_)
}
get('api/user/activity', {}, (res) => {
if (res.data) {
res.data.currentTime = formatActivity(res.data.expire_time_seconds)
this.setData({
activity: res.data,
})
this.timer_ = setInterval(() => {
if (res.data.expire_time_seconds <= 0) {
clearInterval(this.timer_)
}
res.data.expire_time_seconds--
res.data.currentTime = formatActivity(res.data.expire_time_seconds)
this.setData({
activity: res.data,
})
}, 1000)
}
})
},
/**
* 获取会员守则
*/
getmemberRules() {
let memberRules = getApp().globalData.memberRules
return new Promise((resolve, reject) => {
if (memberRules) {
resolve(memberRules)
} else {
get(
'api/agreement', {},
(res) => {
memberRules = res.data.member_rules
memberRules = this.removeCss(memberRules)
getApp().globalData.memberRules = memberRules
resolve(memberRules)
},
() => {
reject()
}
)
}
})
},
/**
* 去除富文本图片默认样式
*/
removeCss(content) {
let reg = /(style|class)="[^"]+"/gi
let img = /]+>/gi
let res
if (img.test(content)) {
res = content.match(img)
for (let i = 0; i < res.length; i++) {
content = content.replace(res[i], res[i].replace(reg, ''))
}
}
return content.replace(/\