common.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // 存储当前用户的权限
  2. let permsSet = JSON.parse(sessionStorage.getItem("permsSet"));
  3. //jqGrid的配置信息
  4. if ($.jgrid) {
  5. $.jgrid.defaults.width = 1000;
  6. $.jgrid.defaults.responsive = true;
  7. $.jgrid.defaults.styleUI = 'Bootstrap';
  8. }
  9. $.ajaxSetup({
  10. dataType: "json",
  11. cache: false
  12. });
  13. //iframe自适应
  14. $(window).on('resize', function () {
  15. var $content = $('#mainApp');
  16. $content.height($(this).height());
  17. $content.find('iframe').each(function () {
  18. $(this).height($content.height() - 150);
  19. });
  20. var $rrapp = $('#rrapp').parent();
  21. $rrapp.height($(this).height());
  22. $(this).height($content.height());
  23. }).resize();
  24. //重写alert
  25. window.alert = function (msg, callback) {
  26. // parent.layer.alert 弹出在iframe外的页面。
  27. layer.alert(msg, function (index) {
  28. layer.close(index);
  29. if (typeof(callback) === "function") {
  30. callback("ok");
  31. }
  32. });
  33. };
  34. //重写confirm式样框
  35. window.confirm = function (msg, callback) {
  36. layer.confirm(msg, {
  37. skin: 'layui-layer-molv', btn: ['确定', '取消']
  38. },
  39. function () {//确定事件
  40. if (typeof(callback) === "function") {
  41. callback("ok");
  42. }
  43. });
  44. };
  45. /**
  46. *
  47. * @param options
  48. */
  49. window.openWindow = function (options) {
  50. let globalParams = {
  51. skin: 'layui-layer-molv',//皮肤
  52. title: '标题',//标题
  53. type: 1,//打开窗口的类型 1:html里的div内容 2:iframe方式,页面的路径
  54. closeBtn: 1, //关闭按钮的形状 0、1
  55. anim: -1,
  56. isOutAnim: false,
  57. shadeClose: false,
  58. area: ['90%', '90%'],
  59. content: '',
  60. btn: false, //按钮
  61. top: false //窗口弹出是否在iframe上层
  62. };
  63. globalParams = $.extend(globalParams, options);
  64. if (globalParams.top) {
  65. parent.layer.open(globalParams);
  66. } else {
  67. layer.open(globalParams);
  68. }
  69. };
  70. //获取选中的数据
  71. function getSelectedRowData() {
  72. var id = getSelectedRow();
  73. return $("#jqGrid").jqGrid('getRowData', id);
  74. }
  75. //选择一条记录
  76. function getSelectedRow() {
  77. var grid = $("#jqGrid");
  78. var rowKey = grid.getGridParam("selrow");
  79. if (!rowKey) {
  80. iview.Message.error("请选择一条记录");
  81. return;
  82. }
  83. var selectedIDs = grid.getGridParam("selarrrow");
  84. if (selectedIDs.length > 1) {
  85. iview.Message.error("只能选择一条记录");
  86. return;
  87. }
  88. return selectedIDs[0];
  89. };
  90. //选择多条记录
  91. function getSelectedRows() {
  92. var grid = $("#jqGrid");
  93. var rowKey = grid.getGridParam("selrow");
  94. if (!rowKey) {
  95. iview.Message.error("请选择一条记录");
  96. return;
  97. }
  98. return grid.getGridParam("selarrrow");
  99. };
  100. //选择多条记录
  101. function getSelectedRowsSku() {
  102. var grid = $("#jqGrid");
  103. var rowKey = grid.getGridParam("selrow");
  104. if (!rowKey) {
  105. iview.Message.error("请选择一条记录");
  106. return;
  107. }
  108. return grid.getGridParam("selarrrow");
  109. };
  110. /**
  111. * 预览图片
  112. * @param url
  113. */
  114. function eyeImage(url) {
  115. if (!url) {
  116. iview.Message.error('请先上传图片');
  117. return;
  118. }
  119. layer.photos({
  120. photos: {
  121. "title": "预览", //相册标题
  122. "start": 0, //初始显示的图片序号,默认0
  123. "data": [ //相册包含的图片,数组格式
  124. {
  125. "src": url //原图地址
  126. }
  127. ]
  128. }, anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机
  129. });
  130. };
  131. function eyeVideo(url) {
  132. if (!url) {
  133. iview.Message.error('请先上传视频');
  134. return;
  135. }
  136. var loadstr='<video width="100%" height="100%" controls="controls" autobuffer="autobuffer" ' +
  137. 'autoplay="autoplay" loop="loop" style="position:fixed!important;top:0;left:0;"><source src="'+url+'" type="video/mp4"></source></video>';
  138. layer.open({
  139. type:1,
  140. title: "预览",
  141. area: ['730px', '460px'],
  142. shade: 0,
  143. closeBtn: 1,
  144. content: loadstr,
  145. });
  146. };
  147. /**
  148. * 预览图片
  149. * @param data
  150. */
  151. function eyeImages(data) {
  152. layer.photos({
  153. photos: {
  154. "title": "预览", //相册标题
  155. "start": 0, //初始显示的图片序号,默认0
  156. "data": data
  157. }, anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机
  158. });
  159. };
  160. /**
  161. * 重置验证
  162. * @param vue vue对象
  163. * @param name
  164. */
  165. function handleResetForm(vue, name) {
  166. vue.$refs[name].resetFields();
  167. };
  168. /**
  169. * 表单验证
  170. * @param vue vue对象
  171. * @param name 验证规则
  172. * @param callback 验证通过回调函数
  173. */
  174. function handleSubmitValidate(vue, name, callback) {
  175. vue.$refs[name].validate(function (valid) {
  176. if (valid) {
  177. callback();
  178. } else {
  179. iview.Message.error('请填写完整信息!');
  180. return false;
  181. }
  182. })
  183. };
  184. /**
  185. * 翻译日期
  186. * @param date
  187. * @param fmt
  188. * @returns {*}
  189. */
  190. function transDate(date, fmt) {
  191. if (!fmt) {
  192. fmt = 'yyyy-MM-dd';
  193. }
  194. if (date) {
  195. if (typeof date == 'number') {
  196. return new Date(date).dateFormat(fmt);
  197. }
  198. if (date instanceof Date) {
  199. return date.dateFormat(fmt);
  200. } else {
  201. try {
  202. return new Date(date.replace('-', '/').replace('-', '/')).dateFormat(fmt);
  203. } catch (e) {
  204. return '-';
  205. }
  206. }
  207. } else {
  208. return '-';
  209. }
  210. };
  211. /**
  212. * 翻译图片
  213. * @param url
  214. * @returns {*}
  215. */
  216. function transImg(url) {
  217. if (url) {
  218. return '<img width="50px" height="50px" src="' + url + '">';
  219. } else {
  220. return '-';
  221. }
  222. };
  223. function transImgBy100(url) {
  224. if (url) {
  225. return '<img width="100px" height="100px" src="' + url + '">';
  226. } else {
  227. return '-';
  228. }
  229. };
  230. function transImg30(url) {
  231. if (url) {
  232. return '<img width="40px" height="40px" src="' + url + '">';
  233. } else {
  234. return '-';
  235. }
  236. };
  237. /**
  238. * 翻译性别
  239. * @param gender
  240. * @returns {*}
  241. */
  242. function transGender(gender) {
  243. if (gender == 1) {
  244. return '男';
  245. }
  246. if (gender == 2) {
  247. return '女';
  248. }
  249. return '未知';
  250. };
  251. function transIsNot(value) {
  252. if (value == 1) {
  253. return '<span class="label label-success">是</span>';
  254. }
  255. return '<span class="label label-danger">否</span>';
  256. };
  257. function transStatus(value) {
  258. if (value == 1) {
  259. return '<span class="label label-success">有效</span>';
  260. }
  261. return '<span class="label label-danger">无效</span>';
  262. };
  263. function toUrl(href) {
  264. window.location.href = href;
  265. }
  266. function dialogLoading(flag) {
  267. if (flag) {
  268. top.layer.load(0, {
  269. shade: [0.5, '#fff'],
  270. time: 10000,
  271. content: '处理中...'
  272. });
  273. } else {
  274. top.layer.closeAll('loading');
  275. }
  276. }
  277. /**
  278. * 用JS获取地址栏参数的方法
  279. * 使用示例 location.href = http://localhost:8080/index.html?id=123
  280. * getQueryString('id') --> 123;
  281. * @param name
  282. * @returns {null}
  283. * @constructor
  284. */
  285. function getQueryString(name) {
  286. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  287. var r = window.location.search.substr(1).match(reg);
  288. if (r != null) {
  289. return unescape(r[2]);
  290. }
  291. return null;
  292. }
  293. /**
  294. * 主要功能:导出功能公共方法
  295. *
  296. * @param formId 表单ID,带'#'号,如'#formId'
  297. * @param url 请求后台地址
  298. * @param extraObj 往后台请求额外参数,对象格式,如:{'aaa': 111}
  299. */
  300. function exportFile(formId, url, extraObj) {
  301. window.confirm('确认要导出数据吗?', function () {
  302. var form = $('<form>'); //定义一个form表单
  303. form.attr('style', 'display: none');
  304. form.attr('target', '');
  305. form.attr('method', 'post');
  306. form.attr('action', url);
  307. var json = getJson(formId);
  308. if (typeof extraObj != 'undefined') {
  309. json = $.extend(json, extraObj);
  310. }
  311. $('body').append(form);//将表单放置在web中
  312. for (var i in json) {
  313. var input = $('<input>');
  314. input.attr('type', 'hidden');
  315. input.attr('name', i);
  316. input.attr('value', json[i]);
  317. form.append(input);
  318. }
  319. form.submit();//表单提交
  320. window.alert('操作成功,正在导出,请勿重复点击!');
  321. });
  322. }
  323. /**
  324. * 将form转化为json
  325. * @param form 传入 form表单的dom $("#baseFm")
  326. * @returns {___anonymous49_50} 序列化的键值对 {key:value,key2:value2,....}
  327. */
  328. function getJson(form) {
  329. var o = {};
  330. var $form = $(form).find('input,textarea,select');
  331. $.each($form, function (i, item) {
  332. var $this = $(item);
  333. if ($this.attr("type") == 'radio') {
  334. o[$this.attr("name")] = $("input[name='" + $this.attr("name") + "']:checked").val();
  335. return true;
  336. }
  337. o[$this.attr("name")] = $this.val();
  338. })
  339. return o;
  340. }
  341. /**
  342. *
  343. Ajax.request({
  344. url: '', //访问路径
  345. dataType: 'json', //访问类型 'json','html'等
  346. params: getJson(form),
  347. resultMsg: true, false, //是否需要提示信息
  348. type: 'GET',//,'get','post'
  349. beforeSubmit: function (data) {},//提交前处理
  350. successCallback: function (data) {} //提交后处理
  351. });
  352. */
  353. Ajax = function () {
  354. //var opt = { type:'GET',dataType:'json',resultMsg:true };
  355. function request(opt) {
  356. //添加遮罩层
  357. dialogLoading(true);
  358. if (typeof opt.cache == 'undefined') {
  359. opt.cache = false;
  360. }
  361. if (typeof opt == 'undefined') {
  362. return;
  363. }
  364. //opt = $.extend(opt, p);
  365. if (typeof(opt.type) == 'undefined') {
  366. opt.type = 'GET'
  367. }
  368. if (typeof(opt.async) == 'undefined') {
  369. opt.async = false;
  370. }
  371. if (typeof(opt.dataType) == 'undefined') {
  372. opt.dataType = 'json'
  373. }
  374. if (typeof(opt.contentType) == 'undefined') {
  375. opt.contentType = 'application/x-www-form-urlencoded;chartset=UTF-8'
  376. }
  377. if (typeof(opt.params) == 'undefined' || opt.params == null) {
  378. opt.params = {};
  379. }
  380. opt.params.date = new Date();
  381. if (typeof(opt.beforeSubmit) != 'undefined') {
  382. var flag = opt.beforeSubmit(opt);
  383. if (!flag) {
  384. return;
  385. }
  386. }
  387. if (typeof(opt.resultMsg) == 'undefined') {
  388. opt.resultMsg = true;
  389. }
  390. $.ajax({
  391. async: opt.async,
  392. url: opt.url,
  393. dataType: opt.dataType,
  394. contentType: opt.contentType,
  395. data: opt.params,
  396. crossDomain: opt.crossDomain || false,
  397. type: opt.type,
  398. cache: opt.cache,
  399. success: function (data) {
  400. if (typeof data == 'string' && data.indexOf("exception") > 0 || typeof data.code != 'undefined' && data.code != '0') {
  401. var result = {code: null};
  402. if (typeof data == 'string') {
  403. result = eval('(' + data + ')')
  404. } else if (typeof data == 'object') {
  405. result = data;
  406. }
  407. if (opt.resultMsg && result.msg) {
  408. layer.alert(result.msg, {icon: 5});
  409. }
  410. return;
  411. }
  412. if (opt.resultMsg && data.msg) {
  413. layer.alert(data.msg, {icon: 6}, function () {
  414. if (typeof(opt.successCallback) != 'undefined') {
  415. opt.successCallback(data);
  416. }
  417. });
  418. return;
  419. }
  420. if (typeof(opt.successCallback) != 'undefined') {
  421. opt.successCallback(data);
  422. }
  423. //关闭遮罩
  424. dialogLoading(false);
  425. },
  426. error: function () {
  427. layer.alert("此页面发生未知异常,请联系管理员", {icon: 5});
  428. //关闭遮罩
  429. dialogLoading(false);
  430. }
  431. });
  432. }
  433. return {
  434. /**
  435. * Ajax调用request
  436. */
  437. request: request
  438. };
  439. }();
  440. /**
  441. * 判断用户是否用权限
  442. * @param perm
  443. * @returns {boolean}
  444. */
  445. function hasPermission(perm) {
  446. if (null == permsSet || permsSet.length < 1) {
  447. return false;
  448. }
  449. for (let i = 0; i < permsSet.length; i++) {
  450. if (permsSet[i] == perm) {
  451. return true;
  452. }
  453. }
  454. return false;
  455. }
  456. function dateFormat(fmt, date) {
  457. let ret;
  458. const opt = {
  459. "Y+": date.getFullYear().toString(), // 年
  460. "m+": (date.getMonth() + 1).toString(), // 月
  461. "d+": date.getDate().toString(), // 日
  462. "H+": date.getHours().toString(), // 时
  463. "M+": date.getMinutes().toString(), // 分
  464. "S+": date.getSeconds().toString() // 秒
  465. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  466. };
  467. for (let k in opt) {
  468. ret = new RegExp("(" + k + ")").exec(fmt);
  469. if (ret) {
  470. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  471. };
  472. };
  473. return fmt;
  474. }