comm-handle.js 5.0 KB
Newer Older
ksaria@outlook.com's avatar
ksaria@outlook.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
function getToday (str) {
  return str.replace(/year/gi, curruYear).replace(/month/gi, currMonth).replace(/date/gi, currDate);
}

/**
 * 
 * @param {JQuery<HTMLElement>} $dom jq dom
 * @param {string} text 可有可无
 */
function copyWx ($dom, _text) {
  let wx = '';
  let text = '';
  if (typeof $dom === 'string') {
    wx = $dom;
  } else if ($dom) {
    wx = $dom.parents('[id*=-popup]').find('.wx').text();
    if (!wx) wx = $dom.attr('data-copy-wx');
    if (!wx) wx = $dom.attr('data-to-wx');
  }
  if (!wx) return;
  if (typeof _text === 'string') {
    text = _text;
  } else if ($dom && $dom.attr('data-wx-alter')) {
    text = $dom.attr('data-wx-alter');
  }
  const element = document.createElement('input');
  element.value = wx;
  document.body.append(element);
  element.select();
  document.execCommand('Copy');
  document.body.removeChild(element);
  alert(text || '复制成功!');
}

function showSuccessModal () {
  var successPopup = document.querySelector('[id^=success-popup]');
  if (successPopup) {
    const dynamicWeChatImg = $(successPopup).find('.dynamic-wechat-img');
    const dynamicWeChat = $(successPopup).find('.dynamic-wechat');
    // 动态二维码and动态微信
    if ((dynamicWeChatImg.length || dynamicWeChat.length) && window.pageData && window.pageData.weChatGroupID) {
      const result = Fn && Fn.IpRecord && Fn.IpRecord.GetCurrentWeChat(Number(window.pageData.weChatGroupID)) || {};
      if (result.Id) {
        result.Qrcode && dynamicWeChatImg.attr('src', result.Qrcode);
        result.Wechat && dynamicWeChat.text(result.Wechat);
        Fn.IpRecord.AddUserRecord(result.Id, Fn.User.GetUserUIDBySenSors()) 
      }
    }
    $(successPopup).fadeIn();
  } else {
    if (toPayCourse()) {
      alert("领取成功,稍后将有学习顾问联系您!");
      toHref('/WebNew/user/Personal/UserInfo.aspx');
    }
  }
}

function toPayCourse() {
  // handle pay
  if (window.defaultPay && window.payConfig) {
    if (window.defaultPay.value || window.payConfig.value) {
      let value = window.defaultPay.value;
      let sort = window.defaultPay.sort;
      if (window.payConfig.value) {
        value = window.payConfig.value;
        sort = window.payConfig.sort;
      }
      if (!value) return;
      if (window.paytype == "wechat" && sort === 'order') {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
          toHref("https://www.acadsoc.com.cn/WebNew/user/MobilePay/AuthResponse.aspx?id=" + value + "&payid=4");
        } else {
          toHref("https://www.acadsoc.com.cn/WebNew/user/MobilePay/MobilePay.aspx?DesCid=" + value + "&RedPack=0&payid=11");
        }
      } else if (window.paytype == "alipay" && sort === 'order') {
        toHref("https://www.acadsoc.com.cn/WebNew/user/MobilePay/MobilePay.aspx?DesCid=" + value + "&RedPack=0&payid=10");
      } else {
        toHref(value);
      }
      return false;
    }
  }
  return true;
}

// 阻止返回
function holdBack () {
  if (sessionStorage.getItem('mini_chat_show')) {
    return sessionStorage.removeItem('mini_chat_show');
  }
  $('[id*=back-popup]').fadeIn();
  history.pushState(null, null, document.URL);
}
// 监听客服,阻止弹出返回弹窗
function listenerChat () {
  if (!document.getElementById('mini_chat')) {
    setTimeout(() => {
      listenerChat();
    }, 1500);
  } else {
    const obs = new MutationObserver((_target) => {
      if ($('#mini_chat').css('display') === 'block') {
        sessionStorage.setItem('mini_chat_show', 1);
      }
    })
    obs.observe(document.getElementById('mini_chat'), { attributes: true })
  }
}

function openModal (dom, _selector) {
  const id = $(dom).attr('data-popup-id');
  const popup = $('#register-popup-' + id);
  const attrs = dom.attributes;
  // 更改字符
  for (let i = 0; i < attrs.length; i++) {
    const name = attrs[i].name;
    const value = decodeURI(attrs[i].value);
    if (name.indexOf('data-popup-hash-') !== -1) {
      const ele = popup.find('[data-modal-hash=' + name.split('data-popup-hash-')[1] + ']');
      if (!value) continue;
      if (value === 'false') {
        ele.hide();
        continue;
      } else if (value === 'true') {
        ele.show();
        continue;
      }
      if (!ele[0]) continue;
      setDomParams(ele[0], value);
    }
  }
  popup.fadeIn();
}


function setDomParams (dom, value) {
  var ele = $(dom);
  switch (dom.nodeName) {
    case 'IMG':
    case 'VIDEO':
      if (ele.attr('src') !== value) {
        ele.attr('src', value);
      }
      break;
    case 'INPUT':
      if (ele.attr('placeholder') !== value) {
        ele.attr('placeholder', value);
      }
      break;
    default:
      if (ele.html() !== value) {
        ele.html(getToday(value));
      }
      break;
  }
}


function toHref (href) {
  var _a = document.createElement('a');
  _a.href = href;
  _a.target = '_blank';
  _a.click();
  _a.remove();
}


$(function () {
  listenerChat();
})


window.registerCallback = function () {
  showSuccessModal();
}