You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/SoftMng/Content/common-js/common.js

248 lines
9.3 KiB
JavaScript

2 years ago
// Date对象 - 自定义格式化
// ReSharper disable once NativeTypePrototypeExtending
Date.prototype.format = function (format) {
if (this.getFullYear() === 0) return undefined;
var date = {
"m+": this.getMonth() + 1,//月份
"d+": this.getDate(),//日
"h+": this.getHours(),//时(24)
"i+": this.getMinutes(),//分
"s+": this.getSeconds(),//秒
"q+": Math.floor((this.getMonth() + 3) / 3),//季度;第一季度1月、2月、三月...
"S+": this.getMilliseconds()//毫秒
};
if (/(y+)/i.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in date) {
if (date.hasOwnProperty(k))
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length === 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
return format;
};
// 通用Json处理对象
var CommonJson = {
//生成随机GUID函数
NewGuid: function ()
{
var guid = "";
for (var i = 1; i <= 32; i++)
{
var n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if ((i === 8) || (i === 12) || (i === 16) || (i === 20)) guid += "-";
}
return guid;
},
//Json 日期转 Date对象
GetDate: function (jsonDate)
{
return new Date(jsonDate == null ? '0000-01-01' : (jsonDate.indexOf("/Date") < 0 ? jsonDate:parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)));
},
//获取下拉框对应的值
GetOptionValue: function (value, options)
{
if (value != null)
{
for (var i = 0; i < options.length; i++)
for (var p in options[i]) if (options[i].hasOwnProperty(p)) if (p === value.toString())
return options[i][p];
}
return undefined;
},
//获取select2对应的值
GetSelect2Value: function (value)
{
try
{
return eval('(' + value + ')').Value;
} catch (e)
{
return undefined;
}
},
//获取列表文件值
GetFileValue: function (value)
{
try
{
var fileArr = eval(value);
var regex = /\w+\.\w{3,4}$/gi;
var str = "";
for (var i = 0; i < fileArr.length; i++)
{
if (i !== 0) str = str.concat("<br/>");
str = str.concat("<a href='" +
fileArr[i].url +
"' target='_blank'>" +
fileArr[i].url.match(regex)[0] +
"</a>");
}
return str;
} catch (e)
{
return undefined;
}
},
//Json 对象给对应字段赋值
SetModel: function ($form, model)
{
if (model != undefined)
{
for (var obj in model)
{
if (model.hasOwnProperty(obj) && obj !== "0")
{
var $this = $form.find("[name='" + obj + "']");//当前 Jquery 对象
if ($this.length > 0)
{
var value = model[obj];
if ($this.hasClass("date4")) //日期控件赋值
{
var dateFormat = $this.attr("date-format") == null ? "yyyy-mm-dd hh:ii" : $this.attr("date-format");
$this.val(CommonJson.GetDate(value).format(dateFormat));
}
else if ($this.hasClass("select2"))
{ //select2赋值
try
{
var text = [];
if (model[obj + "_Text"] != undefined)
text = model[obj + "_Text"].split(";");
if (value.constructor !== Array)
{
value = value.split(";");
}
if (text.length>0)
{
for (var i = 0; i < value.length; i++)
{
$this.append("<option value=\"" +
value[i] +
"\" selected='selected '>" +
text[i] +
"<option>").trigger('change');
}
}
else
{
if (value[0].match(/\{/) == null)
$this.append("<option value=\"" +
value[0] +
"\" selected='selected '>" +
value[0] +
"<option>").trigger('change');
else
$this.append("<option value=\"" +
value[0] +
"\" selected='selected '>" +
eval('(' + value[0] + ')').Value +
"<option>").trigger('change');
}
} catch (e)
{
console.log(e);
}
} else if ($this.prop("localName") === "select") //下拉框赋值
$this.find("option[value='" + value + "']").prop("selected", true);
else $this.val(value); //文本框赋值
}
}
}
}
},
//获取初始化文件对象
GetInitFile: function (value)
{
var obj = new Object();
try
{
var fileArr = eval(value);
var regex = /\w+\.\w{3,4}$/gi;
var initDataArr = new Array();
var initConfigArr = new Array();
for (var i = 0; i < fileArr.length; i++)
{
//initDataArr.push("<img src='" + fileArr[i].url + "' class='file-preview-image' alt='" + fileArr[i].url.match(regex)[0] + "' title='" + fileArr[i].url.match(regex)[0] + "'>");
initDataArr.push(fileArr[i].url);
initConfigArr.push({
caption: fileArr[i].url.match(regex)[0],
//url: '/SoftMng/FileInputHandler/Delete', // server delete action
key: fileArr[i].id,
size: fileArr[i].size,
//extra: { filepath: fileArr[i].url }//不删除只做移除
});
}
obj.initData = initDataArr;
obj.initConfig = initConfigArr;
obj.initCount = fileArr.length;
} catch (e)
{
obj.initData = [];
obj.initConfig = [];
obj.initCount = 0;
}
return obj;
},
//将form中的值转换为键值对。
GetFormJson: function ($form)
{
var o = {};
var a = $form.serializeArray();
$.each(a, function ()
{
if (o[this.name] !== undefined)
{
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;
}
};
// 通用Ajax请求处理对象
var CommonAjax = {
//一般Ajax提交(url:请求路径data:传递参数callback回调参数[async]:是否异步)
Post: function (url, data, callback, async) {
$.ajax({
url: url,
data: data,
traditional:true,
async: async == undefined ? true : async,
dataType: "json",
type: "post",
success: function (data) {
callback(data);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log("错误信息:" + xmlHttpRequest.responseText + ";errorThrown:" + errorThrown);
layer.msg("请求失败!请前往控制台查看错误信息");
}
});
},
//multipart/form-data 异步表单提交(url:请求路径;$form:表单对象callback回调参数)
MtpPost: function (url, $form, callback) {
var option = {
url: url,
headers: { "ClientCallMode": "ajax" },
dataType: 'json',
type: 'post',
success: function (data) {
callback(data);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log("错误信息:" + xmlHttpRequest.responseText + ";errorThrown:" + errorThrown);
layer.msg("请求失败!请前往控制台查看错误信息");
}
};
$($form).ajaxSubmit(option);
}
}