common.js 13 KB

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