自动化立体仓库 - WMS系统
野心家
2023-06-20 5ce6bfb2e237b9fbf092a03cf80e86d928ac7ca6
src/main/webapp/static/js/user/user.js
@@ -1,33 +1,40 @@
var pageCurr;
layui.use(['table','laydate', 'form'], function(){
var tableData;
var insTb2;
layui.config({
    base: baseUrl + "/static/layui/lay/modules/"
}).use(['table','laydate', 'form', 'util', 'admin'], function(){
    var table = layui.table;
    var $ = layui.jquery;
    var layer = layui.layer;
    var layDate = layui.laydate;
    var form = layui.form;
    var admin = layui.admin;
    var util = layui.util;
    // 数据渲染
    tableIns = table.render({
        elem: '#user',
    insTb2 = table.render({
        elem: '#userTable',
        headers: {token: localStorage.getItem('token')},
        url: baseUrl+'/user/list/auth',
        page: true,
        limit: 16,
        toolbar: '#toolbar',
        even: true,
        cellMinWidth: 50,
        limit: 15,
        limits: [15, 30, 50, 100, 200, 500],
        toolbar: '#userToolbar',
        height: 'full-100',
        cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', title: 'ID', sort: true,align: 'center', fixed: 'left', width: 80}
            ,{field: 'hostName', align: 'center',title: '授权商户',event: 'Host', style: 'text-decoration: underline;cursor:pointer'}
            ,{field: 'username', align: 'center',title: '账号'}
            {type: 'checkbox'}
            // ,{field: 'hostName', align: 'center',title: '授权商户', templet: '#hostTpl', width: 140}
            // ,{field: 'nickname', align: 'center',title: '用户名'}
            ,{field: 'username', align: 'center',title: '登录账户'}
            ,{field: 'mobile', align: 'center',title: '手机号'}
            ,{field: 'password', align: 'center',title: '密码'}
            ,{field: 'roleName', align: 'center',title: '角色',event: 'Role', style: 'text-decoration: underline;cursor:pointer'}
            ,{field: 'createTime$', align: 'center',title: '注册时间'}
            ,{field: 'status$', align: 'center',title: '状态'}
            // ,{field: 'deptName', align: 'center',title: '所属部门'}
            ,{field: 'roleName', align: 'center',title: '角色'}
            ,{field: 'email', align: 'center',title: '邮箱'}
            // ,{field: 'sex$', align: 'center',title: '性别'}
            ,{field: 'createTime$', align: 'center',title: '注册时间', hide: true}
            ,{field: 'status$', align: 'center',title: '状态', templet: '#statusTpl', width: 120, unresize: true}
            ,{fixed: 'right', title:'操作', align: 'center', toolbar: '#operate', width:150}
            ,{fixed: 'right', title:'操作', align: 'center', toolbar: '#operate', width:170}
        ]],
        request: {
            pageName: 'curr',
@@ -46,316 +53,174 @@
        },
        done: function(res, curr, count) {
            if (res.code === 403) {
                top.location.href = "/";
                top.location.href = baseUrl+"/";
            }
            tableData = table.cache.userTable;
            pageCurr=curr;
            limit();
        }
    });
    // 监听排序事件
    table.on('sort(user)', function (obj) {
        var searchData = {};
        $.each($('#search-box [name]').serializeArray(), function() {
            searchData[this.name] = this.value;
        });
        searchData['orderByField'] = obj.field;
        searchData['orderByType'] = obj.type;
        tableIns.reload({
            where: searchData,
            page: {
                curr: 1
            },
            done: function (res, curr, count) {
                if (res.code === 403) {
                    top.location.href = "/";
                }
                pageCurr=curr;
                limit();
            }
        });
    /* 表格2搜索 */
    form.on('submit(userTbSearch)', function (data) {
        insTb2.reload({where: data.field, page: {curr: 1}});
        return false;
    });
    // 监听头工具栏事件
    table.on('toolbar(user)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event) {
            case 'addData':
                layer.open({
                    type: 2,
                    title: '新增',
                    maxmin: true,
                    area: [top.detailWidth, top.detailHeight],
                    shadeClose: false,
                    content: 'user_detail.html',
                    success: function(layero, index){
                       clearFormVal(layer.getChildFrame('#detail', index));
                        layer.iframeAuto(index);layer.style(index, {top: (($(window).height()-layer.getChildFrame('#data-detail', index).height())/3)+"px"});
                    }
                });
                break;
            case 'refreshData':
                tableIns.reload({
                    page: {
                        curr: pageCurr
                    }
                });
                limit();
                break;
            case 'deleteData':
                var data = checkStatus.data;
                var ids=[];
                data.map(function (track) {
                    ids.push(track.id);
                });
                if (ids.length === 0){
                    layer.msg('请选择数据');
                } else {
                    layer.confirm('确定删除'+(ids.length===1?'此':ids.length)+'条数据吗', function(){
                        $.ajax({
                            url: baseUrl+"/user/delete/auth",
                            headers: {'token': localStorage.getItem('token')},
                            data: {ids: ids},
                            method: 'POST',
                            traditional:true,
                            success: function (res) {
                                if (res.code === 200){
                                    layer.closeAll();
                                    tableReload(false);
                                } else if (res.code === 403){
                                    top.location.href = "/";
                                } else {
                                    layer.msg(res.msg)
                                }
                            }
                        })
                    });
                }
                break;
            case 'exportData':
                layer.confirm('确定导出Excel吗', function() {
                    var titles = [];
                    var fields = [];
                    obj.config.cols[0].map(function (col) {
                        if (col.type === 'normal' && col.hide === false && col.toolbar == null) {
                            titles.push(col.title);
                            fields.push(col.field);
                        }
                    });
                    var exportData = {};
                    $.each($('#search-box [name]').serializeArray(), function () {
                        exportData[this.name] = this.value;
                    });
                    var param = {
                        'user': exportData,
                        'fields': fields
                    };
                    $.ajax({
                        url: baseUrl+"/user/export/auth",
                        headers: {'token': localStorage.getItem('token')},
                        data: JSON.stringify(param),
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        method: 'POST',
                        success: function (res) {
                            layer.closeAll();
                            if (res.code === 200) {
                                table.exportFile(titles, res.data, 'xls');
                            } else if (res.code === 403) {
                                top.location.href = "/";
                            } else {
                                layer.msg(res.msg)
                            }
                        }
                    });
                });
                break;
    /* 表格2头工具栏点击事件 */
    table.on('toolbar(userTable)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id).data;
        if (obj.event === 'add') { // 添加
            showEditModel()
        } else if (obj.event === 'del') { // 删除
            if (checkStatus.length === 0) {
                layer.msg('请选择要删除的数据', {icon: 2});
                return;
            }
            del(checkStatus.map(function (d) {
                return d.id;
            }));
        }
    });
    // 监听行工具事件
    table.on('tool(user)', function(obj){
    // 修改状态
    form.on('switch(statusSwitch)', function (obj) {
        var index  = obj.othis.parents('tr').attr("data-index");
        var data = tableData[index];
        data[this.name] = obj.elem.checked?1:0;
        http.post(baseUrl+"/user/edit/auth", {id: data.id, status: data[this.name]}, function (res) {
            layer.msg(res.msg, {icon: 1});
        })
    })
    /* 表格2工具条点击事件 */
    table.on('tool(userTable)', function (obj) {
        var data = obj.data;
        switch (obj.event) {
            // 详情
            case 'detail':
                layer.open({
                    type: 2,
                    title: '详情',
                    maxmin: true,
                    area: [top.detailWidth, top.detailHeight],
                    shadeClose: false,
                    content: 'user_detail.html',
                    success: function(layero, index){
                        setFormVal(layer.getChildFrame('#detail', index), data);
                        top.convertDisabled(layer.getChildFrame('#data-detail :input', index), true);
                        layer.getChildFrame('#data-detail-submit,#prompt', index).hide();
                        layer.iframeAuto(index);layer.style(index, {top: (($(window).height()-layer.getChildFrame('#data-detail', index).height())/3)+"px"});
                        layero.find('iframe')[0].contentWindow.layui.form.render('select');
                    }
                });
                break;
            // 编辑
            case 'edit':
                layer.open({
                    type: 2,
                    title: '修改',
                    maxmin: true,
                    area: [top.detailWidth, top.detailHeight],
                    shadeClose: false,
                    content: 'user_detail.html',
                showEditModel(data)
                break;
            // 删除
            case 'del':
                del([data.id]);
                break;
            // 重置密码
            case 'resetPwd':
                admin.open({
                    type: 1,
                    title: '重置密码',
                    offset: '150px',
                    area: ['360px'],
                    shade: 0.1,
                    shadeClose: true,
                    content: $("#resetpwd-window").html(),
                    success: function(layero, index){
                        setFormVal(layer.getChildFrame('#detail', index), data);
                        top.convertDisabled(layer.getChildFrame('#data-detail :input', index), false);
                        layer.iframeAuto(index);layer.style(index, {top: (($(window).height()-layer.getChildFrame('#data-detail', index).height())/3)+"px"});
                        layero.find('iframe')[0].contentWindow.layui.form.render('select');
                        layer.iframeAuto(index);
                        $('#resetUserId').val(data.id);
                    }
                });
                break;
            case 'Role':
                var param = top.reObject(data).roleId;
                if (param === undefined) {
                    layer.msg("无数据");
                } else {
                    layer.open({
                        type: 2,
                        title: '详情',
                        maxmin: true,
                        area: [top.detailHeight, top.detailWidth],
                        shadeClose: false,
                        content: '../role/role_detail.html',
                        success: function(layero, index){
                            $.ajax({
                                url: baseUrl+"/role/"+ param +"/auth",
                                headers: {'token': localStorage.getItem('token')},
                                method: 'GET',
                                success: function (res) {
                                    if (res.code === 200){
                                        setFormVal(layer.getChildFrame('#detail', index), res.data);
                                        top.convertDisabled(layer.getChildFrame('#data-detail :input', index), true);
                                        layer.getChildFrame('#data-detail-submit,#prompt', index).hide();
                                        layer.iframeAuto(index);layer.style(index, {top: (($(window).height()-layer.getChildFrame('#data-detail', index).height())/3)+"px"});
                                        layero.find('iframe')[0].contentWindow.layui.form.render('select');
                                    } else if (res.code === 403){
                                        parent.location.href = "/";
                                    }else {
                                        layer.msg(res.msg)
                                    }
                                }
                            })
                        }
                    });
                }
                break;
            case 'Host':
                var param = top.reObject(data).hostId;
                if (param === undefined) {
                    layer.msg("无数据");
                } else {
                    layer.open({
                        type: 2,
                        title: '详情',
                        maxmin: true,
                        area: [top.detailHeight, top.detailWidth],
                        shadeClose: false,
                        content: '../host/host_detail.html',
                        success: function(layero, index){
                            $.ajax({
                                url: baseUrl+"/host/"+ param +"/auth",
                                headers: {'token': localStorage.getItem('token')},
                                method: 'GET',
                                success: function (res) {
                                    if (res.code === 200){
                                        setFormVal(layer.getChildFrame('#detail', index), res.data);
                                        top.convertDisabled(layer.getChildFrame('#data-detail :input', index), true);
                                        layer.getChildFrame('#data-detail-submit,#prompt', index).hide();
                                        layer.iframeAuto(index);
                                        layer.style(index, {top: (($(window).height()-layer.getChildFrame('#data-detail', index).height())/3)+"px"});
                                        layero.find('iframe')[0].contentWindow.layui.form.render('select');
                                    } else if (res.code === 403){
                                        parent.location.href = "/";
                                    }else {
                                        layer.msg(res.msg)
                                    }
                                }
                            })
                        }
                    });
                }
                break;
        }
    });
    // 数据修改动作
    form.on('submit(edit)', function () {
        var index = layer.load(1, {
            shade: [0.5,'#000'] //0.1透明度的背景
    /* 显示表单弹窗 */
    function showEditModel(mData) {
        admin.open({
            type: 1,
            area: '600px',
            title: (mData ? '修改' : '添加') + '用户',
            content: $('#editDialog').html(),
            success: function (layero, dIndex) {
                // 回显表单数据
                form.val('detail', mData);
                // 表单提交事件
                form.on('submit(editSubmit)', function (data) {
                    if (isEmpty(data.field.roleId)) {
                        layer.msg('请选择角色', {icon: 2});
                        return false;
                    }
                    var loadIndex = layer.load(2);
                    $.ajax({
                        url: baseUrl+"/user/"+(mData?'update':'add')+"/auth",
                        headers: {'token': localStorage.getItem('token')},
                        data: data.field,
                        method: 'POST',
                        success: function (res) {
                            layer.close(loadIndex);
                            if (res.code === 200){
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                $(".layui-laypage-btn")[0].click();
                            } else if (res.code === 403){
                                top.location.href = baseUrl+"/";
                            }else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    })
                    return false;
                });
                // 弹窗不出现滚动条
                $(layero).children('.layui-layer-content').css('overflow', 'visible');
                layui.form.render('select');
            }
        });
        var data = {
            id: $('#id').val(),
            hostId: $('#hostId').val(),
            username: $('#username').val(),
            mobile: $('#mobile').val(),
            password: $('#password').val(),
            roleId: $('#roleId').val(),
            createTime: top.strToDate($('#createTime\\$').val()),
            status: $('#status').val(),
    }
        };
    /* 删除订单 */
    function del(ids) {
        layer.confirm('确定要删除选中数据吗?', {
            skin: 'layui-layer-admin',
            shade: .1
        }, function (i) {
            layer.close(i);
            var loadIndex = layer.load(2);
            $.ajax({
                url: baseUrl+"/user/delete/auth",
                headers: {'token': localStorage.getItem('token')},
                data: {ids: ids},
                method: 'POST',
                success: function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200){
                        layer.msg(res.msg, {icon: 1});
                        $(".layui-laypage-btn")[0].click();
                    } else if (res.code === 403){
                        top.location.href = baseUrl+"/";
                    }else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            })
        });
    }
    // 重置密码
    form.on('submit(savePwd)', function (data) {
        $.ajax({
            url: baseUrl+"/user/edit/auth",
            url: baseUrl+"/user/update/auth",
            headers: {'token': localStorage.getItem('token')},
            data: top.reObject(data),
            data: {
                id: data.field.resetUserId,
                password: hex_md5(data.field.resetPassword)
            },
            method: 'POST',
            success: function (res) {
                if (res.code === 200){
                    parent.layer.closeAll();
                    tableReload(true);
                    $("#data-detail :input").each(function () {
                        $(this).val("");
                    });
                    layer.closeAll();
                    layer.msg("重置密码成功", {icon: 1});
                } else if (res.code === 403){
                    top.location.href = "/";
                    top.location.href = baseUrl+"/";
                }else {
                    layer.msg(res.msg)
                    layer.msg(res.msg, {icon: 2})
                }
                layer.close(index);
            }
        })
    });
        return false;
    })
    // 搜索栏搜索事件
    form.on('submit(search)', function (data) {
        pageCurr = 1;
        tableReload(false);
    });
    // 搜索栏重置事件
    form.on('submit(reset)', function (data) {
        pageCurr = 1;
        clearFormVal($('#search-box'));
        tableReload(false);
    });
    // 时间选择器
    layDate.render({
        elem: '#createTime\\$',
        type: 'datetime'
    });
    layDate.render({
        elem: '#create_time\\>',
        type: 'datetime'
    });
    layDate.render({
        elem: '#create_time\\<',
        type: 'datetime'
    });
});
// 关闭动作
$(document).on('click','#data-detail-close', function () {
    parent.layer.closeAll();
});
function tableReload(child) {
@@ -367,53 +232,7 @@
        where: searchData,
        page: {
            curr: pageCurr
        },
        done: function (res, curr, count) {
            if (res.code === 403) {
                top.location.href = "/";
            }
            pageCurr=curr;
            if (res.data.length === 0 && count !== 0) {
                tableIns.reload({
                    where: searchData,
                    page: {
                        curr: pageCurr-1
                    }
                });
                pageCurr -= 1;
            }
            limit(child);
        }
    });
}
function setFormVal(el, data) {
    for (var val in data) {
        el.find(":input[id='" + val + "']").val(data[val]);
    }
}
function clearFormVal(el) {
    $(':input', el)
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
}
function detailScreen(index) {
    var detail = layer.getChildFrame('#data-detail', index);
    var height = detail.height()+60;
    if (height > ($(window).height()*0.9)) {
        height = ($(window).height()*0.9);
    }
    layer.style(index, {
        height: height +'px'
    });
}
$('body').keydown(function () {
    if (event.keyCode === 13) {
        $("#search").click();
    }
});