(function () {
|
function authHeaders() {
|
return {
|
token: localStorage.getItem('token') || ''
|
};
|
}
|
|
function isForbidden(res) {
|
return res && Number(res.code) === 403;
|
}
|
|
function isOk(res) {
|
return res && Number(res.code) === 200;
|
}
|
|
function normalizeId(value) {
|
if (value === null || value === undefined || value === '') {
|
return null;
|
}
|
var numberValue = Number(value);
|
return isNaN(numberValue) ? value : numberValue;
|
}
|
|
function createLoginForm() {
|
return {
|
id: null,
|
userId: null,
|
token: '',
|
createTime: ''
|
};
|
}
|
|
function escapeHtml(value) {
|
return String(value == null ? '' : value)
|
.replace(/&/g, '&')
|
.replace(/</g, '<')
|
.replace(/>/g, '>')
|
.replace(/"/g, '"')
|
.replace(/'/g, ''');
|
}
|
|
function downloadExcel(filename, headers, rows) {
|
var headHtml = headers.map(function (title) {
|
return '<th style="mso-number-format:\\@;">' + escapeHtml(title) + '</th>';
|
}).join('');
|
var bodyHtml = rows.map(function (row) {
|
return '<tr>' + row.map(function (cell) {
|
return '<td style="mso-number-format:\\@;">' + escapeHtml(cell) + '</td>';
|
}).join('') + '</tr>';
|
}).join('');
|
var workbook = [
|
'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel">',
|
'<head><meta charset="UTF-8"></head>',
|
'<body><table border="1"><tr>',
|
headHtml,
|
'</tr>',
|
bodyHtml,
|
'</table></body></html>'
|
].join('');
|
var blob = new Blob([workbook], {type: 'application/vnd.ms-excel;charset=utf-8;'});
|
var link = document.createElement('a');
|
link.href = URL.createObjectURL(blob);
|
link.download = filename;
|
document.body.appendChild(link);
|
link.click();
|
document.body.removeChild(link);
|
URL.revokeObjectURL(link.href);
|
}
|
|
new Vue({
|
el: '#app',
|
data: function () {
|
return {
|
loading: false,
|
exportLoading: false,
|
userSearchLoading: false,
|
dialogUserLoading: false,
|
tableHeight: Math.max(window.innerHeight - 220, 360),
|
tableData: [],
|
selection: [],
|
pagination: {
|
curr: 1,
|
limit: 16,
|
total: 0
|
},
|
searchForm: {
|
userId: null
|
},
|
userSearchOptions: [],
|
loginDialog: {
|
visible: false,
|
mode: 'create',
|
readonly: false,
|
submitting: false
|
},
|
loginForm: createLoginForm(),
|
loginRules: {
|
userId: [
|
{required: true, message: '请选择员工', trigger: 'change'}
|
],
|
token: [
|
{required: true, message: '请输入凭证值', trigger: 'blur'}
|
],
|
createTime: [
|
{required: true, message: '请选择添加时间', trigger: 'change'}
|
]
|
},
|
dialogUserOptions: [],
|
userDetail: {
|
visible: false,
|
data: {}
|
}
|
};
|
},
|
created: function () {
|
this.loadList();
|
},
|
mounted: function () {
|
window.addEventListener('resize', this.handleResize);
|
},
|
beforeDestroy: function () {
|
window.removeEventListener('resize', this.handleResize);
|
},
|
methods: {
|
handleResize: function () {
|
this.tableHeight = Math.max(window.innerHeight - 220, 360);
|
this.refreshTableLayout();
|
},
|
handleForbidden: function (res) {
|
if (isForbidden(res)) {
|
top.location.href = baseUrl + '/';
|
return true;
|
}
|
return false;
|
},
|
refreshTableLayout: function () {
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.dataTable && vm.$refs.dataTable.doLayout) {
|
vm.$refs.dataTable.doLayout();
|
}
|
});
|
},
|
currentQuery: function () {
|
return {
|
curr: this.pagination.curr,
|
limit: this.pagination.limit,
|
userId: this.searchForm.userId
|
};
|
},
|
loadList: function () {
|
var vm = this;
|
vm.loading = true;
|
$.ajax({
|
url: baseUrl + '/userLogin/list/auth',
|
method: 'GET',
|
headers: authHeaders(),
|
data: vm.currentQuery(),
|
success: function (res) {
|
vm.loading = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '凭证加载失败');
|
return;
|
}
|
var data = res.data || {};
|
vm.tableData = data.records || [];
|
vm.pagination.total = data.total || 0;
|
vm.selection = [];
|
vm.refreshTableLayout();
|
},
|
error: function () {
|
vm.loading = false;
|
vm.$message.error('凭证加载失败');
|
}
|
});
|
},
|
fetchUserOptions: function (keyword, target) {
|
var vm = this;
|
var loadingKey = target === 'search' ? 'userSearchLoading' : 'dialogUserLoading';
|
vm[loadingKey] = true;
|
$.ajax({
|
url: baseUrl + '/userQuery/auth',
|
method: 'GET',
|
headers: authHeaders(),
|
data: {
|
condition: keyword || ''
|
},
|
success: function (res) {
|
vm[loadingKey] = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '用户查询失败');
|
return;
|
}
|
if (target === 'search') {
|
vm.userSearchOptions = res.data || [];
|
} else {
|
vm.dialogUserOptions = res.data || [];
|
}
|
},
|
error: function () {
|
vm[loadingKey] = false;
|
vm.$message.error('用户查询失败');
|
}
|
});
|
},
|
searchUserOptions: function (keyword) {
|
this.fetchUserOptions(keyword, 'search');
|
},
|
searchDialogUserOptions: function (keyword) {
|
this.fetchUserOptions(keyword, 'dialog');
|
},
|
ensureDialogUserOption: function (id, label) {
|
if (!id || !label) {
|
return;
|
}
|
var exists = this.dialogUserOptions.some(function (item) {
|
return normalizeId(item.id) === normalizeId(id);
|
});
|
if (!exists) {
|
this.dialogUserOptions = [{
|
id: normalizeId(id),
|
value: label
|
}].concat(this.dialogUserOptions);
|
}
|
},
|
handleSelectionChange: function (rows) {
|
this.selection = rows || [];
|
},
|
handleSearch: function () {
|
this.pagination.curr = 1;
|
this.loadList();
|
},
|
handleReset: function () {
|
this.searchForm.userId = null;
|
this.userSearchOptions = [];
|
this.pagination.curr = 1;
|
this.loadList();
|
},
|
handleCurrentChange: function (page) {
|
this.pagination.curr = page;
|
this.loadList();
|
},
|
handleSizeChange: function (size) {
|
this.pagination.limit = size;
|
this.pagination.curr = 1;
|
this.loadList();
|
},
|
resetLoginDialog: function () {
|
this.loginForm = createLoginForm();
|
this.dialogUserOptions = [];
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.loginForm) {
|
vm.$refs.loginForm.clearValidate();
|
}
|
});
|
},
|
openCreateDialog: function () {
|
this.loginDialog.mode = 'create';
|
this.loginDialog.readonly = false;
|
this.loginDialog.visible = true;
|
this.resetLoginDialog();
|
},
|
openEditDialog: function (row) {
|
this.loginDialog.mode = 'edit';
|
this.loginDialog.readonly = false;
|
this.loginDialog.visible = true;
|
this.loginForm = {
|
id: normalizeId(row.id),
|
userId: normalizeId(row.userId),
|
token: row.token || '',
|
createTime: row['createTime$'] || ''
|
};
|
this.dialogUserOptions = [];
|
this.ensureDialogUserOption(row.userId, row.userUsername);
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.loginForm) {
|
vm.$refs.loginForm.clearValidate();
|
}
|
});
|
},
|
openDetailDialog: function (row) {
|
this.loginDialog.mode = 'edit';
|
this.loginDialog.readonly = true;
|
this.loginDialog.visible = true;
|
this.loginForm = {
|
id: normalizeId(row.id),
|
userId: normalizeId(row.userId),
|
token: row.token || '',
|
createTime: row['createTime$'] || ''
|
};
|
this.dialogUserOptions = [];
|
this.ensureDialogUserOption(row.userId, row.userUsername);
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.loginForm) {
|
vm.$refs.loginForm.clearValidate();
|
}
|
});
|
},
|
submitLogin: function () {
|
var vm = this;
|
if (!vm.$refs.loginForm) {
|
return;
|
}
|
vm.$refs.loginForm.validate(function (valid) {
|
if (!valid) {
|
return false;
|
}
|
vm.loginDialog.submitting = true;
|
$.ajax({
|
url: baseUrl + '/userLogin/' + (vm.loginDialog.mode === 'create' ? 'add' : 'update') + '/auth',
|
method: 'POST',
|
headers: authHeaders(),
|
data: {
|
id: vm.loginDialog.mode === 'edit' ? normalizeId(vm.loginForm.id) : null,
|
userId: normalizeId(vm.loginForm.userId),
|
token: $.trim(vm.loginForm.token),
|
createTime: vm.loginForm.createTime
|
},
|
success: function (res) {
|
vm.loginDialog.submitting = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '保存失败');
|
return;
|
}
|
vm.$message.success(res.msg || '保存成功');
|
vm.loginDialog.visible = false;
|
vm.loadList();
|
},
|
error: function () {
|
vm.loginDialog.submitting = false;
|
vm.$message.error('保存失败');
|
}
|
});
|
return true;
|
});
|
},
|
removeSelection: function () {
|
var vm = this;
|
if (!vm.selection.length) {
|
vm.$message.warning('请选择数据');
|
return;
|
}
|
var ids = vm.selection.map(function (row) {
|
return normalizeId(row.id);
|
}).filter(function (id) {
|
return id !== null;
|
});
|
vm.$confirm('确定删除选中凭证吗?', '提示', {
|
confirmButtonText: '确定',
|
cancelButtonText: '取消',
|
type: 'warning'
|
}).then(function () {
|
$.ajax({
|
url: baseUrl + '/userLogin/delete/auth',
|
method: 'POST',
|
headers: authHeaders(),
|
traditional: true,
|
data: {
|
ids: ids
|
},
|
success: function (res) {
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '删除失败');
|
return;
|
}
|
vm.$message.success(res.msg || '删除成功');
|
if (vm.tableData.length === ids.length && vm.pagination.curr > 1) {
|
vm.pagination.curr -= 1;
|
}
|
vm.loadList();
|
},
|
error: function () {
|
vm.$message.error('删除失败');
|
}
|
});
|
}).catch(function () {
|
});
|
},
|
exportRows: function () {
|
var vm = this;
|
vm.exportLoading = true;
|
$.ajax({
|
url: baseUrl + '/userLogin/export/auth',
|
method: 'POST',
|
headers: authHeaders(),
|
dataType: 'json',
|
contentType: 'application/json;charset=UTF-8',
|
data: JSON.stringify({
|
userLogin: {
|
userId: vm.searchForm.userId
|
},
|
fields: ['id', 'userUsername', 'token', 'createTime$']
|
}),
|
success: function (res) {
|
vm.exportLoading = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '导出失败');
|
return;
|
}
|
downloadExcel('凭证记录.xls', ['ID', '员工', '凭证值', '添加时间'], res.data || []);
|
},
|
error: function () {
|
vm.exportLoading = false;
|
vm.$message.error('导出失败');
|
}
|
});
|
},
|
openUserDetail: function (row) {
|
var vm = this;
|
if (!row.userId) {
|
vm.$message.warning('无用户数据');
|
return;
|
}
|
$.ajax({
|
url: baseUrl + '/user/' + row.userId + '/auth',
|
method: 'GET',
|
headers: authHeaders(),
|
success: function (res) {
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '员工详情加载失败');
|
return;
|
}
|
vm.userDetail.data = res.data || {};
|
vm.userDetail.visible = true;
|
},
|
error: function () {
|
vm.$message.error('员工详情加载失败');
|
}
|
});
|
}
|
},
|
watch: {
|
'loginDialog.visible': function (visible) {
|
if (!visible) {
|
this.loginDialog.submitting = false;
|
this.loginDialog.readonly = false;
|
this.resetLoginDialog();
|
}
|
},
|
'userDetail.visible': function (visible) {
|
if (!visible) {
|
this.userDetail.data = {};
|
}
|
}
|
}
|
});
|
})();
|