CreateUserApe.js 12.7 KB
//*
// 用户管理模块
// */

import Loger from "../Loger";
import Ape from "./Ape";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
import ErrorApe from "./ErrorApe";

let loger = Loger.getLoger('PC-CreateUserApe');
const adminListTit = `<table width="100%" align="center" cellpadding="0" cellspacing="0" border="0" class="t_box">
                        <tbody>
                            <tr align="center" height="44" class="tr_center" bgcolor="#fff" bordercolor="#e6e6e6">
                                <th width="8%" class="tr_center">
                                    登录名
                                </th>
                                <th width="8%" class="tr_center">
                                    姓名
                                </th>
                                <th width="8%" class="tr_center">
                                    科目
                                </th>
                                <th width="8%" class="tr_center">
                                    上课总数
                                </th>
                                <th width="8%" class="tr_center">
                                    最新约课
                                </th>
                                <th width="10%" class="tr_center">
                                    操作
                                </th>
                            </tr>
`;
//管理首页
const adminList = `<tr align="center" bgcolor="#FFFFFF" height="44" bordercolor="#e6e6e6">
                                <td width="8%" class="tr_main tr_mainLeft">{_loginName}</td>
                                <td width="8%" class="tr_main">{_userName}</td>
                                <td width="8%" class="tr_main">{_course}</td>
                                <td width="8%" class="tr_main">{_totalNmu}</td>
                                <td width="8%" class="tr_main">{_latest}</td>
                                <td  width="10%" class="tr_main tr_mainRight" align="center" >
                                    <a id="{_removeInd}" class="classHandlePort" href="###">删除</a>
                                    <a id="{_joinInd}" class="classHandlePort"" data="{_aboutMsg}" href="###">查看约课信息</a>
                                </td>
                            </tr>
`;
const adminListEnd = `</tbody></table>`;
class CreateUserApe extends Ape {
    constructor() {
        super();
        this.pageNo = 0;
        this.presentInd = 1;
        this.addEvent();
        this.init();
    }

    init() {

        $('#userClassList3 li').on('click',this._userClassListHandler.bind(this));//用户列表

        //用户管理
        $('#addUserBtn').on('click',this._addUserBtnHandler.bind(this));//创建用户按钮
        $('#createUserYes').on('click',this._createUserYesHandler.bind(this));//进入创建用户界面
        $('#createUserConfirm').on('click',this._createUserConfirmHandler.bind(this));//创建用户按钮
        $('#createUserA').on('click',this._closeUserClassHandler.bind(this));//关闭创建课堂
        $('#createUserCancel').on('click',this._closeUserClassHandler.bind(this));//关闭创建课堂

        $('#classHandlerHomePage3').on('click',this._homePageHandler.bind(this));//首页
        $('#classHandlerUpPage3').on('click',this._upPageHandler.bind(this));//上页
        $('#classHandlerDownPage3').on('click',this._downPageHandler.bind(this));//下页
        $('#classHandlerEndPage3').on('click',this._endPageHandler.bind(this));//尾页
        $('#classHandlerSpecificPage3').on('click',this._assignPageHandler.bind(this));//指定页


    }

    addEvent() {

    }

    _switchover(evt){
        $(evt.currentTarget).addClass('liveInfoLeftLiCheck').siblings().removeClass('liveInfoLeftLiCheck');
    }
    _userClassListHandler(evt){
        this._switchover(evt);
    }
    //首页
    _homePageHandler(){
        this.presentInd = 1;
        this.userAdminHandler(this.presentInd);
    }
    //上页
    _upPageHandler(){
        if(this.presentInd >1){
            this.presentInd--;
            this.userAdminHandler(this.presentInd);
        }
    }
    //下页
    _downPageHandler(){
        if(this.presentInd < this.pageNo){
            this.presentInd++;
            this.userAdminHandler(this.presentInd);
        }
    }
    //尾页
    _endPageHandler(){
        this.presentInd = this.pageNo;
        this.userAdminHandler(this.presentInd);
    }
    //指定页
    _assignPageHandler(){
        let inputVal = $('#classHandlerPageInfoInput3').val();
        if(parseInt(inputVal) <= this.pageNo || parseInt(inputVal) >= 1 ){
            this.userAdminHandler(parseInt(inputVal));
        }
    }
    //用户管理 创建用户界面
    userAdminHandler(ind){
        this.createUserClassUI();

        let _url = ClassDataProxy.locationProtocol + ClassDataProxy.locationProt + '/users/getUser';
        let that = this;
        $.ajax({
            type: "POST",
            url: _url,
            timeout:5000,
            data:{siteId:ClassDataProxy.siteId,userType:ClassDataProxy.USER_TYPE_1},
            headers: {siteId:ClassDataProxy.siteId,
                pageno:7,
                'page':ind,
                'token':'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IlhXQnJlRU00T0swYTVqUUIyUTFicGtObHlxOUFWbXZkIiwibmFtZSI6ImFkbWluIiwiY29kZSI6MSwiaWF0IjoxNTAzNDUxODU0LCJleHAiOjIyODEwNTE4NTR9.3kTVzKd_jbP7l69nxOpNaFeSjVnnjZkagYMzRB8fjNo'},
            success:function(_data){
                loger.log(_data,'创建用户界面用户列表数据')
                loger.log(ClassDataProxy.USER_TYPE_1,'创建用户界面用户列表数据')
                //获取课堂数据
                if(_data && _data.code == 200){
                    that._gainUserClassData(_data.returnData.data);
                }
            },
            error:function(error){
                //alert('直播数据获取失败')
                loger.log(error,'直播数据获取失败')
            }
        })
    }
    _gainUserClassData(_data){
        let _dataList = _data.users;
        let courseContent = $('#courseContent3');
        courseContent.empty();
        let count = _data.count;
        let page = _data.page;
        let pageno = _data.pageno;
        this.pageNo = pageno;
        $('#classHandlerPageMsg3').html('第'+page+'页/共'+pageno+'页,有'+count+'个记录');
        courseContent.append(adminListTit);
        if(_dataList){
            if(count <= 7){
                $('#classHandlerPageBox3').hide();
            }
            for(let i = 0;i<_dataList.length;i++){
                let _adminList = this._format(adminList,{
                    _loginName : _dataList[i].loginName,
                    _userName : _dataList[i].userName,
                    _course : '暂无',
                    _totalNmu : '2',
                    _latest : '1',
                    _removeInd : 'classDoc'+i,
                    _joinInd : 'classMedia'+i,
                    _aboutMsg : _dataList[i].id
                })
                courseContent.append(_adminList);
            }
            courseContent.append(adminListEnd);
        }
    }
    createUserClassUI(){
    }
    //用户管理
    _addUserBtnHandler(){
        $('#createUserAccountBox').show();
    }
    _createUserYesHandler(){
        $('#createUserAccountBox').hide();
        $('#createUserBox').show();
        let val = $('#createUserRole').val();
        let name = '';
        switch (parseInt(val)){
            case 0:
                name = '老师';
                break;
            case 1:
                name = '兼课';
                break;
            default:
                return;
        }
        $('#createUserTitle').html('添加'+name)
        $("input[name='createUserTeacher']").get(val).checked = true;//创建身份
    }
    //创建用户
    _createUserConfirmHandler(){
        let _name = document.getElementById("createUserName");//老师名字
        let _passWord = document.getElementById("createUserPassWord");//密码
        let createUserRealName = document.getElementById("createUserRealName");//真实姓名
        let  createCharacter = $("input[name='createUserTeacher']:checked").val();//角色
        let phone = document.getElementById("createUserPhone");//手机号
        let identity = document.getElementById("createUserIdentity");//身份证号
        let mail = document.getElementById("createUserMail");//邮箱

        let createUserNote = $('#createUserNote').val();//备注信息
        let createUserAbortTime = $('#createUserAbortTime').html();//有效期

        if(_name.value && createUserRealName.value && phone.value && mail.value){
            let createUserList = {
                "loginName": _name.value, // 登录名
                "password": parseInt(_passWord.value),  //登录密码
                "companyName":null,  //公司名字
                "userName":createUserRealName.value, //用户名
                "userRole":3, // 用户角色 0超级管理员,1管理员,2监课,3老师,4学生
                "userType": 1,
                "userEmail":mail.value, // 用户邮箱
                "userMobile":parseInt(phone.value), // 用户手机号
                "content":createUserNote, // 用户简介
                "groupId":1, //用户组id
                "IDcard":123123,
                "time":1,
                "siteId":ClassDataProxy.siteId
            }
            let that = this;
            let _url = ClassDataProxy.locationProtocol + ClassDataProxy.locationProt + '/users/addUser';
            $.ajax({
                type: "POST",
                url: _url,
                data: createUserList,
                timeout:5000,
                headers: {siteId:ClassDataProxy.siteId},
                success:function(_data){
                    //注册成功后 跳转登录界面
                    if(_data && _data.code == 200){
                        loger.log(_data,'注册老师账号成功')
                        ErrorApe.showWarnError('注册成功');
                        $('#createUserBox').hide();
                    }else{
                        ErrorApe.showWarnError(_data.returnData.data.msg);
                    }
                },
                error:function(error){
                    console.log(error,'失败')
                }
            })
        }else{
            this._signMail(mail);//判断邮箱
            this._signIdentity(identity);
            this._signPhone(phone);//判断手机号
            this._signName(createUserRealName);//判断用户名
            this._signName(_name);//判断登录名
        }
    }
    _signName(_name){
        if(!_name.value){
            _name.focus();
            return false;
        }
    }
    _signPhone(phone){
        let reg = "^1[3|4|5|8][0-9]\\d{8}$";
        let re = new RegExp(reg);
        if(!phone.value){
            phone.focus();
            return false;
        }
        if (re.test(phone.value)) {
            $('#createUserPhone').css('color','#000');
        } else {
            $('#createUserPhone').val('手机号输入不合法')
                .css('color','#d95136');
            phone.focus();
            return false;
        }
    }
    _signMail(mail){
        let filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(!mail.value){
            mail.focus();
            return false;
        }
        if (filter.test(mail.value)){
            $('#createUserMail').css('color','#000');
        }
        else {
            $('#createUserMail').val('电子邮箱输入不合法')
                .css('color','#d95136');
            mail.focus();
            return false;
        }
    }
    _signIdentity(identity){
        let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if(!identity.value){
            identity.focus();
            return false;
        }
        if (reg.test(identity.value)){
            $('#createUserIdentity').css('color','#000');
        }
        else {
            $('#createUserIdentity').val('身份证号码输入不合法')
                .css('color','#d95136');
            identity.focus();
            return false;
        }
    }
    _closeUserClassHandler(){
        $('#createUserBox').hide();
        this._clearUserClssMsg();
    }
    _clearUserClssMsg(){
        $('#createUserName').val('');
        $('#createUserPassWord').val('');
        $('#createUserRealName').val('');
        $('#createUserPhone').val('');
        $('#createUserIdentity').val('');
        $('#createUserMail').val('');
        $('#createUserNote').val('');
    }

    //工具类
    _format(str, obj) {
        return str.replace(/\{(\w+)\}/g, function (match, group, index) {
            return obj[group];
        });
    };
}
export default CreateUserApe;