choose_area.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. var AreaChoose = {
  2. selectObj: {},//已选择,未购买城市;
  3. selectedIndustryArr: ["一个行业"],//计算价格临时变量
  4. timeSelect: [1, 2],//计算价格临时变量 timeSelect[0]时长 timeSelect[1] 1:年 2:月
  5. vipSubisTrial: false,
  6. inintData: function () {
  7. //已选择
  8. try {
  9. let select = sessionStorage.getItem("vipSubSelectArea");
  10. if (select) {
  11. this.selectObj = JSON.parse(select);
  12. }
  13. if (sessionStorage.getItem("vipSubSelectIndustry")) {
  14. this.selectedIndustryArr = JSON.parse(sessionStorage.getItem("vipSubSelectIndustry"));
  15. }
  16. if (sessionStorage.getItem("vipSubSelectTime")) {
  17. this.timeSelect = JSON.parse(sessionStorage.getItem("vipSubSelectTime"));
  18. }
  19. //是否试用界面
  20. if (sessionStorage.getItem("vipSubisTrial")) this.vipSubisTrial = true;
  21. } catch (e) {
  22. console.log(e);
  23. this.selectObj = {};
  24. }
  25. },
  26. inintPage: function () {//初始化省份城市选项
  27. $(".select-area-box ul li:not('.index')").each(function () {
  28. var text = $(this).find(".province").text().trim()
  29. var data = null
  30. chinaMapJSON.some(function (v) {
  31. data = v
  32. return v.name.indexOf(text) !== -1
  33. })
  34. var box = $(this).find('div.tab_content')
  35. var html = AreaChoose.createMoreCity(data.city)
  36. box.html(html)
  37. })
  38. },
  39. createMoreCity: function (arr) {
  40. var tempHtml = arr.map(function (v) {
  41. return '<button class="city">' + v.name + '</button>'
  42. }).join('')
  43. return tempHtml
  44. },
  45. showSelected: function (canClick) { //回显已选择
  46. let ran = this.selectObj;
  47. for (var province in ran) {
  48. let citys = ran[province]
  49. if (citys.length > 0) {//地市
  50. this.selectCity(citys, canClick)
  51. } else {//省份
  52. this.selectProvince(province, canClick)
  53. }
  54. }
  55. },
  56. selectProvince: function (province, canClick) { //设置选中省份 province 省份
  57. $($('.tab .province')).each(function (index, dom) {
  58. if (province == $.trim($(dom).text())) {
  59. if (province != "全国") {
  60. $(".checkbox.other").attr("checked", false).parents('.all-country').removeClass('active'); //取消全国选中
  61. } else {
  62. $(".checkbox.other").attr("checked", true).parents('.all-country').addClass('active');
  63. }
  64. $(dom).children('.checkbox').attr({"checked": true, "disabled": !canClick});//选中此省份
  65. $(dom).parents().siblings('.tab_content').find(".city").addClass("active").attr({"disabled": !canClick});
  66. }
  67. })
  68. },
  69. selectCity: function (citys, canClick) { //设置选中城市
  70. citys.forEach(v => {
  71. $($('.tab_content .city')).each(function (index, dom) {
  72. if (v == $(dom).text()) {
  73. $(".checkbox.other").attr("checked", false).parents('.all-country').removeClass('active'); //取消全国选中
  74. $(dom).addClass('active').attr('disabled', !canClick).parents().siblings('.tab')
  75. .addClass('selected').children('.province').children('.checkbox').attr({
  76. "checked": true
  77. }).addClass('opt');
  78. }
  79. })
  80. })
  81. },
  82. getResult: function (changeCity, isFirst) { /* 选中结果 */
  83. //如果选中所有,则转为全国
  84. if ($(".city").length == $(".city.active").length) {
  85. $(".checkbox.other").trigger("click");
  86. $(".checkbox").removeClass("opt");
  87. return
  88. }
  89. var tmpSelect = {};
  90. $('.result_name').empty()
  91. var data = []; //定义一个总数组
  92. var cityArr = []; //定义一个选中城市数组
  93. var val = $('.other').parents('.province').text().trim();
  94. if ($('.other').is(':checked')) {
  95. data = [{name: val, children: []}] //全国
  96. //$(".tips_d_text").slideUp();
  97. }
  98. $(".tab_content").find(".city.active:not('[disabled]')").each(function () {
  99. var length = $(this).parent('.tab_content').find('.city').length; //省份下城市的length
  100. var activeLeng = $(this).parent('.tab_content').find('.city.active').length; //省份下选中城市的length
  101. let arr = [];
  102. // 判断省份下的城市是否等于选中的城市长度,相等就是全部选中
  103. if (length == activeLeng) {
  104. // data.push($(this).parent('.tab_content').siblings('.tab:not(.whole)').children('.province').text().trim())
  105. let province = $(this).parent('.tab_content').siblings('.tab:not(.whole)').children('.province').text().trim();
  106. data.push({
  107. name: province,
  108. children: []
  109. })
  110. } else {
  111. let province = $(this).parent('.tab_content').siblings('.tab:not(.whole)').children('.province').text().trim();
  112. let city = $(this).html()
  113. arr.push({name: city, parent: province})
  114. for (let i = 0; i < arr.length; i++) {
  115. let obj = {};
  116. obj.name = arr[i].name;
  117. obj.parent = arr[i].parent;
  118. cityArr.push(obj)
  119. }
  120. data.push({
  121. name: province,
  122. children: cityArr
  123. })
  124. }
  125. })
  126. //数组对象去重
  127. let obj = {};
  128. data = data.reduce((cur, next) => {
  129. obj[next.name] ? "" : obj[next.name] = true && cur.push(next);
  130. return cur
  131. }, [])
  132. // 数组筛选
  133. let result = data.map(v => {
  134. let filterArr = v.children.filter(s => {
  135. return s.parent === v.name
  136. })
  137. return {
  138. name: v.name,
  139. children: filterArr
  140. }
  141. })
  142. var html = '';
  143. var selectProvince = 0;
  144. var selectCityMax = 0;
  145. for (var i = 0; i < result.length; i++) {
  146. let children = result[i].children;
  147. if (children.length > 0) {
  148. // 判断每个省份下选中的城市长度,当大于等于4的时候 弹出"选择全省更划算的弹窗"
  149. let childrenArr = [];
  150. //判断操作城市省份下城市的数量
  151. if (result[i].name === $.trim(changeCity)) {
  152. selectCityMax = children.length;
  153. }
  154. html += `${result[i].name}(`
  155. for (var j = 0; j < children.length; j++) {
  156. childrenArr.push(children[j].name)
  157. if (j != children.length - 1) {
  158. html += `${children[j].name}、`
  159. } else {
  160. html += `${children[j].name}`
  161. }
  162. }
  163. if (i != result.length - 1) {
  164. html += `)、`
  165. } else {
  166. html += `)`
  167. }
  168. tmpSelect[result[i].name] = childrenArr;//
  169. } else {
  170. selectProvince++
  171. if (i != result.length - 1) {
  172. html += `${result[i].name}、`
  173. } else {
  174. html += `${result[i].name}`
  175. }
  176. tmpSelect[result[i].name] = [];//
  177. }
  178. }
  179. //提示
  180. /*if (!this.vipSubisTrial) {
  181. if (selectCityMax > 2) {
  182. $('.tips_btn .tips_d_text').text("已选择" + selectCityMax + "个市,建议购买“全省”").slideDown();
  183. } else {
  184. if (selectProvince > 9 && !isFirst) {
  185. $('.tips_btn .tips_d_text').text("已选择" + selectProvince + "个省,建议购买“全国”").slideDown()
  186. } else {
  187. $(".tips_d_text").slideUp();
  188. }
  189. }
  190. }*/
  191. this.selectObj = tmpSelect;
  192. // console.log("this.selectObj:", tmpSelect)
  193. // $('.result_name').append(html)
  194. AreaChoose.isOpen()
  195. // console.log(result)
  196. this.initAlreadyBuy(result)
  197. if (result.length > 0) { //已选择
  198. $('.reset-btn').removeAttr("disabled");
  199. $('.save-btn').removeAttr("disabled");
  200. AreaChoose.flushPrice(); //刷新价格
  201. } else {
  202. $('.save-btn').attr("disabled", "disabled");
  203. AreaChoose.flushPrice(-1); //刷新价格
  204. }
  205. },
  206. submitArea: function () {
  207. let addCity = $('.result_name').text(); //新增的城市
  208. sessionStorage.setItem("vipSubSelectArea", JSON.stringify(AreaChoose.selectObj));
  209. history.go(-1);
  210. },
  211. isAllSelected: function () {
  212. var _f = true
  213. $($('.tab_content .city')).each(function (index, dom) {
  214. if ($(dom).hasClass('active')) {
  215. _f = false
  216. $('.other').prop('checked', false);
  217. }
  218. })
  219. // 如果循环结束仍为true说明 没有全部都被选中
  220. if (_f) {
  221. $('.other').prop('checked', true);
  222. }
  223. },
  224. isOpen: function () {
  225. let pHeight = $('.result_text').height()
  226. let minHeight = $('.result_text').css('min-height')
  227. let rows = Math.round(Math.round(pHeight) / parseFloat(minHeight));
  228. // console.log("高度:" + pHeight, "最小高度" + minHeight, "行数:" + rows)
  229. if (rows == 1) {
  230. $('.packup').hide()
  231. $('.detail').hide()
  232. } else if (rows == 2) {
  233. console.log($('.result_name').html().trim().length)
  234. let length = $('.result_name').html().trim().length;
  235. if (length >= 50) {
  236. $('.detail').show()
  237. $('.packup').hide()
  238. } else {
  239. $('.detail').hide()
  240. $('.packup').hide()
  241. }
  242. } else {
  243. $('.packup').show()
  244. $('.detail').hide()
  245. }
  246. },
  247. inintClick: function () {
  248. $('.detail').click(function () {
  249. $('.result_text').removeClass('line_two')
  250. $(".result").css("padding-bottom", "0.3rem")
  251. $(this).hide();
  252. $('.packup').show()
  253. })
  254. $('.packup').click(function () {
  255. $(".result").css("padding-bottom", "0.2rem")
  256. $('.result_text').addClass('line_two')
  257. $(this).hide();
  258. $('.detail').show()
  259. })
  260. $("body").on('click', '.slide a', function () {
  261. var s = $(this).html()
  262. if (s == '#') {
  263. return;
  264. }
  265. document.querySelector('#' + s).scrollIntoView({block: 'center'});
  266. })
  267. $(".select-area-box .tab:not(.municipality)").on('click', function (event) {
  268. if (AreaChoose.checkAnimatedRuning()) {
  269. return
  270. }
  271. if ($(this).next('.tab_content:not(:animated)').css("display") == "block") {
  272. $(this).children().children('i').css({
  273. "display": "inline-block",
  274. "transform": "rotate(0)"
  275. })
  276. } else {
  277. $(this).children().children('i').css({
  278. "display": "inline-block",
  279. "transform": "rotate(-180deg)"
  280. })
  281. }
  282. $(this).toggleClass('selected').next('.tab_content:not(:animated)').stop(true, true).slideToggle()
  283. $(this).parent().siblings().children('.tab').removeClass('selected');
  284. })
  285. $('.checkbox').click(function (e) {
  286. e.stopPropagation();
  287. })
  288. $('.tab .checkbox').on('click', function () {
  289. if ($(this).hasClass("other")) {//点击全国
  290. $(".province input").prop('checked', false);
  291. $(".city").removeClass('active');
  292. $(this).prop('checked', true).parents('.all-country').addClass('active');
  293. $(".checkbox").removeClass("opt");
  294. } else {
  295. $(".checkbox.other").prop('checked', false).parents('.all-country').removeClass('active');
  296. if (!$(this).is(':checked')) {
  297. if ($(this).hasClass("opt")) {//判断当前是否是半选
  298. $(this).prop("checked", true).removeClass("opt");
  299. $(this).parent().parent().siblings('.tab_content').children('.city').addClass('active');
  300. } else {
  301. $(this).parent().parent().siblings('.tab_content').children('.city').removeClass('active');
  302. if ($(".province .checkbox:checked").length == 0) {//若没有省份被选中 则选中全国
  303. //$(".checkbox.other").prop('checked', true);
  304. $(".checkbox").removeClass("opt");
  305. }
  306. }
  307. } else {
  308. $(this).parent().parent().siblings('.tab_content').children('.city').addClass('active');
  309. }
  310. }
  311. AreaChoose.getResult()
  312. })
  313. //执行已购买城市函操作dom函数
  314. $('.tab_content').on('click', '.city', function () {
  315. $(".checkbox.other").prop('checked', false).parents('.all-country').removeClass('active');;
  316. //AreaChoose.showSelected(false)
  317. $(this).toggleClass('active')
  318. var isActive = $(this).parent().find('.city.active').length
  319. if (isActive) {
  320. var input = $(this).parents('li').find('input.checkbox')
  321. window.input = input
  322. input.prop('checked', true)
  323. if (isActive != $(this).parent().find('.city').length) {//是否全选
  324. $(this).parent('div').siblings('.tab').find('.province .checkbox').addClass('opt');
  325. } else {
  326. $(this).parent('div').siblings('.tab').find('.province .checkbox').removeClass('opt');
  327. }
  328. } else {
  329. $(this).parent('div').siblings('.tab').children().children('.checkbox').prop(
  330. 'checked', false).removeClass('opt');
  331. }
  332. //AreaChoose.isAllSelected()
  333. AreaChoose.getResult($(this).parent('div').siblings('.tab').find('.province').text()) //出入选择的省份
  334. })
  335. /*$('.other').on('change', function () { //选择全国
  336. AreaChoose.reset();
  337. })*/
  338. $('.save-btn').click(function () { // 提交
  339. AreaChoose.submitArea();
  340. })
  341. $('.reset-btn').click(function () { //重置
  342. AreaChoose.reset();
  343. })
  344. },
  345. reset: function () {
  346. $('.tab_content').slideUp(500)
  347. $('.tab span i').css({
  348. "display": "inline-block",
  349. "transform": "rotate(0)"
  350. })
  351. $('.tab').removeClass('selected').children('.province').find(
  352. 'input').prop({
  353. 'checked': false,
  354. "disabled": false
  355. });
  356. $('.tab_content').find('.city').removeClass('active').attr({
  357. "disabled": false,
  358. "checked": false
  359. })
  360. $('.all-country').removeClass('active')
  361. $('input').removeClass('opt')
  362. AreaChoose.getResult()
  363. },
  364. flushPrice: function (flag) {
  365. if (!this.vipSubisTrial) {
  366. $('.tips_d_money').css("display", "block");
  367. var price = getsubVipOrderPrice(this.selectObj, this.selectedIndustryArr, this.timeSelect);
  368. $('.tips_d_money').text('¥' + price);
  369. }
  370. if (flag === -1 || getParam('header') == 'upgrade') {
  371. $('.tips_d_money').css("display", "none");
  372. }
  373. },
  374. animatedRuning: false,
  375. checkAnimatedRuning: function () {
  376. if (AreaChoose.animatedRuning) {
  377. return true
  378. }
  379. AreaChoose.animatedRuning = true;
  380. setTimeout(function () {
  381. AreaChoose.animatedRuning = false;
  382. }, 500);
  383. return false
  384. },
  385. // 根据数据进行求总数
  386. initAlreadyBuy: function(arr) {
  387. var pArr = [];
  388. var hpArr = [];
  389. var cArr = [];
  390. var dunhao = '';
  391. var html= '';
  392. arr.forEach(function(item){
  393. if(item.name == '全国'){
  394. $('.result_name').text('全国')
  395. // html = '全国'
  396. // console.log(html)
  397. } else {
  398. if (item.children.length == 0 || item.children.length > 2){
  399. pArr.push(item.name)
  400. } else {
  401. hpArr.push(item.name);
  402. item.children.forEach(function(s,j){
  403. cArr.push(s)
  404. })
  405. }
  406. }
  407. // console.log(pArr,cArr,hpArr)
  408. if (pArr.length > 0 && cArr.length > 0){
  409. dunhao = '、'
  410. }
  411. var text = {
  412. p: pArr.length > 0 ? pArr.length + '个省' : '',
  413. c: cArr.length > 0 ? dunhao + cArr.length + '个市' : '',
  414. hp: hpArr.length >= 2 ? '(分布在' + hpArr.length+ '个省内)' : ''
  415. };
  416. html = text.p + text.c + text.hp
  417. })
  418. $('.result_name').append(html)
  419. }
  420. };
  421. $(function () {
  422. AreaChoose.isOpen();
  423. AreaChoose.inintData(); //初始化 已选择和已购买数据
  424. AreaChoose.inintPage(); //初始化城市数据
  425. //AreaChoose.getNationwide();//默认选中全国
  426. AreaChoose.showSelected(true);//回显已选择
  427. /* 判断已购买的城市 在不在 全部城市里面,在,将按钮置灰,不可点击 */
  428. AreaChoose.getResult("", true);
  429. AreaChoose.inintClick();
  430. // 升级 解绑下拉选择城市事件
  431. $(".select-area-box .tab:not(.municipality)").unbind('click')
  432. $('.tab_content').unbind('click')
  433. })