common.js 13 KB

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