/*************************/
/* 1. 由main.html引用 */
/* 2. 其他界面不引用 */
/*************************/
// 日志
function debug(message, obj) {
if (config.isDebug) {
if (obj) {
console.log(message, obj);
}
else {
console.log(message);
}
}
}
/*********** object *************/
Object.init = function(config) {
if (prototyping) {
return;
}
this.config = config;
if (config) {
var name = null,
value;
for (name in config) {
value = config[name];
if (typeof value === "function" && typeof value.nodeType !== "number") {
this[name] = value;
}
}
}
};
Object.subClass = function(properties) {
if (!properties) {
return;
}
var clazz = properties.init;
delete properties.init;
if (!clazz) {
clazz = new Function();
}
prototyping = true;
try {
var prototype = new this();
} finally {
prototyping = false;
}
if($) {
$.extend(prototype, properties);
}
clazz.prototype = prototype;
clazz.subClass = arguments.callee;
return clazz;
};
/********* Server 请求*************/
var ServerClass = Object.subClass({
ajaxRequest: function(url, param, callback, onFail, onSuccess, onError) {
var me = this;
if (!url) {
return;
}
if (url.substring(0, 8) == "rootact/") {
url = config.url_act + url.substring(8);
}
else {
url = config.url_root + url;
}
if (!param) {
param = {}
}
if (Root) {
var token = Root.getToken();
if (url.indexOf("?") > 0) {
url = url + "&" + "token=" + token;
}
else {
url = url + "?" + "token=" + token;
}
if (!param.token) {
param.token = token;
}
}
var option = {
contentType: param.contentType || "text/plain" ,
dataType: param.dataType || "json",
type: param.type || "post",
headers: param.headers || {}
}
param = util.encode(param);
debug("请求:" + url);
debug("参数:" + param);
try {
$.ajax({
url: url,
contentType: option.contentType,
dataType: option.dataType,
type: option.type,
headers: option.headers,
data: param,
success: function(result) {
debug("返回:", result);
if (result) {
if ("timeout" == result.errorcode) {
window.top.location.href = config.page_timeout;
}
else if (!result.success) {
console.log(result);
if (onFail) {
onError(onFail, result);
}
else if(!result.success && result.message) {
Root.message({
type: 'error',
message: result.message
});
}
else {
var error = util.decode(result.errormessage);
try {
if (console.log) {
console.log(error);
}
} catch (e) {}
alert(error);
}
}
else if (result.success){
onSuccess(callback, result);
}
}
},
error: function(d1, error, message) {
if (error == "parsererror" && d1.responseText) {
var result_ = clone(d1.responseText);
console.log(result_);
result_ = result_.replace(/[\t\r\n]/g,"");
result_ = JSON.parse(result_);
if (result_.success) {
onSuccess(callback, result_);
return
}
}
alert(error);
try {
if (console.log) {
console.log(error);
}
} catch (e) {}
}
});
} catch (e) {}
},
call: function(url, param, callback, onFail) {
if (!url) {
return;
}
var me = this;
var afterRequest = function(doCallback, result) {
if (doCallback) {
try {
result = util.decode(result);
} finally {
doCallback(result);
}
}
};
me.ajaxRequest(url, param, callback, onFail, afterRequest, afterRequest);
},
upload: function(url, file, onProgress, onSuccess, onError) {
var formdata = new FormData();
formdata.append("fileList", file);
try {
$.ajax({
cache: true,
type: "POST",
url: serverAddress + url,
data: formdata,
dataType: "json",
processData: false,
contentType: false,
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if (onProgress && xhr.upload) {
xhr.upload.addEventListener("progress", onProgress, false);
return xhr;
}
},
error: function(request) {
if (onError) {
onError();
}
},
success: function(data) {
data = util.decode(data);
if (onSuccess) {
onSuccess(data);
}
}
});
} catch (e) {
if (onError) {
onError();
}
}
}
});
Server = new ServerClass();
/********* Dictionary 字典*************/
DictionaryClass = Object.subClass({
init: function() {
data: {}
},
getLabel: function(value, dictCode) {
var dict = this.data[dictCode];
if (!dict) {
return value;
}
var result = dict.map[value];
if (!result) {
return value;
}
return result.label;
},
getList: function(dictCode) {
var dict = this.data[dictCode];
if (!dict) {
return [];
}
return dict.list;
},
getMap: function(dictCode) {
var dict = this.data[dictCode];
if (!dict) {
return {};
}
return dict.map;
},
load: function(datalist) {
if (!(datalist && datalist.length > 0)) {
return;
}
var dataobj_ = {};
var dictionary_list = [
{code: "sysdictionary", value: "字典组名"}
];
var dictionary_map = {code: "sysdictionary", value: "字典组名"};
datalist.map(e=>{
var dictionary_ = {
code: e.code,
value: e.name
}
dictionary_list.push(dictionary_);
dictionary_map[e.code] = dictionary_;
if (e.items.length) {
dataobj_[e.code] = e.items;
}
});
var data = {};
data.sysdictionary = {
"list": dictionary_list,
"map": dictionary_map
}
for (var prop in dataobj_) {
var dict = dataobj_[prop];
var list = dict,
map = {};
for (var i = 0; i < list.length; i++) {
var item = list[i];
map[item.code] = item.value;
}
data[prop] = {
"list": list,
"map": map
}
}
this.data = data;
}
});
Dictionary = new DictionaryClass();
/********* util 小工具*************/
util = {
debug: function(message) {
if (RootSetting.isDebug) {
console.log(message);
}
},
buildURL: function(url) {
if (!url) {
return null;
}
if (url.startsWith("http")) {
return url;
}
if (url.startsWith("/")) {
return "/" + RootSetting.appName + url;
}
url = window.top.config.url_page + url
return url;
},
decode: function(obj) {
if (!obj) {
return null;
}
if ($.isArray(obj)) {
return this.decodeArray(obj);
} else if (typeof obj == "object") {
return this.decodeObject(obj);
} else if (typeof obj == "string") {
try {
return decodeURIComponent(obj);
} catch (e) {
return obj;
}
} else {
return obj;
}
},
decodeArray: function(data) {
for (var i = 0; i < data.length; i++) {
if (typeof data[i] == 'string') {
data[i] = this.decode(data[i]);
} else {
data[i] = this.decodeObject(data[i]);
}
}
return data;
},
decodeObject: function(data) {
for (var prop in data) {
if (data[prop]) {
data[prop] = this.decode(data[prop]);
}
}
return data;
},
encode: function(obj) {
if ($.isArray(obj)) {
return this.encodeArray(obj);
} else if (typeof obj == "object") {
return this.encodeObject(obj);
} else if (typeof obj == "string") {
return "\"" + encodeURI(obj) + "\"";
} else if (typeof obj == "boolean") {
return String(obj);
} else if (typeof obj == "number") {
return String(obj);
} else if (typeof obj === "function") {
return "";
}
},
encodeArray: function(o) {
var a = ["[", ""],
len = o.length,
i;
for (i = 0; i < len; i += 1) {
a.push(this.encode(o[i]), ',');
}
a[a.length - 1] = ']';
return a.join("");
},
encodeObject: function(o) {
if (!o) {
return "null";
}
if (o.length) {
return "el";
}
var a = ["{", ""];
for (var i in o) {
if (i == 'parent') {
continue;
}
a.push(this.encode(i), ":", this.encode(o[i]), ',');
}
a[a.length - 1] = '}';
return a.join("");
},
objectToURI: function(object) {
if (!object) {
return null;
}
if (typeof object == "String") {
return encodeURI(object);
}
var param = null;
for (var prop in object) {
if (object[prop] != null) {
if (param) {
param = param + "&" + prop + "=" + encodeURI(object[prop]);
} else {
param = prop + "=" + encodeURI(object[prop]);
}
}
}
return param;
},
combine: function(obj1, obj2, override) {
if (!obj1) {
return obj2;
}
if (!obj2) {
return obj1;
}
for (var name in obj2) {
if (override || obj1[name] === undefined) {
obj1[name] = obj2[name];
}
}
return obj1;
},
combineObj: function(obj1, obj2, override) {
if (!obj1) {
return obj2;
}
if (!obj2) {
return obj1;
}
for (var name in obj2) {
//如果1没有该属性
if (override || obj1[name] === undefined) {
obj1[name] = obj2[name];
}
//如果1存在该属性
else if (typeof(obj1[name]) == "object") {
util.combine(obj1[name], obj2[name]);
}
}
return obj1;
},
setVisible: function(el, visible) {
if (visible) {
el.show();
} else {
el.hide();
}
},
getEl: function(element) {
var type = typeof element;
if (type == "string") {
if ("#" != element.substring(0, 1)) {
element = "#" + element;
}
var el = $(element);
return el;
}
return element;
},
pageTo: function(frame, url) {
if (!url) {
url = frame;
frame = null;
}
if (!url) {
return;
}
//1. root
var exists = url && url.lastIndexOf("root/");
if (exists >= 0) {
var current = window.top.location.pathname;
var pos = current.indexOf("root/");
if (pos >= 0) {
url = current.substring(0, pos) + url;
}
}
//2. random
if (url.indexOf("?") > 0) {
url = url + "&" + new Date().getTime();
} else {
url = url + "?" + new Date().getTime();
}
//3. set
if (frame) {
frame.attr("src", window.top.config.url_page + url);
} else {
window.top.location.href = url;
}
},
//日期格式转换
dateFormat: function(date, fmt) {
if (typeof date == 'string') {
date = new Date(date);
}
if (null == date || undefined == date) return '';
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k])
.substr(("" + o[k]).length)));
return fmt;
},
//数值转金额格式,可设置小数位数
toMoney: function(num, digit) {
if (digit == null) {//几位小数
digit = 2;
}
if (num) {
if (isNaN(num)) {
//alert('金额中含有不能识别的字符');
return;
}
num = typeof num == 'string' ? parseFloat(num) : num // 判断是否是字符串如果是字符串转成数字
num = num.toFixed(digit); // 保留两位
num = parseFloat(num); // 转成数字
num = num.toLocaleString(); // 转成金额显示模式
// 判断是否有小数
if (num.indexOf('.') === -1) {
num = '¥' + num + '.00';
} else {
// console.log(num.split('.')[1].length)
num = num.split('.')[1].length < 2 ? '¥' + num + '0' : '¥' + num;
}
return num; // 返回的是字符串23,245.12保留2位小数
} else {
return num = null;
}
},
//数值转率,可设置小数位数
toPercent: function(num, digit) {
if (digit == null) {//几位小数
digit = 2;
}
if (num) {
if (isNaN(num)) {
//alert('金额中含有不能识别的字符');
return;
}
num = typeof num == 'string' ? parseFloat(num) : num // 判断是否是字符串如果是字符串转成数字
num = num.toFixed(digit); // 保留两位
num = parseFloat(num); // 转成数字
num += "%";
return num; // 返回的是字符串23,245.12保留2位小数
} else {
return num = null;
}
},
//数值保留几位小数
toFloat: function(num, digit) {
if (digit == null) {//几位小数
digit = 2;
}
if (num) {
if (isNaN(num)) {
//alert('金额中含有不能识别的字符');
return;
}
num = typeof num == 'string' ? parseFloat(num) : num // 判断是否是字符串如果是字符串转成数字
num = num.toFixed(digit); // 保留两位
num = parseFloat(num); // 转成数字
return num; // 返回的是字符串23,245.12保留2位小数
} else {
return num = null;
}
},
};
///////////////////formatter//////////////////
/////////////////组件中的formatter///////////////////
//通用formatterjson转换,根据json对应关系显示,
function formatter_json(row, column, cellValue, index, json_) {
var json = json_;
var val = "";
if (typeof(json_)=="string") {
json = JSON.parse(json_);
}
if (json[cellValue]) {
return json[cellValue];
}
else if (cellValue && json.othervaldefaultval) {
//没有对应关系但是有值,可设置其它数值的显示值
return json.othervaldefaultval;
}
else if (!cellValue && json.defaultval) {
//没有值,可设置默认显示值
return json.defaultval;
}
else {
return cellValue
}
};
//日期格式化
function formatter_date(row, column, cellValue, index, format) {
var format_ = "yyyy-MM-dd";
if (format) {
format_ = format;
}
if (cellValue) {
return util.dateFormat(cellValue, format_);
}
return cellValue;
};
//金额格式化
function formatter_money(row, column, cellValue, index, digit) {
if (cellValue) {
return util.toMoney(cellValue, digit);
}
return cellValue;
};
//小数格式化
function formatter_float(row, column, cellValue, index, digit) {
if (cellValue) {
return util.toFloat(cellValue, digit);
}
return cellValue;
};
//率转换格式化
function formatter_percent(row, column, cellValue, index, digit) {
if (cellValue) {
return util.toPercent(cellValue, digit);
}
return cellValue;
};
//有前缀或后缀的格式化
function formatter_prefixorunit(row, column, cellValue, index, prefix, unit) {
if (cellValue != null) {
var cellValue_ = cellValue;
if (prefix) {
cellValue_ = prefix + (cellValue_);
}
if (unit) {
cellValue_ = (cellValue_) + unit;
}
return cellValue_;
}
return cellValue;
};
////////////////非通用的格式化////////////////
function formatter_earlyWarning(row, column, cellValue, index) {
var d_ = 31;
var date_ = Date.parse(new Date());
var closedate_ = Date.parse(new Date(row.closedate));
var d_ = parseInt((closedate_-date_)/ (1000 * 60 * 60 * 24));
return d_;
};
/********* root *************/
var RootClass = Object.subClass({
popupParames: {},
popupList: [],
userInfo: {},
tab: null,
message: null,
init: function(config) {
Object.call(this, {});
this.tab = config.tab;
this.userInfo = config.userInfo;
},
setUserInfo: function(userInfo) {
this.userInfo = userInfo;
},
showSubTab: function(config) {
this.tab.showSubItem(config);
},
hideSubTab: function(config) {
this.tab.hideSubItem(config);
},
showPopup: function(config) {
config.url = util.buildURL(config.url);
this.popupParames = config;
var popup = new PopupWindowClass(config);
this.popupList.push(popup);
popup.show();
},
setPopupWH: function(w, h) {
if (!this.popupList.length) {
return;
}
var popup = this.popupList[this.popupList.length - 1];
popup.setSize(w, h);
},
hidePopup: function(closecallback) {
if (!this.popupList.length) {
return;
}
var popup = this.popupList.pop();
if (closecallback) {
closecallback()
}
popup.hide();
},
init_vue: function() {
this.confirm = vue.$confirm;
this.message = vue.$message;
},
getToken: function() {
return this.userInfo.token;
}
});
Root = null;
/********* RootParent *************/
RootBodyClass = Object.subClass({
init: function() {
this.controls = [];
this.win = $(window);
this.document = $(document);
this._width = this.win.width();
this._height = this.win.height();
},
width: function() {
return this._width;
},
height: function() {
return this._height;
},
append: function(element) {
if (!this.body) {
this.body = $('body');
var me = this;
this.document.click(function() {
me.notifyAll();
});
}
this.body.append(element);
},
register: function(control) {
this.controls.push(control);
},
notifyAll: function() {
for (var i = 0; i < this.controls.length; i++) {
this.notifyOne(this.controls[i]);
}
},
notifyOne: function(control) {
if (control && control.onNotify) {
control.onNotify.call(control, "rootClick");
}
}
});
RootBody = new RootBodyClass();