frontend.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. $form.ajaxSubmit({
  184. url: $btn.data('action') ? $btn.data('action') : $form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
  185. dataType: 'json',
  186. beforeSubmit: function (arr, $form, options) {
  187. $btn.data("loading", true);
  188. var text = $btn.text();
  189. //按钮文案、状态修改
  190. $btn.text(text + '中...').prop('disabled', true).addClass('disabled');
  191. },
  192. success: function (data, statusText, xhr, $form) {
  193. function _refresh() {
  194. if (data.url) {
  195. if (window.parent.art) {
  196. //iframe弹出页
  197. window.parent.location.href = data.url;
  198. } else {
  199. window.location.href = data.url;
  200. }
  201. } else {
  202. if (data.code == 1) {
  203. var wait = $btn.data("wait");
  204. if (window.parent.art) {
  205. reloadPage(window.parent);
  206. } else {
  207. //刷新当前页
  208. reloadPage(window);
  209. }
  210. }
  211. }
  212. }
  213. var text = $btn.text();
  214. //按钮文案、状态修改
  215. $btn.removeClass('disabled').prop('disabled', false).text(text.replace('中...', '')).parent().find('span').remove();
  216. if (data.code == 1) {
  217. if ($btn.data('success')) {
  218. var successCallback = $btn.data('success');
  219. window[successCallback](data, statusText, xhr, $form);
  220. return;
  221. }
  222. noty({
  223. text: data.msg,
  224. type: 'success',
  225. layout: 'center',
  226. modal: true,
  227. callback: {
  228. afterClose: function () {
  229. _refresh();
  230. }
  231. }
  232. });
  233. } else if (data.code == 0) {
  234. if ($btn.data('error')) {
  235. var errorCallback = $btn.data('error');
  236. window[errorCallback](data, statusText, xhr, $form);
  237. return;
  238. }
  239. var $verify_img = $form.find(".verify_img");
  240. if ($verify_img.length) {
  241. $verify_img.attr("src", $verify_img.attr("src") + "&refresh=" + Math.random());
  242. }
  243. var $verify_input = $form.find("[name='verify']");
  244. $verify_input.val("");
  245. noty({
  246. text: data.msg,
  247. type: 'error',
  248. layout: 'center',
  249. callback: {
  250. afterClose: function () {
  251. _refresh();
  252. }
  253. }
  254. });
  255. }
  256. },
  257. error: function (xhr, e, statusText) {
  258. noty({
  259. text: statusText,
  260. type: 'error',
  261. layout: 'center',
  262. callback: {
  263. // afterClose: function () {
  264. // if (window.parent.art) {
  265. // reloadPage(window.parent);
  266. // } else {
  267. // //刷新当前页
  268. // reloadPage(window);
  269. // }
  270. // }
  271. }
  272. });
  273. },
  274. complete: function () {
  275. $btn.data("loading", false);
  276. }
  277. });
  278. }
  279. });
  280. });
  281. });
  282. }
  283. //dialog弹窗内的关闭方法
  284. $('#js-dialog-close').on('click', function (e) {
  285. e.preventDefault();
  286. try {
  287. art.dialog.close();
  288. } catch (err) {
  289. Wind.use('artDialog', 'iframeTools', function () {
  290. art.dialog.close();
  291. });
  292. }
  293. ;
  294. });
  295. //所有的删除操作,删除数据后刷新页面
  296. if ($('a.js-ajax-delete').length) {
  297. Wind.use('noty', function () {
  298. $('.js-ajax-delete').on('click', function (e) {
  299. e.preventDefault();
  300. var $_this = this,
  301. $this = $($_this),
  302. href = $this.data('href'),
  303. msg = $this.data('msg');
  304. href = href ? href : $this.attr('href');
  305. noty({
  306. text: msg ? msg : '确定要删除吗?',
  307. type: 'confirm',
  308. layout: "center",
  309. timeout: false,
  310. modal: true,
  311. buttons: [
  312. {
  313. addClass: 'btn btn-primary',
  314. text: '确定',
  315. onClick: function ($noty) {
  316. $noty.close();
  317. $.getJSON(href).done(function (data) {
  318. if (data.code == 1) {
  319. if (data.url) {
  320. location.href = data.url;
  321. } else {
  322. reloadPage(window);
  323. }
  324. } else if (data.code == 0) {
  325. noty({
  326. text: data.msg,
  327. type: 'error',
  328. layout: 'center',
  329. callback: {
  330. afterClose: function () {
  331. if (data.url) {
  332. location.href = data.url;
  333. }
  334. }
  335. }
  336. });
  337. }
  338. });
  339. }
  340. },
  341. {
  342. addClass: 'btn btn-danger',
  343. text: '取消',
  344. onClick: function ($noty) {
  345. $noty.close();
  346. }
  347. }
  348. ]
  349. });
  350. });
  351. });
  352. }
  353. if ($('a.js-ajax-dialog-btn').length) {
  354. Wind.use('noty', function () {
  355. $('.js-ajax-dialog-btn').on('click', function (e) {
  356. e.preventDefault();
  357. var $_this = this,
  358. $this = $($_this),
  359. href = $this.data('href'),
  360. msg = $this.data('msg');
  361. href = href ? href : $this.attr('href');
  362. noty({
  363. text: msg,
  364. type: 'confirm',
  365. layout: "center",
  366. timeout: false,
  367. modal: true,
  368. buttons: [
  369. {
  370. addClass: 'btn btn-primary',
  371. text: '确定',
  372. onClick: function ($noty) {
  373. $noty.close();
  374. $.getJSON(href).done(function (data) {
  375. if (data.code == 1) {
  376. if (data.url) {
  377. location.href = data.url;
  378. } else {
  379. reloadPage(window);
  380. }
  381. } else if (data.code == 0) {
  382. noty({
  383. text: data.msg,
  384. type: 'error',
  385. layout: 'center',
  386. callback: {
  387. afterClose: function () {
  388. if (data.url) {
  389. location.href = data.url;
  390. }
  391. }
  392. }
  393. });
  394. }
  395. });
  396. }
  397. },
  398. {
  399. addClass: 'btn btn-danger',
  400. text: '取消',
  401. onClick: function ($noty) {
  402. $noty.close();
  403. }
  404. }
  405. ]
  406. });
  407. });
  408. });
  409. }
  410. if ($('a.js-ajax-btn').length) {
  411. Wind.use('noty', function () {
  412. $('.js-ajax-btn').on('click', function (e) {
  413. e.preventDefault();
  414. var $_this = this,
  415. $this = $($_this),
  416. href = $this.data('href'),
  417. msg = $this.data('msg');
  418. refresh = $this.data('refresh');
  419. href = href ? href : $this.attr('href');
  420. refresh = refresh == undefined ? 1 : refresh;
  421. $.getJSON(href).done(function (data) {
  422. if (data.code == 1) {
  423. noty({
  424. text: data.msg,
  425. type: 'success',
  426. layout: 'center',
  427. callback: {
  428. afterClose: function () {
  429. if (data.url) {
  430. location.href = data.url;
  431. return;
  432. }
  433. if (refresh || refresh == undefined) {
  434. reloadPage(window);
  435. }
  436. }
  437. }
  438. });
  439. } else if (data.code == 0) {
  440. noty({
  441. text: data.msg,
  442. type: 'error',
  443. layout: 'center',
  444. callback: {
  445. afterClose: function () {
  446. if (data.url) {
  447. location.href = data.url;
  448. }
  449. }
  450. }
  451. });
  452. }
  453. });
  454. });
  455. });
  456. }
  457. //所有的请求刷新操作
  458. var ajax_refresh = $('a.js-ajax-refresh'),
  459. refresh_lock = false;
  460. if (ajax_refresh.length) {
  461. ajax_refresh.on('click', function (e) {
  462. e.preventDefault();
  463. if (refresh_lock) {
  464. return false;
  465. }
  466. refresh_lock = true;
  467. $.post(this.href, function (data) {
  468. refresh_lock = false;
  469. if (data.code == 1) {
  470. if (data.url) {
  471. location.href = data.url;
  472. } else {
  473. reloadPage(window);
  474. }
  475. } else if (data.code == 0) {
  476. Wind.art.dialog.alert(data.msg);
  477. }
  478. }, 'json');
  479. });
  480. }
  481. //短信验证码
  482. var $js_get_mobile_code = $('.js-get-mobile-code');
  483. if ($js_get_mobile_code.length > 0) {
  484. Wind.use('noty', function () {
  485. $js_get_mobile_code.on('click', function () {
  486. var $this = $(this);
  487. if ($this.data('loading')) return;
  488. if ($this.data('sending')) return;
  489. var $mobile_input = $($this.data('mobile-input'));
  490. var mobile = $mobile_input.val();
  491. if (mobile == '') {
  492. $mobile_input.focus();
  493. return;
  494. }
  495. $this.data('loading', true);
  496. $this.data('sending', true);
  497. var url = $this.data('url');
  498. var init_secode_left = parseInt($this.data('init-second-left'));
  499. init_secode_left = init_secode_left > 0 ? init_secode_left : 60;
  500. var init_text = $this.text();
  501. $this.data('second-left', init_secode_left);
  502. var wait_msg = $this.data('wait-msg');
  503. $.ajax({
  504. url: url,
  505. type: 'POST',
  506. dataType: 'json',
  507. data: {username: mobile},
  508. success: function (data) {
  509. if (data.code == 1) {
  510. noty({
  511. text: data.msg,
  512. type: 'success',
  513. layout: 'center'
  514. });
  515. $this.text(wait_msg.replace('[second]', init_secode_left));
  516. var mtimer = setInterval(function () {
  517. if (init_secode_left > 0) {
  518. init_secode_left--;
  519. $this.text(wait_msg.replace('[second]', init_secode_left));
  520. } else {
  521. clearInterval(mtimer);
  522. $this.text(init_text);
  523. $this.data('sending', false);
  524. }
  525. }, 1000);
  526. } else {
  527. noty({
  528. text: data.msg,
  529. type: 'error',
  530. layout: 'center'
  531. });
  532. $this.data('sending', false);
  533. }
  534. },
  535. error: function () {
  536. $this.data('sending', false);
  537. },
  538. complete: function () {
  539. $this.data('loading', false);
  540. }
  541. });
  542. });
  543. });
  544. }
  545. //邮件验证码
  546. var $js_get_email_code = $('.js-get-email-code');
  547. if ($js_get_email_code.length > 0) {
  548. Wind.use('noty', function () {
  549. $js_get_email_code.on('click', function () {
  550. var $this = $(this);
  551. if ($this.data('loading')) return;
  552. if ($this.data('sending')) return;
  553. var $email_input = $($this.data('email-input'));
  554. var email = $email_input.val();
  555. if (email == '') {
  556. $email_input.focus();
  557. return;
  558. }
  559. $this.data('loading', true);
  560. $this.data('sending', true);
  561. var url = $this.data('url');
  562. var init_secode_left = parseInt($this.data('init-second-left'));
  563. init_secode_left = init_secode_left > 0 ? init_secode_left : 60;
  564. var init_text = $this.text();
  565. $this.data('second-left', init_secode_left);
  566. var wait_msg = $this.data('wait-msg');
  567. $.ajax({
  568. url: url,
  569. type: 'POST',
  570. dataType: 'json',
  571. data: {username: email},
  572. success: function (data) {
  573. if (data.code == 1) {
  574. noty({
  575. text: data.msg,
  576. type: 'success',
  577. layout: 'center'
  578. });
  579. $this.text(wait_msg.replace('[second]', init_secode_left));
  580. var mtimer = setInterval(function () {
  581. if (init_secode_left > 0) {
  582. init_secode_left--;
  583. $this.text(wait_msg.replace('[second]', init_secode_left));
  584. } else {
  585. clearInterval(mtimer);
  586. $this.text(init_text);
  587. $this.data('sending', false);
  588. }
  589. }, 1000);
  590. } else {
  591. noty({
  592. text: data.msg,
  593. type: 'error',
  594. layout: 'center'
  595. });
  596. $this.data('sending', false);
  597. }
  598. },
  599. error: function () {
  600. $this.data('sending', false);
  601. },
  602. complete: function () {
  603. $this.data('loading', false);
  604. }
  605. });
  606. });
  607. });
  608. }
  609. //日期选择器
  610. var dateInput = $("input.js-date");
  611. if (dateInput.length) {
  612. Wind.use('datePicker', function () {
  613. dateInput.datePicker();
  614. });
  615. }
  616. //日期+时间选择器
  617. var dateTimeInput = $("input.js-datetime");
  618. if (dateTimeInput.length) {
  619. Wind.use('datePicker', function () {
  620. dateTimeInput.datePicker({
  621. time: true
  622. });
  623. });
  624. }
  625. // bootstrap日期选择器
  626. var bootstrapDateInput = $("input.js-bootstrap-date")
  627. if (bootstrapDateInput.length) {
  628. Wind.css('bootstrapDatetimePicker');
  629. Wind.use('bootstrapDatetimePicker', function () {
  630. bootstrapDateInput.datetimepicker({
  631. language: 'zh-CN',
  632. format: 'yyyy-mm-dd',
  633. minView: 'month',
  634. todayBtn: 1,
  635. autoclose: true
  636. });
  637. });
  638. }
  639. // bootstrap日期选择器日期+时间选择器
  640. var bootstrapDateTimeInput = $("input.js-bootstrap-datetime");
  641. if (bootstrapDateTimeInput.length) {
  642. Wind.css('bootstrapDatetimePicker');
  643. Wind.use('bootstrapDatetimePicker', function () {
  644. bootstrapDateTimeInput.datetimepicker({
  645. language: 'zh-CN',
  646. format: 'yyyy-mm-dd hh:ii',
  647. todayBtn: 1,
  648. autoclose: true
  649. });
  650. });
  651. }
  652. //赞,拍等,有数量操作的按钮
  653. var $js_count_btn = $('a.js-count-btn');
  654. if ($js_count_btn.length) {
  655. Wind.use('noty', function () {
  656. $js_count_btn.on('click', function (e) {
  657. e.preventDefault();
  658. var $this = $(this),
  659. href = $this.prop('href');
  660. $.post(href, {}, function (data) {
  661. if (data.code == 1) {
  662. var $count = $this.find(".count");
  663. var count = parseInt($count.text());
  664. $count.text(count + 1);
  665. if (data.msg) {
  666. noty({
  667. text: data.msg,
  668. type: 'success',
  669. layout: 'center',
  670. callback: {
  671. afterClose: function () {
  672. if (data.url) {
  673. location.href = data.url;
  674. }
  675. }
  676. }
  677. });
  678. }
  679. } else if (data.code == 0) {
  680. noty({
  681. text: data.msg,
  682. type: 'error',
  683. layout: 'center',
  684. callback: {
  685. afterClose: function () {
  686. if (data.url) {
  687. location.href = data.url;
  688. }
  689. }
  690. }
  691. });
  692. }
  693. }, "json");
  694. });
  695. });
  696. }
  697. //地址联动
  698. var $js_address_select = $('.js-address-select');
  699. if ($js_address_select.length > 0) {
  700. $('.js-address-province-select,.js-address-city-select').change(function () {
  701. var $this = $(this);
  702. var id = $this.val();
  703. var $child_area_select;
  704. var $this_js_address_select = $this.parents('.js-address-select');
  705. if ($this.is('.js-address-province-select')) {
  706. $child_area_select = $this_js_address_select.find('.js-address-city-select');
  707. $this_js_address_select.find('.js-address-district-select').hide();
  708. } else {
  709. $child_area_select = $this_js_address_select.find('.js-address-district-select');
  710. }
  711. var empty_option = '<option class="js-address-empty-option" value="">' + $child_area_select.find('.js-address-empty-option').text() + '</option>';
  712. $child_area_select.html(empty_option);
  713. var child_area_html = $this.data('childarea' + id);
  714. if (child_area_html) {
  715. $child_area_select.show();
  716. $child_area_select.html(child_area_html);
  717. return;
  718. }
  719. $.ajax({
  720. url: $this_js_address_select.data('url'),
  721. type: 'POST',
  722. dataType: 'JSON',
  723. data: {id: id},
  724. success: function (data) {
  725. if (data.code == 1) {
  726. if (data.data.areas.length > 0) {
  727. var html = [empty_option];
  728. $.each(data.data.areas, function (i, area) {
  729. var area_html = '<option value="[id]">[name]</option>';
  730. area_html = area_html.replace('[name]', area.name);
  731. area_html = area_html.replace('[id]', area.id);
  732. html.push(area_html);
  733. });
  734. html = html.join('', html);
  735. $this.data('childarea' + id, html);
  736. $child_area_select.html(html);
  737. $child_area_select.show();
  738. } else {
  739. $child_area_select.hide();
  740. }
  741. }
  742. },
  743. error: function () {
  744. },
  745. complete: function () {
  746. }
  747. });
  748. });
  749. }
  750. //地址联动end
  751. //
  752. var $js_action_btn = $('a.js-action-btn');
  753. if ($js_action_btn.length) {
  754. Wind.use('noty', function () {
  755. $js_action_btn.on('click', function (e) {
  756. e.preventDefault();
  757. var $this = $(this),
  758. href = $this.prop('href');
  759. $.post(href, {}, function (data) {
  760. if (data.code == '1') {
  761. if (data.msg) {
  762. noty({
  763. text: data.msg,
  764. type: 'success',
  765. layout: 'center',
  766. callback: {
  767. afterClose: function () {
  768. if (data.url) {
  769. location.href = data.url;
  770. }
  771. }
  772. }
  773. });
  774. }
  775. } else if (data.code == 0) {
  776. noty({
  777. text: data.msg,
  778. type: 'error',
  779. layout: 'center',
  780. callback: {
  781. afterClose: function () {
  782. if (data.url) {
  783. location.href = data.url;
  784. }
  785. }
  786. }
  787. });
  788. }
  789. }, "json");
  790. });
  791. });
  792. }
  793. var $js_favorite_btn = $('a.js-favorite-btn');
  794. if ($js_favorite_btn.length) {
  795. Wind.use('noty', function () {
  796. $js_favorite_btn.on('click', function (e) {
  797. e.preventDefault();
  798. var $this = $(this),
  799. href = $this.prop('href'),
  800. url = $this.data("url"),
  801. id = $this.data("id"),
  802. table = $this.data('table'),
  803. title = $this.data("title"),
  804. description = $this.data("description");
  805. $.post(href, {
  806. id: id,
  807. table: table,
  808. url: url,
  809. title: title,
  810. description: description
  811. }, function (data) {
  812. if (data.code == 1) {
  813. if (data.msg) {
  814. noty({
  815. text: data.msg,
  816. type: 'success',
  817. layout: 'center',
  818. callback: {
  819. afterClose: function () {
  820. if (data.url) {
  821. location.href = data.url;
  822. }
  823. }
  824. }
  825. });
  826. }
  827. } else if (data.code == 0) {
  828. noty({
  829. text: data.msg,
  830. type: 'error',
  831. layout: 'center',
  832. callback: {
  833. afterClose: function () {
  834. if (data.url) {
  835. location.href = data.url;
  836. }
  837. }
  838. }
  839. });
  840. }
  841. }, "json");
  842. });
  843. });
  844. }
  845. })();
  846. //重新刷新页面,使用location.reload()有可能导致重新提交
  847. function reloadPage(win) {
  848. if (win) {
  849. } else {
  850. win = window;
  851. }
  852. var location = win.location;
  853. location.href = location.pathname + location.search;
  854. }
  855. //页面跳转
  856. function redirect(url) {
  857. location.href = url;
  858. }
  859. /**
  860. * 读取cookie
  861. * @param name
  862. * @returns
  863. */
  864. function getCookie(name) {
  865. var cookieValue = null;
  866. if (document.cookie && document.cookie != '') {
  867. var cookies = document.cookie.split(';');
  868. for (var i = 0; i < cookies.length; i++) {
  869. var cookie = jQuery.trim(cookies[i]);
  870. // Does this cookie string begin with the name we want?
  871. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  872. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  873. break;
  874. }
  875. }
  876. }
  877. return cookieValue;
  878. }
  879. /**
  880. * 设置cookie
  881. */
  882. function setCookie(name, value, options) {
  883. options = options || {};
  884. if (value === null) {
  885. value = '';
  886. options.expires = -1;
  887. }
  888. var expires = '';
  889. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  890. var date;
  891. if (typeof options.expires == 'number') {
  892. date = new Date();
  893. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  894. } else {
  895. date = options.expires;
  896. }
  897. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  898. }
  899. var path = options.path ? '; path=' + options.path : '';
  900. var domain = options.domain ? '; domain=' + options.domain : '';
  901. var secure = options.secure ? '; secure' : '';
  902. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  903. }
  904. function openIframeDialog(url, title, options) {
  905. var params = {
  906. title: title,
  907. lock: true,
  908. opacity: 0,
  909. width: "95%"
  910. };
  911. params = options ? $.extend(params, options) : params;
  912. Wind.use('artDialog', 'iframeTools', function () {
  913. art.dialog.open(url, params);
  914. });
  915. }
  916. /**
  917. * 打开地图对话框
  918. *
  919. * @param url
  920. * @param title
  921. * @param options
  922. * @param callback
  923. */
  924. function openMapDialog(url, title, options, callback) {
  925. Wind.css('artDialog');
  926. var params = {
  927. title: title,
  928. lock: true,
  929. opacity: 0,
  930. width: "95%",
  931. height: 400,
  932. ok: function () {
  933. if (callback) {
  934. var d = this.iframe.contentWindow;
  935. var lng = $("#lng_input", d.document).val();
  936. var lat = $("#lat_input", d.document).val();
  937. var address = {};
  938. address.address = $("#address_input", d.document).val();
  939. address.province = $("#province_input", d.document).val();
  940. address.city = $("#city_input", d.document).val();
  941. address.district = $("#district_input", d.document).val();
  942. callback.apply(this, [lng, lat, address]);
  943. }
  944. }
  945. };
  946. params = options ? $.extend(params, options) : params;
  947. Wind.use('artDialog', 'iframeTools', function () {
  948. art.dialog.open(url, params);
  949. });
  950. }
  951. /**
  952. * 打开文件上传对话框
  953. * @param dialog_title 对话框标题
  954. * @param callback 回调方法,参数有(当前dialog对象,选择的文件数组,你设置的extra_params)
  955. * @param extra_params 额外参数,object
  956. * @param multi 是否可以多选
  957. * @param filetype 文件类型,image,video,audio,file
  958. * @param app 应用名,CMF的应用名
  959. */
  960. function openUploadDialog(dialog_title, callback, extra_params, multi, filetype, app) {
  961. Wind.css('artDialog');
  962. multi = multi ? 1 : 0;
  963. filetype = filetype ? filetype : 'image';
  964. app = app ? app : GV.APP;
  965. var params = '&multi=' + multi + '&filetype=' + filetype + '&app=' + app;
  966. Wind.use("artDialog", "iframeTools", function () {
  967. art.dialog.open(GV.ROOT + 'user/Asset/webuploader?' + params, {
  968. title: dialog_title,
  969. id: new Date().getTime(),
  970. width: '600px',
  971. height: '350px',
  972. lock: true,
  973. fixed: true,
  974. background: "#CCCCCC",
  975. opacity: 0,
  976. ok: function () {
  977. if (typeof callback == 'function') {
  978. var iframewindow = this.iframe.contentWindow;
  979. var files = iframewindow.get_selected_files();
  980. console.log(files);
  981. if (files && files.length > 0) {
  982. callback.apply(this, [this, files, extra_params]);
  983. } else {
  984. return false;
  985. }
  986. }
  987. },
  988. cancel: true
  989. });
  990. });
  991. }
  992. /**
  993. * 单个文件上传
  994. * @param dialog_title 上传对话框标题
  995. * @param input_selector 图片容器
  996. * @param filetype 文件类型,image,video,audio,file
  997. * @param extra_params 额外参数,object
  998. * @param app 应用名,CMF的应用名
  999. */
  1000. function uploadOne(dialog_title, input_selector, filetype, extra_params, app) {
  1001. openUploadDialog(dialog_title, function (dialog, files) {
  1002. $(input_selector).val(files[0].filepath);
  1003. $(input_selector + '-preview').attr('href', files[0].preview_url);
  1004. $(input_selector + '-name').val(files[0].name);
  1005. }, extra_params, 0, filetype, app);
  1006. }
  1007. /**
  1008. * 单个图片上传
  1009. * @param dialog_title 上传对话框标题
  1010. * @param input_selector 图片容器
  1011. * @param extra_params 额外参数,object
  1012. * @param app 应用名,CMF的应用名
  1013. */
  1014. function uploadOneImage(dialog_title, input_selector, extra_params, app) {
  1015. openUploadDialog(dialog_title, function (dialog, files) {
  1016. $(input_selector).val(files[0].filepath);
  1017. $(input_selector + '-preview').attr('src', files[0].preview_url);
  1018. $(input_selector + '-name').val(files[0].name);
  1019. }, extra_params, 0, 'image', app);
  1020. }
  1021. /**
  1022. * 多图上传
  1023. * @param dialog_title 上传对话框标题
  1024. * @param container_selector 图片容器
  1025. * @param item_tpl_wrapper_id 单个图片html模板容器id
  1026. * @param extra_params 额外参数,object
  1027. * @param app 应用名,CMF 的应用名
  1028. */
  1029. function uploadMultiImage(dialog_title, container_selector, item_tpl_wrapper_id, extra_params, app) {
  1030. openUploadDialog(dialog_title, function (dialog, files) {
  1031. var tpl = $('#' + item_tpl_wrapper_id).html();
  1032. var html = '';
  1033. $.each(files, function (i, item) {
  1034. var itemtpl = tpl;
  1035. itemtpl = itemtpl.replace(/\{id\}/g, item.id);
  1036. itemtpl = itemtpl.replace(/\{url\}/g, item.url);
  1037. itemtpl = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
  1038. itemtpl = itemtpl.replace(/\{filepath\}/g, item.filepath);
  1039. itemtpl = itemtpl.replace(/\{name\}/g, item.name);
  1040. html += itemtpl;
  1041. });
  1042. $(container_selector).append(html);
  1043. }, extra_params, 1, 'image', app);
  1044. }
  1045. /**
  1046. * 多文件上传
  1047. * @param dialog_title 上传对话框标题
  1048. * @param container_selector 图片容器
  1049. * @param item_tpl_wrapper_id 单个图片html模板容器id
  1050. * @param filetype 文件类型,image,video,audio,file
  1051. * @param extra_params 额外参数,object
  1052. * @param app 应用名,CMF 的应用名
  1053. */
  1054. function uploadMultiFile(dialog_title, container_selector, item_tpl_wrapper_id, filetype, extra_params, app) {
  1055. filetype = filetype ? filetype : 'file';
  1056. openUploadDialog(dialog_title, function (dialog, files) {
  1057. var tpl = $('#' + item_tpl_wrapper_id).html();
  1058. var html = '';
  1059. $.each(files, function (i, item) {
  1060. var itemtpl = tpl;
  1061. itemtpl = itemtpl.replace(/\{id\}/g, item.id);
  1062. itemtpl = itemtpl.replace(/\{url\}/g, item.url);
  1063. itemtpl = itemtpl.replace(/\{preview_url\}/g, item.preview_url);
  1064. itemtpl = itemtpl.replace(/\{filepath\}/g, item.filepath);
  1065. itemtpl = itemtpl.replace(/\{name\}/g, item.name);
  1066. html += itemtpl;
  1067. });
  1068. $(container_selector).append(html);
  1069. }, extra_params, 1, filetype, app);
  1070. }
  1071. function openIframeLayer(url, title, options) {
  1072. var params = {
  1073. type: 2,
  1074. title: title,
  1075. shadeClose: true,
  1076. // skin: 'layui-layer-nobg',
  1077. shade: [0.001, '#000000'],
  1078. shadeClose: true,
  1079. area: ['95%', '90%'],
  1080. move: false,
  1081. content: url,
  1082. yes: function (index, layero) {
  1083. //do something
  1084. layer.close(index); //如果设定了yes回调,需进行手工关闭
  1085. }
  1086. };
  1087. params = options ? $.extend(params, options) : params;
  1088. Wind.css('layer');
  1089. Wind.use("layer", function () {
  1090. layer.open(params);
  1091. });
  1092. }