sa.js 42 KB

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