joinClass.js 8.4 KB
// pages/joinClass/joinClass.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        students: [],
      array: ['爸爸', '妈妈', '其他家长', '学生本人'],
        index: 0,
        ind: -1,
        showModel: false,
        value: '',
        class_id:'',
        className: [],
        formId:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      console.log(options)
        if (options.class_id == 'undefined'){
            options.class_id = ""
        }
        this.setData({
            class_id: options.class_id,
            id: options.id,
            where: options.where,
        })
        var inent = wx.getStorageSync('ident');
        if (inent == "") {
            wx.redirectTo({
                url: '/pages/home/home',
            })
        } else if (inent === "0"||inent == '2') {
            console.log(options.class_id)
            if (options.class_id == "undefined") {
                wx.showModal({
                    title: '提示',
                    content: '请通过分享链接加入班级',
                    showCancel: false
                })
            }else{
                
            }
            this.addClass();
        } else if (inent === "1"){
            wx.showModal({
                title: '提示',
                content: '您的身份不可查看',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        wx.redirectTo({
                            url: '/pages/index/index',
                        })
                    }
                }
            })
        } else {
            wx.redirectTo({
                url: '/pages/home/home',
            })
        }
    },
    // 学生列表
    addClass() {
        let t = this;
        if(t.data.class_id == 'undefined'){
            t.setData({
                class_id: ''
            })
        }
        let url = "/api/student/studentList";
        let params = {
            class_id: t.data.class_id
        };
        app.post(url, params).then(res => {
            console.log(res)
            if (res.data.code == 5000) {
                setTimeout(() => {
                    wx.redirectTo({
                        url: '/pages/home/home?class_id=' + t.data.class_id + '&id=' + t.data.id + '&where=' + t.data.where,
                    })
                }, 300)
            } else if (res.data.code == 2000) {
                if (t.data.where == "1") {
                    setTimeout(() => {
                        wx.redirectTo({
                            url: '/pages/submit/submit?id=' + t.data.id + '&class_id=' + t.data.class_id,
                        })
                    }, 500)
                }else{
                  wx.showToast({
                    title: '您已绑定过了',
                    icon: 'none'
                  })
                  setTimeout(() => {
                    wx.redirectTo({
                      url: '/pages/parentsDo/parentsDo',
                    })
                  }, 500)
                }


            } else {
                console.log(res.data.msg.class)
                console.log(res.data.msg.res)
                 let all = res.data.msg.res.length-1;
                t.setData({
                    className: res.data.msg.class,
                    students: res.data.msg.res ? res.data.msg.res : [],
                    studentsId: t.data.addStudent?res.data.msg.res[all].id:'',
                    ind: t.data.addStudent?all:'-1'
                })
                console.log(t.data.students, 'students')
            }
        })
    },
    formIdFun() {
        let url = "/api/index/formId";
        let params = {
            formId: this.data.formId
        };
        app.post(url, params).then((res) => {
            console.log(res)
        })
    },
    // 加入班级
    addClassFun(e) {
        // wx.showLoading({
        //     title: '加载中',
        // });
        let t = this;        
        this.data.formId = e.detail.formId
        let url = '/api/student/joinstudent';
        let params = {
            student: t.data.studentsId,
            class_id: t.data.class_id,
            studentInfo: t.data.array[t.data.index]  
        };
        this.formIdFun();
        console.log(t.data.class_id)
        if (t.data.class_id == undefined || t.data.class_id == '') {
            wx.showModal({
                title: '提示',
                content: '请通过分享链接加入班级',
                showCancel: false
            });
            return false;
            // wx.hideLoading();            

        } else if (t.data.ind == -1) {
            wx.showToast({
                title: '请选择学生',
                icon: 'none'
            })
            // wx.hideLoading();            
        } else {
            app.post(url, params).then(res => {
                wx.showToast({
                    title: '加入成功',
                })
                if (t.data.where == "1") {
                    console.log(13123)
                    setTimeout(() => {
                        wx.redirectTo({
                            url: '/pages/submit/submit?id=' + t.data.id + '&class_id=' + t.data.class_id,
                        })
                    }, 200)
                } else {
                    console.log(999)
                    setTimeout(() => {
                        wx.redirectTo({
                            url: '/pages/parentsDo/parentsDo?class_id=' + t.data.class_id,
                        })
                    }, 200)
                }
                // wx.hideLoading();
            })
        }

    },
    // 学生
    gay(e) {
        console.log(e);
        this.setData({
            ind: e.currentTarget.dataset.ind,
            studentsId: e.currentTarget.dataset.id
        })
    },
    // 与学生关系picker
    bindPickerChange(e) {
        console.log(e);
        this.setData({
            index: e.detail.value,
        })
    },
    // 添加学生的 ‘加’
    addFun() {
        this.setData({
            showModel: true
        })
    },
    // 添加学生input
    maskChangeFun(e) {
        console.log(e)
        let length = e.detail.value.length;
        this.setData({
            length: length,
            value: e.detail.value
        })
        // console.log(length);
    },
    // 关闭弹框
    ShutdownFun() {
        this.setData({
            showModel: false,
            value: ''
        })
    },
    // 添加学生确定按钮
    maskOkFun() {
        let t = this;
        let url = '/api/student/addStudent';
        let params = {
            class_id: t.data.class_id,
            name: t.data.value
        };
        // let obj = [];
        // t.data.students.forEach(function (value, index) {
        //     console.log(value.name);
        //     obj.push(value.name)
        // })
        // t.setData({
        //     obj: obj
        // })
        if (t.data.length > 6) {
            wx.showToast({
                title: '学生姓名不得超过6字符',
                icon: 'none'
            })
        } else if (t.data.value == '') {
            wx.showToast({
                title: '学生姓名不得为空',
                icon: 'none'
            })
        } else {
            app.post(url, params).then(res => {
                console.log(res);
                t.setData({
                  addStudent: true,
                })
                t.addClass();
            })
            t.setData({
                showModel: false,
                value: '',
            })
        }

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // wx.hideHomeButton({
        //     success: function() {
        //         console.log('---')
        //     }
        // })
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})