123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997 |
- // =========================== sa对象封装一系列工具方法 ===========================
- var sa = {
- version: '2.2',
- update_time: '2020-4-17',
- info: '增加了sa_admin字段,方便调用'
- };
- // =========================== 当前环境配置 =======================================
- (function(){
- // 公司开发环境
- var cfg_dev = {
- api_url: 'http://127.0.0.1:8989', // 所有ajax请求接口父地址
- }
- sa.cfg = cfg_dev; // 最终环境 , 上线前请选择正确的环境
- })();
- // =========================== ajax的封装 =======================================
- (function(){
-
- sa.ajax = function(url, type, header, data, isCookie, successFn, errorFn){
-
- // // 默认配置
- // 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);
-
- // 打印请求地址和参数, 以便调试
- console.log("请求地址:" + url);
- console.log("请求参数:" + JSON.stringify(data));
-
- // 开始显示loading图标
- sa.loading('努力加载中...');
-
- // 开始请求ajax
- return $.ajax({
- url: url,
- type: type,
- data: data,
- dataType: 'json',
- headers: header,
- // 跨域处理
- xhrFields: {
- withCredentials: isCookie // 携带跨域cookie
- },
- crossDomain: true,
- beforeSend: function(xhr) {
- xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
- },
- success: function(res){
- console.log('返回数据:', res);
- sa.hideLoading();
- successFn(res);
- // 如果相应的处理函数存在
- // if(cfg['success' + res.code] != undefined) {
- // return cfg['success' + res.code](res);
- // }
- // layer.alert('未知状态码:' + JSON.stringify(res));
- },
- error: function(xhr, type, errorThrown){
- sa.hideLoading();
- errorFn(xhr, type, errorThrown)
- // console.log(JSON.stringify(xhr));
- // console.log(xhr, type, errorThrown);
- },
- // 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);
-
- // 打印请求地址和参数, 以便调试
- console.log("======= 模拟ajax =======");
- console.log("请求地址:" + cfg.baseUrl + url);
- console.log("请求参数:" + JSON.stringify(data));
-
- // 模拟ajax的延时
- setTimeout(function() {
- sa.hideLoading(); // 隐藏掉转圈圈
- console.log('返回数据:', cfg.res);
- 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=提示文字, cfg=配置(可省略), 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=图片路径数组, index=打开立即显示哪张(可填下标, 也可填写src路径)
- me.showImageList = function(srcList, index) {
- // 如果填的是个string
- if(typeof srcList === 'string') {
- try{
- srcList = JSON.parse(srcList);
- }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.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
- // 参数: 标题,地址,宽,高 , 点击遮罩是否关闭, 默认false
- me.showIframe3 = 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: false, // 是否点击遮罩关闭
- maxmin: true, // 显示最大化按钮
- shade: 0, // 遮罩透明度
- scrollbar: true, // 屏蔽掉外层的滚动条
- moveOut: true, // 是否可拖动到外面
- zIndex: layer.zIndex,
- area: [w, h], // 大小
- content: url, // 传值
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- resizing: function(layero) {
- solveLayerBug(index);
- },
- // 操作这个layer的时候置顶它
- success: function(layero){
- layer.setTop(layero);
- }
- });
- // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
- $('#layui-layer' + index + ' .layui-layer-max').click(function() {
- setTimeout(function() {
- solveLayerBug(index);
- }, 200)
- })
- }
-
- // 当前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');
- }
-
-
- })();
- // =========================== 常用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);
- }
-
- // 判断一个变量是否为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;
- };
-
- // 将时间转化为 个性化 如: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[34578]\d{9}$/.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;
- }
-
-
- // == if 结束
- }
-
- // =========================== 数组操作 =======================================
- if (true) {
-
-
- // 从数组里获取数据,根据指定数据
- 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){
- 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('textarea');
- 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 结束
- }
-
- })();
- // =========================== $sys 有关当前系统的方法 一般不能复制到别的项目中用 =======================================
- (function(){
-
- // 超级对象
- var me = {};
- sa.$sys = me;
-
- // 定义key
- var pcode_key = 'permission_code';
-
- // 写入当前会话的权限码集合
- sa.setAuth = function(codeList) {
- sa.keyListSet(pcode_key, codeList);
- }
-
- // 清除当前会话的权限码集合
- 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-html/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-html/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.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.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: '登录',
- shadeClose: true,
- shade: 0.8,
- area: ['90%', '90%'],
- content: login_url || '../../login.html'
- });
- }
-
- // 打开admin信息界面
- me.openAdminInfo = function(id, username) {
- var title = username + ' - 账号详情';
- if(username === undefined) {
- title = '账号详情';
- }
- sa.showIframe(title, '../sf-admin/admin-info.html?id=' + id, '700px', '600px');
- }
-
- })();
- // 如果当前是Vue环境, 则挂在到Vue示例
- if(window.Vue) {
- Vue.prototype.sa = sa;
- }
- // 如果是sa_admin环境
- window.sa_admin = window.sa_admin || parent.sa_admin || top.sa_admin;
- // 对外开放, 在模块化时解开此注释
- // export default sa;
|