cwhelp/admin/src/main/resources/static/js/main.js

546 lines
21 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

layui.use(['element', 'form', 'layer', 'upload'], function () {
var $ = layui.jquery;
var element = layui.element; //加载element模块
var form = layui.form; //加载form模块
var layer = layui.layer; //加载layer模块
var upload = layui.upload; //加载upload模块
/* 侧边栏开关 */
$(".side-toggle").on("click", function (e) {
e.preventDefault();
var to = $(".layui-layout-admin");
to.toggleClass("layui-side-shrink");
to.attr("toggle") === 'on' ? to.attr("toggle", "off") : to.attr("toggle", "on");
});
$(".layui-side").on("click", function () {
var to = $(".layui-layout-admin");
if (to.attr("toggle") === 'on') {
to.attr("toggle", "off");
to.removeClass("layui-side-shrink");
}
});
/* 最大化窗口 */
$(".timo-screen-full").on("click", function (e) {
e.preventDefault();
if (!$(this).hasClass("full-on")) {
var docElm = document.documentElement;
var full = docElm.requestFullScreen || docElm.webkitRequestFullScreen ||
docElm.mozRequestFullScreen || docElm.msRequestFullscreen;
"undefined" !== typeof full && full && full.call(docElm);
} else {
document.exitFullscreen ? document.exitFullscreen()
: document.mozCancelFullScreen ? document.mozCancelFullScreen()
: document.webkitCancelFullScreen ? document.webkitCancelFullScreen()
: document.msExitFullscreen && document.msExitFullscreen()
}
$(this).toggleClass("full-on");
});
/* 新建或切换标签栏 */
var tabs = function (url) {
var item = $('[lay-url="' + url + '"]');
if (url !== undefined && url !== '#' && item.length > 0) {
var bootLay = $('[lay-id="' + url + '"]');
if (bootLay.length === 0) {
var title = item.attr("lay-icon") === 'true' ? item.html()
: item.children(".layui-nav-title").text();
var iframeUrl = (window.location.pathname + url).replace('//','/');
element.tabAdd('iframe-tabs', {
title: title
, content: '<iframe src="' + iframeUrl + '" frameborder="0" class="layui-layout-iframe"></iframe>'
, id: url
});
}
element.tabChange('iframe-tabs', url);
}
};
/* 监听导航栏事件,实现标签页的切换 */
element.on("nav(layui-nav-side)", function ($this) {
var url = $this.attr('lay-url');
tabs(url);
});
/* 监听标签栏事件,实现导航栏高亮显示 */
element.on("tab(iframe-tabs)", function () {
var layId = $(this).attr("lay-id");
$(".layui-side .layui-this").removeClass("layui-this");
$('[lay-url="' + layId + '"]').parent().addClass("layui-this");
// 改变地址hash值
location.hash = this.getAttribute('lay-id');
});
/* 监听hash来切换选项卡*/
window.onhashchange = function (e) {
var url = location.hash.replace(/^#/, '');
var index = $(".layui-layout-admin .layui-side .layui-nav-item")[0];
$(index).children("a").attr("lay-icon", "true");
if (url === "" || url === undefined) {
url = $(index).children("[lay-url]").attr("lay-url");
}
tabs(url);
};
window.onhashchange();
/* 初始化时展开子菜单 */
$("dd.layui-this").parents(".layui-nav-child").parent()
.addClass("layui-nav-itemed");
/* 刷新iframe页面 */
$(".refresh-btn").click(function () {
location.reload();
});
/* AJAX请求默认选项处理连接超时问题 */
$.ajaxSetup({
complete: function (xhr, status) {
if (xhr.status == 401) {
layer.confirm('session连接超时是否重新登录', {
btn: ['是', '否']
}, function () {
if (window.parent.window != window) {
window.top.location = window.location.pathname + '/login';
}
});
}
}
});
/* 漂浮消息 */
$.fn.Messager = function (result) {
if (result.code === 200) {
layer.msg(result.msg, {offset: '15px', time: 3000, icon: 1});
setTimeout(function () {
if (result.data === 'submit[refresh]') {
parent.location.reload();
return;
}
if (result.data == null) {
window.location.reload();
} else {
window.location.href = result.data
}
}, 2000);
} else {
layer.msg(result.msg, {offset: '15px', time: 3000, icon: 2});
}
};
/* 提交表单数据 */ // update by hjc
$(document).on("click", ".ajax-submit", function (e) {
e.preventDefault();
var form = $(this).parents("form");
var url = form.attr("action");
var serializeArray = form.serializeArray();
var _newData = null;
var from = false; // from = true 代表增值税页面请求用设置content-type的请求
for(var i=0; i<serializeArray.length; i++){
if(serializeArray[i].name == 'salerTaxInfoId'){
from = true;
_newData = trans.serialize( serializeArray );
var goods = createGoodsParams();
_newData.bssGoods = goods;
delete _newData.name;
delete _newData.specification;
delete _newData.unit;
delete _newData.num;
delete _newData.unitPrice;
delete _newData.detailAmount;
delete _newData.taxRate;
delete _newData.goodTaxAmount;
break;
}else {
continue;
}
}
if(from){
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(_newData),
success: function(result) {
if (result.data == null) {
result.data = 'submit[refresh]';
}
$.fn.Messager(result);
}
});
}else {
$.post(url, _newData, function(result) {
if (result.data == null) {
result.data = 'submit[refresh]';
}
$.fn.Messager(result);
});
}
});
/* get方式异步 */
$(document).on("click", ".ajax-get", function (e) {
e.preventDefault();
var msg = $(this).data("msg");
if (msg !== undefined) {
layer.confirm(msg + '', {
title: '提示',
btn: ['确认', '取消']
}, function () {
$.get(e.target.href, function (result) {
$.fn.Messager(result);
});
});
} else {
$.get(e.target.href, function (result) {
$.fn.Messager(result);
});
}
});
// post方式异步-操作状态
$(".ajax-status").on("click", function (e) {
e.preventDefault();
var checked = [];
var tdcheckbox = $(".timo-table td .timo-checkbox :checkbox:checked");
if (tdcheckbox.length > 0) {
tdcheckbox.each(function (key, val) {
checked.push("ids=" + $(val).attr("value"));
});
$.post(e.target.href, checked.join("&"), function (result) {
$.fn.Messager(result);
});
} else {
layer.msg('请选择一条记录');
}
});
/* 添加/修改弹出层 */
$(document).on("click", ".open-popup, .open-popup-param", function () {
var title = $(this).data("title");
var url = $(this).attr("data-url");
if ($(this).hasClass("open-popup-param")) {
var tdcheckbox = $(".timo-table td .timo-checkbox :checkbox:checked");
var param = '';
if (tdcheckbox.length === 0) {
layer.msg('请选择一条记录');
return;
}
if (tdcheckbox.length > 1 && $(this).data("type") === 'radio') {
layer.msg('只允许选中一个');
return;
}
tdcheckbox.each(function (key, val) {
param += "ids=" + $(val).attr("value") + "&";
});
param = param.substr(0, param.length - 1);
url += "?" + param;
}
var size = $(this).attr("data-size");
if (size === undefined || size === "auto") {
size = ['50%', '80%'];
}else if (size === "max") {
size = ['100%', '100%'];
}else if (size.indexOf(',') !== -1) {
var split = size.split(",");
size = [split[0] + 'px', split[1] + 'px'];
}
window.layerIndex = layer.open({
type: 2,
title: title,
shadeClose: true,
maxmin: true,
area: size,
content: [url, 'on']
});
});
/* 关闭弹出层 */
$(".close-popup").click(function (e) {
e.preventDefault();
parent.layer.close(window.parent.layerIndex);
});
/* 下拉按钮组 */
$(".btn-group").click(function (e) {
e.stopPropagation();
$this = $(this);
$this.toggleClass("show");
$(document).one("click", function () {
if ($this.hasClass("show")) {
$this.removeClass("show");
}
});
});
// 展示数据列表-多选框
var thcheckbox = $(".timo-table th .timo-checkbox :checkbox");
thcheckbox.on("change", function () {
var tdcheckbox = $(".timo-table td .timo-checkbox :checkbox");
if (thcheckbox.is(':checked')) {
tdcheckbox.prop("checked", true);
} else {
tdcheckbox.prop("checked", false);
}
});
// 检测列表数据是否为空
var timoTable = $(".timo-table tbody");
if (timoTable.length > 0) {
var children = timoTable.children();
if (children.length === 0) {
var length = $(".timo-table thead th").length;
var trNullInfo = "<tr><td class='timo-table-null' colspan='"
+ length + "'>没有找到匹配的记录</td></tr>";
timoTable.append(trNullInfo);
}
}
// 携带参数跳转
var paramSkip = function () {
var getSearch = "";
// 搜索框参数
$('.timo-search-box [name]').each(function (key, val) {
if ($(val).val() !== "" && $(val).val() !== undefined) {
getSearch += $(val).attr("name") + "=" + $(val).val() + "&";
}
});
// 页数参数
var pageSize = $(".page-number").val();
if (pageSize !== undefined && pageSize !== "") {
getSearch += "size=" + pageSize + "&";
}
// 排序参数
var asc = $(".sortable.asc").data("field");
if(asc !== undefined){
getSearch += "orderByColumn=" + asc + "&isAsc=asc&";
}
var desc = $(".sortable.desc").data("field");
if(desc !== undefined){
getSearch += "orderByColumn=" + desc + "&isAsc=desc&";
}
if (getSearch !== "") {
getSearch = "?" + getSearch.substr(0, getSearch.length - 1);
}
window.location.href = window.location.pathname + getSearch;
};
/* 展示列表数据搜索 */
$(document).on("click", ".timo-search-btn", function () {
paramSkip();
});
/* 改变显示页数 */
$(document).on("change", ".page-number", function () {
paramSkip();
});
/* 触发字段排序 */
$(document).on("click", ".sortable", function () {
$(".sortable").not(this).removeClass("asc").removeClass("desc");
if($(this).hasClass("asc")){
$(this).removeClass("asc").addClass("desc");
}else {
$(this).removeClass("desc").addClass("asc");
}
paramSkip();
});
/* 参数化字段排序 */
var getSearch = function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
};
var field = getSearch("orderByColumn");
var isAsc = getSearch("isAsc");
if(field != null){
$("[data-field='"+ field +"']").addClass(isAsc);
}
/** 上传图片操作 */
upload.render({
elem: '.upload-image' //绑定元素
,url: $('.upload-image').attr('up-url') //上传接口
,field: 'image' //文件域的字段名
,acceptMime: 'image/*' //选择文件类型
,exts: 'jpg|jpeg|png|gif' //支持的图片格式
,multiple: true //开启多文件选择
,choose: function (obj) {
var upload = $('.upload-image');
obj.preview(function (index, file, result) {
var name = upload.attr('name');
var show = upload.parents('.layui-form-item').children('.upload-show');
show.append("<div class='upload-item'><img src='"+ result +"'/>" +
"<input id='"+ index +"' type='hidden' name='"+name+"'/>" +
"<i class='upload-item-close layui-icon layui-icon-close'></i></div>");
});
}
,done: function(res, index, upload){
var field = $('.upload-image').attr('up-field') || 'id';
// 解决节点渲染和异步上传不同步问题
var interval = window.setInterval(function(){
var hide = $("#"+index);
if(hide.length > 0){
var item = hide.parent('.upload-item');
if (res.code === 200) {
alert(res.data[field]);
hide.val(res.data[field]);
item.addClass('succeed');
}else {
hide.remove();
item.addClass('error');
}
clearInterval(interval);
}
}, 100);
}
});
/** 上传图片(增值税发票)操作 */
upload.render({
elem: '#vatInvoiceImg' //绑定元素
,url: $('#vatInvoiceImg').attr('up-url') //上传接口
,field: 'image' //文件域的字段名
,acceptMime: 'image/*' //选择文件类型
,exts: 'jpg|jpeg|png|gif' //支持的图片格式
,multiple: false
,choose: function (obj) {
var upload = $('#vatInvoiceImg');
var show = upload.parents('.layui-form-item').children('.upload-show');
if ($(show).children("div").length == 1) {
alert("只允许上传一张照片!");
return ;
}
obj.preview(function (index, file, result) {
var name = upload.attr('name');
var show = upload.parents('.layui-form-item').children('.upload-show');
show.append("<div class='upload-item'><img src='"+ result +"'/>" +
"<input id='"+ index +"' type='hidden' name='"+name+"'/>" +
"<i class='upload-item-close layui-icon layui-icon-close'></i></div>");
});
}
,done: function(res, index, upload){
// 解决节点渲染和异步上传不同步问题
var interval = window.setInterval(function(){
var hide = $("#"+index);
if(hide.length > 0){
var item = hide.parent('.upload-item');
if (res.code === 200) {
$('#invoiceType').find("option[value='" + res.data['invoiceType'] + "']").attr("selected",true);
$('#invoiceCode').val(res.data['invoiceCode']);
$('#invoiceNo').val(res.data['invoiceNo']);
$('#checkCode').val(res.data['checkCode']);
var invoiceDate = res.data['invoiceDate'];
invoiceDate = invoiceDate.replace('年','-')
invoiceDate = invoiceDate.replace('月','-')
invoiceDate = invoiceDate.replace('日','')
$('#laydate').val(invoiceDate);
$('#remark').val(res.data['remark']);
$('#invoiceMoney').val(res.data['invoiceMoney']);
$('#taxAmount').val(res.data['taxAmount']);
$('#totalAmount').val(res.data['totalAmount']);
$('#totalAmountCn').val(res.data['totalAmountCn']);
$('#salerTaxInfoId').val(res.data['salerTaxInfoId']);
$('#buyTaxInfoId').val(res.data['buyTaxInfoId']);
form.render("select");
hide.val(res.data['invoiceImg']);
item.addClass('succeed');
/* 实体模型操作 */
var entity = $("#entity");
var goods = res.data['bssGoods'];
var element = null;
for(var i=0; i<goods.length; i++){
element = entity.children("tr:last-child").clone();
element.find("input[name='name']").val(goods[i].name);
element.find("input[name='specification']").val(goods[i].specification);
element.find("input[name='unit']").val(goods[i].unit);
element.find("input[name='num']").val(goods[i].num);
element.find("input[name='unitPrice']").val(goods[i].unitPrice);
element.find("input[name='detailAmount']").val(goods[i].detailAmount);
element.find("input[name='taxRate']").val(goods[i].taxRate);
element.find("input[name='goodTaxAmount']").val(goods[i].goodTaxAmount);
entity.append(element);
element.children(".entity-number").click();
$.each(entity.children(), function (key, val) {
$(val).children(".entity-number").text(key + 1);
});
}
form.render();
}else {
hide.remove();
item.addClass('error');
}
clearInterval(interval);
}
}, 100);
}
});
// 删除上传图片展示项
$(document).on("click", ".upload-item-close", function () {
$(this).parent('.upload-item').remove();
});
/* 解决时间控件弹不出 */
layui.use('laydate', function(){
var laydate = layui.laydate;
//同时绑定多个
lay('.test-item').each(function(){
laydate.render({
elem: this
,format:'yyyy-MM-dd'
,type:'date'
,trigger: 'click'
});
});
});
function createGoodsParams() {
var entity = $("#entity");
var trs = entity.children("tr");
var goods = new Array();
$.each(trs, function(index, element){
var name = element.children[1].children[0].value;
if(name == null || name == '' || name == undefined){
return true;
}
var good = {};
good.name = name;
good.specification = element.children[2].children[0].value;
good.unit = element.children[3].children[0].value;
good.num = element.children[4].children[0].value;
good.unitPrice = element.children[5].children[0].value;
good.detailAmount = element.children[6].children[0].value;
good.taxRate = element.children[7].children[0].value;
good.goodTaxAmount = element.children[8].children[0].value;
goods.push(good);
});
return goods;
}
var trans={
serialize:function(obj){
var o ={};
$.each(obj,function(){
if(o[this.name]){
if(!o[this.name].push){
o[this.name]=[o[this.name]];
}
o[this.name].push(this.value||"");
}else {
o[this.name] = this.value || "";
}
});
return o;
}
}
});