frontend.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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.use('artDialog', 'iframeTools', function () {
  43. $('.js-dialog').on('click', function (e) {
  44. e.preventDefault();
  45. var $_this = this,
  46. _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.use('ajaxForm', 'noty', 'validate', function () {
  61. //var form = btn.parents('form.js-ajax-form');
  62. var $btn;
  63. $('button.js-ajax-submit').on('click', function (e) {
  64. //e.preventDefault();
  65. /*var btn = $(this).find('button.js-ajax-submit'),
  66. form = $(this);*/
  67. var btn = $(this), form = btn.parents('form.js-ajax-form');
  68. $btn = btn;
  69. if (btn.data("loading")) {
  70. return false;
  71. }
  72. //批量操作 判断选项
  73. if (btn.data('subcheck')) {
  74. btn.parent().find('span').remove();
  75. if (form.find('input.js-check:checked').length) {
  76. var msg = btn.data('msg');
  77. if (msg) {
  78. noty({
  79. text: msg,
  80. type: 'confirm',
  81. layout: "center",
  82. timeout: false,
  83. modal: true,
  84. buttons: [
  85. {
  86. addClass: 'btn btn-primary',
  87. text: '确定',
  88. onClick: function ($noty) {
  89. $noty.close();
  90. btn.data('subcheck', false);
  91. btn.click();
  92. }
  93. },
  94. {
  95. addClass: 'btn btn-danger',
  96. text: '取消',
  97. onClick: function ($noty) {
  98. $noty.close();
  99. }
  100. }
  101. ]
  102. });
  103. } else {
  104. btn.data('subcheck', false);
  105. btn.click();
  106. }
  107. } else {
  108. noty({
  109. text: "请至少选择一项",
  110. type: 'error',
  111. layout: 'center'
  112. });
  113. }
  114. return false;
  115. }
  116. //ie处理placeholder提交问题
  117. if ($.browser && $.browser.msie) {
  118. form.find('[placeholder]').each(function () {
  119. var input = $(this);
  120. if (input.val() == input.attr('placeholder')) {
  121. input.val('');
  122. }
  123. });
  124. }
  125. });
  126. ajaxForm_list.each(function () {
  127. $(this).validate({
  128. //是否在获取焦点时验证
  129. //onfocusout : false,
  130. //是否在敲击键盘时验证
  131. //onkeyup : false,
  132. //当鼠标点击时验证
  133. //onclick : false,
  134. //给未通过验证的元素加效果,闪烁等
  135. highlight: function (element, errorClass, validClass) {
  136. if (element.type === "radio") {
  137. this.findByName(element.name).addClass(errorClass).removeClass(validClass);
  138. } else {
  139. var $element = $(element);
  140. $element.addClass(errorClass).removeClass(validClass);
  141. $element.parent().addClass("has-error");//bootstrap3表单
  142. $element.parents('.control-group').addClass("error");//bootstrap2表单
  143. }
  144. },
  145. unhighlight: function (element, errorClass, validClass) {
  146. if (element.type === "radio") {
  147. this.findByName(element.name).removeClass(errorClass).addClass(validClass);
  148. } else {
  149. var $element = $(element);
  150. $element.removeClass(errorClass).addClass(validClass);
  151. $element.parent().removeClass("has-error");//bootstrap3表单
  152. $element.parents('.control-group').removeClass("error");//bootstrap2表单
  153. }
  154. },
  155. showErrors: function (errorMap, errorArr) {
  156. var i, elements, error;
  157. for (i = 0; this.errorList[i]; i++) {
  158. error = this.errorList[i];
  159. if (this.settings.highlight) {
  160. this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass);
  161. }
  162. //this.showLabel( error.element, error.message );
  163. }
  164. if (this.errorList.length) {
  165. //this.toShow = this.toShow.add( this.containers );
  166. }
  167. if (this.settings.success) {
  168. for (i = 0; this.successList[i]; i++) {
  169. //this.showLabel( this.successList[ i ] );
  170. }
  171. }
  172. if (this.settings.unhighlight) {
  173. for (i = 0, elements = this.validElements(); elements[i]; i++) {
  174. this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass);
  175. }
  176. }
  177. this.toHide = this.toHide.not(this.toShow);
  178. this.hideErrors();
  179. this.addWrapper(this.toShow).show();
  180. },
  181. submitHandler: function (form) {
  182. var $form = $(form);
  183. if (!$btn) {
  184. $btn = $form.find('button.js-ajax-submit');
  185. }
  186. $form.ajaxSubmit({
  187. url: $btn && $btn.data('action') ? $btn.data('action') : $form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
  188. dataType: 'json',
  189. beforeSubmit: function (arr, $form, options) {
  190. if ($btn) {
  191. $btn.data("loading", true);
  192. var text = $btn.text();
  193. //按钮文案、状态修改
  194. $btn.text(text + '...').prop('disabled', true).addClass('disabled');
  195. }
  196. },
  197. success: function (data, statusText, xhr, $form) {
  198. function _refresh() {
  199. if (data.url) {
  200. if (window.parent.art) {
  201. //iframe弹出页
  202. window.parent.location.href = data.url;
  203. } else {
  204. window.location.href = data.url;
  205. }
  206. } else {
  207. if (data.code == 1) {
  208. var wait = $btn.data("wait");
  209. if (window.parent.art) {
  210. reloadPage(window.parent);
  211. } else {
  212. //刷新当前页
  213. reloadPage(window);
  214. }
  215. }
  216. }
  217. }
  218. var text = $btn.text();
  219. //按钮文案、状态修改
  220. $btn.removeClass('disabled').prop('disabled', false).text(text.replace('...', '')).parent().find('span').remove();
  221. if (data.code == 1) {
  222. if ($btn.data('success')) {
  223. var successCallback = $btn.data('success');
  224. window[successCallback](data, statusText, xhr, $form);
  225. return;
  226. }
  227. noty({
  228. text: data.msg,
  229. type: 'success',
  230. layout: 'center',
  231. modal: true,
  232. callback: {
  233. afterClose: function () {
  234. _refresh();
  235. }
  236. }
  237. });
  238. } else if (data.code == 0) {
  239. if ($btn.data('error')) {
  240. var errorCallback = $btn.data('error');
  241. window[errorCallback](data, statusText, xhr, $form);
  242. return;
  243. }
  244. var $verify_img = $form.find(".verify_img");
  245. if ($verify_img.length) {
  246. $verify_img.attr("src", $verify_img.attr("src") + "&refresh=" + Math.random());
  247. }
  248. var $verify_input = $form.find("[name='verify']");
  249. $verify_input.val("");
  250. noty({
  251. text: data.msg,
  252. type: 'error',
  253. layout: 'center',
  254. callback: {
  255. afterClose: function () {
  256. _refresh();
  257. }
  258. }
  259. });
  260. }
  261. },
  262. error: function (xhr, e, statusText) {
  263. noty({
  264. text: statusText,
  265. type: 'error',
  266. layout: 'center',
  267. callback: {
  268. // afterClose: function () {
  269. // if (window.parent.art) {
  270. // reloadPage(window.parent);
  271. // } else {
  272. // //刷新当前页
  273. // reloadPage(window);
  274. // }
  275. // }
  276. }
  277. });
  278. },
  279. complete: function () {
  280. $btn.data("loading", false);
  281. }
  282. });
  283. }
  284. });
  285. });
  286. });
  287. }
  288. //dialog弹窗内的关闭方法
  289. $('#js-dialog-close').on('click', function (e) {
  290. e.preventDefault();
  291. try {
  292. art.dialog.close();
  293. } catch (err) {
  294. Wind.use('artDialog', 'iframeTools', function () {
  295. art.dialog.close();
  296. });
  297. }
  298. ;
  299. });
  300. //所有的删除操作,删除数据后刷新页面
  301. if ($('a.js-ajax-delete').length) {
  302. Wind.use('noty', function () {
  303. $('.js-ajax-delete').on('click', function (e) {
  304. e.preventDefault();
  305. var $_this = this,
  306. $this = $($_this),
  307. href = $this.data('href'),
  308. refresh = $this.data('refresh'),
  309. msg = $this.data('msg');
  310. okBtnText = $this.data('ok-btn');
  311. cancelBtnText = $this.data('cancel-btn');
  312. href = href ? href : $this.attr('href');
  313. noty({
  314. text: msg ? msg : '确定要删除吗?',
  315. type: 'confirm',
  316. layout: "center",
  317. timeout: false,
  318. modal: true,
  319. buttons: [
  320. {
  321. addClass: 'btn btn-primary',
  322. text: okBtnText ? okBtnText : '确定',
  323. onClick: function ($noty) {
  324. $noty.close();
  325. $.ajax({
  326. url: href,
  327. type: 'POST',
  328. dataType: 'json',
  329. success: function (data) {
  330. if (data.code == 1) {
  331. if (data.url) {
  332. location.href = data.url;
  333. } else if (refresh || refresh == undefined) {
  334. reloadPage(window);
  335. }
  336. } else if (data.code == 0) {
  337. noty({
  338. text: data.msg,
  339. type: 'error',
  340. layout: 'center',
  341. callback: {
  342. afterClose: function () {
  343. if (data.url) {
  344. location.href = data.url;
  345. }
  346. }
  347. }
  348. });
  349. }
  350. }
  351. });
  352. }
  353. },
  354. {
  355. addClass: 'btn btn-danger',
  356. text: cancelBtnText ? cancelBtnText : '取消',
  357. onClick: function ($noty) {
  358. $noty.close();
  359. }
  360. }
  361. ]
  362. });
  363. });
  364. });
  365. }
  366. if ($('a.js-ajax-dialog-btn').length) {
  367. Wind.use('noty', function () {
  368. $('.js-ajax-dialog-btn').on('click', function (e) {
  369. e.preventDefault();
  370. var $_this = this,
  371. $this = $($_this),
  372. href = $this.data('href'),
  373. refresh = $this.data('refresh'),
  374. msg = $this.data('msg');
  375. href = href ? href : $this.attr('href');
  376. noty({
  377. text: msg,
  378. type: 'confirm',
  379. layout: "center",
  380. timeout: false,
  381. modal: true,
  382. buttons: [
  383. {
  384. addClass: 'btn btn-primary',
  385. text: '确定',
  386. onClick: function ($noty) {
  387. $noty.close();
  388. $.ajax({
  389. url: href,
  390. type: 'POST',
  391. dataType: 'json',
  392. success: function (data) {
  393. if (data.code == 1) {
  394. if (data.url) {
  395. location.href = data.url;
  396. } else if (refresh || refresh == undefined) {
  397. reloadPage(window);
  398. }
  399. } else if (data.code == 0) {
  400. noty({
  401. text: data.msg,
  402. type: 'error',
  403. layout: 'center',
  404. callback: {
  405. afterClose: function () {
  406. if (data.url) {
  407. location.href = data.url;
  408. }
  409. }
  410. }
  411. });
  412. }
  413. }
  414. });
  415. }
  416. },
  417. {
  418. addClass: 'btn btn-danger',
  419. text: '取消',
  420. onClick: function ($noty) {
  421. $noty.close();
  422. }
  423. }
  424. ]
  425. });
  426. });
  427. });
  428. }
  429. Wind.use('noty', function () {
  430. $('body').on('click', '.js-ajax-btn', function (e) {
  431. e.preventDefault();
  432. var $_this = this,
  433. $this = $($_this),
  434. href = $this.data('href'),
  435. msg = $this.data('msg');
  436. refresh = $this.data('refresh');
  437. href = href ? href : $this.attr('href');
  438. refresh = refresh == undefined ? 1 : refresh;
  439. $.ajax({
  440. url: href,
  441. type: 'POST',
  442. dataType: 'json',
  443. success: function (data) {
  444. if (data.code == 1) {
  445. noty({
  446. text: data.msg,
  447. type: 'success',
  448. layout: 'center',
  449. callback: {
  450. afterClose: function () {
  451. if (data.url) {
  452. location.href = data.url;
  453. return;
  454. }
  455. if (refresh || refresh == undefined) {
  456. reloadPage(window);
  457. }
  458. }
  459. }
  460. });
  461. } else if (data.code == 0) {
  462. noty({
  463. text: data.msg,
  464. type: 'error',
  465. layout: 'center',
  466. callback: {
  467. afterClose: function () {
  468. if (data.url) {
  469. location.href = data.url;
  470. }
  471. }
  472. }
  473. });
  474. }
  475. }
  476. });
  477. });
  478. });
  479. //所有的请求刷新操作
  480. var ajax_refresh = $('a.js-ajax-refresh'),
  481. refresh_lock = false;
  482. if (ajax_refresh.length) {
  483. ajax_refresh.on('click', function (e) {
  484. e.preventDefault();
  485. if (refresh_lock) {
  486. return false;
  487. }
  488. refresh_lock = true;
  489. $.post(this.href, function (data) {
  490. refresh_lock = false;
  491. if (data.code == 1) {
  492. if (data.url) {
  493. location.href = data.url;
  494. } else {
  495. reloadPage(window);
  496. }
  497. } else if (data.code == 0) {
  498. Wind.art.dialog.alert(data.msg);
  499. }
  500. }, 'json');
  501. });
  502. }
  503. //短信验证码
  504. var $js_get_mobile_code = $('.js-get-mobile-code');
  505. if ($js_get_mobile_code.length > 0) {
  506. Wind.use('noty', function () {
  507. $js_get_mobile_code.on('click', function () {
  508. var $this = $(this);
  509. if ($this.data('loading')) return;
  510. if ($this.data('sending')) return;
  511. var $mobile_input = $($this.data('mobile-input'));
  512. var mobile = $mobile_input.val();
  513. if (mobile == '') {
  514. $mobile_input.focus();
  515. return;
  516. }
  517. var $form = $this.parents('form');
  518. var $captchaInput = $("input[name='captcha']", $form);
  519. var $captchaIdInput = $("input[name='_captcha_id']", $form);
  520. var captcha = $captchaInput.val();
  521. var captchaId = $captchaIdInput.val();
  522. if (!captcha) {
  523. $captchaInput.focus();
  524. return;
  525. }
  526. $this.data('loading', true);
  527. $this.data('sending', true);
  528. var url = $this.data('url');
  529. var init_secode_left = parseInt($this.data('init-second-left'));
  530. init_secode_left = init_secode_left > 0 ? init_secode_left : 60;
  531. var init_text = $this.text();
  532. $this.data('second-left', init_secode_left);
  533. var wait_msg = $this.data('wait-msg');
  534. var codeType = $this.data('type');
  535. $.ajax({
  536. url: url,
  537. type: 'POST',
  538. dataType: 'json',
  539. data: {username: mobile, captcha: captcha, captcha_id: captchaId, type: codeType},
  540. success: function (data) {
  541. if (data.code == 1) {
  542. noty({
  543. text: data.msg,
  544. type: 'success',
  545. layout: 'center'
  546. });
  547. $this.text(wait_msg.replace('[second]', init_secode_left));
  548. var mtimer = setInterval(function () {
  549. if (init_secode_left > 0) {
  550. init_secode_left--;
  551. $this.text(wait_msg.replace('[second]', init_secode_left));
  552. } else {
  553. clearInterval(mtimer);
  554. $this.text(init_text);
  555. $this.data('sending', false);
  556. }
  557. }, 1000);
  558. } else {
  559. $captchaInput.val('');
  560. var $verify_img = $form.find(".verify_img");
  561. if ($verify_img.length) {
  562. $verify_img.attr("src", $verify_img.attr("src") + "&refresh=" + Math.random());
  563. }
  564. noty({
  565. text: data.msg,
  566. type: 'error',
  567. layout: 'center'
  568. });
  569. $this.data('sending', false);
  570. }
  571. },
  572. error: function () {
  573. $this.data('sending', false);
  574. },
  575. complete: function () {
  576. $this.data('loading', false);
  577. }
  578. });
  579. });
  580. });
  581. }
  582. //邮件验证码
  583. var $js_get_email_code = $('.js-get-email-code');
  584. if ($js_get_email_code.length > 0) {
  585. Wind.use('noty', function () {
  586. $js_get_email_code.on('click', function () {
  587. var $this = $(this);
  588. if ($this.data('loading')) return;
  589. if ($this.data('sending')) return;
  590. var $email_input = $($this.data('email-input'));
  591. var email = $email_input.val();
  592. if (email == '') {
  593. $email_input.focus();
  594. return;
  595. }
  596. var $form = $this.parents('form');
  597. var $captchaInput = $("input[name='captcha']", $form);
  598. var $captchaIdInput = $("input[name='_captcha_id']", $form);
  599. var captcha = $captchaInput.val();
  600. var captchaId = $captchaIdInput.val();
  601. if (!captcha) {
  602. $captchaInput.focus();
  603. return;
  604. }
  605. $this.data('loading', true);
  606. $this.data('sending', true);
  607. var url = $this.data('url');
  608. var init_secode_left = parseInt($this.data('init-second-left'));
  609. init_secode_left = init_secode_left > 0 ? init_secode_left : 60;
  610. var init_text = $this.text();
  611. $this.data('second-left', init_secode_left);
  612. var wait_msg = $this.data('wait-msg');
  613. var codeType = $this.data('type');
  614. $.ajax({
  615. url: url,
  616. type: 'POST',
  617. dataType: 'json',
  618. data: {username: email, captcha: captcha, captcha_id: captchaId, type: codeType},
  619. success: function (data) {
  620. if (data.code == 1) {
  621. noty({
  622. text: data.msg,
  623. type: 'success',
  624. layout: 'center'
  625. });
  626. $this.text(wait_msg.replace('[second]', init_secode_left));
  627. var mtimer = setInterval(function () {
  628. if (init_secode_left > 0) {
  629. init_secode_left--;
  630. $this.text(wait_msg.replace('[second]', init_secode_left));
  631. } else {
  632. clearInterval(mtimer);
  633. $this.text(init_text);
  634. $this.data('sending', false);
  635. }
  636. }, 1000);
  637. } else {
  638. $captchaInput.val('');
  639. var $verify_img = $form.find(".verify_img");
  640. if ($verify_img.length) {
  641. $verify_img.attr("src", $verify_img.attr("src") + "&refresh=" + Math.random());
  642. }
  643. noty({
  644. text: data.msg,
  645. type: 'error',
  646. layout: 'center'
  647. });
  648. $this.data('sending', false);
  649. }
  650. },
  651. error: function () {
  652. $this.data('sending', false);
  653. },
  654. complete: function () {
  655. $this.data('loading', false);
  656. }
  657. });
  658. });
  659. });
  660. }
  661. /*复选框全选(支持多个,纵横双控全选)。
  662. *实例:版块编辑-权限相关(双控),验证机制-验证策略(单控)
  663. *说明:
  664. * "js-check"的"data-xid"对应其左侧"js-check-all"的"data-checklist";
  665. * "js-check"的"data-yid"对应其上方"js-check-all"的"data-checklist";
  666. * 全选框的"data-direction"代表其控制的全选方向(x或y);
  667. * "js-check-wrap"同一块全选操作区域的父标签class,多个调用考虑
  668. */
  669. if ($('.js-check-wrap').length) {
  670. var total_check_all = $('input.js-check-all');
  671. //遍历所有全选框
  672. $.each(total_check_all, function () {
  673. var check_all = $(this),
  674. check_items;
  675. //分组各纵横项
  676. var check_all_direction = check_all.data('direction');
  677. check_items = $('input.js-check[data-' + check_all_direction + 'id="' + check_all.data('checklist') + '"]').not(":disabled");
  678. //点击全选框
  679. check_all.change(function (e) {
  680. var check_wrap = check_all.parents('.js-check-wrap'); //当前操作区域所有复选框的父标签(重用考虑)
  681. if ($(this).prop('checked')) {
  682. //全选状态
  683. check_items.prop('checked', true);
  684. //所有项都被选中
  685. if (check_wrap.find('input.js-check').length === check_wrap.find('input.js-check:checked').length) {
  686. check_wrap.find(total_check_all).prop('checked', true);
  687. }
  688. } else {
  689. //非全选状态
  690. check_items.removeProp('checked');
  691. check_wrap.find(total_check_all).removeProp('checked');
  692. //另一方向的全选框取消全选状态
  693. var direction_invert = check_all_direction === 'x' ? 'y' : 'x';
  694. check_wrap.find($('input.js-check-all[data-direction="' + direction_invert + '"]')).removeProp('checked');
  695. }
  696. });
  697. //点击非全选时判断是否全部勾选
  698. check_items.change(function () {
  699. if ($(this).prop('checked')) {
  700. if (check_items.filter(':checked').length === check_items.length) {
  701. //已选择和未选择的复选框数相等
  702. check_all.prop('checked', true);
  703. }
  704. } else {
  705. check_all.removeProp('checked');
  706. }
  707. });
  708. });
  709. }
  710. //日期选择器
  711. var dateInput = $("input.js-date");
  712. if (dateInput.length) {
  713. Wind.use('datePicker', function () {
  714. dateInput.datePicker();
  715. });
  716. }
  717. //日期+时间选择器
  718. var dateTimeInput = $("input.js-datetime");
  719. if (dateTimeInput.length) {
  720. Wind.use('datePicker', function () {
  721. dateTimeInput.datePicker({
  722. time: true
  723. });
  724. });
  725. }
  726. // bootstrap年选择器
  727. var bootstrapYearInput = $("input.js-bootstrap-year")
  728. if (bootstrapYearInput.length) {
  729. Wind.css('bootstrapDatetimePicker');
  730. Wind.use('bootstrapDatetimePicker', function () {
  731. bootstrapYearInput.datetimepicker({
  732. language: 'zh-CN',
  733. format: 'yyyy',
  734. minView: 'decade',
  735. startView: 'decade',
  736. todayBtn: 1,
  737. autoclose: true
  738. });
  739. });
  740. }
  741. // bootstrap年月份选择器
  742. var bootstrapYearMonthInput = $("input.js-bootstrap-year-month");
  743. if (bootstrapYearMonthInput.length) {
  744. Wind.css('bootstrapDatetimePicker');
  745. Wind.use('bootstrapDatetimePicker', function () {
  746. bootstrapYearMonthInput.datetimepicker({
  747. language: 'zh-CN',
  748. format: 'yyyy-mm',
  749. minView: 'year',
  750. startView: 'decade',
  751. todayBtn: 1,
  752. autoclose: true
  753. });
  754. });
  755. }
  756. // bootstrap日期选择器
  757. var bootstrapDateInput = $("input.js-bootstrap-date")
  758. if (bootstrapDateInput.length) {
  759. Wind.css('bootstrapDatetimePicker');
  760. Wind.use('bootstrapDatetimePicker', function () {
  761. bootstrapDateInput.datetimepicker({
  762. language: 'zh-CN',
  763. format: 'yyyy-mm-dd',
  764. minView: 'month',
  765. todayBtn: 1,
  766. autoclose: true
  767. });
  768. });
  769. }
  770. // bootstrap日期选择器日期+时间选择器
  771. var bootstrapDateTimeInput = $("input.js-bootstrap-datetime");
  772. if (bootstrapDateTimeInput.length) {
  773. Wind.css('bootstrapDatetimePicker');
  774. Wind.use('bootstrapDatetimePicker', function () {
  775. bootstrapDateTimeInput.datetimepicker({
  776. language: 'zh-CN',
  777. format: 'yyyy-mm-dd hh:ii',
  778. todayBtn: 1,
  779. autoclose: true
  780. });
  781. });
  782. }
  783. //赞,拍等,有数量操作的按钮
  784. var $js_count_btn = $('a.js-count-btn');
  785. if ($js_count_btn.length) {
  786. Wind.use('noty', function () {
  787. $js_count_btn.on('click', function (e) {
  788. e.preventDefault();
  789. var $this = $(this),
  790. href = $this.prop('href');
  791. $.post(href, {}, function (data) {
  792. if (data.code == 1) {
  793. var $count = $this.find(".count");
  794. var count = parseInt($count.text());
  795. $count.text(count + 1);
  796. if (data.msg) {
  797. noty({
  798. text: data.msg,
  799. type: 'success',
  800. layout: 'center',
  801. callback: {
  802. afterClose: function () {
  803. if (data.url) {
  804. location.href = data.url;
  805. }
  806. }
  807. }
  808. });
  809. }
  810. } else if (data.code == 0) {
  811. noty({
  812. text: data.msg,
  813. type: 'error',
  814. layout: 'center',
  815. callback: {
  816. afterClose: function () {
  817. if (data.url) {
  818. location.href = data.url;
  819. }
  820. }
  821. }
  822. });
  823. }
  824. }, "json");
  825. });
  826. });
  827. }
  828. //地址联动
  829. var $js_address_select = $('.js-address-select');
  830. if ($js_address_select.length > 0) {
  831. $('.js-address-province-select,.js-address-city-select,.js-address-district-select').change(function () {
  832. var $this = $(this);
  833. var id = $this.val();
  834. var $child_area_select;
  835. var $this_js_address_select = $this.parents('.js-address-select');
  836. if ($this.is('.js-address-province-select')) {
  837. $child_area_select = $this_js_address_select.find('.js-address-city-select');
  838. $this_js_address_select.find('.js-address-district-select').hide();
  839. } else if($this.is('.js-address-city-select')){
  840. $child_area_select = $this_js_address_select.find('.js-address-district-select');
  841. $this_js_address_select.find('.js-address-town-select').hide();
  842. } else {
  843. $child_area_select = $this_js_address_select.find('.js-address-town-select');
  844. }
  845. var empty_option = '<option class="js-address-empty-option" value="">' + $child_area_select.find('.js-address-empty-option').text() + '</option>';
  846. $child_area_select.html(empty_option);
  847. var child_area_html = $this.data('childarea' + id);
  848. if (child_area_html) {
  849. $child_area_select.show();
  850. $child_area_select.html(child_area_html);
  851. return;
  852. }
  853. $.ajax({
  854. url: $this_js_address_select.data('url'),
  855. type: 'POST',
  856. dataType: 'JSON',
  857. data: {id: id},
  858. success: function (data) {
  859. if (data.code == 1) {
  860. if (data.data.areas.length > 0) {
  861. var html = [empty_option];
  862. $.each(data.data.areas, function (i, area) {
  863. var area_html = '<option value="[id]">[name]</option>';
  864. area_html = area_html.replace('[name]', area.name);
  865. area_html = area_html.replace('[id]', area.id);
  866. html.push(area_html);
  867. });
  868. html = html.join('', html);
  869. $this.data('childarea' + id, html);
  870. $child_area_select.html(html);
  871. $child_area_select.show();
  872. } else {
  873. $child_area_select.hide();
  874. }
  875. }
  876. },
  877. error: function () {
  878. },
  879. complete: function () {
  880. }
  881. });
  882. });
  883. }
  884. //地址联动end
  885. //
  886. var $js_action_btn = $('a.js-action-btn');
  887. if ($js_action_btn.length) {
  888. Wind.use('noty', function () {
  889. $js_action_btn.on('click', function (e) {
  890. e.preventDefault();
  891. var $this = $(this),
  892. href = $this.prop('href');
  893. $.post(href, {}, function (data) {
  894. if (data.code == '1') {
  895. if (data.msg) {
  896. noty({
  897. text: data.msg,
  898. type: 'success',
  899. layout: 'center',
  900. callback: {
  901. afterClose: function () {
  902. if (data.url) {
  903. location.href = data.url;
  904. }
  905. }
  906. }
  907. });
  908. }
  909. } else if (data.code == 0) {
  910. noty({
  911. text: data.msg,
  912. type: 'error',
  913. layout: 'center',
  914. callback: {
  915. afterClose: function () {
  916. if (data.url) {
  917. location.href = data.url;
  918. }
  919. }
  920. }
  921. });
  922. }
  923. }, "json");
  924. });
  925. });
  926. }
  927. var $js_favorite_btn = $('a.js-favorite-btn');
  928. if ($js_favorite_btn.length) {
  929. Wind.use('noty', function () {
  930. $js_favorite_btn.on('click', function (e) {
  931. e.preventDefault();
  932. var $this = $(this),
  933. href = $this.prop('href'),
  934. url = $this.data("url"),
  935. id = $this.data("id"),
  936. table = $this.data('table'),
  937. title = $this.data("title"),
  938. description = $this.data("description");
  939. $.post(href, {
  940. id: id,
  941. table: table,
  942. url: url,
  943. title: title,
  944. description: description
  945. }, function (data) {
  946. if (data.code == 1) {
  947. if (data.msg) {
  948. noty({
  949. text: data.msg,
  950. type: 'success',
  951. layout: 'center',
  952. callback: {
  953. afterClose: function () {
  954. if (data.url) {
  955. location.href = data.url;
  956. }
  957. }
  958. }
  959. });
  960. }
  961. } else if (data.code == 0) {
  962. noty({
  963. text: data.msg,
  964. type: 'error',
  965. layout: 'center',
  966. callback: {
  967. afterClose: function () {
  968. if (data.url) {
  969. location.href = data.url;
  970. }
  971. }
  972. }
  973. });
  974. }
  975. }, "json");
  976. });
  977. });
  978. }
  979. })();
  980. //重新刷新页面,使用location.reload()有可能导致重新提交
  981. function reloadPage(win) {
  982. if (win) {
  983. } else {
  984. win = window;
  985. }
  986. var location = win.location;
  987. location.href = location.pathname + location.search;
  988. }
  989. //页面跳转
  990. function redirect(url) {
  991. location.href = url;
  992. }
  993. /**
  994. * 读取cookie
  995. * @param name
  996. * @returns
  997. */
  998. function getCookie(name) {
  999. var cookieValue = null;
  1000. if (document.cookie && document.cookie != '') {
  1001. var cookies = document.cookie.split(';');
  1002. for (var i = 0; i < cookies.length; i++) {
  1003. var cookie = jQuery.trim(cookies[i]);
  1004. // Does this cookie string begin with the name we want?
  1005. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  1006. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  1007. break;
  1008. }
  1009. }
  1010. }
  1011. return cookieValue;
  1012. }
  1013. /**
  1014. * 设置cookie
  1015. */
  1016. function setCookie(name, value, options) {
  1017. options = options || {};
  1018. if (value === null) {
  1019. value = '';
  1020. options.expires = -1;
  1021. }
  1022. var expires = '';
  1023. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  1024. var date;
  1025. if (typeof options.expires == 'number') {
  1026. date = new Date();
  1027. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  1028. } else {
  1029. date = options.expires;
  1030. }
  1031. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  1032. }
  1033. var path = options.path ? '; path=' + options.path : '';
  1034. var domain = options.domain ? '; domain=' + options.domain : '';
  1035. var secure = options.secure ? '; secure' : '';
  1036. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  1037. }
  1038. function openIframeDialog(url, title, options) {
  1039. var params = {
  1040. title: title,
  1041. lock: true,
  1042. opacity: 0,
  1043. width: "95%"
  1044. };
  1045. params = options ? $.extend(params, options) : params;
  1046. Wind.use('artDialog', 'iframeTools', function () {
  1047. art.dialog.open(url, params);
  1048. });
  1049. }
  1050. /**
  1051. * 打开地图对话框
  1052. *
  1053. * @param url
  1054. * @param title
  1055. * @param options
  1056. * @param callback
  1057. */
  1058. function openMapDialog(url, title, options, callback) {
  1059. Wind.css('artDialog');
  1060. var params = {
  1061. title: title,
  1062. lock: true,
  1063. opacity: 0,
  1064. width: "95%",
  1065. height: 400,
  1066. ok: function () {
  1067. if (callback) {
  1068. var d = this.iframe.contentWindow;
  1069. var lng = $("#lng_input", d.document).val();
  1070. var lat = $("#lat_input", d.document).val();
  1071. var address = {};
  1072. address.address = $("#address_input", d.document).val();
  1073. address.province = $("#province_input", d.document).val();
  1074. address.city = $("#city_input", d.document).val();
  1075. address.district = $("#district_input", d.document).val();
  1076. callback.apply(this, [lng, lat, address]);
  1077. }
  1078. }
  1079. };
  1080. params = options ? $.extend(params, options) : params;
  1081. Wind.use('artDialog', 'iframeTools', function () {
  1082. art.dialog.open(url, params);
  1083. });
  1084. }
  1085. /**
  1086. * 打开文件上传对话框
  1087. * @param dialog_title 对话框标题
  1088. * @param callback 回调方法,参数有(当前dialog对象,选择的文件数组,你设置的extra_params)
  1089. * @param extra_params 额外参数,object
  1090. * @param multi 是否可以多选
  1091. * @param filetype 文件类型,image,video,audio,file
  1092. * @param app 应用名,CMF的应用名
  1093. */
  1094. function openUploadDialog(dialog_title, callback, extra_params, multi, filetype, app) {
  1095. Wind.css('artDialog');
  1096. multi = multi ? 1 : 0;
  1097. filetype = filetype ? filetype : 'image';
  1098. app = app ? app : GV.APP;
  1099. var params = '&multi=' + multi + '&filetype=' + filetype + '&app=' + app;
  1100. Wind.use("artDialog", "iframeTools", function () {
  1101. art.dialog.open(GV.ROOT + 'user/Asset/webuploader?' + params, {
  1102. title: dialog_title,
  1103. id: new Date().getTime(),
  1104. width: '600px',
  1105. height: '350px',
  1106. lock: true,
  1107. fixed: true,
  1108. background: "#CCCCCC",
  1109. opacity: 0,
  1110. ok: function () {
  1111. if (typeof callback == 'function') {
  1112. var iframewindow = this.iframe.contentWindow;
  1113. var files = iframewindow.get_selected_files();
  1114. console.log(files);
  1115. if (files && files.length > 0) {
  1116. callback.apply(this, [this, files, extra_params]);
  1117. } else {
  1118. return false;
  1119. }
  1120. }
  1121. },
  1122. cancel: true
  1123. });
  1124. });
  1125. }
  1126. /**
  1127. * 单个文件上传
  1128. * @param dialog_title 上传对话框标题
  1129. * @param input_selector 图片容器
  1130. * @param filetype 文件类型,image,video,audio,file
  1131. * @param extra_params 额外参数,object
  1132. * @param app 应用名,CMF的应用名
  1133. */
  1134. function uploadOne(dialog_title, input_selector, filetype, extra_params, app) {
  1135. filetype = filetype ? filetype : 'file';
  1136. openUploadDialog(dialog_title, function (dialog, files) {
  1137. $(input_selector).val(files[0].filepath);
  1138. $(input_selector + '-preview').attr('href', files[0].preview_url);
  1139. $(input_selector + '-name').val(files[0].name);
  1140. $(input_selector + '-name-text').text(files[0].name);
  1141. }, extra_params, 0, filetype, app);
  1142. }
  1143. /**
  1144. * 单个图片上传
  1145. * @param dialog_title 上传对话框标题
  1146. * @param input_selector 图片容器
  1147. * @param extra_params 额外参数,object
  1148. * @param app 应用名,CMF的应用名
  1149. */
  1150. function uploadOneImage(dialog_title, input_selector, extra_params, app) {
  1151. openUploadDialog(dialog_title, function (dialog, files) {
  1152. $(input_selector).val(files[0].filepath);
  1153. $(input_selector + '-preview').attr('src', files[0].preview_url);
  1154. $(input_selector + '-name').val(files[0].name);
  1155. $(input_selector + '-name-text').text(files[0].name);
  1156. }, extra_params, 0, 'image', app);
  1157. }
  1158. /**
  1159. * 多图上传
  1160. * @param dialog_title 上传对话框标题
  1161. * @param container_selector 图片容器
  1162. * @param item_tpl_wrapper_id 单个图片html模板容器id
  1163. * @param extra_params 额外参数,object
  1164. * @param app 应用名,CMF 的应用名
  1165. */
  1166. function uploadMultiImage(dialog_title, container_selector, item_tpl_wrapper_id, extra_params, app) {
  1167. openUploadDialog(dialog_title, function (dialog, files) {
  1168. var tpl = $('#' + item_tpl_wrapper_id).html();
  1169. var html = '';
  1170. $.each(files, function (i, item) {
  1171. var itemtpl = tpl;
  1172. itemtpl = itemtpl.replace(/\{id\}/g, item.id);
  1173. itemtpl = itemtpl.replace(/\{url\}/g, item.url);
  1174. itemtpl = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
  1175. itemtpl = itemtpl.replace(/\{filepath\}/g, item.filepath);
  1176. itemtpl = itemtpl.replace(/\{name\}/g, item.name);
  1177. html += itemtpl;
  1178. });
  1179. $(container_selector).append(html);
  1180. }, extra_params, 1, 'image', app);
  1181. }
  1182. /**
  1183. * 多文件上传
  1184. * @param dialog_title 上传对话框标题
  1185. * @param container_selector 图片容器
  1186. * @param item_tpl_wrapper_id 单个图片html模板容器id
  1187. * @param filetype 文件类型,image,video,audio,file
  1188. * @param extra_params 额外参数,object
  1189. * @param app 应用名,CMF 的应用名
  1190. */
  1191. function uploadMultiFile(dialog_title, container_selector, item_tpl_wrapper_id, filetype, extra_params, app) {
  1192. filetype = filetype ? filetype : 'file';
  1193. openUploadDialog(dialog_title, function (dialog, files) {
  1194. var tpl = $('#' + item_tpl_wrapper_id).html();
  1195. var html = '';
  1196. $.each(files, function (i, item) {
  1197. var itemtpl = tpl;
  1198. itemtpl = itemtpl.replace(/\{id\}/g, item.id);
  1199. itemtpl = itemtpl.replace(/\{url\}/g, item.url);
  1200. itemtpl = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
  1201. itemtpl = itemtpl.replace(/\{filepath\}/g, item.filepath);
  1202. itemtpl = itemtpl.replace(/\{name\}/g, item.name);
  1203. html += itemtpl;
  1204. });
  1205. $(container_selector).append(html);
  1206. }, extra_params, 1, filetype, app);
  1207. }
  1208. function openIframeLayer(url, title, options) {
  1209. var params = {
  1210. type: 2,
  1211. title: title,
  1212. shadeClose: true,
  1213. // skin: 'layui-layer-nobg',
  1214. shade: [0.001, '#000000'],
  1215. shadeClose: true,
  1216. area: ['95%', '90%'],
  1217. move: false,
  1218. content: url,
  1219. yes: function (index, layero) {
  1220. //do something
  1221. layer.close(index); //如果设定了yes回调,需进行手工关闭
  1222. }
  1223. };
  1224. params = options ? $.extend(params, options) : params;
  1225. Wind.css('layer');
  1226. Wind.use("layer", function () {
  1227. layer.open(params);
  1228. });
  1229. }