change_area.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. $(function () {
  2. // 后端数据结构
  3. var areaData = {
  4. 'data': {
  5. // 选择的数量
  6. 'area': {
  7. '北京': [],
  8. '河南': [
  9. '洛阳市',
  10. '驻马店市',
  11. '周口市',
  12. '郑州市'
  13. ],
  14. '湖南': ['长沙市'],
  15. '澳门': [],
  16. '甘肃': []
  17. },
  18. 'buyset': {
  19. // 购买的全省的数量(不带市)
  20. 'areacount': 5,
  21. 'buyerclasscount': 11,
  22. // 购买的城市
  23. 'newcitys': []
  24. }
  25. },
  26. 'errMsg': '',
  27. 'success': true
  28. };
  29. // 已经购买过的城市数量
  30. var alreadyBuy = {
  31. country: 0,
  32. province: 0,
  33. city: {
  34. // 一共买了多少个城市
  35. totalCount: 0,
  36. // 分布在几个省份
  37. pCount: 0
  38. }
  39. }
  40. // ----------------- 以下定义功能函数 ----------------------------
  41. function createMoreCity(arr) {
  42. var tempHtml = arr.map(function (v) {
  43. return '<button class="city">' + v.name + '</button>'
  44. }).join('')
  45. return tempHtml
  46. }
  47. // 数组求和
  48. function arrSum(arr) {
  49. var sum = 0
  50. arr.forEach(function (item, i) {
  51. sum += item
  52. })
  53. return sum
  54. }
  55. // 是否锁定按钮
  56. function isLockedTipButtons(f) {
  57. $('.tips_btn button.save-btn').prop('disabled', f)
  58. }
  59. // 省下拉市事件
  60. var isAnimating = false
  61. function slideFunc() {
  62. if (isAnimating) return
  63. isAnimating = true
  64. if ($(this).next('.tab_content:not(:animated)').css("display") == "block") {
  65. $(this).children().children('i').removeClass('rotate-arrow')
  66. } else {
  67. $(this).children().children('i').addClass('rotate-arrow')
  68. }
  69. $(this).toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500)
  70. $(this).parent().siblings().children('.tab').removeClass('selected');
  71. setTimeout(function () {
  72. isAnimating = false
  73. }, 500)
  74. }
  75. // dev4.5.5选中区域文案修改
  76. var new_selected
  77. // 新增求值 ---- 统计数量,不做视图操作
  78. function getResult() {
  79. var selectedCount = {
  80. country: 0,
  81. province: 0,
  82. city: {
  83. // 一共选了多少个城市
  84. totalCount: 0,
  85. // 分布在几个省份
  86. pCount: 0
  87. }
  88. }
  89. // 获取所有点亮的城市的值
  90. var selected = getActiveCityDetail()
  91. if (Object.keys(selected).length === 0) {
  92. isLockedTipButtons(true)
  93. } else {
  94. isLockedTipButtons(false)
  95. }
  96. //是否需要升级
  97. if (needUpgrade(getBuySet(selected, areaData.data.industry), {
  98. areacount: areaData.data.buyset.areacount,
  99. citys: areaData.data.buyset.newcitys,
  100. buyerclasscount: areaData.data.buyset.buyerclasscount
  101. })[1]) {
  102. if (areaData.data.isTrial) {
  103. $(".result-selected .info-tip").text("已超出现订单购买范围,需购买");
  104. }
  105. $(".result-selected .info-tip").show();
  106. } else {
  107. $(".result-selected .info-tip").hide();
  108. }
  109. for (var p in selected) {
  110. if (p === '全国') {
  111. selectedCount = {
  112. country: -1,
  113. province: 0,
  114. city: {
  115. totalCount: 0,
  116. pCount: 0
  117. }
  118. }
  119. new_selected = selected
  120. return setDataInResult('.result-selected .result-info .info-overview', getBuySet(selected, []));
  121. }
  122. if (selected[p].length === 0) {
  123. selectedCount.province++
  124. } else {
  125. selectedCount.city.pCount++
  126. selected[p].forEach(function (item, index) {
  127. selectedCount.city.totalCount++
  128. })
  129. }
  130. }
  131. if ($.isEmptyObject(selected)) {
  132. // 1. 隐藏提示,并清空已选择
  133. $('.result .info-tip').hide().siblings('.info-overview').text('');
  134. } else {
  135. new_selected = selected
  136. setDataInResult('.result-selected .result-info .info-overview', getBuySet(selected, []));
  137. }
  138. }
  139. // 参数1:selector 需要赋值的选择器
  140. // 参数2:selectedData 数据
  141. // 注意:selectedData的数据结构
  142. function setDataInResult (selector ,selectedData) {
  143. // selectedData = {
  144. // areacount: 2,
  145. // buyerclasscount: 2,
  146. // newcitys: []
  147. // }
  148. // 设置已选择区域
  149. var citySum = arrSum(selectedData.citys);
  150. var text = {
  151. p: selectedData.areacount === 0 ? '' : selectedData.areacount + '个省',
  152. c: citySum === 0 ? '' : citySum + '个市',
  153. s: selectedData.citys.length === 1 ? '' : '(分布在' + selectedData.citys.length + '个省内)'
  154. };
  155. if (selectedData.areacount === -1) {
  156. $(selector).text('全国')
  157. } else {
  158. var dunhao = '';
  159. if (citySum !== 0 && selectedData.areacount !== 0) {
  160. dunhao = '、'
  161. }
  162. if (citySum === 0 || citySum === 1) {
  163. text.s = ''
  164. }
  165. let proStr = ''
  166. Object.keys(new_selected).forEach(function(item,index) {
  167. if(new_selected[item].length == 0) {
  168. proStr += item + '、'
  169. } else {
  170. proStr += item+ '(' + new_selected[item].length+ '个市' + ')' + '、'
  171. }
  172. })
  173. if(proStr[proStr.length - 1] == '、') {
  174. proStr = proStr.substr(0,proStr.length - 1);
  175. }
  176. // $(selector).text(text.p + dunhao + text.c + text.s);
  177. $(selector).text(Object.keys(new_selected).length+'个省:'+proStr + ',');
  178. }
  179. }
  180. // 点亮的城市数量详情(需要省和城市名称)
  181. function getActiveCityDetail() {
  182. var isWholeCountry = $('.tab.whole input').is(':checked')
  183. if (isWholeCountry) {
  184. // 选择全国
  185. var str = $('.area-list li .tab.whole .province').text().replace(/\s+ | [\r\n]/g, '')
  186. var obj = {}
  187. obj[str] = []
  188. return obj
  189. } else {
  190. var arr = []
  191. $('.area-list li:not(.index)').each(function (i, dom) {
  192. var $dom = $(dom)
  193. if ($dom.children('.tab').hasClass('whole')) return
  194. var isMunicipality = $dom.find('.tab').hasClass('municipality')
  195. var pName = $dom.find('.province').text().replace(/\s+ | [\r\n]/g, '')
  196. var selectedObj = {
  197. name: pName,
  198. children: []
  199. }
  200. // 是否是直辖市或自治区
  201. if (isMunicipality) {
  202. if ($dom.find('input:not([disabled])').is(':checked')) {
  203. arr.push(selectedObj)
  204. }
  205. } else {
  206. var allCitylength = $dom.find('button').length
  207. if ($dom.find('button.active').length === allCitylength) {
  208. arr.push(selectedObj)
  209. } else {
  210. $dom.find('button.active').each(function (c, buttonDOM) {
  211. var cName = $(buttonDOM).text().replace(/\s+ | [\r\n]/g, '')
  212. selectedObj.children.push(cName)
  213. })
  214. if (selectedObj.children.length) {
  215. arr.push(selectedObj)
  216. }
  217. }
  218. }
  219. })
  220. // 处理arr
  221. // [
  222. // {
  223. // name: '安徽',
  224. // children: ["合肥市", "蚌埠市", "阜阳市"]
  225. // },
  226. // {
  227. // name: '海南',
  228. // children: ["三亚市", "文昌市"]
  229. // },
  230. // {
  231. // name: '河南',
  232. // children: ["郑州市", "信阳市"]
  233. // }
  234. // ]
  235. var selectedCityDetail = {}
  236. arr.forEach(function (item, i) {
  237. selectedCityDetail[item.name] = item.children
  238. })
  239. return selectedCityDetail
  240. }
  241. }
  242. // 初始选择的城市恢复,根据传入的p恢复为Active蓝色按钮
  243. // 需要放到init之后
  244. function renderSelectedCityActive(p) {
  245. if (Object.keys(p).length === 0) {
  246. $('.area-list li:not(.index):eq(0)').find('input').trigger('click')
  247. return
  248. }
  249. for (const k in p) {
  250. if (k === '全国') {
  251. $('.area-list li:not(.index):eq(0)').find('input').trigger('click')
  252. return
  253. }
  254. if (p[k].length == 0) {
  255. // 购买的省份
  256. $('.province').each(function () {
  257. var t = $(this).text().trim();
  258. if (k == t && p[k].length == 0) {
  259. var li = $(this).parents('li')
  260. // 改变按钮状态
  261. li.find('.city').addClass('active')
  262. // 改变input的状态
  263. li.find('input.checkbox').prop('checked', true)
  264. }
  265. })
  266. } else {
  267. $('.city').each(function () {
  268. var t = $(this).text().trim();
  269. p[k].forEach(v => {
  270. if (v !== t) return
  271. // 改变按钮状态
  272. $this = $(this)
  273. $this.addClass('active')
  274. // 改变input的样式
  275. $this.parents('li').find('input.checkbox').addClass('opt')
  276. })
  277. })
  278. }
  279. }
  280. }
  281. function resetButtonLock(f) {
  282. $('#cancel').prop('disabled', f)
  283. }
  284. // 根据数据进行求总数 --- 上方已购买数据信息的初始化
  285. function initAlreadyBuy(buyset, citys) {
  286. // 购买的全省的数量(不包含市)
  287. alreadyBuy.province = buyset.areacount;
  288. // 购买的城市总数量
  289. alreadyBuy.city.pCount = citys.length
  290. alreadyBuy.city.totalCount = arrSum(citys)
  291. var text = {
  292. p: alreadyBuy.province === 0 ? '' : alreadyBuy.province + '个省',
  293. c: alreadyBuy.city.totalCount === 0 ? '' : alreadyBuy.city.totalCount + '个市',
  294. s: alreadyBuy.city.pCount === 1 ? '' : '(分布在' + alreadyBuy.city.pCount + '个省内)'
  295. }
  296. if (buyset.areacount === -1) {
  297. $('.result-purchased .result-info .buy-set-info').text('全国')
  298. } else {
  299. var dunhao = '';
  300. if (alreadyBuy.city.totalCount !== 0 && alreadyBuy.province !== 0) {
  301. dunhao = '、'
  302. }
  303. if (alreadyBuy.city.totalCount === 0 || alreadyBuy.city.totalCount === 1) {
  304. text.s = ''
  305. }
  306. $('.result-purchased .result-info .buy-set-info').text(text.p + dunhao + text.c + text.s);
  307. }
  308. }
  309. // 初始化方法
  310. function init() {
  311. var data = areaData.data
  312. var buyset = data.buyset;
  313. var citys = buyset.newcitys;
  314. initAlreadyBuy(buyset, citys)
  315. // 根据返回数据进行渲染(默认选中已选择的)
  316. renderSelectedCityActive(data.area)
  317. getResult()
  318. }
  319. // ----------------- 以下绑定事件 ----------------------------
  320. // 0.渲染城市列表
  321. $(".select-area-box ul li:not('.index')").each(function () {
  322. var text = $(this).find(".province").text().trim()
  323. var data = null
  324. chinaMapJSON.some(function (v) {
  325. data = v
  326. return v.name.indexOf(text) !== -1
  327. })
  328. var box = $(this).find('div.tab_content')
  329. var html = createMoreCity(data.city)
  330. box.html(html)
  331. })
  332. // fastClick初始化: https://github.com/ftlabs/fastclick
  333. FastClick.attach(document.body)
  334. // 1.省下拉市
  335. $(".select-area-box .tab:not(.municipality)").on('click', slideFunc)
  336. // 2.点击全国按钮onchange事件;
  337. $('.checkbox.other').on('change', function () {
  338. var isChecked = $(this).is(':checked')
  339. if (isChecked) {
  340. $(this).prop('checked', true).parents('.all-country').addClass('active');
  341. $('.checkbox:not(.other):not([disabled])').prop('checked', false).removeClass('opt');
  342. $('.city:not([disabled])').removeClass('active');
  343. $('.tab_content').slideUp();
  344. } else{
  345. $(this).prop('checked', false).parents('.all-country').removeClass('active');
  346. }
  347. getResult()
  348. })
  349. // 3.点击非全国按钮onchange事件
  350. $('.checkbox:not(.other)').on('change', function () {
  351. $('.checkbox.other').prop('checked', false).parents('.all-country').removeClass('active');
  352. var isChecked = $(this).is(':checked');
  353. if (isChecked) {
  354. $(this).prop('checked', true).removeClass('opt');
  355. $(this).parents('li').find('.city').addClass('active');
  356. // 控制点击checkbox时候是否展开下方的城市列表
  357. // $(this).parent().parent('.tab:not(.municipality)').siblings('.tab_content').slideDown();
  358. } else {
  359. $(this).prop('checked', false);
  360. $(this).parents('li').find('.city').removeClass('active');
  361. }
  362. getResult();
  363. })
  364. // 4.点击城市按钮触发的事件
  365. $('.tab_content').on('click', '.city', function () {
  366. // 全国取消选中
  367. $('.checkbox.other').prop('checked', false).parents('.all-country').removeClass('active');;
  368. $(this).toggleClass('active')
  369. // 省下所有选中的地市数量
  370. var activeCityLength = $(this).parent().find('.city.active').length;
  371. // 省下一共有几个城市
  372. var cityLength = $(this).parent().find('.city').length;
  373. var oInput = $(this).parents('li').find('input.checkbox')
  374. if (activeCityLength === cityLength) {//当选中的城市数量等于该省下所有城市总数时,即为全选
  375. if ($(this).siblings('.city[disabled]').length === 0) {
  376. oInput.removeClass('opt').prop('checked', true)
  377. }
  378. } else if (activeCityLength === 0) {
  379. oInput.prop('checked', false).removeClass('opt')
  380. } else {//半选
  381. oInput.prop("checked", false).addClass('opt')
  382. }
  383. getResult();
  384. })
  385. // 5. 重置按钮点击事件
  386. $('.reset-btn').on('click', function () {
  387. alreadyBuy = {
  388. country: 0,
  389. province: 0,
  390. city: {
  391. // 一共买了多少个城市
  392. totalCount: 0,
  393. // 分布在几个省份
  394. pCount: 0
  395. }
  396. }
  397. $('.checkbox').prop('checked', false).removeAttr('disabled');
  398. $('.all-country').removeClass('active')
  399. $('.city').removeClass('active');
  400. $('input').removeClass('opt')
  401. $('.tab_content').slideUp();
  402. $('.tab .iconfont').removeClass('rotate-arrow')
  403. init();
  404. })
  405. // 6.锚点跳转
  406. $("body").on('click', '.slide a', function () {
  407. var s = $(this).html()
  408. if (s == '#') {
  409. return;
  410. }
  411. document.querySelector('#' + s).scrollIntoView({
  412. block: 'center'
  413. });
  414. })
  415. // 阻止input checkbox选中取消 触发父元素下拉事件
  416. $('.checkbox').click(function (e) {
  417. e.stopPropagation();
  418. })
  419. // ----------------------- 以下数据交互 ------------------
  420. function getDataWitXHR() {
  421. $DoPost('/subscribepay/vipsubscribe/getSubBuyMsg', {}, function (res) {
  422. if (!res.success) return
  423. if(!res.data.area) {
  424. res.data.area = {}
  425. }
  426. if(!res.data.industry) {
  427. res.data.industry = []
  428. }
  429. areaData.data = res.data
  430. res.data.oldArea = res.data.area;
  431. res.data.oldIndustry = res.data.industry;
  432. sessionStorage.setItem('sub_vip_state', JSON.stringify(res.data))
  433. init()
  434. })
  435. }
  436. // 2021-5-21修改 需要每次都从接口里查询订阅条件
  437. var subVipState = sessionStorage.getItem('sub_vip_state')
  438. if (subVipState && !getParam('header')) {
  439. areaData.data = JSON.parse(subVipState)
  440. init()
  441. } else {
  442. getDataWitXHR()
  443. }
  444. // 确认按钮事件
  445. $('.save-btn').on('click', function () {
  446. // 获取点亮的城市详情
  447. areaData.data.area = getActiveCityDetail()
  448. // 超级订阅新套餐 修改订阅条件需要提交后台保存, 因是共用页面,为不影响其他页面逻辑,url加上参数进行判断
  449. var params = {
  450. area: JSON.stringify(areaData.data.area).indexOf('全国') > -1 ? '{}' : JSON.stringify(areaData.data.area),
  451. industry: (areaData.data.industry).join(',')
  452. }
  453. if (getParam('header') === 'save') {
  454. let url = ''
  455. if(getParam('subvip') === 'free') {
  456. url = '/subscribepay/vipsubscribe/freeSaveChange'
  457. } else {
  458. url = '/subscribepay/vipsubscribe/saveChange'
  459. }
  460. $DoPost(url, params, function (res) {
  461. if (res.data && res.data.doSuccess) {
  462. weui.toast('修改成功', {
  463. duration: 2000,
  464. className: 'custom-toast',
  465. callback: function() {
  466. history.back()
  467. }
  468. });
  469. } else {
  470. weui.toast(res.errMsg, {
  471. duration: 2000,
  472. className: 'custom-toast'
  473. });
  474. }
  475. })
  476. } else {
  477. sessionStorage.setItem('sub_vip_state', JSON.stringify(areaData.data))
  478. history.back()
  479. }
  480. })
  481. // 获取点亮的城市的列表详情(带省名和城市名)
  482. // getActiveCityDetail()
  483. // 回显为蓝色激活样式
  484. // renderSelectedCityActive()
  485. })