123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- 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 = /<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(/\<img/gi, '<img class="richImg" ')
- },
- })
|