admin.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. ;(function () {
  2. //全局ajax处理
  3. $.ajaxSetup({
  4. complete: function (jqXHR) {
  5. },
  6. data: {},
  7. error: function (jqXHR, textStatus, errorThrown) {
  8. //请求失败处理
  9. }
  10. });
  11. if ($.browser && $.browser.msie) {
  12. //ie 都不缓存
  13. $.ajaxSetup({
  14. cache: false
  15. });
  16. }
  17. //不支持placeholder浏览器下对placeholder进行处理
  18. if (document.createElement('input').placeholder !== '') {
  19. $('[placeholder]').focus(function () {
  20. var input = $(this);
  21. if (input.val() == input.attr('placeholder')) {
  22. input.val('');
  23. input.removeClass('placeholder');
  24. }
  25. }).blur(function () {
  26. var input = $(this);
  27. if (input.val() == '' || input.val() == input.attr('placeholder')) {
  28. input.addClass('placeholder');
  29. input.val(input.attr('placeholder'));
  30. }
  31. }).blur().parents('form').submit(function () {
  32. $(this).find('[placeholder]').each(function () {
  33. var input = $(this);
  34. if (input.val() == input.attr('placeholder')) {
  35. input.val('');
  36. }
  37. });
  38. });
  39. }
  40. // 所有加了dialog类名的a链接,自动弹出它的href
  41. if ($('a.js-dialog').length) {
  42. Wind.css('artDialog');
  43. Wind.use('artDialog', 'iframeTools', function () {
  44. $('.js-dialog').on('click', function (e) {
  45. e.preventDefault();
  46. var $this = $(this);
  47. art.dialog.open($(this).prop('href'), {
  48. close: function () {
  49. $this.focus(); // 关闭时让触发弹窗的元素获取焦点
  50. return true;
  51. },
  52. title: $this.prop('title')
  53. });
  54. }).attr('role', 'button');
  55. });
  56. }
  57. // 所有的ajax form提交,由于大多业务逻辑都是一样的,故统一处理
  58. var ajaxForm_list = $('form.js-ajax-form');
  59. if (ajaxForm_list.length) {
  60. Wind.css('artDialog');
  61. Wind.use('ajaxForm', 'artDialog', 'noty', 'validate', function () {
  62. var $btn;
  63. $('button.js-ajax-submit').on('click', function (e) {
  64. var btn = $(this), form = btn.parents('form.js-ajax-form');
  65. $btn = btn;
  66. if (btn.data("loading")) {
  67. return;
  68. }
  69. //批量操作 判断选项
  70. if (btn.data('subcheck')) {
  71. btn.parent().find('span').remove();
  72. if (form.find('input.js-check:checked').length) {
  73. btn.data('subcheck', false);
  74. } else {
  75. $('<span class="tips_error">请至少选择一项</span>').appendTo(btn.parent()).fadeIn('fast');
  76. return false;
  77. }
  78. }
  79. var msg = btn.data('msg');
  80. if (msg) {
  81. art.dialog({
  82. id: 'warning',
  83. icon: 'warning',
  84. content: btn.data('msg'),
  85. cancelVal: '关闭',
  86. cancel: function () {
  87. //btn.data('subcheck', false);
  88. //btn.click();
  89. },
  90. ok: function () {
  91. btn.data('msg', false);
  92. btn.click();
  93. }
  94. });
  95. return false;
  96. }
  97. //ie处理placeholder提交问题
  98. if ($.browser && $.browser.msie) {
  99. form.find('[placeholder]').each(function () {
  100. var input = $(this);
  101. if (input.val() == input.attr('placeholder')) {
  102. input.val('');
  103. }
  104. });
  105. }
  106. });
  107. ajaxForm_list.each(function () {
  108. $(this).validate({
  109. //是否在获取焦点时验证
  110. //onfocusout : false,
  111. //是否在敲击键盘时验证
  112. //onkeyup : false,
  113. //当鼠标点击时验证
  114. //onclick : false,
  115. //给未通过验证的元素加效果,闪烁等
  116. highlight: function (element, errorClass, validClass) {
  117. if (element.type === "radio") {
  118. this.findByName(element.name).addClass(errorClass).removeClass(validClass);
  119. } else {
  120. var $element = $(element);
  121. $element.addClass(errorClass).removeClass(validClass);
  122. $element.parent().addClass("has-error");//bootstrap3表单
  123. $element.parents('.control-group').addClass("error");//bootstrap2表单
  124. }
  125. },
  126. unhighlight: function (element, errorClass, validClass) {
  127. if (element.type === "radio") {
  128. this.findByName(element.name).removeClass(errorClass).addClass(validClass);
  129. } else {
  130. var $element = $(element);
  131. $element.removeClass(errorClass).addClass(validClass);
  132. $element.parent().removeClass("has-error");//bootstrap3表单
  133. $element.parents('.control-group').removeClass("error");//bootstrap2表单
  134. }
  135. },
  136. showErrors: function (errorMap, errorArr) {
  137. var i, elements, error;
  138. for (i = 0; this.errorList[i]; i++) {
  139. error = this.errorList[i];
  140. if (this.settings.highlight) {
  141. this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass);
  142. }
  143. //this.showLabel( error.element, error.message );
  144. }
  145. if (this.errorList.length) {
  146. //this.toShow = this.toShow.add( this.containers );
  147. }
  148. if (this.settings.success) {
  149. for (i = 0; this.successList[i]; i++) {
  150. //this.showLabel( this.successList[ i ] );
  151. }
  152. }
  153. if (this.settings.unhighlight) {
  154. for (i = 0, elements = this.validElements(); elements[i]; i++) {
  155. this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass);
  156. }
  157. }
  158. this.toHide = this.toHide.not(this.toShow);
  159. this.hideErrors();
  160. this.addWrapper(this.toShow).show();
  161. },
  162. submitHandler: function (form) {
  163. var $form = $(form);
  164. $form.ajaxSubmit({
  165. url: $btn.data('action') ? $btn.data('action') : $form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
  166. dataType: 'json',
  167. beforeSubmit: function (arr, $form, options) {
  168. $btn.data("loading", true);
  169. var text = $btn.text();
  170. //按钮文案、状态修改
  171. $btn.text(text + '...').prop('disabled', true).addClass('disabled');
  172. },
  173. success: function (data, statusText, xhr, $form) {
  174. function _refresh() {
  175. if (data.url) {
  176. //返回带跳转地址
  177. window.location.href = data.url;
  178. } else {
  179. if (data.code == 1) {
  180. //刷新当前页
  181. reloadPage(window);
  182. }
  183. }
  184. }
  185. var text = $btn.text();
  186. //按钮文案、状态修改
  187. $btn.removeClass('disabled').prop('disabled', false).text(text.replace('...', '')).parent().find('span').remove();
  188. if (data.code == 1) {
  189. if ($btn.data('success')) {
  190. var successCallback = $btn.data('success');
  191. window[successCallback](data, statusText, xhr, $form);
  192. return;
  193. }
  194. noty({
  195. text: data.msg,
  196. type: 'success',
  197. layout: 'topCenter',
  198. modal: true,
  199. // animation: {
  200. // open: 'animated bounceInDown', // Animate.css class names
  201. // close: 'animated bounceOutUp', // Animate.css class names
  202. // },
  203. timeout: 800,
  204. callback: {
  205. afterClose: function () {
  206. if ($btn.data('refresh') == undefined || $btn.data('refresh')) {
  207. if ($btn.data('success_refresh')) {
  208. var successRefreshCallback = $btn.data('success_refresh');
  209. window[successRefreshCallback](data, statusText, xhr, $form);
  210. return;
  211. } else {
  212. _refresh();
  213. }
  214. }
  215. }
  216. }
  217. }).show();
  218. $(window).focus();
  219. } else if (data.code == 0) {
  220. var $verify_img = $form.find(".verify_img");
  221. if ($verify_img.length) {
  222. $verify_img.attr("src", $verify_img.attr("src") + "&refresh=" + Math.random());
  223. }
  224. var $verify_input = $form.find("[name='verify']");
  225. $verify_input.val("");
  226. //$('<span class="tips_error">' + data.msg + '</span>').appendTo($btn.parent()).fadeIn('fast');
  227. $btn.removeProp('disabled').removeClass('disabled');
  228. noty({
  229. text: data.msg,
  230. type: 'error',
  231. layout: 'topCenter',
  232. modal: true,
  233. // animation: {
  234. // open: 'animated bounceInDown', // Animate.css class names
  235. // close: 'animated bounceOutUp', // Animate.css class names
  236. // },
  237. timeout: 800,
  238. callback: {
  239. afterClose: function () {
  240. _refresh();
  241. }
  242. }
  243. }).show();
  244. $(window).focus();
  245. }
  246. },
  247. error: function (xhr, e, statusText) {
  248. art.dialog({
  249. id: 'warning',
  250. icon: 'warning',
  251. content: statusText,
  252. cancelVal: '关闭',
  253. cancel: function () {
  254. reloadPage(window);
  255. },
  256. ok: function () {
  257. reloadPage(window);
  258. }
  259. });
  260. },
  261. complete: function () {
  262. $btn.data("loading", false);
  263. }
  264. });
  265. }
  266. });
  267. });
  268. });
  269. }
  270. //dialog弹窗内的关闭方法
  271. $('#js-dialog-close').on('click', function (e) {
  272. e.preventDefault();
  273. try {
  274. art.dialog.close();
  275. } catch (err) {
  276. Wind.css('artDialog');
  277. Wind.use('artDialog', 'iframeTools', function () {
  278. art.dialog.close();
  279. });
  280. }
  281. ;
  282. });
  283. //所有的删除操作,删除数据后刷新页面
  284. if ($('a.js-ajax-delete').length) {
  285. Wind.css('artDialog');
  286. Wind.use('artDialog', 'noty', function () {
  287. $('body').on('click', '.js-ajax-delete', function (e) {
  288. e.preventDefault();
  289. var $_this = this,
  290. $this = $($_this),
  291. href = $this.data('href'),
  292. refresh = $this.data('refresh'),
  293. msg = $this.data('msg');
  294. href = href ? href : $this.attr('href');
  295. art.dialog({
  296. title: false,
  297. icon: 'question',
  298. content: msg ? msg : '确定要删除吗?',
  299. follow: $_this,
  300. close: function () {
  301. $_this.focus(); //关闭时让触发弹窗的元素获取焦点
  302. return true;
  303. },
  304. okVal: "确定",
  305. ok: function () {
  306. $.getJSON(href).done(function (data) {
  307. if (data.code == '1') {
  308. noty({
  309. text: data.msg,
  310. type: 'success',
  311. layout: 'topCenter',
  312. modal: true,
  313. // animation: {
  314. // open: 'animated bounceInDown', // Animate.css class names
  315. // close: 'animated bounceOutUp', // Animate.css class names
  316. // },
  317. timeout: 800,
  318. callback: {
  319. afterClose: function () {
  320. if (refresh == undefined || refresh) {
  321. if (data.url) {
  322. //返回带跳转地址
  323. window.location.href = data.url;
  324. } else {
  325. //刷新当前页
  326. reloadPage(window);
  327. }
  328. }
  329. }
  330. }
  331. }).show();
  332. } else if (data.code == '0') {
  333. //art.dialog.alert(data.info);
  334. //alert(data.info);//暂时处理方案
  335. art.dialog({
  336. content: data.msg,
  337. icon: 'warning',
  338. ok: function () {
  339. this.title(data.msg);
  340. return true;
  341. }
  342. });
  343. }
  344. });
  345. },
  346. cancelVal: '关闭',
  347. cancel: true
  348. });
  349. });
  350. });
  351. }
  352. if ($('a.js-ajax-dialog-btn').length) {
  353. Wind.use('artDialog', 'noty', function () {
  354. $('.js-ajax-dialog-btn').on('click', function (e) {
  355. e.preventDefault();
  356. var $_this = this,
  357. $this = $($_this),
  358. href = $this.data('href'),
  359. refresh = $this.data('refresh'),
  360. msg = $this.data('msg');
  361. href = href ? href : $this.attr('href');
  362. if (!msg) {
  363. msg = "您确定要进行此操作吗?";
  364. }
  365. art.dialog({
  366. title: false,
  367. icon: 'question',
  368. content: msg,
  369. follow: $_this,
  370. close: function () {
  371. $_this.focus(); //关闭时让触发弹窗的元素获取焦点
  372. return true;
  373. },
  374. ok: function () {
  375. $.ajax({
  376. url: href,
  377. type: 'post',
  378. success: function (data) {
  379. if (data.code == 1) {
  380. noty({
  381. text: data.msg,
  382. type: 'success',
  383. layout: 'topCenter',
  384. modal: true,
  385. // animation: {
  386. // open: 'animated bounceInDown', // Animate.css class names
  387. // close: 'animated bounceOutUp', // Animate.css class names
  388. // },
  389. timeout: 800,
  390. callback: {
  391. afterClose: function () {
  392. if (refresh == undefined || refresh) {
  393. if (data.url) {
  394. //返回带跳转地址
  395. window.location.href = data.url;
  396. } else {
  397. //刷新当前页
  398. reloadPage(window);
  399. }
  400. }
  401. }
  402. }
  403. }).show();
  404. } else if (data.code == 0) {
  405. //art.dialog.alert(data.info);
  406. art.dialog({
  407. content: data.msg,
  408. icon: 'warning',
  409. ok: function () {
  410. this.title(data.msg);
  411. return true;
  412. }
  413. });
  414. }
  415. }
  416. })
  417. },
  418. cancelVal: '关闭',
  419. cancel: true
  420. });
  421. });
  422. });
  423. }
  424. if ($('a.js-ajax-btn').length) {
  425. Wind.use('noty', function () {
  426. $('.js-ajax-btn').on('click', function (e) {
  427. e.preventDefault();
  428. var $_this = this,
  429. $this = $($_this),
  430. href = $this.data('href'),
  431. msg = $this.data('msg');
  432. refresh = $this.data('refresh');
  433. href = href ? href : $this.attr('href');
  434. refresh = refresh == undefined ? 1 : refresh;
  435. $.getJSON(href).done(function (data) {
  436. if (data.code == 1) {
  437. noty({
  438. text: data.msg,
  439. type: 'success',
  440. layout: 'center',
  441. callback: {
  442. afterClose: function () {
  443. if (data.url) {
  444. location.href = data.url;
  445. return;
  446. }
  447. if (refresh || refresh == undefined) {
  448. reloadPage(window);
  449. }
  450. }
  451. }
  452. });
  453. } else if (data.code == 0) {
  454. noty({
  455. text: data.msg,
  456. type: 'error',
  457. layout: 'center',
  458. callback: {
  459. afterClose: function () {
  460. if (data.url) {
  461. location.href = data.url;
  462. }
  463. }
  464. }
  465. });
  466. }
  467. });
  468. });
  469. });
  470. }
  471. /*复选框全选(支持多个,纵横双控全选)。
  472. *实例:版块编辑-权限相关(双控),验证机制-验证策略(单控)
  473. *说明:
  474. * "js-check"的"data-xid"对应其左侧"js-check-all"的"data-checklist";
  475. * "js-check"的"data-yid"对应其上方"js-check-all"的"data-checklist";
  476. * 全选框的"data-direction"代表其控制的全选方向(x或y);
  477. * "js-check-wrap"同一块全选操作区域的父标签class,多个调用考虑
  478. */
  479. if ($('.js-check-wrap').length) {
  480. var total_check_all = $('input.js-check-all');
  481. //遍历所有全选框
  482. $.each(total_check_all, function () {
  483. var check_all = $(this),
  484. check_items;
  485. //分组各纵横项
  486. var check_all_direction = check_all.data('direction');
  487. check_items = $('input.js-check[data-' + check_all_direction + 'id="' + check_all.data('checklist') + '"]').not(":disabled");
  488. if($('.js-check-all').is(':checked')) {
  489. check_items.prop('checked', true);
  490. }
  491. //点击全选框
  492. check_all.change(function (e) {
  493. var check_wrap = check_all.parents('.js-check-wrap'); //当前操作区域所有复选框的父标签(重用考虑)
  494. if ($(this).prop('checked')) {
  495. //全选状态
  496. check_items.prop('checked', true);
  497. //所有项都被选中
  498. if (check_wrap.find('input.js-check').length === check_wrap.find('input.js-check:checked').length) {
  499. check_wrap.find(total_check_all).prop('checked', true);
  500. }
  501. } else {
  502. //非全选状态
  503. check_items.removeProp('checked');
  504. check_wrap.find(total_check_all).removeProp('checked');
  505. //另一方向的全选框取消全选状态
  506. var direction_invert = check_all_direction === 'x' ? 'y' : 'x';
  507. check_wrap.find($('input.js-check-all[data-direction="' + direction_invert + '"]')).removeProp('checked');
  508. }
  509. });
  510. //点击非全选时判断是否全部勾选
  511. check_items.change(function () {
  512. if ($(this).prop('checked')) {
  513. if (check_items.filter(':checked').length === check_items.length) {
  514. //已选择和未选择的复选框数相等
  515. check_all.prop('checked', true);
  516. }
  517. } else {
  518. check_all.removeProp('checked');
  519. }
  520. });
  521. });
  522. }
  523. //日期选择器
  524. var dateInput = $("input.js-date");
  525. if (dateInput.length) {
  526. Wind.use('datePicker', function () {
  527. dateInput.datePicker();
  528. });
  529. }
  530. //日期+时间选择器
  531. var dateTimeInput = $("input.js-datetime");
  532. if (dateTimeInput.length) {
  533. Wind.use('datePicker', function () {
  534. dateTimeInput.datePicker({
  535. time: true
  536. });
  537. });
  538. }
  539. var yearInput = $("input.js-year");
  540. if (yearInput.length) {
  541. Wind.use('datePicker', function () {
  542. yearInput.datePicker({
  543. startView: 'decade',
  544. minView: 'decade',
  545. format: 'yyyy',
  546. autoclose: true
  547. });
  548. });
  549. }
  550. // bootstrap年选择器
  551. var bootstrapYearInput = $("input.js-bootstrap-year")
  552. if (bootstrapYearInput.length) {
  553. Wind.css('bootstrapDatetimePicker');
  554. Wind.use('bootstrapDatetimePicker', function () {
  555. bootstrapYearInput.datetimepicker({
  556. language: 'zh-CN',
  557. format: 'yyyy',
  558. minView: 'decade',
  559. startView: 'decade',
  560. todayBtn: 1,
  561. autoclose: true
  562. });
  563. });
  564. }
  565. // bootstrap日期选择器
  566. var bootstrapDateInput = $("input.js-bootstrap-date")
  567. if (bootstrapDateInput.length) {
  568. Wind.css('bootstrapDatetimePicker');
  569. Wind.use('bootstrapDatetimePicker', function () {
  570. bootstrapDateInput.datetimepicker({
  571. language: 'zh-CN',
  572. format: 'yyyy-mm-dd',
  573. minView: 'month',
  574. todayBtn: 1,
  575. autoclose: true
  576. });
  577. });
  578. }
  579. // bootstrap日期选择器日期+时间选择器
  580. var bootstrapDateTimeInput = $("input.js-bootstrap-datetime");
  581. if (bootstrapDateTimeInput.length) {
  582. Wind.css('bootstrapDatetimePicker');
  583. Wind.use('bootstrapDatetimePicker', function () {
  584. bootstrapDateTimeInput.datetimepicker({
  585. language: 'zh-CN',
  586. format: 'yyyy-mm-dd hh:ii',
  587. todayBtn: 1,
  588. autoclose: true
  589. });
  590. });
  591. }
  592. //tab
  593. var tabs_nav = $('ul.js-tabs-nav');
  594. if (tabs_nav.length) {
  595. Wind.use('tabs', function () {
  596. tabs_nav.tabs('.js-tabs-content > div');
  597. });
  598. }
  599. //地址联动
  600. var $js_address_select = $('.js-address-select');
  601. if ($js_address_select.length > 0) {
  602. $('.js-address-province-select,.js-address-city-select').change(function () {
  603. var $this = $(this);
  604. var id = $this.val();
  605. var $child_area_select;
  606. var $this_js_address_select = $this.parents('.js-address-select');
  607. if ($this.is('.js-address-province-select')) {
  608. $child_area_select = $this_js_address_select.find('.js-address-city-select');
  609. $this_js_address_select.find('.js-address-district-select').hide();
  610. } else {
  611. $child_area_select = $this_js_address_select.find('.js-address-district-select');
  612. }
  613. var empty_option = '<option class="js-address-empty-option" value="">' + $child_area_select.find('.js-address-empty-option').text() + '</option>';
  614. $child_area_select.html(empty_option);
  615. var child_area_html = $this.data('childarea' + id);
  616. if (child_area_html) {
  617. $child_area_select.show();
  618. $child_area_select.html(child_area_html);
  619. return;
  620. }
  621. $.ajax({
  622. url: $this_js_address_select.data('url'),
  623. type: 'POST',
  624. dataType: 'JSON',
  625. data: {id: id},
  626. success: function (data) {
  627. if (data.code == 1) {
  628. if (data.data.areas.length > 0) {
  629. var html = [empty_option];
  630. $.each(data.data.areas, function (i, area) {
  631. var area_html = '<option value="[id]">[name]</option>';
  632. area_html = area_html.replace('[name]', area.name);
  633. area_html = area_html.replace('[id]', area.id);
  634. html.push(area_html);
  635. });
  636. html = html.join('', html);
  637. $this.data('childarea' + id, html);
  638. $child_area_select.html(html);
  639. $child_area_select.show();
  640. } else {
  641. $child_area_select.hide();
  642. }
  643. }
  644. },
  645. error: function () {
  646. },
  647. complete: function () {
  648. }
  649. });
  650. });
  651. }
  652. //地址联动end
  653. })();
  654. //重新刷新页面,使用location.reload()有可能导致重新提交
  655. function reloadPage(win) {
  656. var location = win.location;
  657. location.href = location.pathname + location.search;
  658. }
  659. /**
  660. * 页面跳转
  661. * @param url 要打开的页面地址
  662. */
  663. function redirect(url) {
  664. location.href = url;
  665. }
  666. /**
  667. * 读取cookie
  668. * @param name
  669. * @returns
  670. */
  671. function getCookie(name) {
  672. var cookieValue = null;
  673. if (document.cookie && document.cookie != '') {
  674. var cookies = document.cookie.split(';');
  675. for (var i = 0; i < cookies.length; i++) {
  676. var cookie = jQuery.trim(cookies[i]);
  677. // Does this cookie string begin with the name we want?
  678. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  679. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  680. break;
  681. }
  682. }
  683. }
  684. return cookieValue;
  685. }
  686. /**
  687. * 设置cookie
  688. */
  689. function setCookie(name, value, options) {
  690. options = options || {};
  691. if (value === null) {
  692. value = '';
  693. options.expires = -1;
  694. }
  695. var expires = '';
  696. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  697. var date;
  698. if (typeof options.expires == 'number') {
  699. date = new Date();
  700. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  701. } else {
  702. date = options.expires;
  703. }
  704. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  705. }
  706. var path = options.path ? '; path=' + options.path : '';
  707. var domain = options.domain ? '; domain=' + options.domain : '';
  708. var secure = options.secure ? '; secure' : '';
  709. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  710. }
  711. /**
  712. * 打开iframe式的窗口对话框
  713. * @param url
  714. * @param title
  715. * @param options
  716. */
  717. function openIframeDialog(url, title, options) {
  718. Wind.css('artDialog');
  719. var params = {
  720. title: title,
  721. lock: true,
  722. opacity: 0,
  723. width: "95%",
  724. height: '90%'
  725. };
  726. params = options ? $.extend(params, options) : params;
  727. Wind.use('artDialog', 'iframeTools', function () {
  728. art.dialog.open(url, params);
  729. });
  730. }
  731. /**
  732. * 打开地图对话框
  733. *
  734. * @param url
  735. * @param title
  736. * @param options
  737. * @param callback
  738. */
  739. function openMapDialog(url, title, options, callback) {
  740. Wind.css('artDialog');
  741. var params = {
  742. title: title,
  743. lock: true,
  744. opacity: 0,
  745. width: "95%",
  746. height: 400,
  747. ok: function () {
  748. if (callback) {
  749. var d = this.iframe.contentWindow;
  750. var lng = $("#lng_input", d.document).val();
  751. var lat = $("#lat_input", d.document).val();
  752. var address = {};
  753. address.address = $("#address_input", d.document).val();
  754. address.province = $("#province_input", d.document).val();
  755. address.city = $("#city_input", d.document).val();
  756. address.district = $("#district_input", d.document).val();
  757. callback.apply(this, [lng, lat, address]);
  758. }
  759. }
  760. };
  761. params = options ? $.extend(params, options) : params;
  762. Wind.use('artDialog', 'iframeTools', function () {
  763. art.dialog.open(url, params);
  764. });
  765. }
  766. /**
  767. * 打开文件上传对话框
  768. * @param dialog_title 对话框标题
  769. * @param callback 回调方法,参数有(当前dialog对象,选择的文件数组,你设置的extra_params)
  770. * @param extra_params 额外参数,object
  771. * @param multi 是否可以多选
  772. * @param filetype 文件类型,image,video,audio,file
  773. * @param app 应用名,CMF的应用名
  774. */
  775. function openUploadDialog(dialog_title, callback, extra_params, multi, filetype, app) {
  776. Wind.css('artDialog');
  777. multi = multi ? 1 : 0;
  778. filetype = filetype ? filetype : 'image';
  779. app = app ? app : GV.APP;
  780. var params = '&multi=' + multi + '&filetype=' + filetype + '&app=' + app;
  781. Wind.use("artDialog", "iframeTools", function () {
  782. art.dialog.open(GV.ROOT + 'user/Asset/webuploader?' + params, {
  783. title: dialog_title,
  784. id: new Date().getTime(),
  785. width: '600px',
  786. height: '350px',
  787. lock: true,
  788. fixed: true,
  789. background: "#CCCCCC",
  790. opacity: 0,
  791. ok: function () {
  792. if (typeof callback == 'function') {
  793. var iframewindow = this.iframe.contentWindow;
  794. var files = iframewindow.get_selected_files();
  795. console.log(files);
  796. if (files && files.length > 0) {
  797. callback.apply(this, [this, files, extra_params]);
  798. } else {
  799. return false;
  800. }
  801. }
  802. },
  803. cancel: true
  804. });
  805. });
  806. }
  807. /**
  808. * 单个文件上传
  809. * @param dialog_title 上传对话框标题
  810. * @param input_selector 图片容器
  811. * @param filetype 文件类型,image,video,audio,file
  812. * @param extra_params 额外参数,object
  813. * @param app 应用名,CMF的应用名
  814. */
  815. function uploadOne(dialog_title, input_selector, filetype, extra_params, app) {
  816. filetype = filetype ? filetype : 'file';
  817. openUploadDialog(dialog_title, function (dialog, files) {
  818. $(input_selector).val(files[0].filepath);
  819. $(input_selector + '-preview').attr('href', files[0].preview_url);
  820. $(input_selector + '-name').val(files[0].name);
  821. $(input_selector + '-name-text').text(files[0].name);
  822. }, extra_params, 0, filetype, app);
  823. }
  824. /**
  825. * 单个图片上传
  826. * @param dialog_title 上传对话框标题
  827. * @param input_selector 图片容器
  828. * @param extra_params 额外参数,object
  829. * @param app 应用名,CMF的应用名
  830. */
  831. function uploadOneImage(dialog_title, input_selector, extra_params, app) {
  832. openUploadDialog(dialog_title, function (dialog, files) {
  833. $(input_selector).val(files[0].filepath);
  834. $(input_selector + '-preview').attr('src', files[0].preview_url);
  835. $(input_selector + '-name').val(files[0].name);
  836. $(input_selector + '-name-text').text(files[0].name);
  837. }, extra_params, 0, 'image', app);
  838. }
  839. /**
  840. * 多图上传
  841. * @param dialog_title 上传对话框标题
  842. * @param container_selector 图片容器
  843. * @param item_tpl_wrapper_id 单个图片html模板容器id
  844. * @param extra_params 额外参数,object
  845. * @param app 应用名,CMF 的应用名
  846. */
  847. function uploadMultiImage(dialog_title, container_selector, item_tpl_wrapper_id, extra_params, app) {
  848. openUploadDialog(dialog_title, function (dialog, files) {
  849. var tpl = $('#' + item_tpl_wrapper_id).html();
  850. var html = '';
  851. $.each(files, function (i, item) {
  852. var itemtpl = tpl;
  853. itemtpl = itemtpl.replace(/\{id\}/g, item.id);
  854. itemtpl = itemtpl.replace(/\{url\}/g, item.url);
  855. itemtpl = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
  856. itemtpl = itemtpl.replace(/\{filepath\}/g, item.filepath);
  857. itemtpl = itemtpl.replace(/\{name\}/g, item.name);
  858. html += itemtpl;
  859. });
  860. $(container_selector).append(html);
  861. }, extra_params, 1, 'image', app);
  862. }
  863. /**
  864. * 多文件上传
  865. * @param dialog_title 上传对话框标题
  866. * @param container_selector 图片容器
  867. * @param item_tpl_wrapper_id 单个图片html模板容器id
  868. * @param filetype 文件类型,image,video,audio,file
  869. * @param extra_params 额外参数,object
  870. * @param app 应用名,CMF 的应用名
  871. */
  872. function uploadMultiFile(dialog_title, container_selector, item_tpl_wrapper_id, filetype, extra_params, app) {
  873. filetype = filetype ? filetype : 'file';
  874. openUploadDialog(dialog_title, function (dialog, files) {
  875. var tpl = $('#' + item_tpl_wrapper_id).html();
  876. var html = '';
  877. $.each(files, function (i, item) {
  878. var itemtpl = tpl;
  879. itemtpl = itemtpl.replace(/\{id\}/g, item.id);
  880. itemtpl = itemtpl.replace(/\{url\}/g, item.url);
  881. itemtpl = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
  882. itemtpl = itemtpl.replace(/\{filepath\}/g, item.filepath);
  883. itemtpl = itemtpl.replace(/\{name\}/g, item.name);
  884. html += itemtpl;
  885. });
  886. $(container_selector).append(html);
  887. }, extra_params, 1, filetype, app);
  888. }
  889. /**
  890. * 查看图片对话框
  891. * @param img 图片地址
  892. */
  893. function imagePreviewDialog(img) {
  894. Wind.css('layer');
  895. Wind.use("layer", function () {
  896. layer.photos({
  897. photos: {
  898. "title": "", //相册标题
  899. "id": 'image_preview', //相册id
  900. "start": 0, //初始显示的图片序号,默认0
  901. "data": [ //相册包含的图片,数组格式
  902. {
  903. "alt": "",
  904. "pid": 666, //图片id
  905. "src": img, //原图地址
  906. "thumb": img //缩略图地址
  907. }
  908. ]
  909. } //格式见API文档手册页
  910. , anim: 5, //0-6的选择,指定弹出图片动画类型,默认随机
  911. shadeClose: true,
  912. // skin: 'layui-layer-nobg',
  913. shade: [0.5, '#000000'],
  914. shadeClose: true,
  915. })
  916. });
  917. }
  918. function artdialogAlert(msg) {
  919. Wind.css('artDialog');
  920. Wind.use("artDialog", function () {
  921. art.dialog({
  922. id: new Date().getTime(),
  923. icon: "error",
  924. fixed: true,
  925. lock: true,
  926. background: "#CCCCCC",
  927. opacity: 0,
  928. content: msg,
  929. ok: function () {
  930. return true;
  931. }
  932. });
  933. });
  934. }
  935. function openIframeLayer(url, title, options) {
  936. var params = {
  937. type: 2,
  938. title: title,
  939. shadeClose: true,
  940. // skin: 'layui-layer-nobg',
  941. anim: -1,
  942. shade: [0.001, '#000000'],
  943. shadeClose: true,
  944. area: ['95%', '90%'],
  945. move: false,
  946. content: url,
  947. yes: function (index, layero) {
  948. //do something
  949. layer.close(index); //如果设定了yes回调,需进行手工关闭
  950. }
  951. };
  952. params = options ? $.extend(params, options) : params;
  953. Wind.css('layer');
  954. Wind.use("layer", function () {
  955. layer.open(params);
  956. });
  957. }
  958. /**
  959. *
  960. * @param msg
  961. * @param time
  962. */
  963. function showMsg(msg, time){
  964. time = typeof(time) !='undefined'?time : 1;
  965. Wind.css('artDialog');
  966. Wind.use("artDialog", function () {
  967. art.dialog({
  968. id: new Date().getTime(),
  969. icon: "error",
  970. fixed: true,
  971. lock: true,
  972. time: 1,
  973. background: "#CCCCCC",
  974. opacity: 0,
  975. content: msg,
  976. ok: function () {
  977. return true;
  978. }
  979. });
  980. });
  981. }