123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- // =========================== sa对象封装一系列工具方法 ===========================
- var sa = {
- updateTime: '2022-2-25',
- info: '升级部分API'
- };
- // =========================== 当前环境配置 =======================================
- (function() {
- // 公司开发环境
- var cfg_dev = {
- api_url: 'http://127.0.0.1:8099/pro', // 所有ajax请求接口父地址
- web_url: 'http://www.baidu.com' // 此项目前台地址 (此配置项非必须)
- }
- // 服务器测试环境
- var cfg_test = {
- api_url: 'http://face.aseanbusiness.cn/pro',
- web_url: 'http://www.baidu.com'
- }
- // 正式生产环境
- var cfg_prod = {
- api_url: 'http://221.7.233.237:9898/pro',
- web_url: 'http://www.baidu.com'
- }
- sa.cfg = cfg_dev; // 最终环境 , 上线前请选择正确的环境
- })();
- // =========================== ajax的封装 =======================================
- (function() {
- /** 对ajax的再封装, 这个ajax假设你的接口会返回以下格式的内容
- {
- "code": 200,
- "msg": "ok",
- "data": []
- }
- 如果返回的不是这个格式, 你可能需要改动一下源码, 要么改动服务端适应此ajax, 要么改动这个ajax适应你的服务端
- * @param {Object} url 请求地址
- * @param {Object} data 请求参数
- * @param {Object} success200 当返回的code码==200时的回调函数
- * @param {Object} 其它配置,可配置项有:
- {
- msg: '', // 默认的提示文字 填null为不提示
- type: 'get', // 设定请求类型 默认post
- baseUrl: '', // ajax请求拼接的父路径 默认取 sa.cfg.api_url
- sleep: 0, // ajax模拟的延时毫秒数, 默认0
- success500: fn, // code码等于500时的回调函数 (一般代表服务器错误)
- success403: fn, // code码等于403时的回调函数 (一般代表无权限)
- success401: fn, // code码等于401时的回调函数 (一般代表未登录)
- errorfn: fn, // ajax发生错误时的回调函数 (一般是ajax请求本身发生了错误)
- complete: fn, // ajax无论成功还是失败都会执行的回调函数
- }
- */
- sa.ajaxNoLoading = function(url, data, success200, cfg) {
-
- // 如果是简写模式(省略了data参数)
- if (typeof data === 'function') {
- cfg = success200;
- success200 = data;
- data = {};
- }
-
- // 默认配置
- var defaultCfg = {
- baseUrl: (url.indexOf('http') === 0 ? '' : sa.cfg.api_url), // 父url,拼接在url前面
- sleep: 0, // 休眠n毫秒处理回调函数
- type: 'post', // 默认请求类型
- success200: success200, // code=200, 代表成功
- success500: function(res) { // code=500, 代表失败
- let msg = res.msg;
- if (msg === '操作超时') {
- window.parent.location.href = '../../login.html';
- return;
- }
- return layer.alert('失败:' + res.msg);
- },
- success403: function(res) { // code=403, 代表权限不足
- return layer.alert("权限不足," + res.msg, {
- icon: 5
- });
- },
- success401: function(res) { // code=401, 代表未登录
- return layer.confirm("您当前暂未登录,是否立即登录?", {}, function() {
- layer.closeAll();
- return sa.$page.openLogin(cfg.login_url);
- });
- },
- success405: function(res) { // code=401, 代表未登录
- window.parent.location.href = '../../login.html';
- },
- errorfn: function(xhr, type, errorThrown) { // ajax发生异常时的默认处理函数
- if (xhr.status == 0) {
- return layer.alert('无法连接到服务器,请检查网络');
- }
- return layer.alert("异常:" + JSON.stringify(xhr));
- },
- complete: function(xhr, ts) { // 成功失败都会执行
-
- }
- }
-
- // 将调用者的配置和默认配置合并
- cfg = sa.extendJson(cfg, defaultCfg);
-
- // 开始请求ajax
- return $.ajax({
- url: cfg.baseUrl + url,
- type: cfg.type,
- data: data,
- dataType: 'json',
- // xhrFields: {
- // withCredentials: true // 携带跨域cookie
- // },
- // crossDomain: true,
- beforeSend: function(xhr) {
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
- // 追加token
- if (localStorage.tokenName) {
- xhr.setRequestHeader(localStorage.tokenName, localStorage.tokenValue);
- }
- },
- success: function(res) {
- setTimeout(function() {
- // 如果相应的处理函数存在
- if (cfg['success' + res.code] != undefined) {
- return cfg['success' + res.code](res);
- }
- layer.alert('未知状态码:' + JSON.stringify(res));
- }, cfg.sleep);
- },
- error: function(xhr, type, errorThrown) {
- setTimeout(function() {
- sa.hideLoading();
- return cfg.errorfn(xhr, type, errorThrown);
- }, cfg.sleep);
- },
- complete: cfg.complete
- });
-
- };
- sa.ajax = function(url, data, success200, cfg) {
- // 如果是简写模式(省略了data参数)
- if (typeof data === 'function') {
- cfg = success200;
- success200 = data;
- data = {};
- }
- // 默认配置
- var defaultCfg = {
- msg: '努力加载中...', // 提示语
- baseUrl: (url.indexOf('http') === 0 ? '' : sa.cfg.api_url), // 父url,拼接在url前面
- sleep: 0, // 休眠n毫秒处理回调函数
- type: 'post', // 默认请求类型
- success200: success200, // code=200, 代表成功
- success500: function(res) { // code=500, 代表失败
- return layer.alert('失败:' + res.msg);
- },
- success403: function(res) { // code=403, 代表权限不足
- return layer.alert("权限不足," + res.msg, {
- icon: 5
- });
- },
- success401: function(res) { // code=401, 代表未登录
- return layer.confirm("您当前暂未登录,是否立即登录?", {}, function() {
- layer.closeAll();
- return sa.$page.openLogin(cfg.login_url);
- });
- },
- errorfn: function(xhr, type, errorThrown) { // ajax发生异常时的默认处理函数
- if (xhr.status == 0) {
- return layer.alert('无法连接到服务器,请检查网络');
- }
- return layer.alert("异常:" + JSON.stringify(xhr));
- },
- complete: function(xhr, ts) { // 成功失败都会执行
- }
- }
- // 将调用者的配置和默认配置合并
- cfg = sa.extendJson(cfg, defaultCfg);
- // 开始显示loading图标
- if (cfg.msg != null) {
- sa.loading(cfg.msg);
- }
- // 开始请求ajax
- return $.ajax({
- url: cfg.baseUrl + url,
- type: cfg.type,
- data: data,
- dataType: 'json',
- // xhrFields: {
- // withCredentials: true // 携带跨域cookie
- // },
- // crossDomain: true,
- beforeSend: function(xhr) {
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
- // 追加token
- xhr.setRequestHeader('satoken', sessionStorage.runAsToken || sessionStorage
- .satoken || localStorage.satoken || '');
- },
- success: function(res) {
- setTimeout(function() {
- sa.hideLoading();
- // 如果相应的处理函数存在
- if (cfg['success' + res.code] != undefined) {
- return cfg['success' + res.code](res);
- }
- layer.alert('未知状态码:' + JSON.stringify(res));
- }, cfg.sleep);
- },
- error: function(xhr, type, errorThrown) {
- setTimeout(function() {
- sa.hideLoading();
- return cfg.errorfn(xhr, type, errorThrown);
- }, cfg.sleep);
- },
- complete: cfg.complete
- });
- };
- // 模拟一个ajax
- // 请注意: 本模板中所有ajax请求调用的均为此模拟函数
- sa.ajax2 = function(url, data, success200, cfg) {
- // 如果是简写模式(省略了data参数)
- if (typeof data === 'function') {
- cfg = success200;
- success200 = data;
- data = {};
- }
- // 几个默认配置
- cfg = cfg || {};
- cfg.baseUrl = (url.indexOf('http') === 0 ? '' : sa.cfg.api_url); // 父url,拼接在url前面
- // 设定一个默认的提示文字
- if (cfg.msg == undefined || cfg.msg == null || cfg.msg == '') {
- cfg.msg = '正在努力加载...';
- }
- // 默认延时函数
- if (cfg.sleep == undefined || cfg.sleep == null || cfg.sleep == '' || cfg.sleep == 0) {
- cfg.sleep = 600;
- }
- // 默认的模拟数据
- cfg.res = cfg.res || {
- code: 200,
- msg: 'ok',
- data: []
- }
- // 开始loding
- sa.loading(cfg.msg);
- // 模拟ajax的延时
- setTimeout(function() {
- sa.hideLoading(); // 隐藏掉转圈圈
- success200(cfg.res);
- }, cfg.sleep)
- };
- })();
- // =========================== 封装弹窗相关函数 =======================================
- (function() {
- var me = sa;
- if (window.layer) {
- layer.ready(function() {});
- }
- // tips提示文字
- me.msg = function(msg, cfg) {
- msg = msg || '操作成功';
- layer.msg(msg, cfg);
- };
- // 操作成功的提示
- me.ok = function(msg) {
- msg = msg || '操作成功';
- layer.msg(msg, {
- anim: 0,
- icon: 1
- });
- }
- me.ok2 = function(msg) {
- msg = msg || '操作成功';
- layer.msg(msg, {
- anim: 0,
- icon: 6
- });
- }
- // 操作失败的提示
- me.error = function(msg) {
- msg = msg || '操作失败';
- layer.msg(msg, {
- anim: 6,
- icon: 2
- });
- }
- me.error2 = function(msg) {
- msg = msg || '操作失败';
- layer.msg(msg, {
- anim: 6,
- icon: 5
- });
- }
- // alert弹窗 [text=提示文字, okFn=点击确定之后的回调函数]
- me.alert = function(text, okFn) {
- // 开始弹窗
- layer.alert(text, function(index) {
- layer.close(index);
- if (okFn) {
- okFn();
- }
- });
- };
- // 询问框 [text=提示文字, okFn=点击确定之后的回调函数]
- me.confirm = function(text, okFn) {
- layer.confirm(text, {}, function(index) {
- layer.close(index);
- if (okFn) {
- okFn();
- }
- }.bind(this));
- };
- // 输入框 [title=提示文字, okFn=点击确定后的回调函数, formType=输入框类型(0=文本,1=密码,2=多行文本域) 可省略, value=默认值 可省略 ]
- me.prompt = function(title, okFn, formType, value) {
- layer.prompt({
- title: title,
- formType: formType,
- value: value
- }, function(pass, index) {
- layer.close(index);
- if (okFn) {
- okFn(pass);
- }
- });
- }
- // 打开loading
- me.loading = function(msg) {
- layer.closeAll(); // 开始前先把所有弹窗关了
- return layer.msg(msg, {
- icon: 16,
- shade: 0.3,
- time: 1000 * 20,
- skin: 'ajax-layer-load'
- });
- };
- // 隐藏loading
- me.hideLoading = function() {
- layer.closeAll();
- };
- // ============== 一些常用弹窗 =====================
- // 大窗显示一个图片
- // 参数: src=地址、w=宽度(默认80%)、h=高度(默认80%)
- me.showImage = function(src, w, h) {
- w = w || '80%';
- h = h || '80%';
- var content = '<div style="height: 100%; overflow: hidden !important;">' +
- '<img src="' + src + ' " style="width: 100%; height: 100%;" />' +
- '</div>';
- layer.open({
- type: 1,
- title: false,
- shadeClose: true,
- closeBtn: 0,
- area: [w, h], //宽高
- content: content
- });
- }
- // 预览一组图片
- // srcList=图片路径数组(可以是json样,也可以是逗号切割式), index=打开立即显示哪张(可填下标, 也可填写src路径)
- me.showImageList = function(srcList, index) {
- // 如果填的是个string
- srcList = srcList || [];
- if (typeof srcList === 'string') {
- try {
- srcList = JSON.parse(srcList);
- } catch (e) {
- try {
- srcList = srcList.split(','); // 尝试字符串切割
- } catch (e) {
- srcList = [];
- }
- }
- }
- // 如果填的是路径
- index = index || 0;
- if (typeof index === 'string') {
- index = srcList.indexOf(index);
- index = (index == -1 ? 0 : index);
- }
- // 开始展示
- var arr_list = [];
- srcList.forEach(function(item) {
- arr_list.push({
- alt: '左右键切换',
- pid: 1,
- src: item,
- thumb: item
- })
- })
- layer.photos({
- photos: {
- title: '',
- id: new Date().getTime(),
- start: index,
- data: arr_list
- },
- anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
- });
- }
- // 显示一个iframe
- // 参数: 标题,地址,宽,高 , 点击遮罩是否关闭, 默认false
- me.showIframe = function(title, url, w, h, shadeClose) {
- // 参数修正
- w = w || '95%';
- h = h || '95%';
- shadeClose = (shadeClose === undefined ? false : shadeClose);
- // 弹出面板
- var index = layer.open({
- type: 2,
- title: title, // 标题
- shadeClose: shadeClose, // 是否点击遮罩关闭
- maxmin: true, // 显示最大化按钮
- shade: 0.8, // 遮罩透明度
- scrollbar: false, // 屏蔽掉外层的滚动条
- moveOut: true, // 是否可拖动到外面
- area: [w, h], // 大小
- content: url, // 传值
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- resizing: function(layero) {
- solveLayerBug(index);
- }
- });
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- $('#layui-layer' + index + ' .layui-layer-max').click(function() {
- setTimeout(function() {
- solveLayerBug(index);
- }, 200)
- })
- }
- me.showIframeNoClose = function(title, url, w, h, shadeClose) {
- // 参数修正
- w = w || '95%';
- h = h || '95%';
- shadeClose = (shadeClose === undefined ? false : shadeClose);
- // 弹出面板
- var index = layer.open({
- type: 2,
- closeBtn:0,
- title: title, // 标题
- shadeClose: shadeClose, // 是否点击遮罩关闭
- maxmin: true, // 显示最大化按钮
- shade: 0.8, // 遮罩透明度
- scrollbar: false, // 屏蔽掉外层的滚动条
- moveOut: true, // 是否可拖动到外面
- area: [w, h], // 大小
- content: url, // 传值
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- resizing: function(layero) {
- solveLayerBug(index);
- }
- });
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- $('#layui-layer' + index + ' .layui-layer-max').click(function() {
- setTimeout(function() {
- solveLayerBug(index);
- }, 200)
- })
- }
- me.showView = me.showIframe;
- // 显示一个iframe, 底部按钮方式
- // 参数: 标题,地址,点击确定按钮执行的代码(在子窗口执行),宽,高
- me.showIframe2 = function(title, url, evalStr, w, h) {
- // 参数修正
- w = w || '95%';
- h = h || '95%';
- // 弹出面板
- var index = layer.open({
- type: 2,
- title: title, // 标题
- closeBtn: (title ? 1 : 0), // 是否显示关闭按钮
- btn: ['确定', '取消'],
- shadeClose: false, // 是否点击遮罩关闭
- maxmin: true, // 显示最大化按钮
- shade: 0.8, // 遮罩透明度
- scrollbar: false, // 屏蔽掉外层的滚动条
- moveOut: true, // 是否可拖动到外面
- area: [w, h], // 大小
- content: url, // 传值
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- resizing: function(layero) {
- },
- yes: function(index, layero) {
- var iframe = document.getElementById('layui-layer-iframe' + index);
- var iframeWindow = iframe.contentWindow;
- iframeWindow.eval(evalStr);
- }
- });
- }
- // 当前iframe关闭自身 (在iframe中调用)
- me.closeCurrIframe = function() {
- try {
- var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
- parent.layer.close(index); //再执行关闭
- } catch (e) {
- //TODO handle the exception
- }
- }
- me.closeCurrView = me.closeCurrIframe;
- //执行一个函数, 解决layer拉伸或者最大化的时候,iframe高度不能自适应的问题
- function solveLayerBug(index) {
- var selected = '#layui-layer' + index;
- var height = $(selected).height();
- var title_height = $(selected).find('.layui-layer-title').height();
- $(selected).find('iframe').css('height', (height - title_height) + 'px');
- }
- // 监听回车事件,达到回车关闭弹窗的效果
- if (window.$) {
- $(document).on('keydown', function() {
- if (event.keyCode === 13 && $(".layui-layer-btn0").length == 1 && !window.is_not_watch_enter &&
- $(this).find('.layui-layer-input').length == 0) {
- $(".layui-layer-btn0").click();
- return false;
- }
- });
- }
- })();
- // =========================== 常用util函数封装 =======================================
- (function() {
- // 超级对象
- var me = sa;
- // =========================== 常用util函数封装 =======================================
- if (true) {
- // 从url中查询到指定参数值
- me.p = function(name, defaultValue) {
- var query = window.location.search.substring(1);
- var vars = query.split("&");
- for (var i = 0; i < vars.length; i++) {
- var pair = vars[i].split("=");
- if (pair[0] == name) {
- return pair[1];
- }
- }
- return (defaultValue == undefined ? null : defaultValue);
- }
- me.q = function(name, defaultValue) {
- var query = window.location.search.substring(1);
- var vars = query.split("&");
- for (var i = 0; i < vars.length; i++) {
- var pair = vars[i].split("=");
- if (pair[0] == name) {
- return pair[1];
- }
- }
- return (defaultValue == undefined ? null : defaultValue);
- }
- // 判断一个变量是否为null
- // 返回true或false,如果return_obj有值,则在true的情况下返回return_obj
- me.isNull = function(obj, return_obj) {
- var flag = [null, undefined, '', 'null', 'undefined'].indexOf(obj) != -1;
- if (return_obj === undefined) {
- return flag;
- } else {
- if (flag) {
- return return_obj;
- } else {
- return obj;
- }
- }
- }
- // 将时间戳转化为指定时间
- // way:方式(1=年月日,2=年月日时分秒)默认1, 也可以指定格式:yyyy-MM-dd HH:mm:ss
- me.forDate = function(inputTime, way) {
- if (me.isNull(inputTime) == true) {
- return "";
- }
- var date = new Date(inputTime);
- var y = date.getFullYear();
- var m = date.getMonth() + 1;
- m = m < 10 ? ('0' + m) : m;
- var d = date.getDate();
- d = d < 10 ? ('0' + d) : d;
- var h = date.getHours();
- h = h < 10 ? ('0' + h) : h;
- var minute = date.getMinutes();
- var second = date.getSeconds();
- minute = minute < 10 ? ('0' + minute) : minute;
- second = second < 10 ? ('0' + second) : second;
- var ms = date.getMilliseconds();
- way = way || 1;
- // way == 1 年月日
- if (way === 1) {
- return y + '-' + m + '-' + d;
- }
- // way == 1 年月日时分秒
- if (way === 2) {
- return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
- }
- // way == 具体格式 标准格式: yyyy-MM-dd HH:mm:ss
- if (typeof way == 'string') {
- return way.replace("yyyy", y).replace("MM", m).replace("dd", d).replace("HH", h).replace("mm",
- minute).replace("ss", second).replace("ms", ms);
- }
- return y + '-' + m + '-' + d;
- };
- // 时间日期
- me.forDatetime = function(inputTime) {
- return me.forDate(inputTime, 2);
- }
- // 将时间转化为 个性化 如:3小时前,
- // d1 之于 d2 ,d2不填则默认取当前时间
- me.forDate2 = function(d, d2) {
- var hou = "前";
- if (d == null || d == '') {
- return '';
- }
- if (d2 == null || d2 == '') {
- d2 = new Date();
- }
- d2 = new Date(d2).getTime();
- var timestamp = new Date(d).getTime() - 1000;
- var mistiming = Math.round((d2 - timestamp) / 1000);
- if (mistiming < 0) {
- mistiming = 0 - mistiming;
- hou = '后'
- }
- var arrr = ['年', '月', '周', '天', '小时', '分钟', '秒'];
- var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
- for (var i = 0; i < arrn.length; i++) {
- var inm = Math.floor(mistiming / arrn[i]);
- if (inm != 0) {
- return inm + arrr[i] + hou;
- }
- }
- }
- // 综合以上两种方式,进行格式化
- // 小于24小时的走forDate2,否则forDat
- me.forDate3 = function(d, way) {
- if (d == null || d == '') {
- return '';
- }
- var cha = new Date().getTime() - new Date(d).getTime();
- cha = (cha > 0 ? cha : 0 - cha);
- if (cha < (86400 * 1000)) {
- return me.forDate2(d);
- }
- return me.forDate(d, way);
- }
- // 返回时间差, 此格式数组:[x, x, x, 天, 时, 分, 秒]
- me.getSJC = function(small_time, big_time) {
- var date1 = new Date(small_time); //开始时间
- var date2 = new Date(big_time); //结束时间
- var date3 = date2.getTime() - date1.getTime(); //时间差秒
- //计算出相差天数
- var days = Math.floor(date3 / (24 * 3600 * 1000));
- //计算出小时数
- var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
- var hours = Math.floor(leave1 / (3600 * 1000));
- //计算相差分钟数
- var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
- var minutes = Math.floor(leave2 / (60 * 1000));
- //计算相差秒数
- var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
- var seconds = Math.round(leave3 / 1000);
- // 返回数组
- return [0, 0, 0, days, hours, minutes, seconds];
- }
- // 将日期,加上指定天数
- me.dateAdd = function(d, n) {
- var s = new Date(d).getTime();
- s += 86400000 * n;
- return new Date(s);
- }
- // 转化json,出错返回默认值
- me.JSONParse = function(obj, default_obj) {
- try {
- return JSON.parse(obj) || default_obj;
- } catch (e) {
- return default_obj || {};
- }
- }
- // 截取指定长度字符,默认50
- me.maxLength = function(str, length) {
- length = length || 50;
- if (!str) {
- return "";
- }
- return (str.length > length) ? str.substr(0, length) + ' ...' : str;
- },
- // 过滤掉标签
- me.text = function(str) {
- if (!str) {
- return "";
- }
- return str.replace(/<[^>]+>/g, "");
- }
- // 为指定集合的每一项元素添加上is_update属性
- me.listAU = function(list) {
- list.forEach(function(ts) {
- ts.is_update = false;
- })
- return list;
- }
- // 获得一段文字中所有图片的路径
- me.getSrcList = function(str) {
- try {
- var imgReg = /<img.*?(?:>|\/>)/gi; //匹配图片(g表示匹配所有结果i表示区分大小写)
- var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配src属性
- var arr = str.match(imgReg); // 图片数组
- var srcList = [];
- for (var i = 0; i < arr.length; i++) {
- var src = arr[i].match(srcReg);
- srcList.push(src[1]);
- }
- return srcList;
- } catch (e) {
- return [];
- }
- }
- // 无精度损失的乘法
- me.accMul = function(arg1, arg2) {
- var m = 0,
- s1 = arg1.toString(),
- s2 = arg2.toString(),
- t;
- t = s1.split(".");
- // 判断有没有小数位,避免出错
- if (t[1]) {
- m += t[1].length
- }
- t = s2.split(".");
- if (t[1]) {
- m += t[1].length
- }
- return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
- }
- // 正则验证是否为手机号
- me.isPhone = function(str) {
- str = str + '';
- if ((/^1[3456789]\d{9}$/.test(str))) {
- return true;
- }
- return false;
- }
- me.isCarNo = function(str) {
- str = str + '';
- let reg =
- /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/
- if (reg.test(str)) {
- return true;
- }
- return false;
- }
- // 产生随机字符串
- me.randomString = function(len) {
- len = len || 32;
- var $chars =
- 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
- var maxPos = $chars.length;
- var str = '';
- for (i = 0; i < len; i++) {
- str += $chars.charAt(Math.floor(Math.random() * maxPos));
- }
- return str;
- }
- // 刷新页面
- me.f5 = function() {
- location.reload();
- }
- // 动态加载js
- me.loadJS = function(src, onload) {
- var script = document.createElement("script");
- script.setAttribute("type", "text/javascript");
- script.src = src;
- script.onload = onload;
- document.body.appendChild(script);
- }
- // 产生随机数字
- me.randomNum = function(min, max) {
- return parseInt(Math.random() * (max - min + 1) + min, 10);
- }
- // 打开页面
- me.open = function(url) {
- window.open(url);
- }
- // == if 结束
- }
- // =========================== 数组操作 =======================================
- if (true) {
- // 从数组里获取数据,根据指定数据
- me.getArrayField = function(arr, prop) {
- var propArr = [];
- for (var i = 0; i < arr.length; i++) {
- propArr.push(arr[i][prop]);
- }
- return propArr;
- }
- // 从数组里获取数据,根据指定数据
- me.arrayGet = function(arr, prop, value) {
- for (var i = 0; i < arr.length; i++) {
- if (arr[i][prop] == value) {
- return arr[i];
- }
- }
- return null;
- }
- // 从数组删除指定记录
- me.arrayDelete = function(arr, item) {
- if (item instanceof Array) {
- for (let i = 0; i < item.length; i++) {
- let ite = item[i];
- let index = arr.indexOf(ite);
- if (index > -1) {
- arr.splice(index, 1);
- }
- }
- } else {
- var index = arr.indexOf(item);
- if (index > -1) {
- arr.splice(index, 1);
- }
- }
- }
- // 从数组删除指定id的记录
- me.arrayDeleteById = function(arr, id) {
- var item = me.arrayGet(arr, 'id', id);
- me.arrayDelete(arr, item);
- }
- // 将数组B添加到数组A的开头
- me.unshiftArray = function(arrA, arrB) {
- if (arrB) {
- arrB.reverse().forEach(function(ts) {
- arrA.unshift(ts);
- })
- }
- return arrA;
- }
- // 将数组B添加到数组A的末尾
- me.pushArray = function(arrA, arrB) {
- if (arrB) {
- arrB.forEach(function(ts) {
- arrA.push(ts);
- })
- }
- return arrA;
- }
- // == if 结束
- }
- // =========================== 浏览器相关 =======================================
- if (true) {
- // set cookie 值
- me.setCookie = function setCookie(cname, cvalue, exdays) {
- exdays = exdays || 30;
- var d = new Date();
- d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
- var expires = "expires=" + d.toGMTString();
- document.cookie = cname + "=" + escape(cvalue) + "; " + expires + "; path=/";
- }
- // get cookie 值
- me.getCookie = function(objName) {
- var arrStr = document.cookie.split("; ");
- for (var i = 0; i < arrStr.length; i++) {
- var temp = arrStr[i].split("=");
- if (temp[0] == objName) {
- return unescape(temp[1])
- };
- }
- return "";
- }
- // 复制指定文本
- me.copyText = function(str) {
- var oInput = document.createElement('input');
- oInput.value = str;
- document.body.appendChild(oInput);
- oInput.select(); // 选择对象
- document.execCommand("Copy"); // 执行浏览器复制命令
- oInput.className = 'oInput';
- oInput.style.display = 'none';
- }
- // jquery序列化表单增强版: 排除空值
- me.serializeNotNull = function(selected) {
- var serStr = $(selected).serialize();
- return serStr.split("&").filter(function(str) {
- return !str.endsWith("=")
- }).join("&");
- }
- // 将cookie序列化为k=v形式
- me.strCookie = function() {
- return document.cookie.replace(/; /g, "&");
- }
- // 回到顶部
- me.goTop = function() {
- function smoothscroll() {
- var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
- if (currentScroll > 0) {
- window.requestAnimationFrame(smoothscroll);
- window.scrollTo(0, currentScroll - (currentScroll / 5));
- }
- };
- smoothscroll();
- }
- // == if 结束
- }
- // =========================== javascript对象操作 =======================================
- if (true) {
- // 去除json对象中的空值
- me.removeNull = function(obj) {
- var newObj = {};
- if (obj != undefined && obj != null) {
- for (var key in obj) {
- if (obj[key] === undefined || obj[key] === null || obj[key] === '') {
- //
- } else {
- newObj[key] = obj[key];
- }
- }
- }
- return newObj;
- }
- // JSON 浅拷贝, 返回拷贝后的obj
- me.copyJSON = function(obj) {
- if (obj === null || obj === undefined) {
- return obj;
- };
- var new_obj = {};
- for (var key in obj) {
- new_obj[key] = obj[key];
- }
- return new_obj;
- }
- // json合并, 将 defaulet配置项 转移到 user配置项里 并返回 user配置项
- me.extendJson = function(userOption, defaultOption) {
- if (!userOption) {
- return defaultOption;
- };
- for (var key in defaultOption) {
- if (userOption[key] === undefined) {
- userOption[key] = defaultOption[key];
- } else if (userOption[key] == null) {
- } else if (typeof userOption[key] == "object") {
- me.extendJson(userOption[key], defaultOption[key]); //深度匹配
- }
- }
- return userOption;
- }
- // == if 结束
- }
- // =========================== 本地集合存储 =======================================
- if (true) {
- // 获取指定key的list
- me.keyListGet = function(key) {
- try {
- var str = localStorage.getItem('LIST_' + key);
- if (str == undefined || str == null || str == '' || str == 'undefined' || typeof(JSON.parse(
- str)) == 'string') {
- //alert('key' + str);
- str = '[]';
- }
- return JSON.parse(str);
- } catch (e) {
- return [];
- }
- },
- me.keyListSet = function(key, list) {
- localStorage.setItem('LIST_' + key, JSON.stringify(list));
- },
- me.keyListHas = function(key, item) {
- var arr2 = me.keyListGet(key);
- return arr2.indexOf(item) != -1;
- },
- me.keyListAdd = function(key, item) {
- var arr = me.keyListGet(key);
- arr.push(item);
- me.keyListSet(key, arr);
- },
- me.keyListRemove = function(key, item) {
- var arr = me.keyListGet(key);
- var index = arr.indexOf(item);
- if (index > -1) {
- arr.splice(index, 1);
- }
- me.keyListSet(key, arr);
- }
- // == if 结束
- }
- // =========================== 对sa-admin的优化 =======================================
- if (true) {
- // 封装element-ui的导出表格
- // 参数:选择器(默认.data-count),fileName=导出的文件名称
- me.exportExcel = function(select, fileName) {
- // 声明函数
- let exportExcel_fn = function(select, fileName) {
- // 赋默认值
- select = select || '.data-table';
- fileName = fileName || 'table.xlsx';
- // 开始导出
- let wb = XLSX.utils.table_to_book(document.querySelector(select)); // 这里就是表格
- let sheet = wb.Sheets.Sheet1; // 单元表
- try {
- // 强改宽度
- sheet['!cols'] = sheet['!cols'] || [];
- let thList = document.querySelector(select).querySelectorAll(
- '.el-table__header-wrapper tr th');
- for (var i = 0; i < thList.length; i++) {
- // 如果是多选框
- if (thList[i].querySelector('.el-checkbox')) {
- sheet['!cols'].push({
- wch: 5
- }); // 强改宽度
- continue;
- }
- sheet['!cols'].push({
- wch: 15
- }); // 强改宽度
- }
- // 强改高度
- sheet['!rows'] = sheet['!rows'] || [];
- let trList = document.querySelector(select).querySelectorAll(
- '.el-table__body-wrapper tbody tr');
- for (var i = 0; i < trList.length + 1; i++) {
- sheet['!rows'].push({
- hpx: 20
- }); // 强改高度
- }
- } catch (e) {
- console.err(e);
- }
- // 开始制作并输出
- let wbout = XLSX.write(wb, {
- bookType: 'xlsx',
- bookSST: true,
- type: 'array'
- });
- // 点击
- let blob = new Blob([wbout], {
- type: 'application/octet-stream'
- });
- const a = document.createElement("a")
- a.href = URL.createObjectURL(blob)
- a.download = fileName // 这里填保存成的文件名
- a.click()
- URL.revokeObjectURL(a.href)
- a.remove();
- sa.hideLoading();
- }
- sa.loading('正在导出...');
- // 判断是否首次加载
- if (window.XLSX) {
- return exportExcel_fn(select, fileName);
- } else {
- me.loadJS('https://unpkg.com/xlsx@0.16.6/dist/xlsx.core.min.js', function() {
- return exportExcel_fn(select, fileName);
- });
- }
- }
- // 刷新表格高度, 请务必在所有表格高度发生变化的地方调用此方法
- me.f5TableHeight = function() {
- Vue.nextTick(function() {
- if ($('.el-table.data-table .el-table__body-wrapper table').length == 0) {
- return;
- }
- var _f5Height = function() {
- var height = $('.el-table .el-table__body-wrapper table').height();
- height = height == 0 ? 60 : height;
- // 判断是否有滚动条
- var tw = $('.el-table .el-table__body-wrapper').get(0);
- if (tw.scrollWidth > tw.clientWidth) {
- height = height + 16;
- }
- if ($('.el-table .el-table__body-wrapper table td').width() == 0) {
- return;
- }
- // 设置高度
- $('.el-table .el-table__body-wrapper').css('min-height', height);
- $('.el-table .el-table__body-wrapper').css('max-height', height);
- };
- setTimeout(_f5Height, 0)
- setTimeout(_f5Height, 200)
- })
- }
- // 在表格查询的页面,监听input回车事件,提交查询
- me.onInputEnter = function(app) {
- Vue.nextTick(function() {
- app = app || window.app;
- // document.querySelectorAll('.el-form input').forEach(function(item) {
- // item.onkeydown = function(e) {
- // var theEvent = e || window.event;
- // var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
- // if (code == 13) {
- // app.p.pageNo = 1;
- // app.f5();
- // }
- // }
- // })
- document.querySelectorAll('.el-form').forEach(function(item) {
- item.onkeydown = function(e) {
- var theEvent = e || window.event;
- var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
- if (code == 13) {
- var target = e.target || e.srcElement;
- if (target.tagName.toLowerCase() == "input") {
- app.p.pageNo = 1;
- app.f5();
- }
- }
- }
- })
- })
- }
- // 如果value为true,则抛出异常
- me.check = function(value, errorMsg) {
- if (value === true) {
- throw {
- type: 'sa-error',
- msg: errorMsg
- };
- }
- }
- // 如果value为null,则抛出异常
- me.checkNull = function(value, errorMsg) {
- if (me.isNull(value)) {
- throw {
- type: 'sa-error',
- msg: errorMsg
- };
- }
- }
- // 监听窗口变动
- if (!window.onresize) {
- window.onresize = function() {
- try {
- me.f5TableHeight();
- } catch (e) {
- // console.log(e);
- }
- }
- }
- // 双击layer标题处全屏
- if (window.$) {
- $(document).on('mousedown', '.layui-layer-title', function(e) {
- // console.log('单击中');
- if (window.layer_title_last_click_time) {
- var cz = new Date().getTime() - window.layer_title_last_click_time;
- if (cz < 250) {
- $(this).parent().find('.layui-layer-max').click();
- }
- }
- window.layer_title_last_click_time = new Date().getTime();
- })
- }
- // == if 结束
- }
- })();
- // =========================== $sys 有关当前系统的方法 一般不能复制到别的项目中用 =======================================
- (function() {
- // 超级对象
- var me = {};
- sa.$sys = me;
- // ======================= 登录相关 ============================
- // 写入当前已登陆用户信息
- me.setCurrUser = function(currUser) {
- localStorage.setItem('currUser', JSON.stringify(currUser));
- }
- // 获得当前已登陆用户信息
- me.getCurrUser = function() {
- var user = localStorage.getItem("currUser");
- if (user == undefined || user == null || user == 'null' || user == '' || user == '{}' || user.length <
- 10) {
- user = {
- id: '0',
- username: '未登录'
- }
- } else {
- user = JSON.parse(user);
- }
- return user;
- }
- // 如果未登录,则强制跳转到登录
- me.checkLogin = function(not_login_url) {
- if (me.getCurrUser().id == 0) {
- location.href = not_login_url || '../../login.html';
- throw '未登录,请先登录';
- }
- }
- // 同上, 只不过是以弹窗的形式显示未登录
- me.checkLoginTs = function(not_login_url) {
- if (me.getCurrUser().id == 0) {
- sa.$page.openLogin(not_login_url || '../../login.html');
- throw '未登录,请先登录';
- }
- }
- // ========================= 权限验证 =========================
- // 定义key
- var pcode_key = 'permission_code';
- // 写入当前会话的权限码集合
- sa.setAuth = function(codeList) {
- sa.keyListSet(pcode_key, codeList);
- }
- sa.setDepartmentId=function(departmentId) {
- localStorage.setItem('department_key', departmentId);
- }
- sa.isAdmin=function() {
- return localStorage.getItem('department_key') == '1';
- }
- // 清除当前会话的权限码集合
- sa.clearAuth = function() {
- sa.keyListSet(pcode_key, []);
- }
- // 检查当前会话是否拥有一个权限码, 返回true和false
- sa.isAuth = function(pcode) {
- return sa.keyListHas(pcode_key, pcode);
- }
- // 检查当前会话是否拥有一个权限码, 如果没有, 则跳转到无权限页面
- // 注意: 非二级目录页面请注意调整路径问题
- sa.checkAuth = function(pcode, not_pcode_url) {
- var is_have = sa.keyListHas(pcode_key, pcode);
- if (is_have == false) {
- location.href = not_pcode_url || '../../sa-view/error-page/403.html';
- throw '暂无权限: ' + pcode;
- }
- }
- // 同上, 只不过是以弹窗的形式显示出来无权限来
- sa.checkAuthTs = function(pcode, not_pcode_url) {
- var is_have = sa.keyListHas(pcode_key, pcode);
- if (is_have == false) {
- var url = not_pcode_url || '../../sa-view/error-page/403.html';
- layer.open({
- type: 2,
- title: false, // 标题
- shadeClose: true, // 是否点击遮罩关闭
- shade: 0.8, // 遮罩透明度
- scrollbar: false, // 屏蔽掉外层的滚动条
- closeBtn: false,
- area: ['700px', '600px'], // 大小
- content: url // 传值
- });
- throw '暂无权限: ' + pcode;
- }
- }
- // ======================= 配置相关 ============================
- // 写入配置信息
- me.setAppCfg = function(cfg) {
- if (typeof cfg != 'string') {
- cfg = JSON.stringify(cfg);
- }
- localStorage.setItem('app_cfg', cfg);
- }
- // 获取配置信息
- me.getAppCfg = function() {
- var app_cfg = sa.JSONParse(localStorage.getItem('app_cfg'), {}) || {};
- return app_cfg;
- }
- })();
- // =========================== $page 跳页面相关 避免一次变动,到处乱改 =======================================
- (function() {
- // 超级对象
- var me = {};
- sa.$page = me;
- // 打开登录页面
- me.openLogin = function(login_url) {
- layer.open({
- type: 2,
- // title: '登录',
- title: false,
- closeBtn: false,
- shadeClose: true,
- shade: 0.8,
- // area: ['90%', '100%'],
- area: ['70%', '80%'],
- // area: ['450px', '360px'],
- resize: false,
- content: login_url || '../../login.html'
- });
- }
- // 打开admin信息界面
- me.openAdminInfo = function(id, username) {
- var title = username + ' - 账号详情';
- if (username === undefined) {
- title = '账号详情';
- }
- sa.showIframe(title, '../sp-admin/admin-info.html?id=' + id, '700px', '600px');
- }
- })();
- // 如果是sa_admin环境
- window.sa_admin = window.sa_admin || parent.sa_admin || top.sa_admin;
- window.saAdmin = window.sa_admin;
- // 如果当前是Vue环境, 则挂在到 Vue 示例
- if (window.Vue) {
- // 全局的 sa 对象
- Vue.prototype.sa = window.sa;
- Vue.prototype.sa_admin = window.sa_admin;
- Vue.prototype.saAdmin = window.saAdmin;
- // 表单校验异常捕获
- Vue.config.errorHandler = function(err, vm) {
- if (err.type == 'sa-error') {
- return sa.error(err.msg);
- }
- throw err;
- }
- // Element-UI 全局组件样式
- Vue.prototype.$ELEMENT = {
- size: 'mini',
- zIndex: 3000
- };
- // 加载全局组件 (注意路径问题)
- // if(window.httpVueLoader && window.loadComponent !== false) {
- // Vue.component("sa-item", httpVueLoader('../../sa-frame/com/sa-item.vue'));
- // Vue.component("sa-td", httpVueLoader('../../sa-frame/com/sa-td.vue'));
- // }
- }
- // 对外开放, 在模块化时解开此注释
- // export default sa;
|