| | |
| | | var pageCurr; |
| | | 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; |
| | | |
| | | insTb2 = table.render({ |
| | | elem: '#userTable', |
| | | headers: {token: localStorage.getItem('token')}, |
| | | url: baseUrl+'/user/list/auth', |
| | | page: true, |
| | | limit: 15, |
| | | limits: [15, 30, 50, 100, 200, 500], |
| | | toolbar: '#userToolbar', |
| | | height: 'full-100', |
| | | cols: [[ |
| | | {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: '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:170} |
| | | ]], |
| | | request: { |
| | | pageName: 'curr', |
| | | pageSize: 'limit' |
| | | }, |
| | | parseData: function (res) { |
| | | return { |
| | | 'code': res.code, |
| | | 'msg': res.msg, |
| | | 'count': res.data.total, |
| | | 'data': res.data.records |
| | | } |
| | | }, |
| | | response: { |
| | | statusCode: 200 |
| | | }, |
| | | done: function(res, curr, count) { |
| | | if (res.code === 403) { |
| | | top.location.href = baseUrl+"/"; |
| | | } |
| | | tableData = table.cache.userTable; |
| | | pageCurr=curr; |
| | | limit(); |
| | | (function () { |
| | | function handleForbidden(res) { |
| | | if (res && Number(res.code) === 403) { |
| | | top.location.href = baseUrl + "/"; |
| | | return true; |
| | | } |
| | | }); |
| | | /* 表格2搜索 */ |
| | | form.on('submit(userTbSearch)', function (data) { |
| | | insTb2.reload({where: data.field, page: {curr: 1}}); |
| | | return false; |
| | | }); |
| | | } |
| | | |
| | | /* 表格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; |
| | | })); |
| | | } |
| | | }); |
| | | |
| | | // 修改状态 |
| | | 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 'edit': |
| | | 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){ |
| | | layer.iframeAuto(index); |
| | | $('#resetUserId').val(data.id); |
| | | new Vue({ |
| | | el: "#app", |
| | | data: function () { |
| | | return { |
| | | loading: false, |
| | | dialogSubmitting: false, |
| | | resetSubmitting: false, |
| | | searchForm: { |
| | | username: "", |
| | | mobile: "" |
| | | }, |
| | | tableData: [], |
| | | selection: [], |
| | | roles: [], |
| | | page: { |
| | | curr: 1, |
| | | limit: 15, |
| | | total: 0 |
| | | }, |
| | | dialog: { |
| | | visible: false, |
| | | mode: "create" |
| | | }, |
| | | dialogForm: { |
| | | id: null, |
| | | username: "", |
| | | mobile: "", |
| | | password: "", |
| | | roleId: null, |
| | | status: 1, |
| | | mfaAllow: 0 |
| | | }, |
| | | resetDialog: { |
| | | visible: false, |
| | | userId: null, |
| | | username: "", |
| | | password: "" |
| | | }, |
| | | dialogRules: { |
| | | username: [ |
| | | { required: true, message: "请输入登录账户", trigger: "blur" } |
| | | ], |
| | | mobile: [ |
| | | { required: true, message: "请输入账号", trigger: "blur" } |
| | | ], |
| | | roleId: [ |
| | | { required: true, message: "请选择角色", trigger: "change" } |
| | | ], |
| | | password: [ |
| | | { |
| | | validator: function (rule, value, callback) { |
| | | if (this.dialog.mode !== "create") { |
| | | callback(); |
| | | return; |
| | | } |
| | | if (!value) { |
| | | callback(new Error("请输入初始密码")); |
| | | return; |
| | | } |
| | | if (String(value).length < 4) { |
| | | callback(new Error("初始密码不能少于4位")); |
| | | return; |
| | | } |
| | | callback(); |
| | | }.bind(this), |
| | | trigger: "blur" |
| | | } |
| | | ] |
| | | }, |
| | | resetRules: { |
| | | password: [ |
| | | { required: true, message: "请输入新密码", trigger: "blur" }, |
| | | { |
| | | validator: function (rule, value, callback) { |
| | | if (String(value || "").length < 4) { |
| | | callback(new Error("新密码不能少于4位")); |
| | | return; |
| | | } |
| | | callback(); |
| | | }, |
| | | trigger: "blur" |
| | | } |
| | | ] |
| | | } |
| | | }; |
| | | }, |
| | | created: function () { |
| | | this.loadRoles(); |
| | | this.loadTable(); |
| | | }, |
| | | methods: { |
| | | authHeaders: function () { |
| | | return { token: localStorage.getItem("token") }; |
| | | }, |
| | | loadRoles: function () { |
| | | var vm = this; |
| | | $.ajax({ |
| | | url: baseUrl + "/role/list/auth", |
| | | method: "GET", |
| | | headers: vm.authHeaders(), |
| | | data: { |
| | | curr: 1, |
| | | limit: 500 |
| | | }, |
| | | success: function (res) { |
| | | if (handleForbidden(res)) { |
| | | return; |
| | | } |
| | | if (Number(res.code) !== 200) { |
| | | vm.$message.error(res.msg || "角色加载失败"); |
| | | return; |
| | | } |
| | | var payload = res.data || {}; |
| | | vm.roles = Array.isArray(payload.records) ? payload.records : []; |
| | | }, |
| | | error: function () { |
| | | vm.$message.error("角色加载失败"); |
| | | } |
| | | }); |
| | | break; |
| | | } |
| | | }); |
| | | |
| | | /* 显示表单弹窗 */ |
| | | 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}); |
| | | }, |
| | | loadTable: function () { |
| | | var vm = this; |
| | | vm.loading = true; |
| | | $.ajax({ |
| | | url: baseUrl + "/user/list/auth", |
| | | method: "GET", |
| | | headers: vm.authHeaders(), |
| | | data: { |
| | | curr: vm.page.curr, |
| | | limit: vm.page.limit, |
| | | username: vm.searchForm.username, |
| | | mobile: vm.searchForm.mobile |
| | | }, |
| | | success: function (res) { |
| | | if (handleForbidden(res)) { |
| | | return; |
| | | } |
| | | if (Number(res.code) !== 200) { |
| | | vm.$message.error(res.msg || "加载失败"); |
| | | return; |
| | | } |
| | | var payload = res.data || {}; |
| | | vm.tableData = Array.isArray(payload.records) ? payload.records : []; |
| | | vm.page.total = Number(payload.total || 0); |
| | | }, |
| | | error: function () { |
| | | vm.$message.error("加载失败"); |
| | | }, |
| | | complete: function () { |
| | | vm.loading = false; |
| | | } |
| | | }); |
| | | }, |
| | | handleSearch: function () { |
| | | this.page.curr = 1; |
| | | this.loadTable(); |
| | | }, |
| | | handleResetSearch: function () { |
| | | this.searchForm = { |
| | | username: "", |
| | | mobile: "" |
| | | }; |
| | | this.page.curr = 1; |
| | | this.loadTable(); |
| | | }, |
| | | handleSelectionChange: function (rows) { |
| | | this.selection = rows || []; |
| | | }, |
| | | handleCurrentChange: function (curr) { |
| | | this.page.curr = curr; |
| | | this.loadTable(); |
| | | }, |
| | | handleSizeChange: function (limit) { |
| | | this.page.limit = limit; |
| | | this.page.curr = 1; |
| | | this.loadTable(); |
| | | }, |
| | | openCreateDialog: function () { |
| | | this.dialog.mode = "create"; |
| | | this.dialog.visible = true; |
| | | this.dialogForm = { |
| | | id: null, |
| | | username: "", |
| | | mobile: "", |
| | | password: "", |
| | | roleId: null, |
| | | status: 1, |
| | | mfaAllow: 0 |
| | | }; |
| | | this.clearDialogValidate(); |
| | | }, |
| | | openEditDialog: function (row) { |
| | | this.dialog.mode = "edit"; |
| | | this.dialog.visible = true; |
| | | this.dialogForm = { |
| | | id: row.id, |
| | | username: row.username || "", |
| | | mobile: row.mobile || "", |
| | | password: "", |
| | | roleId: row.roleId == null ? null : Number(row.roleId), |
| | | status: row.status == null ? 1 : Number(row.status), |
| | | mfaAllow: row.mfaAllow == null ? 0 : Number(row.mfaAllow) |
| | | }; |
| | | this.clearDialogValidate(); |
| | | }, |
| | | clearDialogValidate: function () { |
| | | var vm = this; |
| | | vm.$nextTick(function () { |
| | | if (vm.$refs.dialogForm) { |
| | | vm.$refs.dialogForm.clearValidate(); |
| | | } |
| | | }); |
| | | }, |
| | | closeDialog: function () { |
| | | this.dialog.visible = false; |
| | | }, |
| | | submitDialog: function () { |
| | | var vm = this; |
| | | if (!vm.$refs.dialogForm) { |
| | | return; |
| | | } |
| | | vm.$refs.dialogForm.validate(function (valid) { |
| | | if (!valid) { |
| | | return false; |
| | | } |
| | | var loadIndex = layer.load(2); |
| | | vm.dialogSubmitting = true; |
| | | $.ajax({ |
| | | url: baseUrl+"/user/"+(mData?'update':'add')+"/auth", |
| | | headers: {'token': localStorage.getItem('token')}, |
| | | data: data.field, |
| | | method: 'POST', |
| | | url: baseUrl + "/user/" + (vm.dialog.mode === "create" ? "add" : "update") + "/auth", |
| | | method: "POST", |
| | | headers: vm.authHeaders(), |
| | | data: vm.buildDialogPayload(), |
| | | 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}); |
| | | if (handleForbidden(res)) { |
| | | return; |
| | | } |
| | | if (Number(res.code) !== 200) { |
| | | vm.$message.error(res.msg || "保存失败"); |
| | | return; |
| | | } |
| | | vm.$message.success(res.msg || "保存成功"); |
| | | vm.dialog.visible = false; |
| | | vm.loadTable(); |
| | | }, |
| | | error: function () { |
| | | vm.$message.error("保存失败"); |
| | | }, |
| | | complete: function () { |
| | | vm.dialogSubmitting = false; |
| | | } |
| | | }) |
| | | return false; |
| | | }); |
| | | return true; |
| | | }); |
| | | // 弹窗不出现滚动条 |
| | | $(layero).children('.layui-layer-content').css('overflow', 'visible'); |
| | | layui.form.render('select'); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /* 删除订单 */ |
| | | 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/update/auth", |
| | | headers: {'token': localStorage.getItem('token')}, |
| | | data: { |
| | | id: data.field.resetUserId, |
| | | password: hex_md5(data.field.resetPassword) |
| | | }, |
| | | method: 'POST', |
| | | success: function (res) { |
| | | if (res.code === 200){ |
| | | layer.closeAll(); |
| | | layer.msg("重置密码成功", {icon: 1}); |
| | | } else if (res.code === 403){ |
| | | top.location.href = baseUrl+"/"; |
| | | }else { |
| | | layer.msg(res.msg, {icon: 2}) |
| | | buildDialogPayload: function () { |
| | | var payload = { |
| | | username: this.dialogForm.username, |
| | | mobile: this.dialogForm.mobile, |
| | | roleId: this.dialogForm.roleId, |
| | | status: this.dialogForm.status, |
| | | mfaAllow: this.dialogForm.mfaAllow |
| | | }; |
| | | if (this.dialog.mode === "edit") { |
| | | payload.id = this.dialogForm.id; |
| | | } else { |
| | | payload.password = hex_md5(this.dialogForm.password); |
| | | } |
| | | return payload; |
| | | }, |
| | | openResetPasswordDialog: function (row) { |
| | | this.resetDialog = { |
| | | visible: true, |
| | | userId: row.id, |
| | | username: row.username || "", |
| | | password: "" |
| | | }; |
| | | var vm = this; |
| | | vm.$nextTick(function () { |
| | | if (vm.$refs.resetForm) { |
| | | vm.$refs.resetForm.clearValidate(); |
| | | } |
| | | }); |
| | | }, |
| | | closeResetDialog: function () { |
| | | this.resetDialog.visible = false; |
| | | }, |
| | | submitResetPassword: function () { |
| | | var vm = this; |
| | | if (!vm.$refs.resetForm) { |
| | | return; |
| | | } |
| | | vm.$refs.resetForm.validate(function (valid) { |
| | | if (!valid) { |
| | | return false; |
| | | } |
| | | vm.resetSubmitting = true; |
| | | $.ajax({ |
| | | url: baseUrl + "/user/resetPassword/auth", |
| | | method: "POST", |
| | | headers: vm.authHeaders(), |
| | | data: { |
| | | id: vm.resetDialog.userId, |
| | | password: hex_md5(vm.resetDialog.password) |
| | | }, |
| | | success: function (res) { |
| | | if (handleForbidden(res)) { |
| | | return; |
| | | } |
| | | if (Number(res.code) !== 200) { |
| | | vm.$message.error(res.msg || "重置密码失败"); |
| | | return; |
| | | } |
| | | vm.$message.success("重置密码成功"); |
| | | vm.resetDialog.visible = false; |
| | | }, |
| | | error: function () { |
| | | vm.$message.error("重置密码失败"); |
| | | }, |
| | | complete: function () { |
| | | vm.resetSubmitting = false; |
| | | } |
| | | }); |
| | | return true; |
| | | }); |
| | | }, |
| | | toggleStatus: function (row) { |
| | | var vm = this; |
| | | var currentStatus = Number(row.status) === 1 ? 1 : 0; |
| | | $.ajax({ |
| | | url: baseUrl + "/user/edit/auth", |
| | | method: "POST", |
| | | headers: vm.authHeaders(), |
| | | data: { |
| | | id: row.id, |
| | | status: currentStatus |
| | | }, |
| | | success: function (res) { |
| | | if (handleForbidden(res)) { |
| | | return; |
| | | } |
| | | if (Number(res.code) !== 200) { |
| | | row.status = currentStatus === 1 ? 0 : 1; |
| | | vm.$message.error(res.msg || "状态更新失败"); |
| | | return; |
| | | } |
| | | row.status$ = currentStatus === 1 ? "正常" : "禁用"; |
| | | vm.$message.success("状态已更新"); |
| | | }, |
| | | error: function () { |
| | | row.status = currentStatus === 1 ? 0 : 1; |
| | | vm.$message.error("状态更新失败"); |
| | | } |
| | | }); |
| | | }, |
| | | removeSelection: function () { |
| | | var ids = this.selection.map(function (item) { |
| | | return item.id; |
| | | }); |
| | | this.removeRows(ids); |
| | | }, |
| | | removeRows: function (ids) { |
| | | var vm = this; |
| | | if (!ids || ids.length === 0) { |
| | | vm.$message.warning("请选择要删除的数据"); |
| | | return; |
| | | } |
| | | vm.$confirm("确定删除选中的记录吗?", "提示", { |
| | | type: "warning", |
| | | confirmButtonText: "确定", |
| | | cancelButtonText: "取消" |
| | | }).then(function () { |
| | | $.ajax({ |
| | | url: baseUrl + "/user/delete/auth", |
| | | method: "POST", |
| | | headers: vm.authHeaders(), |
| | | traditional: true, |
| | | data: { "ids[]": ids }, |
| | | success: function (res) { |
| | | if (handleForbidden(res)) { |
| | | return; |
| | | } |
| | | if (Number(res.code) !== 200) { |
| | | vm.$message.error(res.msg || "删除失败"); |
| | | return; |
| | | } |
| | | vm.$message.success("删除成功"); |
| | | vm.loadTable(); |
| | | }, |
| | | error: function () { |
| | | vm.$message.error("删除失败"); |
| | | } |
| | | }); |
| | | }).catch(function () { |
| | | }); |
| | | } |
| | | }) |
| | | return false; |
| | | }) |
| | | |
| | | }); |
| | | |
| | | function tableReload(child) { |
| | | var searchData = {}; |
| | | $.each($('#search-box [name]').serializeArray(), function() { |
| | | searchData[this.name] = this.value; |
| | | }); |
| | | (child ? parent.tableIns : tableIns).reload({ |
| | | where: searchData, |
| | | page: { |
| | | curr: pageCurr |
| | | } |
| | | }); |
| | | } |
| | | |
| | | })(); |