charge.js 4.3 KB
// pages/charge/charge.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        code:'',
        index: 0,
        picker_list: [],
        modal_data: {
            is_showModal: false,
            currentBtn: null,
            title: '',
            tab_left: '取消',
            tab_right: '确认',
        },
        is_confirm: false,
    },
    //输入兑换码
    codeInput(e) {
        this.setData({code: e.detail.value})
    },
    //选择成员
    changePicker(e) {
        this.setData({
            index: e.detail.value
        })
    },

    //兑换并绑定按钮
    chargeBtn(e) {
        // console.log('34',e.detail);
        const self = this;
        if(self.data.code === '') {
            wx.showToast({title: '请输入兑换码!',icon: 'none'})
        }else if(self.data.picker_list.length === 0) {
            wx.showToast({title: '请添加学员!',icon: 'none'})
        }else {
            let url = '/user/Student/student_bind';
            let header = {
                "XX-token": wx.getStorageSync('token')
            };
            if(self.data.is_confirm) {
                let params = {
                    id: self.data.student_list[self.data.index].id,
                    card: self.data.code,
                    formId: self.data.detail.formId,
                };
                app.post(url,params,header).then((res) => {
                    console.log('兑换并绑定',res);
                    this.setData({is_confirm: false});
                    if(res === '参数错误') {
                    }
                    // console.log('list', list);
                    // this.setData({picker_list: list})
                })
            }else {
                self.setData({
                    detail: e.detail,
                    'modal_data.is_showModal': true,
                    'modal_data.title': '绑定后无法更改,确认绑定' + self.data.picker_list[self.data.index] + '吗?',
                });
            }
        }
    },

    //添加学员
    addUser() {
        wx.navigateTo({
            url: '../../index/confirm-appointment-info/add-student/add-student?is_back=' + true,
        })
    },
    chooseNo(e) {
        this.setData({
            'modal_data.currentBtn': e.currentTarget.dataset.index,
            'modal_data.is_showModal': false
        });
        // wx.switchTab({url: '/pages/index/index'})
        // wx.navigateBack({delta: 1})
    },
    chooseYes(e) {
        this.setData({
            'modal_data.currentBtn': e.currentTarget.dataset.index,
            'modal_data.is_showModal': false,
            'state_data.is_showSuccessModal': true,
            is_confirm: true,
        });
        if(this.data.is_confirm) {
            this.chargeBtn();
        }
    },
    modalConfirm() {
        this.setData({'state_data.is_showSuccessModal': false})
    },
    disableScroll() {},
    //获取学员列表
    getStudentList() {
        let url = '/user/Student/student_list';
        let header = {
            "XX-token": wx.getStorageSync('token')
        };
        let params = {
            page: 1,
        };
        var list = [];
        app.post(url,params,header).then((res) => {
            console.log('学员列表',res);
            res.list.forEach((item) => {
                list.push(item.name)
            });
            console.log('list', list);
            this.setData({picker_list: list,student_list: res.list})
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const self = this;
        self.getStudentList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})