updateArea.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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': 7,
  21. // 'buyerclasscount': 11,
  22. // // 购买的城市
  23. // 'citys': {
  24. // '河南': 4,
  25. // '湖南': 3,
  26. // '吉林': 2
  27. // }
  28. // }
  29. // },
  30. // 'errMsg': '',
  31. // 'success': true
  32. // };
  33. var areaData = {
  34. 'data': {
  35. // 选择的数量
  36. 'area': {},
  37. 'buyset': {
  38. // 购买的全省的数量(不带市)
  39. 'areacount': 7,
  40. 'buyerclasscount': 11,
  41. // 购买的城市
  42. 'citys': {}
  43. }
  44. },
  45. 'errMsg': '',
  46. 'success': true
  47. };
  48. // 已经购买过的城市数量
  49. var alreadyBuy = {
  50. province: {
  51. totalCount: 0,
  52. selectedCount: 0
  53. },
  54. city: {
  55. totalCount: 0,
  56. selectedCount: 0
  57. }
  58. }
  59. // 新增城市数量(排除修改的数量)
  60. var newlyAdded = {
  61. province: 0,
  62. city: 0
  63. }
  64. // ----------------- 以下定义功能函数 ----------------------------
  65. function createMoreCity(arr) {
  66. var tempHtml = arr.map(function (v) {
  67. return '<button class="city">' + v.name + '</button>'
  68. }).join('')
  69. return tempHtml
  70. }
  71. // 新增求值 ---- 统计数量,不做视图操作
  72. function getResult0(callback) {
  73. if (areaData.data.buyset.areacount === -1) {
  74. $('.result_text.add_new .added-info').hide()
  75. return
  76. }
  77. // 升级新增清空
  78. $('.result_text.add_new .added-info').text('')
  79. var isWholeSelected = $('.tab.whole input').is(':checked')
  80. var isWholeDisabled = $('.tab.whole input').prop('disabled')
  81. // 已购买省份改变的数量
  82. var dBuySetProvinceCount = 0
  83. // 新增省份改变的数量
  84. var dAddedProvinceCount = 0
  85. // 已购买城市改变的数量
  86. var dBuySetCityCount = 0
  87. // 新增城市的数量
  88. var dAddedCityCount = 0
  89. $('.area-list li:not(.index)').each(function(i, dom){
  90. if ($(dom).children('.tab').hasClass('whole')) return true
  91. // 统计非全国的数量
  92. // 没有'data-buy-city-count'属性的 $(dom).attr('data-buy-city-count') 为undefined
  93. if ($(dom).attr('data-buy-city-count')) {
  94. /******** 此处判断的是已购买的城市下的 ********************/
  95. // 获取购买城市的数量
  96. var buyCount = $(dom).attr('data-buy-city-count') - 0 // 隐式转换成Number
  97. // 获取城市不可点击的数量
  98. var alreadySelectedCount = $(dom).find('button[disabled]').length
  99. // 选择城市的数量(包括不可点击的)
  100. var hasActiveCount = $(dom).find('button.active').length
  101. if (hasActiveCount > buyCount) {
  102. // dBuySetCityCount += buyCount
  103. // dAddedCityCount += (hasActiveCount - buyCount)
  104. if (alreadySelectedCount - hasActiveCount === 0) {
  105. dBuySetCityCount += buyCount
  106. dBuySetProvinceCount ++
  107. } else {
  108. dBuySetCityCount += buyCount
  109. dAddedCityCount += (hasActiveCount - buyCount)
  110. }
  111. } else {
  112. // 操作已购买的数量
  113. dBuySetCityCount += hasActiveCount
  114. }
  115. } else {
  116. // 是不是直辖市或自治区,在有data-buy-count没必要判断
  117. var hasCities = $(dom).find('.tab').hasClass('municipality')
  118. // 是否购买过该省份(还需要判断该省份下面的城市是否全部disabled --- 同时满足才算购买过全省)
  119. var provinceInput = $(dom).find('input')
  120. // 循环着的当前的省份是否可选 和 是否被选中
  121. var isBoughtProvince = provinceInput.prop('disabled')
  122. var isChecked = provinceInput.is(':checked')
  123. // 可得到已经购买的省份的数量(循环完成后可得到,循环过程中的值可能不全)
  124. // 判断是否为购买过的城市
  125. if (isBoughtProvince) {
  126. // 将已购买(选中但不能单击的)城市选中
  127. if (isChecked) {
  128. dBuySetProvinceCount ++
  129. }
  130. } else {
  131. // 判断是否是直辖市或自治区(省份下面没有城市的和有城市的分开计算)
  132. if (hasCities) {
  133. // 直辖市、自治区 -----直接进行省份的操作
  134. // 并且没有购买过该省的,并且选中的
  135. if (isChecked) {
  136. dAddedProvinceCount ++
  137. }
  138. } else {
  139. // 带有市的省份城市(该省份下以前没有购买过任何城市)
  140. // 两种情况:1. 全选 2. 不全选
  141. var allCityLength = $(dom).find('button').length
  142. var otherTotalAddCount = $(dom).find('button.active').length
  143. if (allCityLength === otherTotalAddCount && isChecked) {
  144. // 选中了省份下的所有城市
  145. dAddedProvinceCount ++
  146. } else {
  147. // 对城市做新增操作
  148. dAddedCityCount += otherTotalAddCount
  149. }
  150. }
  151. }
  152. }
  153. })
  154. // 循环完成判断已经选择的省份数量 是否 大于购买过的数量
  155. if (dBuySetProvinceCount + dAddedProvinceCount > alreadyBuy.province.totalCount) {
  156. // 如果已选择的数量和新增的数量大于已购买过的数量,说明我有购买新的城市
  157. dAddedProvinceCount -= (alreadyBuy.province.totalCount - dBuySetProvinceCount)
  158. dBuySetProvinceCount = alreadyBuy.province.totalCount
  159. } else {
  160. dBuySetProvinceCount += dAddedProvinceCount
  161. dAddedProvinceCount = 0
  162. }
  163. // 已购买城市下选项改变,状态更新
  164. alreadyBuy.city.selectedCount = dBuySetCityCount
  165. newlyAdded.city = dAddedCityCount
  166. alreadyBuy.province.selectedCount = dBuySetProvinceCount
  167. newlyAdded.province = dAddedProvinceCount
  168. callback && callback()
  169. setDataInResult()
  170. // 全国被选中了
  171. if (isWholeSelected && !isWholeDisabled) {
  172. $('.result_text.add_new .added-info').text('全国')
  173. $('.result_text.add_new').show()
  174. isLockedTipButtons(false)
  175. }
  176. }
  177. // 新增求值 ---- 统计数量,不做视图操作
  178. function getResult(callback) {
  179. if (areaData.data.buyset.areacount === -1) {
  180. $('.result_text.add_new .added-info').hide()
  181. return
  182. }
  183. // 升级新增清空
  184. $('.result_text.add_new .added-info').text('')
  185. var isWholeSelected = $('.tab.whole input').is(':checked')
  186. var isWholeDisabled = $('.tab.whole input').prop('disabled')
  187. // 已购买省份改变的数量
  188. var dBuySetProvinceCount = 0
  189. // 新增省份改变的数量
  190. var dAddedProvinceCount = 0
  191. // 已购买城市改变的数量
  192. var dBuySetCityCount = 0
  193. // 新增城市的数量
  194. var dAddedCityCount = 0
  195. $('.area-list li:not(.index)').each(function(i, dom){
  196. if ($(dom).children('.tab').hasClass('whole')) return true
  197. // 统计非全国的数量
  198. // 没有'data-buy-city-count'属性的 $(dom).attr('data-buy-city-count') 为undefined
  199. if ($(dom).attr('data-buy-city-count')) {
  200. /******** 此处判断的是已购买的城市下的 ********************/
  201. // 获取购买城市的数量
  202. var buyCount = $(dom).attr('data-buy-city-count') - 0 // 隐式转换成Number
  203. // 获取城市不可点击的数量
  204. var alreadySelectedCount = $(dom).find('button[disabled]').length
  205. // 选择城市的数量(包括不可点击的)
  206. var hasActiveCount = $(dom).find('button.active').length
  207. var allCitylength = $(dom).find('button').length
  208. if (hasActiveCount > buyCount) {
  209. if (alreadySelectedCount - hasActiveCount === 0) {
  210. dBuySetCityCount += buyCount
  211. dBuySetProvinceCount ++
  212. } else if (allCitylength === hasActiveCount) {
  213. // 如果选中全省,则多修改
  214. dBuySetCityCount += buyCount
  215. dAddedProvinceCount ++
  216. } else {
  217. dBuySetCityCount += buyCount
  218. dAddedCityCount += (hasActiveCount - buyCount)
  219. }
  220. } else {
  221. // 操作已购买的数量
  222. dBuySetCityCount += hasActiveCount
  223. }
  224. } else {
  225. // 是不是直辖市或自治区,在有data-buy-count没必要判断
  226. var hasCities = $(dom).find('.tab').hasClass('municipality')
  227. // 是否购买过该省份(还需要判断该省份下面的城市是否全部disabled --- 同时满足才算购买过全省)
  228. var provinceInput = $(dom).find('input')
  229. // 循环着的当前的省份是否可选 和 是否被选中
  230. var isBoughtProvince = provinceInput.prop('disabled')
  231. var isChecked = provinceInput.is(':checked')
  232. // 可得到已经购买的省份的数量(循环完成后可得到,循环过程中的值可能不全)
  233. // 判断是否为购买过的城市
  234. if (isBoughtProvince) {
  235. // 将已购买(选中但不能单击的)城市选中
  236. if (isChecked) {
  237. dBuySetProvinceCount ++
  238. }
  239. } else {
  240. // 判断是否是直辖市或自治区(省份下面没有城市的和有城市的分开计算)
  241. if (hasCities) {
  242. // 直辖市、自治区 -----直接进行省份的操作
  243. // 并且没有购买过该省的,并且选中的
  244. if (isChecked) {
  245. dAddedProvinceCount ++
  246. }
  247. } else {
  248. // 带有市的省份城市(该省份下以前没有购买过任何城市)
  249. // 两种情况:1. 全选 2. 不全选
  250. var allCityLength = $(dom).find('button').length
  251. var otherTotalAddCount = $(dom).find('button.active').length
  252. if (allCityLength === otherTotalAddCount && isChecked) {
  253. // 选中了省份下的所有城市
  254. dAddedProvinceCount ++
  255. } else {
  256. // 对城市做新增操作
  257. dAddedCityCount += otherTotalAddCount
  258. }
  259. }
  260. }
  261. }
  262. })
  263. // 循环完成判断已经选择的省份数量 是否 大于购买过的数量
  264. if (dBuySetProvinceCount + dAddedProvinceCount > alreadyBuy.province.totalCount) {
  265. // 如果已选择的数量和新增的数量大于已购买过的数量,说明我有购买新的城市
  266. dAddedProvinceCount -= (alreadyBuy.province.totalCount - dBuySetProvinceCount)
  267. dBuySetProvinceCount = alreadyBuy.province.totalCount
  268. } else {
  269. dBuySetProvinceCount += dAddedProvinceCount
  270. dAddedProvinceCount = 0
  271. }
  272. // 已购买城市下选项改变,状态更新
  273. alreadyBuy.city.selectedCount = dBuySetCityCount
  274. newlyAdded.city = dAddedCityCount
  275. alreadyBuy.province.selectedCount = dBuySetProvinceCount
  276. newlyAdded.province = dAddedProvinceCount
  277. callback && callback()
  278. setDataInResult()
  279. // 全国被选中了
  280. if (isWholeSelected && !isWholeDisabled) {
  281. $('.result_text.add_new .added-info').text('全国')
  282. $('.result_text.add_new').show()
  283. isLockedTipButtons(false)
  284. }
  285. }
  286. function setDataInResult() {
  287. var buySetInfo = {
  288. p: "省级区域"+alreadyBuy.province.selectedCount+"/"+alreadyBuy.province.totalCount,
  289. c: "地市"+alreadyBuy.city.selectedCount+"/"+alreadyBuy.city.totalCount
  290. }
  291. var addedInfo = {
  292. p: "省级区域 "+newlyAdded.province,
  293. c: "地市 "+newlyAdded.city
  294. }
  295. // 修改数量并进行重新赋值
  296. if (alreadyBuy.city.totalCount === 0) {
  297. // 只买了省级区域
  298. $('.result_text.already .buy-set-info').text(buySetInfo.p)
  299. } else if (alreadyBuy.province.totalCount === 0){
  300. // 只买了市
  301. $('.result_text.already .buy-set-info').text(buySetInfo.c)
  302. } else {
  303. $('.result_text.already .buy-set-info').text(buySetInfo.p+"、"+buySetInfo.c)
  304. }
  305. // 升级新增赋值
  306. if (newlyAdded.city === 0) {
  307. // 只买了省级区域
  308. $('.result_text.add_new .added-info').text(addedInfo.p)
  309. } else if (newlyAdded.province === 0){
  310. // 只买了市
  311. $('.result_text.add_new .added-info').text(addedInfo.c)
  312. } else {
  313. $('.result_text.add_new .added-info').text(addedInfo.p+"、"+addedInfo.c)
  314. }
  315. if (newlyAdded.province !== 0 || newlyAdded.city !== 0) {
  316. $('.result_text.add_new').show()
  317. isLockedTipButtons(false)
  318. } else {
  319. $('.result_text.add_new').hide()
  320. isLockedTipButtons(true)
  321. }
  322. }
  323. // 求需要M的数量
  324. // window.getResultWithMCount = getResultWithMCount
  325. function getResultWithMCount() {
  326. var added = {
  327. country: 0,
  328. province: 0,
  329. city: {
  330. // '河南': 1,
  331. // '吉林': 1
  332. }
  333. }
  334. var allDisabledButHasCityCount = {}
  335. // 购买过的省份数量
  336. var buySetProvinceCount = $('.area-list').attr('data-buy-province-count') - 0 // 隐式转换成Number
  337. $('.area-list li:not(.index)').each(function(i, dom) {
  338. if ($(dom).children('.tab').hasClass('whole')) {
  339. // 判断是否被选中
  340. if ($(dom).find('input.other').is(':checked')) {
  341. added.country = -1
  342. added.province = 0
  343. added.city = {}
  344. return false
  345. }
  346. } else {
  347. added.country = 0
  348. var pName = $(dom).find('.province').text().replace(/\s+ | [\r\n]/g, '')
  349. // 获取当前dom下有几个按钮有active样式(点亮和灰色按钮的总数)
  350. var activeButtonLength = $(dom).find('button.active').length
  351. var allCitylength = $(dom).find('button').length
  352. // 判断该省是否有购买过的城市
  353. if ($(dom).attr('data-buy-city-count')) {
  354. var buyCityCount = $(dom).attr('data-buy-city-count') - 0
  355. // 获取城市不可点击的数量(灰色按钮的数量)
  356. var alreadySelectedCount = $(dom).find('button[disabled]').length
  357. // 得到你需要付钱的数量(可能是负值)
  358. var aCityCount = activeButtonLength - buyCityCount
  359. if (aCityCount > 0) {
  360. // 说明有购买的城市
  361. if (activeButtonLength - alreadySelectedCount === 0) {
  362. added.province ++
  363. allDisabledButHasCityCount[pName] = buyCityCount
  364. } else if (activeButtonLength === allCitylength) {
  365. // if(alreadySelectedCount < activeButtonLength){
  366. // allDisabledButHasCityCount[pName] = buyCityCount
  367. // }
  368. added.province ++
  369. } else {
  370. added.city[pName] = aCityCount
  371. }
  372. } else {
  373. // 选不到购买的个数
  374. }
  375. } else {
  376. // 判断是否是直辖市
  377. var isMunicipality = $(dom).find('.tab').hasClass('municipality')
  378. var allCitylength = $(dom).find('button').length
  379. if (isMunicipality) {
  380. if ($(dom).find('input').is(':checked')) {
  381. added.province ++
  382. }
  383. } else {
  384. if (activeButtonLength === allCitylength) {
  385. added.province ++
  386. } else {
  387. if (activeButtonLength !== 0) added.city[pName] = activeButtonLength
  388. }
  389. }
  390. }
  391. }
  392. })
  393. // 循环完成判断计算省份
  394. var aProvinceCount = added.province - buySetProvinceCount
  395. if (aProvinceCount > 0) {
  396. added.province = aProvinceCount
  397. } else {
  398. added.province = 0
  399. }
  400. return { added, allDisabledButHasCityCount }
  401. }
  402. // 点亮的城市数量详情(需要省和城市名称)
  403. function getActiveCityDetail(state = '') {
  404. var isWholeCountry = $('.tab.whole input').is(':checked')
  405. if (isWholeCountry) {
  406. // 选择全国
  407. var str = $('.area-list li .tab.whole .province').text().replace(/\s+ | [\r\n]/g, '')
  408. var obj = {}
  409. obj[str] = []
  410. return obj
  411. } else {
  412. var arr = []
  413. $('.area-list li:not(.index)').each(function(i, dom) {
  414. if ($(dom).children('.tab').hasClass('whole')) return
  415. var isMunicipality = $(dom).find('.tab').hasClass('municipality')
  416. var pName = $(dom).find('.province').text().replace(/\s+ | [\r\n]/g, '')
  417. var selectedObj = {
  418. name: pName,
  419. children: []
  420. }
  421. // 是否是直辖市或自治区
  422. if (isMunicipality) {
  423. if ($(dom).find('input:not([disabled])').is(':checked')) {
  424. arr.push(selectedObj)
  425. }
  426. } else {
  427. var allCitylength = $(dom).find('button').length
  428. if (state === 'notDisabled') {
  429. // 判断是否全省被选中
  430. if ($(dom).find('button.active:not([disabled])').length === allCitylength) {
  431. arr.push(selectedObj)
  432. } else {
  433. $(dom).find('button.active:not([disabled])').each(function(c, dom) {
  434. var cName = $(dom).text().replace(/\s+ | [\r\n]/g, '')
  435. selectedObj.children.push(cName)
  436. })
  437. if (selectedObj.children.length) {
  438. arr.push(selectedObj)
  439. }
  440. }
  441. } else {
  442. // 判断是否全省被选中
  443. if ($(dom).find('button.active').length === allCitylength) {
  444. arr.push(selectedObj)
  445. } else {
  446. $(dom).find('button.active').each(function(c, dom) {
  447. var cName = $(dom).text().replace(/\s+ | [\r\n]/g, '')
  448. selectedObj.children.push(cName)
  449. })
  450. if (selectedObj.children.length) {
  451. arr.push(selectedObj)
  452. }
  453. }
  454. }
  455. }
  456. })
  457. // 处理arr
  458. // [
  459. // {
  460. // name: '安徽',
  461. // children: ["合肥市", "蚌埠市", "阜阳市"]
  462. // },
  463. // {
  464. // name: '海南',
  465. // children: ["三亚市", "文昌市"]
  466. // },
  467. // {
  468. // name: '河南',
  469. // children: ["郑州市", "信阳市"]
  470. // }
  471. // ]
  472. var selectedCityDetail = {}
  473. arr.forEach(function(item, i) {
  474. selectedCityDetail[item.name] = item.children
  475. })
  476. return selectedCityDetail
  477. }
  478. }
  479. // 初始选择的城市恢复,根据传入的p恢复为Disabled灰色按钮
  480. function renderSelectedCityDisabled(p = {}) {
  481. for (const k in p) {
  482. if (p[k].length == 0) {
  483. // 购买的省份
  484. $('.province').each(function () {
  485. var t = $(this).text().trim();
  486. if (k == t) {
  487. $(this).children('.checkbox').prop('checked',true).attr('disabled',true);
  488. $(this).parents('li').children('.tab_content').find('.city').addClass('active').attr('disabled',true);
  489. }
  490. })
  491. } else {
  492. $('.city').each(function () {
  493. var that =$(this)
  494. var t = $(this).text().trim();
  495. p[k].forEach(function(v){
  496. if(v !== t) return
  497. // var that = $(this).parents('li').find('.tab:not(.municipality)');
  498. // 改变状态按钮
  499. that.addClass('active').attr('disabled',true).parent().siblings('.tab').find('input').addClass('half').attr('disabled',true);
  500. })
  501. })
  502. }
  503. }
  504. }
  505. // 初始选择的城市恢复,根据传入的p恢复为Active蓝色按钮
  506. // 需要放到init之后
  507. function renderSelectedCityActive(p = {}) {
  508. for (const k in p) {
  509. if (k === '全国') {
  510. $('.area-list li:not(.index):eq(0)').find('input').trigger('click')
  511. return
  512. }
  513. if (p[k].length == 0) {
  514. // 购买的省份
  515. $('.province').each(function () {
  516. var t = $(this).text().trim();
  517. if (k == t && p[k].length == 0) {
  518. $(this).parent().siblings().find('.city').trigger('click')
  519. }
  520. })
  521. } else {
  522. $('.city').each(function () {
  523. var that =$(this)
  524. var t = that.text().trim();
  525. p[k].forEach(function(v){
  526. if(v !== t) return
  527. // var that = $(this).parents('li').find('.tab:not(.municipality)');
  528. // 改变状态按钮
  529. that.trigger('click')
  530. })
  531. })
  532. }
  533. }
  534. getResult()
  535. }
  536. function isLockedTipButtons(f) {
  537. $('.tips_btn button').prop('disabled', f)
  538. }
  539. // 根据数据进行求总数 --- 上方提示数据信息的初始化
  540. function initAlreadyBuy(p, buyset, citys) {
  541. // 此处在数据返回后根据数据进行求值
  542. if (buyset.areacount === -1) {
  543. $('.result_text.already .buy-set-info').text('全国')
  544. $('.result_text.add_new .added-info').hide().text('')
  545. }
  546. // 选择的省和市的数量
  547. for (var a in p) {
  548. if (p[a].length) {
  549. // 已经选择的市的数量
  550. alreadyBuy.city.selectedCount += p[a].length
  551. } else {
  552. // 已经选择的省的数量
  553. alreadyBuy.province.selectedCount ++
  554. }
  555. }
  556. // 购买的全省的数量(不包含市)
  557. alreadyBuy.province.totalCount = buyset.areacount;
  558. // 购买的城市数量
  559. for (const c in citys) {
  560. alreadyBuy.city.totalCount += citys[c]
  561. }
  562. // console.log(alreadyBuy)
  563. var buySetInfo = {
  564. p: "省级区域"+alreadyBuy.province.selectedCount+"/"+alreadyBuy.province.totalCount+"、",
  565. c: "地市"+alreadyBuy.city.selectedCount+"/"+alreadyBuy.city.totalCount
  566. }
  567. $('.result_text.already .buy-set-info').text(buySetInfo.p + buySetInfo.c)
  568. $('.result_text.add_new .added-info').text('')
  569. }
  570. // 初始化方法
  571. function init() {
  572. var data = areaData.data
  573. var buyset = data.buyset;
  574. var citys = buyset.citys;
  575. initAlreadyBuy(data.area, buyset, citys)
  576. // 1.根据返回数据进行渲染
  577. if (buyset.areacount === -1) {
  578. // 购买全国
  579. if (Object.keys(data.area).length === 0) {
  580. // 选中全国
  581. // 不能操作
  582. $('.checkbox.other').prop('disabled', true).prop('checked', true)
  583. $('.checkbox:not(.other)').prop('disabled', true)
  584. $('.city').attr('disabled', true)
  585. } else {
  586. renderSelectedCityDisabled(data.area)
  587. }
  588. isLockedTipButtons(true)
  589. $('.result_text.already .buy-set-info').text('全国')
  590. } else {
  591. // 购买的非全国
  592. renderSelectedCityDisabled(data.area)
  593. }
  594. // 2.根据数据绑定data-buy-count,并赋值
  595. // area-list上绑定购买的省份数量
  596. $('.area-list').attr('data-buy-province-count', buyset.areacount)
  597. $('.area-list li:not(.index)').each(function(i, dom){
  598. if ($(dom).children('.tab').hasClass('whole')) return
  599. // 获取省的名字,并去空格.回车.换行
  600. var provinceName = $(dom).find('.province').text().replace(/\s+ | [\r\n]/g, '')
  601. for (var c in citys) {
  602. // var areaLeng;
  603. // try{
  604. // areaLeng = data.area[c].length
  605. // if (c === provinceName && areaLeng !== 0) {
  606. // $(dom).attr('data-buy-city-count', citys[c])
  607. // }
  608. // }catch(e){
  609. //TODO handle the exception
  610. if (c === provinceName) {
  611. $(dom).attr('data-buy-city-count', citys[c])
  612. }
  613. // }
  614. }
  615. })
  616. $('.add_new').hide()
  617. getResult()
  618. }
  619. // ----------------- 以下绑定事件 ----------------------------
  620. // 0.渲染城市列表
  621. $(".select-area-box ul li:not('.index')").each(function () {
  622. var text = $(this).find(".province").text().trim()
  623. var data = null
  624. chinaMapJSON.some(function (v) {
  625. data = v
  626. return v.name.indexOf(text) !== -1
  627. })
  628. var box = $(this).find('div.tab_content')
  629. var html = createMoreCity(data.city)
  630. box.html(html)
  631. })
  632. // fastClick初始化: https://github.com/ftlabs/fastclick
  633. // FastClick.attach(document.body)
  634. // 1.省下拉市
  635. var isAnimating = false
  636. $(".select-area-box .tab:not(.municipality)").on('click', function (event) {
  637. if (isAnimating) return
  638. isAnimating = true
  639. if ($(this).next('.tab_content:not(:animated)').css("display") == "block") {
  640. $(this).children().children('i').css({
  641. "display": "inline-block",
  642. "transform": "rotate(0)"
  643. })
  644. } else {
  645. $(this).children().children('i').css({
  646. "display": "inline-block",
  647. "transform": "rotate(-180deg)"
  648. })
  649. }
  650. $(this).toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500)
  651. $(this).parent().siblings().children('.tab').removeClass('selected');
  652. setTimeout(function() {
  653. isAnimating = false
  654. }, 500)
  655. })
  656. // 2.点击全国按钮onchange事件;
  657. $('.checkbox.other').on('change', function () {
  658. var isChecked = $(this).is(':checked')
  659. if(isChecked){
  660. $(this).prop('checked',true);
  661. $('.checkbox:not(.other):not([disabled])').prop('checked',false).removeClass('half');
  662. $('.city:not([disabled])').removeClass('active');
  663. $('.tab_content').slideUp();
  664. }
  665. getResult()
  666. })
  667. // 3.点击非全国按钮onchange事件
  668. $('.checkbox:not(.other)').on('change', function () {
  669. $('.checkbox.other').prop('checked',false);
  670. var isChecked = $(this).is(':checked');
  671. if(isChecked){
  672. $(this).prop('checked',true).removeClass('half');
  673. $(this).parents('li').find('.city').addClass('active');
  674. // 控制点击checkbox时候是否展开下方的城市列表
  675. // $(this).parent().parent('.tab:not(.municipality)').siblings('.tab_content').slideDown();
  676. }else{
  677. $(this).prop('checked',false);
  678. $(this).parents('li').find('.city').removeClass('active');
  679. };
  680. getResult();
  681. showOther();
  682. })
  683. // 4.点击半选按钮触发的事件
  684. // $('.checkbox.half').on('change', function () {
  685. // $('.checkbox.other').prop('checked', false)
  686. // var isChecked = $(this).is(':checked')
  687. // $(this).prop('checked', isChecked).removeClass('half')
  688. // console.log("!1")
  689. // getResult();
  690. // })
  691. // 5.点击城市按钮触发的事件
  692. $('.tab_content').on('click', '.city', function () {
  693. // 全国取消选中
  694. $('.checkbox.other').prop('checked',false);
  695. $(this).toggleClass('active')
  696. // 省下所有选中的地市数量
  697. var activeCityLength = $(this).parent().find('.city.active').length;
  698. // 省下一共有几个城市
  699. var cityLength = $(this).parent().find('.city').length;
  700. var oInput = $(this).parents('li').find('input.checkbox')
  701. if (activeCityLength === cityLength) {//当选中的城市数量等于该省下所有城市总数时,即为全选
  702. if ($(this).siblings('.city[disabled]').length === 0) {
  703. oInput.removeClass('half').prop('checked', true)
  704. }
  705. } else if (activeCityLength === 0){
  706. oInput.prop('checked', false).removeClass('half')
  707. } else {//半选
  708. oInput.prop("checked",false).addClass('half')
  709. }
  710. getResult();
  711. showCityTips(this)
  712. })
  713. // 6. 重置按钮点击事件
  714. $('.reset-btn').on('click', function() {
  715. // alreadyBuy = {
  716. // province: {
  717. // totalCount: 0,
  718. // selectedCount: 0
  719. // },
  720. // city: {
  721. // totalCount: 0,
  722. // selectedCount: 0
  723. // }
  724. // }
  725. // newlyAdded = {
  726. // province: 0,
  727. // city: 0
  728. // }
  729. // sessionStorage.removeItem("vipAddCount");
  730. // sessionStorage.removeItem("vipSubSelectAreaAdd");
  731. // sessionStorage.removeItem("vipSubSelectCity");
  732. // 数组置0,在重新从数据中计算初始状态结果并返回
  733. for (var i in alreadyBuy) {
  734. for(var ii in alreadyBuy[i]) {
  735. alreadyBuy[i][ii] = 0
  736. }
  737. }
  738. for (var j in newlyAdded) {
  739. newlyAdded[j] = 0
  740. }
  741. $(".select-area-box .tab:not(.municipality) .icon").css({
  742. "display": "inline-block",
  743. "transform": "rotate(0)"
  744. });
  745. $('.checkbox').prop('checked', false).removeAttr('disabled');
  746. $('.city').removeClass('active');
  747. $('input').removeClass('half')
  748. $('.tab_content').slideUp();
  749. $('.optional').remove()
  750. init();
  751. $(".tips_d_text").hide();
  752. $(".btns .reset-btn").prop("disabled",false);
  753. $(".btns .save-btn").prop("disabled",false);
  754. })
  755. // 7.锚点跳转
  756. $("body").on('click', '.slide a', function () {
  757. var s = $(this).html()
  758. if (s == '#') {
  759. return;
  760. }
  761. document.querySelector('#' + s).scrollIntoView({
  762. block: 'center'
  763. });
  764. })
  765. // 阻止input checkbox选中取消 触发父元素下拉事件
  766. $('.checkbox').click(function (e) {
  767. e.stopPropagation();
  768. })
  769. function showCityTips(dom){
  770. var isAll = $(".area-list").attr("data-buy-province-count");
  771. if(Number(isAll)===-1){
  772. return
  773. }
  774. var activeCityLength = $(dom).parent().find('.city.active').length;
  775. var selectedCityLength = $(dom).parent().find('.city.active:not([disabled])').length;
  776. var disabledCityLength = $(dom).parent().find('.city[disabled]').length;
  777. var cityLength = $(dom).parent().find('.city').length;
  778. // var dataCount = Number($(dom).parent().parent().attr("data-buy-city-count"));
  779. // if(dataCount === undefined){
  780. // dataCount = 0;
  781. // }else{
  782. // if(Number(dataCount) !== disabledCityLength){
  783. // if(disabledCityLength === 0){
  784. // dataCount = 0;
  785. // }else{
  786. // dataCount = dataCount - disabledCityLength;
  787. // }
  788. // }
  789. // }
  790. // var countShow = 0;
  791. var showCounts = selectedCityLength + disabledCityLength;
  792. // var showCount = selectedCityLength - dataCount;
  793. if(showCounts > 2){
  794. if(selectedCityLength === 0){
  795. $(".tips_d_text").hide();
  796. }else{
  797. $(".tips_d_text").text("已选择"+showCounts+"个市,建议购买“全省”");
  798. $(".tips_d_text").show();
  799. }
  800. }else{
  801. $(".tips_d_text").hide();
  802. }
  803. if(showCounts === cityLength){
  804. showOther();
  805. }
  806. }
  807. function showOther(){
  808. var areaCount = areaData.data.buyset.areacount;
  809. var areaSelect = $('.checkbox:checked:not([disabled])').length;
  810. if(areaSelect === 0){
  811. $(".tips_d_text").hide();
  812. return
  813. }
  814. var showCounts = 0;
  815. if(areaCount === -1){
  816. $(".tips_d_text").hide();
  817. }else{
  818. showCounts = newlyAdded.province + areaCount;
  819. }
  820. if(showCounts > 9){
  821. $(".tips_d_text").text("已选择"+showCounts+"个省,建议购买“全国”")
  822. $(".tips_d_text").show();
  823. }else{
  824. $(".tips_d_text").hide();
  825. }
  826. }
  827. // ----------------------- 以下数据交互 ------------------
  828. $DoPost("/subscribepay/renewUpgrade/getBuyMsg", {}, function (data) {
  829. if(data.success){
  830. areaData["data"] = {
  831. "area" : data.data.area,
  832. "buyset" : data.data.buyset,
  833. }
  834. init();
  835. }
  836. }, false);
  837. // 确认按钮事件
  838. $('.save-btn').on('click', function(){
  839. // 获取added城市详情
  840. var activeCityDetail = getActiveCityDetail()
  841. var areaCount = getResultWithMCount().added
  842. var lastSet = getResultWithMCount().allDisabledButHasCityCount
  843. // console.log("111",areaCount)
  844. // console.log(JSON.stringify(activeCityDetail))
  845. // console.log(activeCityDetail)
  846. // console.log(areaData)
  847. // console.log(alreadyBuy)
  848. sessionStorage.vipAddCount = JSON.stringify(newlyAdded);
  849. sessionStorage.vipSubSelectCity = newlyAdded.city;
  850. sessionStorage.vipSubSelectAreaUpgrade = JSON.stringify(activeCityDetail);
  851. sessionStorage.vipSubSelectAreaAdd = JSON.stringify(areaCount);
  852. sessionStorage.vipallDisabledButHasCityCount = JSON.stringify(lastSet);
  853. //
  854. if(areaCount.country === 0){
  855. if(areaCount.province === 0 && checkObj(areaCount.city)){
  856. sessionStorage.removeItem("vipSubSelectAreaUpgrade");
  857. }
  858. }
  859. history.go(-1);
  860. })
  861. function checkObj(obj) {
  862. //检验数组
  863. if (Array.prototype.isPrototypeOf(obj) && obj.length === 0) {
  864. return true;
  865. }
  866. //检验对象
  867. if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {
  868. return true;
  869. }
  870. return false;
  871. }
  872. //
  873. if(sessionStorage.vipSubSelectAreaUpgrade!==undefined&&sessionStorage.vipSubSelectAreaUpgrade!==""){
  874. var areaObj = JSON.parse(sessionStorage.vipSubSelectAreaUpgrade);
  875. renderSelectedCityActive(areaObj);
  876. }
  877. // 求需要M的城市数量
  878. // getResultWithMCount()
  879. // 获取点亮的城市的列表详情(带省名和城市名)
  880. // getActiveCityDetail()
  881. // 回显为蓝色激活样式
  882. // renderSelectedCityActive()
  883. // 回显为灰色不可点击样式
  884. // renderSelectedCityDisabled()
  885. })