123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- // 后端数据结构
- var areaData = {
- "data": {
- "isTrial": true,
- "area": {
- // "北京": [],
- // "河南": [
- // "洛阳市",
- // "驻马店市",
- // "周口市",
- // // "郑州市"
- // ],
- // "澳门": [],
- // "甘肃": []
- },
- "buyset": {
- "areacount": -1,
- "buyerclasscount": 11,
- "citys": {
- // "河南": 4
- }
- },
- "industry": [
- "党委办",
- "公共资源交易",
- "公安",
- "出版广电",
- "国资委",
- "地震",
- "安监",
- "工商",
- "法院",
- "电信行业",
- "财政"
- ]
- },
- "errMsg": "",
- "success": true
- }
- //加载数据
- function getData() {
- $DoPost("/subscribepay/editSub/getSubBuyMsg", {}, function (r) {
- if (r.success) {
- areaData = r;
- }
- }, false);
- }
- // 渲染城市方法
- function createMoreCity(arr) {
- var tempHtml = arr.map(function (v) {
- return '<button class="city">' + v.name + '</button>'
- }).join('')
- return tempHtml
- }
- var animatedRuning = false;
- function checkAnimatedRuning() {
- if (animatedRuning) {
- return true
- }
- animatedRuning = true;
- setTimeout(function () {
- animatedRuning = false;
- }, 500);
- return false
- }
- // 省下拉市事件
- function slideFun(obj) {
- if (checkAnimatedRuning()) {
- return
- }
- if (obj.next('.tab_content:not(:animated)').css("display") == "block") {
- obj.children().children('i').removeClass("up");
- } else {
- obj.children().children('i').addClass("up");
- }
- obj.toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500)
- obj.parent().siblings().children('.tab').removeClass('selected');
- }
- var obj
- // 改变选择结果方法
- function getResult() {
- obj = {};
- //$('.result_name').empty();
- $('.province').each(function () {
- var provinceText = $(this).text().trim();//省份名称
- var isChecked = $(this).children('.checkbox').is(':checked');
- var isHalf = $(this).children('.checkbox').hasClass('half');
- var arr = [];
- if (isChecked) {
- obj[provinceText] = [];
- } else if (isHalf && !isChecked) {
- var $that = $(this);
- obj[$that.text().trim()] = [];
- $that.parent().siblings().find('.city').each(function () {
- var parent = $(this).parent().siblings().find('.province').text().trim();
- if ($(this).is('.active') && parent == $that.text().trim()) {
- arr.push($(this).text().trim())
- }
- })
- obj[$that.text().trim()] = arr
- }
- })
- $('count').html($('.checkbox:not(.other):checked').length);
- //$('.result_name').append(getAreaClassArr_index(obj).join("、"));
- //校验是否修改
- if (theSameAs(obj) || $.isEmptyObject(obj)) {
- $('#enter').attr("disabled", "disabled");
- } else {
- $('#enter').removeAttr("disabled");
- }
- }
- // 初始化页面方法
- function init() {
- var p = areaData.data.area;
- var count = areaData.data.buyset.citys;
- // 1.渲染城市列表
- $(".select-area-box ul li:not('.index')").each(function () {
- var text = $(this).find(".province").text().trim()
- var data = null
- chinaMapJSON.some(function (v) {
- data = v
- return v.name.indexOf(text) !== -1
- })
- var box = $(this).find('div.tab_content')
- var html = createMoreCity(data.city)
- box.html(html)
- })
- // 2.当areacount == -1时,全国为选中状态,为正数时为选中的省份数目,全国选项不显示
- var pLength = Object.keys(areaData.data.area).length;
- var cLength = Object.keys(areaData.data.buyset.citys).length
- console.log(pLength, cLength)
- if (areaData.data.buyset.areacount == -1 && pLength == 0 && cLength == 0) {
- // 当购买的全国切选中的是全国时
- // $('.optional_count').html("可选择全部区域")
- //$('.all_area').show()
- $('.optional_count').hide()
- $('.total').html('34')
- $('.count').html('34')
- $('.tab.whole').parent('li').show();
- $('.checkbox.other').prop('checked', true);
- } else if (areaData.data.buyset.areacount == -1 && pLength != 0) {
- // 当购买的全国而没有选择全国,选择部分省市时
- //$('.all_area').show();
- $('.province').removeAttr('data-count');
- $('.optional_count').hide();
- $('.total').html('34');
- initResult()
- } else {
- //$('.all_area').hide()
- $('.optional_count').show()
- if (areaData.data.buyset.areacount != 0) {
- $('.total').html(areaData.data.buyset.areacount)
- } else {
- $(".optional_count").css("display", "none");
- }
- $('.tab.whole').parent('li').hide();
- // $('.checkbox.other').attr('disabled',true);
- initResult()
- }
- }
- function initResult() {
- var isAll = areaData.data.buyset.areacount;
- var p = areaData.data.area;
- var count = areaData.data.buyset.citys;
- // 添加可选择城市文字 和自定义属性值
- if (areaData.data.buyset.areacount != -1) {
- for (const c in count) {
- $('.province').each(function () {
- var t = $(this).text().trim();
- if (c == t) {
- $(this).attr("data-count", count[c]);
- $(this).after('<em class="optional">可选择 ' + count[c] + '个市</em>')
- var that = $(this).parent('.tab:not(.municipality)');
- that.bind('click', function () {
- slideFun($(this));
- })
- }
- })
- }
- }
- /*else{ //购买全国是否可以编辑市
- $('.province').each(function () {
- var that = $(this).parent('.tab:not(.municipality)');
- that.bind('click', function () {
- slideFun($(this));
- })
- })
- }*/
- // 渲染已选择结果
- for (const k in p) {
- console.log(p[k], k)
- if (p[k].length == 0) {
- $('.province').each(function () {
- var t = $(this).text().trim();
- if (k == t && p[k].length == 0) {
- // 如果购买的是全省,则不可点击下拉事件(不可修改城市)
- if (isAll != -1) {
- $(this).parent().siblings().children('.city').addClass('active').attr('disabled', true);
- $(this).parent('.tab').unbind('click').parent('li').siblings().find('.checkbox:not(.half)').parents('.tab').unbind('click')
- } else {
- $(this).parent().siblings().children('.city').addClass('active').removeAttr('disabled')
- }
- $(this).children('.checkbox').prop('checked', true)
- }
- })
- } else {
- console.log(k, p[k].length, $('.city.active').length)
- $('.city').each(function () {
- var thisOne=$(this)
- let activeLength = thisOne.parents('li').find('.active').length;
- let t = thisOne.text().trim();
- p[k].forEach(v => {
- if (v == t) {
- let that = thisOne.parents('li').find('.tab:not(.municipality)');
- console.log("v==t")
- // 如果是可修改城市的省份,则可以进行下拉展示
- that.bind('click', function () {
- slideFun($(this));
- })
- thisOne.addClass('active').removeAttr('disabled').parent().siblings('.tab').find('input').addClass('half');
- // 判断购买的地市数与选择的地市是否相等,相等则把其他地市禁用
- $('.province').each(function (item, index) {
- let dataCount = $(this).attr('data-count');
- let activeLength = $(this).parent().siblings().find('.active').length;
- // console.log(dataCount)
- if (dataCount) {
- // console.log(dataCount,activeLength,"object")
- if (activeLength >= dataCount) {
- $(this).parent().siblings().find('.city').not('.active').attr('disabled', true)
- }
- }
- })
- }
- })
- })
- }
- }
- $('ul.area-list .province[data-count]').parent().on('click', function () {
- slideFun($(this));
- })
- //$('.result_name').append(getAreaClassArr_index(areaData.data.area).join("、"));
- $('.count').html($('.checkbox:not(.other):checked').length)
- // 如果选中的省份数量等于购买的省份数量,则将其他未选中的省份禁用
- if ($('.checkbox:not(.other):checked').length == $('.total').html()) {
- $('.checkbox:not(:checked)').attr('disabled', true);
- }
- $('#enter').attr("disabled", "disabled");
- }
- $(function () {
- // 省下拉事件
- /*$('.tab:not(.municipality)').on('click', function () {
- if (checkAnimatedRuning()) {
- return
- }
- if ($(this).next('.tab_content:not(:animated)').css("display") == "block") {
- $(this).children().children('i').removeClass("up");
- } else {
- $(this).children().children('i').addClass("up");
- }
- $(this).toggleClass('selected').next('.tab_content:not(:animated)').slideToggle(500);
- $(this).parent().siblings().children('.tab').removeClass('selected');
- });*/
- getData();
- // 初始化
- init();
- /**** 点击checkbox实现onchange事件 *****/
- // 1.点击全国按钮onchange事件;
- $('.checkbox.other').on('change', function () {
- var isChecked = $(this).is(':checked');
- if (isChecked) {
- $('.count').html('34')
- $('.checkbox:not(.other)').prop('checked', false).removeClass('half')
- $('.city').removeClass('active');
- $('.tab_content').slideUp()
- getResult()
- } else {
- $('.checkbox:not(.other)').removeAttr('disabled')
- $('.city').removeAttr('disabled')
- getResult()
- }
- })
- // 2.点击非全国按钮onchange事件;
- $('.checkbox:not(.other)').on('change', function () {
- $('.checkbox.other').prop('checked', false);
- var checkedLength = $('.checkbox:checked').length;
- var totalLength = $('.total').text();
- $('.count').html(checkedLength)
- if (checkedLength < totalLength) {
- $('.checkbox:not(.other)').removeAttr('disabled')
- getResult()
- } else {
- $('.checkbox:not(:checked)').attr('disabled', true)
- if (areaData.data.isTrial) {
- showTip('<span style="white-space: nowrap;">您只选择试用了' + totalLength + '个省份</span>,<br><span style="white-space: nowrap;">如需更多,请前往超级订阅进行购买</span>');
- } else {
- showTip('<span style="white-space: nowrap;">您只购买了' + totalLength + '个省份,如需更多请升级<span></p>')
- }
- getResult()
- }
- if ($(this).is(':checked')) {
- if (areaData.data.buyset.areacount != -1) {
- $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active').attr('disabled', true);
- } else {
- $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active');
- }
- getResult()
- } else {
- $(this).prop('checked', false).removeClass('half').parent().parent().siblings().find('.city').removeClass('active').removeAttr('disabled');
- getResult()
- }
- })
- // 3.点击半选按钮触发的事件
- $('.checkbox.half').on('change', function () {
- console.log($(this).is(':checked'))
- if ($(this).is(':checked')) {
- if (areaData.data.buyset.areacount != -1) {
- $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active').attr('disabled', true);
- } else {
- $(this).prop('checked', true).removeClass('half').parents('li').find('.city').addClass('active');
- }
- getResult()
- } else {
- $(this).prop('checked', false);
- $(this).parent().parent().siblings().find('.city').removeClass('active').removeAttr('disabled');
- $(this).parent().siblings('.optional').show();
- getResult()
- }
- })
- // 4.点击城市按钮触发的事件
- $('.tab_content').on('click', '.city', function () {
- var count = $(this).parent().siblings().find('.province').attr('data-count');
- $(this).toggleClass('active');
- $('.checkbox.other').prop('checked', false);
- var isActive = $(this).parent().find('.city.active').length
- var cityLength = $(this).parent().find('.city').length
- if (isActive === cityLength) {//当选中的城市数量等于该省下所有城市总数时,即为全选
- var oInput = $(this).parents('li').find('input.checkbox')
- oInput.removeClass('half').prop('checked', true)
- getResult()
- } else {//半选
- $(this).parents('li').find('input.checkbox').addClass('half').prop("checked", false)
- if (isActive > 0 && isActive != count) {
- $(this).parents('li').find('input.checkbox').addClass('half');
- $(this).parent('div').find('.city:not(.active)').removeAttr('disabled')
- getResult()
- } else if (isActive > 0 && isActive == count) {
- $(this).parents('li').find('input.checkbox').addClass('half');
- $(this).parent('div').find('.city:not(.active)').attr('disabled', true)
- //提示订阅
- if (areaData.data.isTrial) {
- showTip('<span style="white-space: nowrap;">您只选择试用了' + $(this).parents('li').find(".province").attr("data-count") + '个市</span>,<br><span style="white-space: nowrap;">如需更多,请前往超级订阅进行购买</span>');
- } else {
- showTip('<span style="white-space: nowrap;">您只购买了' + $(this).parents('li').find(".province").attr("data-count") + '个市,如需更多请升级<span></p>')
- }
- getResult()
- } else {
- $(this).parent('div').find('.city:not(.active)').removeAttr('disabled')
- $(this).parents('li').find('input.checkbox').removeClass('half').prop('checked', false)
- getResult()
- }
- }
- })
- // 阻止input checkbox选中取消 触发父元素下拉事件
- $('.checkbox').click(function (e) {
- e.stopPropagation();
- })
- // 锚点跳转
- $("body").on('click', '.slide a', function () {
- var s = $(this).html()
- if (s == '#') {
- return;
- }
- document.querySelector('#' + s).scrollIntoView({
- block: 'center'
- });
- })
- // 取消按钮事件 返回上一页
- $('#cancel').click(function () {
- // window.history.go(-1)
- //$('.result_name').empty()
- $('.checkbox').prop('checked', false).removeAttr('disabled');
- $('.city').removeClass('active');
- $('.tab_content').slideUp();
- $('.optional').remove();
- $('.tab').find('i').removeClass("up");
- init();
- })
- // 确定修改事件
- $('#enter').click(function () {
- $('#enter').attr("disabled", "disabled");
- $DoPost("/subscribepay/editSub/submit", {
- "type": "area",
- "value": JSON.stringify(obj)
- }, function (r) {
- if (r.success) {
- window.location.replace('/front/vipsubscribe/submitEditSubFinish');
- }
- $('#enter').removeAttr("disabled");
- }, false);
- })
- });
- function theSameAs(select) {
- var tmp1 = getAreaClassArr(select);
- var tmp2 = getAreaClassArr(areaData.data.area);
- return (JSON.stringify(tmp1[0].sort()) === JSON.stringify(tmp2[0].sort())) && (JSON.stringify(tmp1[1].sort()) === JSON.stringify(tmp2[1].sort()));
- }
- function showTip(msg) {
- weui.toast(msg, {
- duration: 2000,
- className: 'custom-toast',
- });
- }
|