common.js 13 KB

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