
/*
 * 本时生活
 */

const app = getApp()


Page({

    /**
     * 页面的初始数据
     */
    data: {
        carmi: ''  //卡密
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
    },

    /**
     * 卡激活
     */
    cardform(e) {
        let code = e.detail.value.code,
            pass = this.data.carmi

        wx.$api.user.cards(code, pass).then(res=>{
            if(res.data.type == "silver") {
                app.globalData.userCurrent = 0
            }else if(res.data.type == "gold") {
                app.globalData.userCurrent = 1
            }else if(res.data.type == "drill") {
                app.globalData.userCurrent = 2
            }else {
                return
            }

            // 写入缓存
            wx.setStorage({
                key     : 'current',
                data    : app.globalData.userCurrent
            })

            wx.showToast({
                title: res.data.message,
                icon : 'none'
            })
            setTimeout(()=>{
                wx.switchTab({
                    url: '/pages/user/user'
                })
            },2000)
        }).catch(err => {});
    },

    /**
     * 获取卡密
     */
    carmiTab(e){
        var number = e.detail.value
        var change = number.replace(/(\d{4})(?=\d)/g, "$1-");//replace(/\s/g,'');
        this.setData({
            carmi: change
        })
      }
})