sa.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. // =========================== sa对象封装一系列工具方法 ===========================
  2. var sa = {
  3. version: '2.4.3',
  4. update_time: '2020-10-2',
  5. info: '新增双击layer标题处全屏'
  6. };
  7. // =========================== 当前环境配置 =======================================
  8. (function() {
  9. // 公司开发环境
  10. var cfg_dev = {
  11. api_url: 'http://127.0.0.1:8099/pro', // 所有ajax请求接口父地址
  12. web_url: 'http://www.baidu.com' // 此项目前台地址 (此配置项非必须)
  13. }
  14. // 服务器测试环境
  15. var cfg_test = {
  16. api_url: 'https://pco.aseanbusiness.cn/pro',
  17. web_url: 'http://www.baidu.com'
  18. }
  19. // 正式生产环境
  20. var cfg_prod = {
  21. api_url: 'https://dxkaa1.gxbtka.com/pro',
  22. web_url: 'http://www.baidu.com'
  23. }
  24. sa.cfg = cfg_dev; // 最终环境 , 上线前请选择正确的环境
  25. })();
  26. // =========================== ajax的封装 =======================================
  27. (function() {
  28. sa.ajaxNoLayer = function(url, data, success200, cfg) {
  29. // 如果是简写模式(省略了data参数)
  30. if (typeof data === 'function') {
  31. cfg = success200;
  32. success200 = data;
  33. data = {};
  34. }
  35. // 默认配置
  36. var defaultCfg = {
  37. baseUrl: (url.indexOf('http') === 0 ? '' : sa.cfg.api_url), // 父url,拼接在url前面
  38. sleep: 0, // 休眠n毫秒处理回调函数
  39. type: 'post', // 默认请求类型
  40. success200: success200, // code=200, 代表成功
  41. }
  42. // 将调用者的配置和默认配置合并
  43. cfg = sa.extendJson(cfg, defaultCfg);
  44. // 开始请求ajax
  45. return $.ajax({
  46. url: cfg.baseUrl + url,
  47. type: cfg.type,
  48. data: data,
  49. dataType: 'json',
  50. beforeSend: function(xhr) {
  51. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  52. // 追加token
  53. if (localStorage.tokenName) {
  54. xhr.setRequestHeader(localStorage.tokenName, localStorage.tokenValue);
  55. }
  56. },
  57. success: function(res) {
  58. if (cfg['success' + res.code] != undefined) {
  59. return cfg['success' + res.code](res);
  60. }
  61. },
  62. });
  63. };
  64. sa.ajaxNoLoading = function(url, data, success200, cfg) {
  65. // 如果是简写模式(省略了data参数)
  66. if (typeof data === 'function') {
  67. cfg = success200;
  68. success200 = data;
  69. data = {};
  70. }
  71. // 默认配置
  72. var defaultCfg = {
  73. baseUrl: (url.indexOf('http') === 0 ? '' : sa.cfg.api_url), // 父url,拼接在url前面
  74. sleep: 0, // 休眠n毫秒处理回调函数
  75. type: 'post', // 默认请求类型
  76. success200: success200, // code=200, 代表成功
  77. success500: function(res) { // code=500, 代表失败
  78. let msg = res.msg;
  79. if (msg === '操作超时') {
  80. window.parent.location.href = '../../login.html';
  81. return;
  82. }
  83. return layer.alert('失败:' + res.msg);
  84. },
  85. success403: function(res) { // code=403, 代表权限不足
  86. return layer.alert("权限不足," + res.msg, {
  87. icon: 5
  88. });
  89. },
  90. success401: function(res) { // code=401, 代表未登录
  91. return layer.confirm("您当前暂未登录,是否立即登录?", {}, function() {
  92. layer.closeAll();
  93. return sa.$page.openLogin(cfg.login_url);
  94. });
  95. },
  96. success405: function(res) { // code=401, 代表未登录
  97. window.parent.location.href = '../../login.html';
  98. },
  99. errorfn: function(xhr, type, errorThrown) { // ajax发生异常时的默认处理函数
  100. if (xhr.status == 0) {
  101. return layer.alert('无法连接到服务器,请检查网络');
  102. }
  103. return layer.alert("异常:" + JSON.stringify(xhr));
  104. },
  105. complete: function(xhr, ts) { // 成功失败都会执行
  106. }
  107. }
  108. // 将调用者的配置和默认配置合并
  109. cfg = sa.extendJson(cfg, defaultCfg);
  110. // 开始请求ajax
  111. return $.ajax({
  112. url: cfg.baseUrl + url,
  113. type: cfg.type,
  114. data: data,
  115. dataType: 'json',
  116. // xhrFields: {
  117. // withCredentials: true // 携带跨域cookie
  118. // },
  119. // crossDomain: true,
  120. beforeSend: function(xhr) {
  121. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  122. // 追加token
  123. if (localStorage.tokenName) {
  124. xhr.setRequestHeader(localStorage.tokenName, localStorage.tokenValue);
  125. }
  126. },
  127. success: function(res) {
  128. setTimeout(function() {
  129. // 如果相应的处理函数存在
  130. if (cfg['success' + res.code] != undefined) {
  131. return cfg['success' + res.code](res);
  132. }
  133. layer.alert('未知状态码:' + JSON.stringify(res));
  134. }, cfg.sleep);
  135. },
  136. error: function(xhr, type, errorThrown) {
  137. setTimeout(function() {
  138. sa.hideLoading();
  139. return cfg.errorfn(xhr, type, errorThrown);
  140. }, cfg.sleep);
  141. },
  142. complete: cfg.complete
  143. });
  144. };
  145. /** 对ajax的再封装, 这个ajax假设你的接口会返回以下格式的内容
  146. {
  147. "code": 200,
  148. "msg": "ok",
  149. "data": []
  150. }
  151. 如果返回的不是这个格式, 你可能需要改动一下源码, 要么改动服务端适应此ajax, 要么改动这个ajax适应你的服务端
  152. * @param {Object} url 请求地址
  153. * @param {Object} data 请求参数
  154. * @param {Object} success200 当返回的code码==200时的回调函数
  155. * @param {Object} 其它配置,可配置项有:
  156. {
  157. msg: '', // 默认的提示文字 填null为不提示
  158. type: 'get', // 设定请求类型 默认post
  159. baseUrl: '', // ajax请求拼接的父路径 默认取 sa.cfg.api_url
  160. sleep: 0, // ajax模拟的延时毫秒数, 默认0
  161. success500: fn, // code码等于500时的回调函数 (一般代表服务器错误)
  162. success403: fn, // code码等于403时的回调函数 (一般代表无权限)
  163. success401: fn, // code码等于401时的回调函数 (一般代表未登录)
  164. errorfn: fn, // ajax发生错误时的回调函数 (一般是ajax请求本身发生了错误)
  165. complete: fn, // ajax无论成功还是失败都会执行的回调函数
  166. }
  167. */
  168. sa.ajax = function(url, data, success200, cfg) {
  169. // 如果是简写模式(省略了data参数)
  170. if (typeof data === 'function') {
  171. cfg = success200;
  172. success200 = data;
  173. data = {};
  174. }
  175. // 默认配置
  176. var defaultCfg = {
  177. msg: '努力加载中...', // 提示语
  178. baseUrl: (url.indexOf('http') === 0 ? '' : sa.cfg.api_url), // 父url,拼接在url前面
  179. sleep: 0, // 休眠n毫秒处理回调函数
  180. type: 'post', // 默认请求类型
  181. success200: success200, // code=200, 代表成功
  182. success500: function(res) { // code=500, 代表失败
  183. let msg = res.msg;
  184. console.log(msg);
  185. if (msg === '操作超时') {
  186. window.parent.location.href = '../../login.html';
  187. return;
  188. }
  189. return layer.alert('失败:' + res.msg);
  190. },
  191. success403: function(res) { // code=403, 代表权限不足
  192. return layer.alert("权限不足," + res.msg, {
  193. icon: 5
  194. });
  195. },
  196. success401: function(res) { // code=401, 代表未登录
  197. return layer.confirm("您当前暂未登录,是否立即登录?", {}, function() {
  198. layer.closeAll();
  199. return sa.$page.openLogin(cfg.login_url);
  200. });
  201. },
  202. errorfn: function(xhr, type, errorThrown) { // ajax发生异常时的默认处理函数
  203. if (xhr.status == 0) {
  204. return layer.alert('无法连接到服务器,请检查网络');
  205. }
  206. if(xhr.responseText.indexOf('code=405')>0){
  207. window.parent.location.href = '../../login.html';
  208. return;
  209. }
  210. return layer.alert("异常:" + JSON.stringify(xhr));
  211. },
  212. complete: function(xhr, ts) { // 成功失败都会执行
  213. }
  214. }
  215. // 将调用者的配置和默认配置合并
  216. cfg = sa.extendJson(cfg, defaultCfg);
  217. // 开始显示loading图标
  218. if (cfg.msg != null) {
  219. sa.loading(cfg.msg);
  220. }
  221. // 开始请求ajax
  222. return $.ajax({
  223. url: cfg.baseUrl + url,
  224. type: cfg.type,
  225. data: data,
  226. dataType: 'json',
  227. // xhrFields: {
  228. // withCredentials: true // 携带跨域cookie
  229. // },
  230. // crossDomain: true,
  231. beforeSend: function(xhr) {
  232. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  233. // 追加token
  234. if (localStorage.tokenName) {
  235. xhr.setRequestHeader(localStorage.tokenName, localStorage.tokenValue);
  236. }
  237. },
  238. success: function(res) {
  239. setTimeout(function() {
  240. sa.hideLoading();
  241. // 如果相应的处理函数存在
  242. if (cfg['success' + res.code] != undefined) {
  243. return cfg['success' + res.code](res);
  244. }
  245. layer.alert('未知状态码:' + JSON.stringify(res));
  246. }, cfg.sleep);
  247. },
  248. error: function(xhr, type, errorThrown) {
  249. setTimeout(function() {
  250. sa.hideLoading();
  251. return cfg.errorfn(xhr, type, errorThrown);
  252. }, cfg.sleep);
  253. },
  254. complete: cfg.complete
  255. });
  256. };
  257. // 模拟一个ajax
  258. // 请注意: 本模板中所有ajax请求调用的均为此模拟函数
  259. sa.ajax2 = function(url, data, success200, cfg) {
  260. // 如果是简写模式(省略了data参数)
  261. if (typeof data === 'function') {
  262. cfg = success200;
  263. success200 = data;
  264. data = {};
  265. }
  266. // 几个默认配置
  267. cfg = cfg || {};
  268. cfg.baseUrl = (url.indexOf('http') === 0 ? '' : sa.cfg.api_url); // 父url,拼接在url前面
  269. // 设定一个默认的提示文字
  270. if (cfg.msg == undefined || cfg.msg == null || cfg.msg == '') {
  271. cfg.msg = '正在努力加载...';
  272. }
  273. // 默认延时函数
  274. if (cfg.sleep == undefined || cfg.sleep == null || cfg.sleep == '' || cfg.sleep == 0) {
  275. cfg.sleep = 600;
  276. }
  277. // 默认的模拟数据
  278. cfg.res = cfg.res || {
  279. code: 200,
  280. msg: 'ok',
  281. data: []
  282. }
  283. // 开始loding
  284. sa.loading(cfg.msg);
  285. // 模拟ajax的延时
  286. setTimeout(function() {
  287. sa.hideLoading(); // 隐藏掉转圈圈
  288. success200(cfg.res);
  289. }, cfg.sleep)
  290. };
  291. })();
  292. // =========================== 封装弹窗相关函数 =======================================
  293. (function() {
  294. var me = sa;
  295. if (window.layer) {
  296. layer.ready(function() {});
  297. }
  298. // tips提示文字
  299. me.msg = function(msg, cfg) {
  300. msg = msg || '操作成功';
  301. layer.msg(msg, cfg);
  302. };
  303. // 操作成功的提示
  304. me.ok = function(msg) {
  305. msg = msg || '操作成功';
  306. layer.msg(msg, {
  307. anim: 0,
  308. icon: 1
  309. });
  310. }
  311. me.ok2 = function(msg) {
  312. msg = msg || '操作成功';
  313. layer.msg(msg, {
  314. anim: 0,
  315. icon: 6
  316. });
  317. }
  318. // 操作失败的提示
  319. me.error = function(msg) {
  320. msg = msg || '操作失败';
  321. layer.msg(msg, {
  322. anim: 6,
  323. icon: 2
  324. });
  325. }
  326. me.error2 = function(msg) {
  327. msg = msg || '操作失败';
  328. layer.msg(msg, {
  329. anim: 6,
  330. icon: 5
  331. });
  332. }
  333. // alert弹窗 [text=提示文字, okFn=点击确定之后的回调函数]
  334. me.alert = function(text, okFn) {
  335. // 开始弹窗
  336. layer.alert(text, function(index) {
  337. layer.close(index);
  338. if (okFn) {
  339. okFn();
  340. }
  341. });
  342. };
  343. // 询问框 [text=提示文字, okFn=点击确定之后的回调函数]
  344. me.confirm = function(text, okFn) {
  345. layer.confirm(text, {}, function(index) {
  346. layer.close(index);
  347. if (okFn) {
  348. okFn();
  349. }
  350. }.bind(this));
  351. };
  352. // 输入框 [title=提示文字, okFn=点击确定后的回调函数, formType=输入框类型(0=文本,1=密码,2=多行文本域) 可省略, value=默认值 可省略 ]
  353. me.prompt = function(title, okFn, formType, value) {
  354. layer.prompt({
  355. title: title,
  356. formType: formType,
  357. value: value
  358. }, function(pass, index) {
  359. layer.close(index);
  360. if (okFn) {
  361. okFn(pass);
  362. }
  363. });
  364. }
  365. // 打开loading
  366. me.loading = function(msg) {
  367. layer.closeAll(); // 开始前先把所有弹窗关了
  368. return layer.msg(msg, {
  369. icon: 16,
  370. shade: 0.3,
  371. time: 1000 * 20,
  372. skin: 'ajax-layer-load'
  373. });
  374. };
  375. // 隐藏loading
  376. me.hideLoading = function() {
  377. layer.closeAll();
  378. };
  379. // ============== 一些常用弹窗 =====================
  380. // 大窗显示一个图片
  381. // 参数: src=地址、w=宽度(默认80%)、h=高度(默认80%)
  382. me.showImage = function(src, w, h) {
  383. w = w || '80%';
  384. h = h || '80%';
  385. var content = '<div style="height: 100%; overflow: hidden !important;">' +
  386. '<img src="' + src + ' " style="width: 100%; height: 100%;" />' +
  387. '</div>';
  388. layer.open({
  389. type: 1,
  390. title: false,
  391. shadeClose: true,
  392. closeBtn: 0,
  393. area: [w, h], //宽高
  394. content: content
  395. });
  396. }
  397. // 预览一组图片
  398. // srcList=图片路径数组(可以是json样,也可以是逗号切割式), index=打开立即显示哪张(可填下标, 也可填写src路径)
  399. me.showImageList = function(srcList, index) {
  400. // 如果填的是个string
  401. srcList = srcList || [];
  402. if (typeof srcList === 'string') {
  403. try {
  404. srcList = JSON.parse(srcList);
  405. } catch (e) {
  406. try {
  407. srcList = srcList.split(','); // 尝试字符串切割
  408. } catch (e) {
  409. srcList = [];
  410. }
  411. }
  412. }
  413. // 如果填的是路径
  414. index = index || 0;
  415. if (typeof index === 'string') {
  416. index = srcList.indexOf(index);
  417. index = (index == -1 ? 0 : index);
  418. }
  419. // 开始展示
  420. var arr_list = [];
  421. srcList.forEach(function(item) {
  422. arr_list.push({
  423. alt: '左右键切换',
  424. pid: 1,
  425. src: item,
  426. thumb: item
  427. })
  428. })
  429. layer.photos({
  430. photos: {
  431. title: '',
  432. id: new Date().getTime(),
  433. start: index,
  434. data: arr_list
  435. },
  436. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  437. });
  438. }
  439. me.showIframeNoClose = function(title, url, w, h, shadeClose) {
  440. // 参数修正
  441. w = w || '95%';
  442. h = h || '95%';
  443. shadeClose = (shadeClose === undefined ? false : shadeClose);
  444. // 弹出面板
  445. var index = layer.open({
  446. type: 2,
  447. closeBtn:0,
  448. title: title, // 标题
  449. shadeClose: shadeClose, // 是否点击遮罩关闭
  450. maxmin: true, // 显示最大化按钮
  451. shade: 0.8, // 遮罩透明度
  452. scrollbar: false, // 屏蔽掉外层的滚动条
  453. moveOut: true, // 是否可拖动到外面
  454. area: [w, h], // 大小
  455. content: url, // 传值
  456. // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
  457. resizing: function(layero) {
  458. solveLayerBug(index);
  459. }
  460. });
  461. // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
  462. $('#layui-layer' + index + ' .layui-layer-max').click(function() {
  463. setTimeout(function() {
  464. solveLayerBug(index);
  465. }, 200)
  466. })
  467. }
  468. // 显示一个iframe
  469. // 参数: 标题,地址,宽,高 , 点击遮罩是否关闭, 默认false
  470. me.showIframe = function(title, url, w, h, shadeClose) {
  471. // 参数修正
  472. w = w || '95%';
  473. h = h || '95%';
  474. shadeClose = (shadeClose === undefined ? false : shadeClose);
  475. // 弹出面板
  476. var index = layer.open({
  477. type: 2,
  478. title: title, // 标题
  479. shadeClose: shadeClose, // 是否点击遮罩关闭
  480. maxmin: true, // 显示最大化按钮
  481. shade: 0.8, // 遮罩透明度
  482. scrollbar: false, // 屏蔽掉外层的滚动条
  483. moveOut: true, // 是否可拖动到外面
  484. area: [w, h], // 大小
  485. content: url, // 传值
  486. // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
  487. resizing: function(layero) {
  488. solveLayerBug(index);
  489. }
  490. });
  491. // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
  492. $('#layui-layer' + index + ' .layui-layer-max').click(function() {
  493. setTimeout(function() {
  494. solveLayerBug(index);
  495. }, 200)
  496. })
  497. }
  498. me.showView = me.showIframe;
  499. // 显示一个iframe, 底部按钮方式
  500. // 参数: 标题,地址,点击确定按钮执行的代码(在子窗口执行),宽,高
  501. me.showIframe2 = function(title, url, evalStr, w, h) {
  502. // 参数修正
  503. w = w || '95%';
  504. h = h || '95%';
  505. // 弹出面板
  506. var index = layer.open({
  507. type: 2,
  508. title: title, // 标题
  509. closeBtn: (title ? 1 : 0), // 是否显示关闭按钮
  510. btn: ['确定', '取消'],
  511. shadeClose: false, // 是否点击遮罩关闭
  512. maxmin: true, // 显示最大化按钮
  513. shade: 0.8, // 遮罩透明度
  514. scrollbar: false, // 屏蔽掉外层的滚动条
  515. moveOut: true, // 是否可拖动到外面
  516. area: [w, h], // 大小
  517. content: url, // 传值
  518. // 解决拉伸或者最大化的时候,iframe高度不能自适应的问题
  519. resizing: function(layero) {
  520. },
  521. yes: function(index, layero) {
  522. var iframe = document.getElementById('layui-layer-iframe' + index);
  523. var iframeWindow = iframe.contentWindow;
  524. iframeWindow.eval(evalStr);
  525. }
  526. });
  527. }
  528. // 当前iframe关闭自身 (在iframe中调用)
  529. me.closeCurrIframe = function() {
  530. try {
  531. var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
  532. parent.layer.close(index); //再执行关闭
  533. } catch (e) {
  534. //TODO handle the exception
  535. }
  536. }
  537. me.closeCurrView = me.closeCurrIframe;
  538. //执行一个函数, 解决layer拉伸或者最大化的时候,iframe高度不能自适应的问题
  539. function solveLayerBug(index) {
  540. var selected = '#layui-layer' + index;
  541. var height = $(selected).height();
  542. var title_height = $(selected).find('.layui-layer-title').height();
  543. $(selected).find('iframe').css('height', (height - title_height) + 'px');
  544. }
  545. // 监听回车事件,达到回车关闭弹窗的效果
  546. if (window.$) {
  547. $(document).on('keydown', function() {
  548. if (event.keyCode === 13 && $(".layui-layer-btn0").length == 1 && !window.is_not_watch_enter &&
  549. $(this).find('.layui-layer-input').length == 0) {
  550. $(".layui-layer-btn0").click();
  551. return false;
  552. }
  553. });
  554. }
  555. })();
  556. // =========================== 常用util函数封装 =======================================
  557. (function() {
  558. // 超级对象
  559. var me = sa;
  560. // =========================== 常用util函数封装 =======================================
  561. if (true) {
  562. // 从url中查询到指定参数值
  563. me.p = function(name, defaultValue) {
  564. var query = window.location.search.substring(1);
  565. var vars = query.split("&");
  566. for (var i = 0; i < vars.length; i++) {
  567. var pair = vars[i].split("=");
  568. if (pair[0] == name) {
  569. return pair[1];
  570. }
  571. }
  572. return (defaultValue == undefined ? null : defaultValue);
  573. }
  574. me.q = function(name, defaultValue) {
  575. var query = window.location.search.substring(1);
  576. var vars = query.split("&");
  577. for (var i = 0; i < vars.length; i++) {
  578. var pair = vars[i].split("=");
  579. if (pair[0] == name) {
  580. return pair[1];
  581. }
  582. }
  583. return (defaultValue == undefined ? null : defaultValue);
  584. }
  585. // 判断一个变量是否为null
  586. // 返回true或false,如果return_obj有值,则在true的情况下返回return_obj
  587. me.isNull = function(obj, return_obj) {
  588. var flag = [null, undefined, '', 'null', 'undefined'].indexOf(obj) != -1;
  589. if (return_obj === undefined) {
  590. return flag;
  591. } else {
  592. if (flag) {
  593. return return_obj;
  594. } else {
  595. return obj;
  596. }
  597. }
  598. }
  599. // 将时间戳转化为指定时间
  600. // way:方式(1=年月日,2=年月日时分秒)默认1, 也可以指定格式:yyyy-MM-dd HH:mm:ss
  601. me.forDate = function(inputTime, way) {
  602. if (me.isNull(inputTime) == true) {
  603. return "";
  604. }
  605. var date = new Date(inputTime);
  606. var y = date.getFullYear();
  607. var m = date.getMonth() + 1;
  608. m = m < 10 ? ('0' + m) : m;
  609. var d = date.getDate();
  610. d = d < 10 ? ('0' + d) : d;
  611. var h = date.getHours();
  612. h = h < 10 ? ('0' + h) : h;
  613. var minute = date.getMinutes();
  614. var second = date.getSeconds();
  615. minute = minute < 10 ? ('0' + minute) : minute;
  616. second = second < 10 ? ('0' + second) : second;
  617. var ms = date.getMilliseconds();
  618. way = way || 1;
  619. // way == 1 年月日
  620. if (way === 1) {
  621. return y + '-' + m + '-' + d;
  622. }
  623. // way == 1 年月日时分秒
  624. if (way === 2) {
  625. return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  626. }
  627. // way == 具体格式 标准格式: yyyy-MM-dd HH:mm:ss
  628. if (typeof way == 'string') {
  629. return way.replace("yyyy", y).replace("MM", m).replace("dd", d).replace("HH", h).replace("mm",
  630. minute).replace("ss", second).replace("ms", ms);
  631. }
  632. return y + '-' + m + '-' + d;
  633. };
  634. // 时间日期
  635. me.forDatetime = function(inputTime) {
  636. return me.forDate(inputTime, 2);
  637. }
  638. // 将时间转化为 个性化 如:3小时前,
  639. // d1 之于 d2 ,d2不填则默认取当前时间
  640. me.forDate2 = function(d, d2) {
  641. var hou = "前";
  642. if (d == null || d == '') {
  643. return '';
  644. }
  645. if (d2 == null || d2 == '') {
  646. d2 = new Date();
  647. }
  648. d2 = new Date(d2).getTime();
  649. var timestamp = new Date(d).getTime() - 1000;
  650. var mistiming = Math.round((d2 - timestamp) / 1000);
  651. if (mistiming < 0) {
  652. mistiming = 0 - mistiming;
  653. hou = '后'
  654. }
  655. var arrr = ['年', '月', '周', '天', '小时', '分钟', '秒'];
  656. var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
  657. for (var i = 0; i < arrn.length; i++) {
  658. var inm = Math.floor(mistiming / arrn[i]);
  659. if (inm != 0) {
  660. return inm + arrr[i] + hou;
  661. }
  662. }
  663. }
  664. // 综合以上两种方式,进行格式化
  665. // 小于24小时的走forDate2,否则forDat
  666. me.forDate3 = function(d, way) {
  667. if (d == null || d == '') {
  668. return '';
  669. }
  670. var cha = new Date().getTime() - new Date(d).getTime();
  671. cha = (cha > 0 ? cha : 0 - cha);
  672. if (cha < (86400 * 1000)) {
  673. return me.forDate2(d);
  674. }
  675. return me.forDate(d, way);
  676. }
  677. // 返回时间差, 此格式数组:[x, x, x, 天, 时, 分, 秒]
  678. me.getSJC = function(small_time, big_time) {
  679. var date1 = new Date(small_time); //开始时间
  680. var date2 = new Date(big_time); //结束时间
  681. var date3 = date2.getTime() - date1.getTime(); //时间差秒
  682. //计算出相差天数
  683. var days = Math.floor(date3 / (24 * 3600 * 1000));
  684. //计算出小时数
  685. var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  686. var hours = Math.floor(leave1 / (3600 * 1000));
  687. //计算相差分钟数
  688. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  689. var minutes = Math.floor(leave2 / (60 * 1000));
  690. //计算相差秒数
  691. var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  692. var seconds = Math.round(leave3 / 1000);
  693. // 返回数组
  694. return [0, 0, 0, days, hours, minutes, seconds];
  695. }
  696. // 将日期,加上指定天数
  697. me.dateAdd = function(d, n) {
  698. var s = new Date(d).getTime();
  699. s += 86400000 * n;
  700. return new Date(s);
  701. }
  702. // 转化json,出错返回默认值
  703. me.JSONParse = function(obj, default_obj) {
  704. try {
  705. return JSON.parse(obj) || default_obj;
  706. } catch (e) {
  707. return default_obj || {};
  708. }
  709. }
  710. // 截取指定长度字符,默认50
  711. me.maxLength = function(str, length) {
  712. length = length || 50;
  713. if (!str) {
  714. return "";
  715. }
  716. return (str.length > length) ? str.substr(0, length) + ' ...' : str;
  717. },
  718. // 过滤掉标签
  719. me.text = function(str) {
  720. if (!str) {
  721. return "";
  722. }
  723. return str.replace(/<[^>]+>/g, "");
  724. }
  725. // 为指定集合的每一项元素添加上is_update属性
  726. me.listAU = function(list) {
  727. list.forEach(function(ts) {
  728. ts.is_update = false;
  729. })
  730. return list;
  731. }
  732. // 获得一段文字中所有图片的路径
  733. me.getSrcList = function(str) {
  734. try {
  735. var imgReg = /<img.*?(?:>|\/>)/gi; //匹配图片(g表示匹配所有结果i表示区分大小写)
  736. var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配src属性
  737. var arr = str.match(imgReg); // 图片数组
  738. var srcList = [];
  739. for (var i = 0; i < arr.length; i++) {
  740. var src = arr[i].match(srcReg);
  741. srcList.push(src[1]);
  742. }
  743. return srcList;
  744. } catch (e) {
  745. return [];
  746. }
  747. }
  748. // 无精度损失的乘法
  749. me.accMul = function(arg1, arg2) {
  750. var m = 0,
  751. s1 = arg1.toString(),
  752. s2 = arg2.toString(),
  753. t;
  754. t = s1.split(".");
  755. // 判断有没有小数位,避免出错
  756. if (t[1]) {
  757. m += t[1].length
  758. }
  759. t = s2.split(".");
  760. if (t[1]) {
  761. m += t[1].length
  762. }
  763. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
  764. }
  765. // 正则验证是否为手机号
  766. me.isPhone = function(str) {
  767. str = str + '';
  768. if ((/^1[3456789]\d{9}$/.test(str))) {
  769. return true;
  770. }
  771. return false;
  772. }
  773. me.isTel = function(str) {
  774. str = str + '';
  775. if ((/^1[345678]\d{9}$/.test(str)) || (/0\d{2,3}-\d{7,8}/.test(str))) {
  776. return true;
  777. }
  778. return false;
  779. }
  780. me.isCarNo = function(str) {
  781. str = str + '';
  782. let reg =
  783. /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][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挂学警港澳使领]))$/
  784. if (reg.test(str)) {
  785. return true;
  786. }
  787. return false;
  788. }
  789. me.isNum = function(str) {
  790. str = str + '';
  791. if ((/^[0-9]*$/.test(str)) || (/^[0-9]+(.[0-9]{1})?$/.test(str))) {
  792. return true;
  793. }
  794. return false;
  795. }
  796. me.isEmail = function(str) {
  797. str = str + '';
  798. if ((/^[A-Za-z0-9_-\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(str))) {
  799. return true;
  800. }
  801. return false;
  802. }
  803. me.isTaxId = function(str) {
  804. str = str + '';
  805. if ((/^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/.test(str))) {
  806. return true;
  807. }
  808. return false;
  809. }
  810. // 产生随机字符串
  811. me.randomString = function(len) {
  812. len = len || 32;
  813. var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  814. /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  815. var maxPos = $chars.length;
  816. var str = '';
  817. for (i = 0; i < len; i++) {
  818. str += $chars.charAt(Math.floor(Math.random() * maxPos));
  819. }
  820. return str;
  821. }
  822. // 刷新页面
  823. me.f5 = function() {
  824. location.reload();
  825. }
  826. // 动态加载js
  827. me.loadJS = function(src, onload) {
  828. var script = document.createElement("script");
  829. script.setAttribute("type", "text/javascript");
  830. script.src = src;
  831. script.onload = onload;
  832. document.body.appendChild(script);
  833. }
  834. // 产生随机数字
  835. me.randomNum = function(min, max) {
  836. return parseInt(Math.random() * (max - min + 1) + min, 10);
  837. }
  838. // 打开页面
  839. me.open = function(url) {
  840. window.open(url);
  841. }
  842. // == if 结束
  843. }
  844. // =========================== 数组操作 =======================================
  845. if (true) {
  846. // 从数组里获取数据,根据指定数据
  847. me.getArrayField = function(arr, prop) {
  848. var propArr = [];
  849. for (var i = 0; i < arr.length; i++) {
  850. propArr.push(arr[i][prop]);
  851. }
  852. return propArr;
  853. }
  854. // 从数组里获取数据,根据指定数据
  855. me.arrayGet = function(arr, prop, value) {
  856. for (var i = 0; i < arr.length; i++) {
  857. if (arr[i][prop] == value) {
  858. return arr[i];
  859. }
  860. }
  861. return null;
  862. }
  863. // 正则验证是否为手机号
  864. me.isPhone = function(str) {
  865. str = str + '';
  866. if ((/^1[3456789]\d{9}$/.test(str))) {
  867. return true;
  868. }
  869. return false;
  870. }
  871. // 从数组删除指定记录
  872. me.arrayDelete = function(arr, item) {
  873. if (item instanceof Array) {
  874. for (let i = 0; i < item.length; i++) {
  875. let ite = item[i];
  876. let index = arr.indexOf(ite);
  877. if (index > -1) {
  878. arr.splice(index, 1);
  879. }
  880. }
  881. } else {
  882. var index = arr.indexOf(item);
  883. if (index > -1) {
  884. arr.splice(index, 1);
  885. }
  886. }
  887. }
  888. // 从数组删除指定id的记录
  889. me.arrayDeleteById = function(arr, id) {
  890. var item = me.arrayGet(arr, 'id', id);
  891. me.arrayDelete(arr, item);
  892. }
  893. // 将数组B添加到数组A的开头
  894. me.unshiftArray = function(arrA, arrB) {
  895. if (arrB) {
  896. arrB.reverse().forEach(function(ts) {
  897. arrA.unshift(ts);
  898. })
  899. }
  900. return arrA;
  901. }
  902. // 将数组B添加到数组A的末尾
  903. me.pushArray = function(arrA, arrB) {
  904. if (arrB) {
  905. arrB.forEach(function(ts) {
  906. arrA.push(ts);
  907. })
  908. }
  909. return arrA;
  910. }
  911. // == if 结束
  912. }
  913. // =========================== 浏览器相关 =======================================
  914. if (true) {
  915. // set cookie 值
  916. me.setCookie = function setCookie(cname, cvalue, exdays) {
  917. exdays = exdays || 30;
  918. var d = new Date();
  919. d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  920. var expires = "expires=" + d.toGMTString();
  921. document.cookie = cname + "=" + escape(cvalue) + "; " + expires + "; path=/";
  922. }
  923. // get cookie 值
  924. me.getCookie = function(objName) {
  925. var arrStr = document.cookie.split("; ");
  926. for (var i = 0; i < arrStr.length; i++) {
  927. var temp = arrStr[i].split("=");
  928. if (temp[0] == objName) {
  929. return unescape(temp[1])
  930. };
  931. }
  932. return "";
  933. }
  934. // 复制指定文本
  935. me.copyText = function(str) {
  936. var oInput = document.createElement('input');
  937. oInput.value = str;
  938. document.body.appendChild(oInput);
  939. oInput.select(); // 选择对象
  940. document.execCommand("Copy"); // 执行浏览器复制命令
  941. oInput.className = 'oInput';
  942. oInput.style.display = 'none';
  943. }
  944. // jquery序列化表单增强版: 排除空值
  945. me.serializeNotNull = function(selected) {
  946. var serStr = $(selected).serialize();
  947. return serStr.split("&").filter(function(str) {
  948. return !str.endsWith("=")
  949. }).join("&");
  950. }
  951. // 将cookie序列化为k=v形式
  952. me.strCookie = function() {
  953. return document.cookie.replace(/; /g, "&");
  954. }
  955. // 回到顶部
  956. me.goTop = function() {
  957. function smoothscroll() {
  958. var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
  959. if (currentScroll > 0) {
  960. window.requestAnimationFrame(smoothscroll);
  961. window.scrollTo(0, currentScroll - (currentScroll / 5));
  962. }
  963. };
  964. smoothscroll();
  965. }
  966. // == if 结束
  967. }
  968. // =========================== javascript对象操作 =======================================
  969. if (true) {
  970. // 去除json对象中的空值
  971. me.removeNull = function(obj) {
  972. var newObj = {};
  973. if (obj != undefined && obj != null) {
  974. for (var key in obj) {
  975. if (obj[key] === undefined || obj[key] === null || obj[key] === '') {
  976. //
  977. } else {
  978. newObj[key] = obj[key];
  979. }
  980. }
  981. }
  982. return newObj;
  983. }
  984. // JSON 浅拷贝, 返回拷贝后的obj
  985. me.copyJSON = function(obj) {
  986. if (obj === null || obj === undefined) {
  987. return obj;
  988. };
  989. var new_obj = {};
  990. for (var key in obj) {
  991. new_obj[key] = obj[key];
  992. }
  993. return new_obj;
  994. }
  995. // json合并, 将 defaulet配置项 转移到 user配置项里 并返回 user配置项
  996. me.extendJson = function(userOption, defaultOption) {
  997. if (!userOption) {
  998. return defaultOption;
  999. };
  1000. for (var key in defaultOption) {
  1001. if (userOption[key] === undefined) {
  1002. userOption[key] = defaultOption[key];
  1003. } else if (userOption[key] == null) {
  1004. } else if (typeof userOption[key] == "object") {
  1005. me.extendJson(userOption[key], defaultOption[key]); //深度匹配
  1006. }
  1007. }
  1008. return userOption;
  1009. }
  1010. // == if 结束
  1011. }
  1012. // =========================== 本地集合存储 =======================================
  1013. if (true) {
  1014. // 获取指定key的list
  1015. me.keyListGet = function(key) {
  1016. try {
  1017. var str = localStorage.getItem('LIST_' + key);
  1018. if (str == undefined || str == null || str == '' || str == 'undefined' || typeof(JSON.parse(
  1019. str)) == 'string') {
  1020. //alert('key' + str);
  1021. str = '[]';
  1022. }
  1023. return JSON.parse(str);
  1024. } catch (e) {
  1025. return [];
  1026. }
  1027. },
  1028. me.keyListSet = function(key, list) {
  1029. localStorage.setItem('LIST_' + key, JSON.stringify(list));
  1030. },
  1031. me.keyListHas = function(key, item) {
  1032. var arr2 = me.keyListGet(key);
  1033. return arr2.indexOf(item) != -1;
  1034. },
  1035. me.keyListAdd = function(key, item) {
  1036. var arr = me.keyListGet(key);
  1037. arr.push(item);
  1038. me.keyListSet(key, arr);
  1039. },
  1040. me.keyListRemove = function(key, item) {
  1041. var arr = me.keyListGet(key);
  1042. var index = arr.indexOf(item);
  1043. if (index > -1) {
  1044. arr.splice(index, 1);
  1045. }
  1046. me.keyListSet(key, arr);
  1047. }
  1048. // == if 结束
  1049. }
  1050. // =========================== 对sa-admin的优化 =======================================
  1051. if (true) {
  1052. // 封装element-ui的导出表格
  1053. // 参数:选择器(默认.data-count),fileName=导出的文件名称
  1054. me.exportExcel = function(select, fileName) {
  1055. // 声明函数
  1056. let exportExcel_fn = function(select, fileName) {
  1057. // 赋默认值
  1058. select = select || '.data-table';
  1059. fileName = fileName || 'table.xlsx';
  1060. // 开始导出
  1061. let wb = XLSX.utils.table_to_book(document.querySelector(select)); // 这里就是表格
  1062. let sheet = wb.Sheets.Sheet1; // 单元表
  1063. try {
  1064. // 强改宽度
  1065. sheet['!cols'] = sheet['!cols'] || [];
  1066. let thList = document.querySelector(select).querySelectorAll(
  1067. '.el-table__header-wrapper tr th');
  1068. for (var i = 0; i < thList.length; i++) {
  1069. // 如果是多选框
  1070. if (thList[i].querySelector('.el-checkbox')) {
  1071. sheet['!cols'].push({
  1072. wch: 5
  1073. }); // 强改宽度
  1074. continue;
  1075. }
  1076. sheet['!cols'].push({
  1077. wch: 15
  1078. }); // 强改宽度
  1079. }
  1080. // 强改高度
  1081. sheet['!rows'] = sheet['!rows'] || [];
  1082. let trList = document.querySelector(select).querySelectorAll(
  1083. '.el-table__body-wrapper tbody tr');
  1084. for (var i = 0; i < trList.length + 1; i++) {
  1085. sheet['!rows'].push({
  1086. hpx: 20
  1087. }); // 强改高度
  1088. }
  1089. } catch (e) {
  1090. console.err(e);
  1091. }
  1092. // 开始制作并输出
  1093. let wbout = XLSX.write(wb, {
  1094. bookType: 'xlsx',
  1095. bookSST: true,
  1096. type: 'array'
  1097. });
  1098. // 点击
  1099. let blob = new Blob([wbout], {
  1100. type: 'application/octet-stream'
  1101. });
  1102. const a = document.createElement("a")
  1103. a.href = URL.createObjectURL(blob)
  1104. a.download = fileName // 这里填保存成的文件名
  1105. a.click()
  1106. URL.revokeObjectURL(a.href)
  1107. a.remove();
  1108. sa.hideLoading();
  1109. }
  1110. sa.loading('正在导出...');
  1111. // 判断是否首次加载
  1112. if (window.XLSX) {
  1113. return exportExcel_fn(select, fileName);
  1114. } else {
  1115. me.loadJS('https://unpkg.com/xlsx@0.16.6/dist/xlsx.core.min.js', function() {
  1116. return exportExcel_fn(select, fileName);
  1117. });
  1118. }
  1119. }
  1120. // 刷新表格高度, 请务必在所有表格高度发生变化的地方调用此方法
  1121. me.f5TableHeight = function() {
  1122. Vue.nextTick(function() {
  1123. if ($('.el-table.data-table .el-table__body-wrapper table').length == 0) {
  1124. return;
  1125. }
  1126. var _f5Height = function() {
  1127. var height = $('.el-table .el-table__body-wrapper table').height();
  1128. height = height == 0 ? 60 : height;
  1129. // 判断是否有滚动条
  1130. var tw = $('.el-table .el-table__body-wrapper').get(0);
  1131. if (tw.scrollWidth > tw.clientWidth) {
  1132. height = height + 16;
  1133. }
  1134. if ($('.el-table .el-table__body-wrapper table td').width() == 0) {
  1135. return;
  1136. }
  1137. // 设置高度
  1138. $('.el-table .el-table__body-wrapper').css('min-height', height);
  1139. $('.el-table .el-table__body-wrapper').css('max-height', height);
  1140. };
  1141. setTimeout(_f5Height, 0)
  1142. setTimeout(_f5Height, 200)
  1143. })
  1144. }
  1145. // 在表格查询的页面,监听input回车事件,提交查询
  1146. me.onInputEnter = function(app) {
  1147. Vue.nextTick(function() {
  1148. app = app || window.app;
  1149. // document.querySelectorAll('.el-form input').forEach(function(item) {
  1150. // item.onkeydown = function(e) {
  1151. // var theEvent = e || window.event;
  1152. // var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
  1153. // if (code == 13) {
  1154. // app.p.pageNo = 1;
  1155. // app.f5();
  1156. // }
  1157. // }
  1158. // })
  1159. document.querySelectorAll('.el-form').forEach(function(item) {
  1160. item.onkeydown = function(e) {
  1161. var theEvent = e || window.event;
  1162. var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
  1163. if (code == 13) {
  1164. var target = e.target || e.srcElement;
  1165. if (target.tagName.toLowerCase() == "input") {
  1166. app.p.pageNo = 1;
  1167. app.f5();
  1168. }
  1169. }
  1170. }
  1171. })
  1172. })
  1173. }
  1174. // 如果value为true,则抛出异常
  1175. me.check = function(value, errorMsg) {
  1176. if (value === true) {
  1177. throw {
  1178. type: 'sa-error',
  1179. msg: errorMsg
  1180. };
  1181. }
  1182. }
  1183. // 如果value为null,则抛出异常
  1184. me.checkNull = function(value, errorMsg) {
  1185. if (me.isNull(value)) {
  1186. throw {
  1187. type: 'sa-error',
  1188. msg: errorMsg
  1189. };
  1190. }
  1191. }
  1192. // 监听窗口变动
  1193. if (!window.onresize) {
  1194. window.onresize = function() {
  1195. try {
  1196. me.f5TableHeight();
  1197. } catch (e) {
  1198. // console.log(e);
  1199. }
  1200. }
  1201. }
  1202. // 双击layer标题处全屏
  1203. if (window.$) {
  1204. $(document).on('mousedown', '.layui-layer-title', function(e) {
  1205. // console.log('单击中');
  1206. if (window.layer_title_last_click_time) {
  1207. var cz = new Date().getTime() - window.layer_title_last_click_time;
  1208. if (cz < 250) {
  1209. $(this).parent().find('.layui-layer-max').click();
  1210. }
  1211. }
  1212. window.layer_title_last_click_time = new Date().getTime();
  1213. })
  1214. }
  1215. // == if 结束
  1216. }
  1217. })();
  1218. // =========================== $sys 有关当前系统的方法 一般不能复制到别的项目中用 =======================================
  1219. (function() {
  1220. // 超级对象
  1221. var me = {};
  1222. sa.$sys = me;
  1223. // ======================= 登录相关 ============================
  1224. // 写入当前已登陆用户信息
  1225. me.setCurrUser = function(currUser) {
  1226. localStorage.setItem('currUser', JSON.stringify(currUser));
  1227. }
  1228. // 获得当前已登陆用户信息
  1229. me.getCurrUser = function() {
  1230. var user = localStorage.getItem("currUser");
  1231. if (user == undefined || user == null || user == 'null' || user == '' || user == '{}' || user.length <
  1232. 10) {
  1233. user = {
  1234. id: '0',
  1235. username: '未登录'
  1236. }
  1237. } else {
  1238. user = JSON.parse(user);
  1239. }
  1240. return user;
  1241. }
  1242. // 如果未登录,则强制跳转到登录
  1243. me.checkLogin = function(not_login_url) {
  1244. if (me.getCurrUser().id == 0) {
  1245. location.href = not_login_url || '../../login.html';
  1246. throw '未登录,请先登录';
  1247. }
  1248. }
  1249. // 同上, 只不过是以弹窗的形式显示未登录
  1250. me.checkLoginTs = function(not_login_url) {
  1251. if (me.getCurrUser().id == 0) {
  1252. sa.$page.openLogin(not_login_url || '../../login.html');
  1253. throw '未登录,请先登录';
  1254. }
  1255. }
  1256. // ========================= 权限验证 =========================
  1257. // 定义key
  1258. var pcode_key = 'permission_code';
  1259. // 写入当前会话的权限码集合
  1260. sa.setAuth = function(codeList) {
  1261. sa.keyListSet(pcode_key, codeList);
  1262. }
  1263. // 清除当前会话的权限码集合
  1264. sa.clearAuth = function() {
  1265. sa.keyListSet(pcode_key, []);
  1266. }
  1267. // 检查当前会话是否拥有一个权限码, 返回true和false
  1268. sa.isAuth = function(pcode) {
  1269. return sa.keyListHas(pcode_key, pcode);
  1270. }
  1271. // 检查当前会话是否拥有一个权限码, 如果没有, 则跳转到无权限页面
  1272. // 注意: 非二级目录页面请注意调整路径问题
  1273. sa.checkAuth = function(pcode, not_pcode_url) {
  1274. var is_have = sa.keyListHas(pcode_key, pcode);
  1275. if (is_have == false) {
  1276. location.href = not_pcode_url || '../../sa-view/error-page/403.html';
  1277. throw '暂无权限: ' + pcode;
  1278. }
  1279. }
  1280. // 同上, 只不过是以弹窗的形式显示出来无权限来
  1281. sa.checkAuthTs = function(pcode, not_pcode_url) {
  1282. var is_have = sa.keyListHas(pcode_key, pcode);
  1283. if (is_have == false) {
  1284. var url = not_pcode_url || '../../sa-view/error-page/403.html';
  1285. layer.open({
  1286. type: 2,
  1287. title: false, // 标题
  1288. shadeClose: true, // 是否点击遮罩关闭
  1289. shade: 0.8, // 遮罩透明度
  1290. scrollbar: false, // 屏蔽掉外层的滚动条
  1291. closeBtn: false,
  1292. area: ['700px', '600px'], // 大小
  1293. content: url // 传值
  1294. });
  1295. throw '暂无权限: ' + pcode;
  1296. }
  1297. }
  1298. // ======================= 配置相关 ============================
  1299. // 写入配置信息
  1300. me.setAppCfg = function(cfg) {
  1301. if (typeof cfg != 'string') {
  1302. cfg = JSON.stringify(cfg);
  1303. }
  1304. localStorage.setItem('app_cfg', cfg);
  1305. }
  1306. // 获取配置信息
  1307. me.getAppCfg = function() {
  1308. var app_cfg = sa.JSONParse(localStorage.getItem('app_cfg'), {}) || {};
  1309. return app_cfg;
  1310. }
  1311. })();
  1312. // =========================== $page 跳页面相关 避免一次变动,到处乱改 =======================================
  1313. (function() {
  1314. // 超级对象
  1315. var me = {};
  1316. sa.$page = me;
  1317. // 打开登录页面
  1318. me.openLogin = function(login_url) {
  1319. layer.open({
  1320. type: 2,
  1321. // title: '登录',
  1322. title: false,
  1323. closeBtn: false,
  1324. shadeClose: true,
  1325. shade: 0.8,
  1326. // area: ['90%', '100%'],
  1327. area: ['70%', '80%'],
  1328. // area: ['450px', '360px'],
  1329. resize: false,
  1330. content: login_url || '../../login.html'
  1331. });
  1332. }
  1333. // 打开admin信息界面
  1334. me.openAdminInfo = function(id, username) {
  1335. var title = username + ' - 账号详情';
  1336. if (username === undefined) {
  1337. title = '账号详情';
  1338. }
  1339. sa.showIframe(title, '../sp-admin/admin-info.html?id=' + id, '700px', '600px');
  1340. }
  1341. })();
  1342. // 如果是sa_admin环境
  1343. window.sa_admin = window.sa_admin || parent.sa_admin || top.sa_admin;
  1344. window.saAdmin = window.sa_admin;
  1345. // 如果当前是Vue环境, 则挂在到 Vue 示例
  1346. if (window.Vue) {
  1347. // 全局的 sa 对象
  1348. Vue.prototype.sa = window.sa;
  1349. Vue.prototype.sa_admin = window.sa_admin;
  1350. Vue.prototype.saAdmin = window.saAdmin;
  1351. // 表单校验异常捕获
  1352. Vue.config.errorHandler = function(err, vm) {
  1353. if (err.type == 'sa-error') {
  1354. return sa.error(err.msg);
  1355. }
  1356. throw err;
  1357. }
  1358. // Element-UI 全局组件样式
  1359. Vue.prototype.$ELEMENT = {
  1360. size: 'mini',
  1361. zIndex: 3000
  1362. };
  1363. // 加载全局组件 (注意路径问题)
  1364. // if(window.httpVueLoader && window.loadComponent !== false) {
  1365. // Vue.component("sa-item", httpVueLoader('../../sa-frame/com/sa-item.vue'));
  1366. // Vue.component("sa-td", httpVueLoader('../../sa-frame/com/sa-td.vue'));
  1367. // }
  1368. }
  1369. // 对外开放, 在模块化时解开此注释
  1370. // export default sa;