editArea.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // 后端数据结构
  2. var areaData = {
  3. "data": {
  4. "isTrial": true,
  5. "area": {
  6. // "北京": [],
  7. // "河南": [
  8. // "洛阳市",
  9. // "驻马店市",
  10. // "周口市",
  11. // // "郑州市"
  12. // ],
  13. // "澳门": [],
  14. // "甘肃": []
  15. },
  16. "buyset": {
  17. "areacount": -1,
  18. "buyerclasscount": 11,
  19. "citys": {
  20. // "河南": 4
  21. }
  22. },
  23. "industry": [
  24. "党委办",
  25. "公共资源交易",
  26. "公安",
  27. "出版广电",
  28. "国资委",
  29. "地震",
  30. "安监",
  31. "工商",
  32. "法院",
  33. "电信行业",
  34. "财政"
  35. ]
  36. },
  37. "errMsg": "",
  38. "success": true
  39. }
  40. //加载数据
  41. function getData() {
  42. $DoPost("/subscribepay/editSub/getSubBuyMsg", {}, function (r) {
  43. if (r.success) {
  44. areaData = r;
  45. }
  46. }, false);
  47. }
  48. // 渲染城市方法
  49. function createMoreCity(arr) {
  50. var tempHtml = arr.map(function (v) {
  51. return '<button class="city">' + v.name + '</button>'
  52. }).join('')
  53. return tempHtml
  54. }
  55. var animatedRuning = false;
  56. function checkAnimatedRuning() {
  57. if (animatedRuning) {
  58. return true
  59. }
  60. animatedRuning = true;
  61. setTimeout(function () {
  62. animatedRuning = false;
  63. }, 500);
  64. return false
  65. }
  66. // 省下拉市事件
  67. function slideFun(obj) {
  68. if (checkAnimatedRuning()) {
  69. return
  70. }
  71. if (obj.next('.tab_content:not(:animated)').css("display") == "block") {
  72. obj.children().children('i').removeClass("up");
  73. } else {
  74. obj.children().children('i').addClass("up");
  75. }
  76. obj.toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500)
  77. obj.parent().siblings().children('.tab').removeClass('selected');
  78. }
  79. var obj
  80. // 改变选择结果方法
  81. function getResult() {
  82. obj = {};
  83. //$('.result_name').empty();
  84. $('.province').each(function () {
  85. var provinceText = $(this).text().trim();//省份名称
  86. var isChecked = $(this).children('.checkbox').is(':checked');
  87. var isHalf = $(this).children('.checkbox').hasClass('half');
  88. var arr = [];
  89. if (isChecked) {
  90. obj[provinceText] = [];
  91. } else if (isHalf && !isChecked) {
  92. var $that = $(this);
  93. obj[$that.text().trim()] = [];
  94. $that.parent().siblings().find('.city').each(function () {
  95. var parent = $(this).parent().siblings().find('.province').text().trim();
  96. if ($(this).is('.active') && parent == $that.text().trim()) {
  97. arr.push($(this).text().trim())
  98. }
  99. })
  100. obj[$that.text().trim()] = arr
  101. }
  102. })
  103. $('count').html($('.checkbox:not(.other):checked').length);
  104. //$('.result_name').append(getAreaClassArr_index(obj).join("、"));
  105. //校验是否修改
  106. if (theSameAs(obj) || $.isEmptyObject(obj)) {
  107. $('#enter').attr("disabled", "disabled");
  108. } else {
  109. $('#enter').removeAttr("disabled");
  110. }
  111. }
  112. // 初始化页面方法
  113. function init() {
  114. var p = areaData.data.area;
  115. var count = areaData.data.buyset.citys;
  116. // 1.渲染城市列表
  117. $(".select-area-box ul li:not('.index')").each(function () {
  118. var text = $(this).find(".province").text().trim()
  119. var data = null
  120. chinaMapJSON.some(function (v) {
  121. data = v
  122. return v.name.indexOf(text) !== -1
  123. })
  124. var box = $(this).find('div.tab_content')
  125. var html = createMoreCity(data.city)
  126. box.html(html)
  127. })
  128. // 2.当areacount == -1时,全国为选中状态,为正数时为选中的省份数目,全国选项不显示
  129. var pLength = Object.keys(areaData.data.area).length;
  130. var cLength = Object.keys(areaData.data.buyset.citys).length
  131. console.log(pLength, cLength)
  132. if (areaData.data.buyset.areacount == -1 && pLength == 0 && cLength == 0) {
  133. // 当购买的全国切选中的是全国时
  134. // $('.optional_count').html("可选择全部区域")
  135. //$('.all_area').show()
  136. $('.optional_count').hide()
  137. $('.total').html('34')
  138. $('.count').html('34')
  139. $('.tab.whole').parent('li').show();
  140. $('.checkbox.other').prop('checked', true);
  141. } else if (areaData.data.buyset.areacount == -1 && pLength != 0) {
  142. // 当购买的全国而没有选择全国,选择部分省市时
  143. //$('.all_area').show();
  144. $('.province').removeAttr('data-count');
  145. $('.optional_count').hide();
  146. $('.total').html('34');
  147. initResult()
  148. } else {
  149. //$('.all_area').hide()
  150. $('.optional_count').show()
  151. if (areaData.data.buyset.areacount != 0) {
  152. $('.total').html(areaData.data.buyset.areacount)
  153. } else {
  154. $(".optional_count").css("display", "none");
  155. }
  156. $('.tab.whole').parent('li').hide();
  157. // $('.checkbox.other').attr('disabled',true);
  158. initResult()
  159. }
  160. }
  161. function initResult() {
  162. var isAll = areaData.data.buyset.areacount;
  163. var p = areaData.data.area;
  164. var count = areaData.data.buyset.citys;
  165. // 添加可选择城市文字 和自定义属性值
  166. if (areaData.data.buyset.areacount != -1) {
  167. for (const c in count) {
  168. $('.province').each(function () {
  169. var t = $(this).text().trim();
  170. if (c == t) {
  171. $(this).attr("data-count", count[c]);
  172. $(this).after('<em class="optional">可选择 ' + count[c] + '个市</em>')
  173. var that = $(this).parent('.tab:not(.municipality)');
  174. that.bind('click', function () {
  175. slideFun($(this));
  176. })
  177. }
  178. })
  179. }
  180. }
  181. /*else{ //购买全国是否可以编辑市
  182. $('.province').each(function () {
  183. var that = $(this).parent('.tab:not(.municipality)');
  184. that.bind('click', function () {
  185. slideFun($(this));
  186. })
  187. })
  188. }*/
  189. // 渲染已选择结果
  190. for (const k in p) {
  191. console.log(p[k], k)
  192. if (p[k].length == 0) {
  193. $('.province').each(function () {
  194. var t = $(this).text().trim();
  195. if (k == t && p[k].length == 0) {
  196. // 如果购买的是全省,则不可点击下拉事件(不可修改城市)
  197. if (isAll != -1) {
  198. $(this).parent().siblings().children('.city').addClass('active').attr('disabled', true);
  199. $(this).parent('.tab').unbind('click').parent('li').siblings().find('.checkbox:not(.half)').parents('.tab').unbind('click')
  200. } else {
  201. $(this).parent().siblings().children('.city').addClass('active').removeAttr('disabled')
  202. }
  203. $(this).children('.checkbox').prop('checked', true)
  204. }
  205. })
  206. } else {
  207. console.log(k, p[k].length, $('.city.active').length)
  208. $('.city').each(function () {
  209. var thisOne=$(this)
  210. let activeLength = thisOne.parents('li').find('.active').length;
  211. let t = thisOne.text().trim();
  212. p[k].forEach(v => {
  213. if (v == t) {
  214. let that = thisOne.parents('li').find('.tab:not(.municipality)');
  215. console.log("v==t")
  216. // 如果是可修改城市的省份,则可以进行下拉展示
  217. that.bind('click', function () {
  218. slideFun($(this));
  219. })
  220. thisOne.addClass('active').removeAttr('disabled').parent().siblings('.tab').find('input').addClass('half');
  221. // 判断购买的地市数与选择的地市是否相等,相等则把其他地市禁用
  222. $('.province').each(function (item, index) {
  223. let dataCount = $(this).attr('data-count');
  224. let activeLength = $(this).parent().siblings().find('.active').length;
  225. // console.log(dataCount)
  226. if (dataCount) {
  227. // console.log(dataCount,activeLength,"object")
  228. if (activeLength >= dataCount) {
  229. $(this).parent().siblings().find('.city').not('.active').attr('disabled', true)
  230. }
  231. }
  232. })
  233. }
  234. })
  235. })
  236. }
  237. }
  238. $('ul.area-list .province[data-count]').parent().on('click', function () {
  239. slideFun($(this));
  240. })
  241. //$('.result_name').append(getAreaClassArr_index(areaData.data.area).join("、"));
  242. $('.count').html($('.checkbox:not(.other):checked').length)
  243. // 如果选中的省份数量等于购买的省份数量,则将其他未选中的省份禁用
  244. if ($('.checkbox:not(.other):checked').length == $('.total').html()) {
  245. $('.checkbox:not(:checked)').attr('disabled', true);
  246. }
  247. $('#enter').attr("disabled", "disabled");
  248. }
  249. $(function () {
  250. // 省下拉事件
  251. /*$('.tab:not(.municipality)').on('click', function () {
  252. if (checkAnimatedRuning()) {
  253. return
  254. }
  255. if ($(this).next('.tab_content:not(:animated)').css("display") == "block") {
  256. $(this).children().children('i').removeClass("up");
  257. } else {
  258. $(this).children().children('i').addClass("up");
  259. }
  260. $(this).toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500);
  261. $(this).parent().siblings().children('.tab').removeClass('selected');
  262. });*/
  263. getData();
  264. // 初始化
  265. init();
  266. /**** 点击checkbox实现onchange事件 *****/
  267. // 1.点击全国按钮onchange事件;
  268. $('.checkbox.other').on('change', function () {
  269. var isChecked = $(this).is(':checked');
  270. if (isChecked) {
  271. $('.count').html('34')
  272. $('.checkbox:not(.other)').prop('checked', false).removeClass('half')
  273. $('.city').removeClass('active');
  274. $('.tab_content').slideUp()
  275. getResult()
  276. } else {
  277. $('.checkbox:not(.other)').removeAttr('disabled')
  278. $('.city').removeAttr('disabled')
  279. getResult()
  280. }
  281. })
  282. // 2.点击非全国按钮onchange事件;
  283. $('.checkbox:not(.other)').on('change', function () {
  284. $('.checkbox.other').prop('checked', false);
  285. var checkedLength = $('.checkbox:checked').length;
  286. var totalLength = $('.total').text();
  287. $('.count').html(checkedLength)
  288. if (checkedLength < totalLength) {
  289. $('.checkbox:not(.other)').removeAttr('disabled')
  290. getResult()
  291. } else {
  292. $('.checkbox:not(:checked)').attr('disabled', true)
  293. if (areaData.data.isTrial) {
  294. showTip('<span style="white-space: nowrap;">您只选择试用了' + totalLength + '个省份</span>,<br><span style="white-space: nowrap;">如需更多,请前往超级订阅进行购买</span>');
  295. } else {
  296. showTip('<span style="white-space: nowrap;">您只购买了' + totalLength + '个省份,如需更多请升级<span></p>')
  297. }
  298. getResult()
  299. }
  300. if ($(this).is(':checked')) {
  301. if (areaData.data.buyset.areacount != -1) {
  302. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active').attr('disabled', true);
  303. } else {
  304. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active');
  305. }
  306. getResult()
  307. } else {
  308. $(this).prop('checked', false).removeClass('half').parent().parent().siblings().find('.city').removeClass('active').removeAttr('disabled');
  309. getResult()
  310. }
  311. })
  312. // 3.点击半选按钮触发的事件
  313. $('.checkbox.half').on('change', function () {
  314. console.log($(this).is(':checked'))
  315. if ($(this).is(':checked')) {
  316. if (areaData.data.buyset.areacount != -1) {
  317. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active').attr('disabled', true);
  318. } else {
  319. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active');
  320. }
  321. getResult()
  322. } else {
  323. $(this).prop('checked', false);
  324. $(this).parent().parent().siblings().find('.city').removeClass('active').removeAttr('disabled');
  325. $(this).parent().siblings('.optional').show();
  326. getResult()
  327. }
  328. })
  329. // 4.点击城市按钮触发的事件
  330. $('.tab_content').on('click', '.city', function () {
  331. var count = $(this).parent().siblings().find('.province').attr('data-count');
  332. $(this).toggleClass('active');
  333. $('.checkbox.other').prop('checked', false);
  334. var isActive = $(this).parent().find('.city.active').length
  335. var cityLength = $(this).parent().find('.city').length
  336. if (isActive === cityLength) {//当选中的城市数量等于该省下所有城市总数时,即为全选
  337. var oInput = $(this).parents('li').find('input.checkbox')
  338. oInput.removeClass('half').prop('checked', true)
  339. getResult()
  340. } else {//半选
  341. $(this).parents('li').find('input.checkbox').addClass('half').prop("checked", false)
  342. if (isActive > 0 && isActive != count) {
  343. $(this).parents('li').find('input.checkbox').addClass('half');
  344. $(this).parent('div').find('.city:not(.active)').removeAttr('disabled')
  345. getResult()
  346. } else if (isActive > 0 && isActive == count) {
  347. $(this).parents('li').find('input.checkbox').addClass('half');
  348. $(this).parent('div').find('.city:not(.active)').attr('disabled', true)
  349. //提示订阅
  350. if (areaData.data.isTrial) {
  351. showTip('<span style="white-space: nowrap;">您只选择试用了' + $(this).parents('li').find(".province").attr("data-count") + '个市</span>,<br><span style="white-space: nowrap;">如需更多,请前往超级订阅进行购买</span>');
  352. } else {
  353. showTip('<span style="white-space: nowrap;">您只购买了' + $(this).parents('li').find(".province").attr("data-count") + '个市,如需更多请升级<span></p>')
  354. }
  355. getResult()
  356. } else {
  357. $(this).parent('div').find('.city:not(.active)').removeAttr('disabled')
  358. $(this).parents('li').find('input.checkbox').removeClass('half').prop('checked', false)
  359. getResult()
  360. }
  361. }
  362. })
  363. // 阻止input checkbox选中取消 触发父元素下拉事件
  364. $('.checkbox').click(function (e) {
  365. e.stopPropagation();
  366. })
  367. // 锚点跳转
  368. $("body").on('click', '.slide a', function () {
  369. var s = $(this).html()
  370. if (s == '#') {
  371. return;
  372. }
  373. document.querySelector('#' + s).scrollIntoView({
  374. block: 'center'
  375. });
  376. })
  377. // 取消按钮事件 返回上一页
  378. $('#cancel').click(function () {
  379. // window.history.go(-1)
  380. //$('.result_name').empty()
  381. $('.checkbox').prop('checked', false).removeAttr('disabled');
  382. $('.city').removeClass('active');
  383. $('.tab_content').slideUp();
  384. $('.optional').remove();
  385. $('.tab').find('i').removeClass("up");
  386. init();
  387. })
  388. // 确定修改事件
  389. $('#enter').click(function () {
  390. $('#enter').attr("disabled", "disabled");
  391. $DoPost("/subscribepay/editSub/submit", {
  392. "type": "area",
  393. "value": JSON.stringify(obj)
  394. }, function (r) {
  395. if (r.success) {
  396. window.location.replace('/front/vipsubscribe/submitEditSubFinish');
  397. }
  398. $('#enter').removeAttr("disabled");
  399. }, false);
  400. })
  401. });
  402. function theSameAs(select) {
  403. var tmp1 = getAreaClassArr(select);
  404. var tmp2 = getAreaClassArr(areaData.data.area);
  405. return (JSON.stringify(tmp1[0].sort()) === JSON.stringify(tmp2[0].sort())) && (JSON.stringify(tmp1[1].sort()) === JSON.stringify(tmp2[1].sort()));
  406. }
  407. function showTip(msg) {
  408. weui.toast(msg, {
  409. duration: 2000,
  410. className: 'custom-toast',
  411. });
  412. }