editArea.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. let provinceText = $(this).text().trim();//省份名称
  86. let isChecked = $(this).children('.checkbox').is(':checked');
  87. let isHalf = $(this).children('.checkbox').hasClass('half');
  88. let arr = [];
  89. if (isChecked) {
  90. obj[provinceText] = [];
  91. } else if (isHalf && !isChecked) {
  92. let $that = $(this);
  93. obj[$that.text().trim()] = [];
  94. $that.parent().siblings().find('.city').each(function () {
  95. let 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. let p = areaData.data.area;
  115. let 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. let pLength = Object.keys(areaData.data.area).length;
  130. let 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. // $('.checkbox:not(.other)').attr('disabled',true)
  142. //$('.result_name').append(`<span>全国</span>`);
  143. } else if (areaData.data.buyset.areacount == -1 && pLength != 0) {
  144. // 当购买的全国而没有选择全国,选择部分省市时
  145. //$('.all_area').show();
  146. $('.province').removeAttr('data-count');
  147. $('.optional_count').hide();
  148. $('.total').html('34');
  149. initResult()
  150. } else {
  151. //$('.all_area').hide()
  152. $('.optional_count').show()
  153. $('.total').html(areaData.data.buyset.areacount)
  154. $('.tab.whole').parent('li').hide();
  155. // $('.checkbox.other').attr('disabled',true);
  156. initResult()
  157. }
  158. }
  159. function initResult() {
  160. let isAll = areaData.data.buyset.areacount;
  161. let p = areaData.data.area;
  162. let count = areaData.data.buyset.citys;
  163. // 渲染已选择结果
  164. for (const k in p) {
  165. console.log(p[k], k)
  166. if (p[k].length == 0) {
  167. $('.province').each(function () {
  168. let t = $(this).text().trim();
  169. if (k == t && p[k].length == 0) {
  170. // 如果购买的是全省,则不可点击下拉事件(不可修改城市)
  171. if (isAll != -1) {
  172. $(this).parent().siblings().children('.city').addClass('active').attr('disabled', true);
  173. $(this).parent('.tab').unbind('click').parent('li').siblings().find('.checkbox:not(.half)').parents('.tab').unbind('click')
  174. } else {
  175. $(this).parent().siblings().children('.city').addClass('active').removeAttr('disabled')
  176. }
  177. $(this).children('.checkbox').prop('checked', true)
  178. }
  179. })
  180. } else {
  181. console.log(k, p[k].length)
  182. $('.city').each(function () {
  183. let t = $(this).text().trim();
  184. p[k].forEach(v => {
  185. // console.log(v,t)
  186. if (v == t) {
  187. let that = $(this).parents('li').find('.tab:not(.municipality)');
  188. console.log("v==t")
  189. // 如果是可修改城市的省份,则可以进行下拉展示
  190. that.bind('click', function () {
  191. slideFun($(this));
  192. })
  193. $(this).addClass('active').removeAttr('disabled').parent().siblings('.tab').find('input').addClass('half');
  194. return;
  195. }
  196. })
  197. })
  198. for (const c in count) {
  199. if (p[k].length >= count[c]) {
  200. if (isAll != -1) {
  201. $('.city:not(.active)').attr('disabled', true)
  202. } else {
  203. $('.city:not(.active)').removeAttr('disabled')
  204. }
  205. }
  206. }
  207. }
  208. }
  209. // 添加可选择城市文字 和自定义属性值
  210. if (areaData.data.buyset.areacount != -1) {
  211. for (const c in count) {
  212. $('.province').each(function () {
  213. let t = $(this).text().trim();
  214. if (c == t) {
  215. $(this).attr("data-count", count[c]);
  216. $(this).after(`<em class="optional">可选择 ${count[c]} 个市</em>`)
  217. let that = $(this).parent('.tab:not(.municipality)');
  218. that.bind('click', function () {
  219. slideFun($(this));
  220. })
  221. }
  222. })
  223. }
  224. }
  225. //$('.result_name').append(getAreaClassArr_index(areaData.data.area).join("、"));
  226. $('.count').html($('.checkbox:not(.other):checked').length)
  227. // 如果选中的省份数量等于购买的省份数量,则将其他未选中的省份禁用
  228. if ($('.checkbox:not(.other):checked').length == $('.total').html()) {
  229. $('.checkbox:not(:checked)').attr('disabled', true);
  230. }
  231. $('#enter').attr("disabled", "disabled");
  232. }
  233. $(function () {
  234. // 省下拉事件
  235. $('.tab:not(.municipality)').on('click', function () {
  236. if (checkAnimatedRuning()) {
  237. return
  238. }
  239. if ($(this).next('.tab_content:not(:animated)').css("display") == "block") {
  240. $(this).children().children('i').removeClass("up");
  241. } else {
  242. $(this).children().children('i').addClass("up");
  243. }
  244. $(this).toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500);
  245. $(this).parent().siblings().children('.tab').removeClass('selected');
  246. });
  247. getData();
  248. // 初始化
  249. init();
  250. /**** 点击checkbox实现onchange事件 *****/
  251. // 1.点击全国按钮onchange事件;
  252. $('.checkbox.other').on('change', function () {
  253. let isChecked = $(this).is(':checked');
  254. if (isChecked) {
  255. $('.count').html('34')
  256. $('.checkbox:not(.other)').prop('checked', false).removeClass('half')
  257. $('.city').removeClass('active');
  258. $('.tab_content').slideUp()
  259. getResult()
  260. } else {
  261. $('.checkbox:not(.other)').removeAttr('disabled')
  262. $('.city').removeAttr('disabled')
  263. getResult()
  264. }
  265. })
  266. // 2.点击非全国按钮onchange事件;
  267. $('.checkbox:not(.other)').on('change', function () {
  268. $('.checkbox.other').prop('checked', false);
  269. let checkedLength = $('.checkbox:checked').length;
  270. let totalLength = $('.total').text();
  271. $('.count').html(checkedLength)
  272. if (checkedLength < totalLength) {
  273. $('.checkbox:not(.other)').removeAttr('disabled')
  274. getResult()
  275. } else {
  276. $('.checkbox:not(:checked)').attr('disabled', true)
  277. if (areaData.data.isTrial) {
  278. showTip(`<span style="white-space: nowrap;">您只选择试用了` + totalLength + `个省份</span>,<span style="white-space: nowrap;">如需更多,请前往VIP订阅进行购买</span>`);
  279. } else {
  280. showTip(`<span style="white-space: nowrap;">您只购买了` + totalLength + `个省份,如需更多请升级<span></p>`)
  281. }
  282. getResult()
  283. }
  284. if ($(this).is(':checked')) {
  285. if (areaData.data.buyset.areacount != -1) {
  286. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active').attr('disabled', true);
  287. } else {
  288. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active');
  289. }
  290. getResult()
  291. } else {
  292. $(this).prop('checked', false).removeClass('half').parent().parent().siblings().find('.city').removeClass('active').removeAttr('disabled');
  293. getResult()
  294. }
  295. })
  296. // 3.点击半选按钮触发的事件
  297. $('.checkbox.half').on('change', function () {
  298. console.log($(this).is(':checked'))
  299. if ($(this).is(':checked')) {
  300. if (areaData.data.buyset.areacount != -1) {
  301. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active').attr('disabled', true);
  302. } else {
  303. $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active');
  304. }
  305. getResult()
  306. } else {
  307. $(this).prop('checked', false);
  308. $(this).parent().parent().siblings().find('.city').removeClass('active').removeAttr('disabled');
  309. $(this).parent().siblings('.optional').show();
  310. getResult()
  311. }
  312. })
  313. // 4.点击城市按钮触发的事件
  314. $('.tab_content').on('click', '.city', function () {
  315. let count = $(this).parent().siblings().find('.province').attr('data-count');
  316. $(this).toggleClass('active');
  317. $('.checkbox.other').prop('checked', false);
  318. var isActive = $(this).parent().find('.city.active').length
  319. let cityLength = $(this).parent().find('.city').length
  320. if (isActive === cityLength) {//当选中的城市数量等于该省下所有城市总数时,即为全选
  321. var oInput = $(this).parents('li').find('input.checkbox')
  322. oInput.removeClass('half').prop('checked', true)
  323. getResult()
  324. } else {//半选
  325. $(this).parents('li').find('input.checkbox').addClass('half').prop("checked", false)
  326. if (isActive > 0 && isActive != count) {
  327. $(this).parents('li').find('input.checkbox').addClass('half');
  328. $(this).parent('div').find('.city:not(.active)').removeAttr('disabled')
  329. getResult()
  330. } else if (isActive > 0 && isActive == count) {
  331. $(this).parents('li').find('input.checkbox').addClass('half');
  332. $(this).parent('div').find('.city:not(.active)').attr('disabled', true)
  333. //提示订阅
  334. if (areaData.data.isTrial) {
  335. showTip(`<span style="white-space: nowrap;">您只选择试用了` + $(this).parents('li').find(".province").attr("data-count") + `个市</span>,<span style="white-space: nowrap;">如需更多,请前往VIP订阅进行购买</span>`);
  336. } else {
  337. showTip(`<span style="white-space: nowrap;">您只购买了` + $(this).parents('li').find(".province").attr("data-count") + `个市,如需更多请升级<span></p>`)
  338. }
  339. getResult()
  340. } else {
  341. $(this).parent('div').find('.city:not(.active)').removeAttr('disabled')
  342. $(this).parents('li').find('input.checkbox').removeClass('half').prop('checked', false)
  343. getResult()
  344. }
  345. }
  346. })
  347. // 阻止input checkbox选中取消 触发父元素下拉事件
  348. $('.checkbox').click(function (e) {
  349. e.stopPropagation();
  350. })
  351. // 锚点跳转
  352. $("body").on('click', '.slide a', function () {
  353. var s = $(this).html()
  354. if (s == '#') {
  355. return;
  356. }
  357. document.querySelector('#' + s).scrollIntoView({
  358. block: 'center'
  359. });
  360. })
  361. // 取消按钮事件 返回上一页
  362. $('#cancel').click(function () {
  363. // window.history.go(-1)
  364. //$('.result_name').empty()
  365. $('.checkbox').prop('checked', false).removeAttr('disabled');
  366. $('.city').removeClass('active');
  367. $('.tab_content').slideUp();
  368. $('.optional').remove();
  369. $('.tab').find('i').removeClass("up");
  370. init();
  371. })
  372. // 确定修改事件
  373. $('#enter').click(function () {
  374. $('#enter').attr("disabled", "disabled");
  375. $DoPost("/subscribepay/editSub/submit", {
  376. "type": "area",
  377. "value": JSON.stringify(obj)
  378. }, function (r) {
  379. if (r.success) {
  380. window.location.replace('/front/vipsubscribe/submitEditSubFinish');
  381. }
  382. $('#enter').removeAttr("disabled");
  383. }, false);
  384. })
  385. });
  386. function theSameAs(select) {
  387. let tmp1 = getAreaClassArr(select);
  388. let tmp2 = getAreaClassArr(areaData.data.area);
  389. return (JSON.stringify(tmp1[0].sort()) === JSON.stringify(tmp2[0].sort())) && (JSON.stringify(tmp1[1].sort()) === JSON.stringify(tmp2[1].sort()));
  390. }
  391. function showTip(msg) {
  392. weui.toast(msg, {
  393. duration: 2000,
  394. className: 'custom-toast',
  395. });
  396. }