main.js 12.5 KB
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
let queryObj;
let getServerTimeApi = '//m.yohobuy.com/activity/feature/getServerTimes';
let getCouponApi = '//m.yohobuy.com/activity/feature/couponSend';

let UFO = {
  app: null
};

function toVersion(str = '') {
  var ver = str.split('.').map(function(i) {
    return Number(i);
  });

  if (!ver[0]) {
    ver[0] = 0;
  }

  if (!ver[1]) {
    ver[1] = 0;
  }

  if (!ver[2]) {
    ver[2] = 0;
  }

  return ver;
}

function toNumber(ver) {
  var major = ver[0] * 10000;
  var minor = ver[1] * 100;
  var patch = ver[2] * 1;

  return major + minor + patch;
}

function compare(left, right) {
  if (left > right) {
    return 1;
  } else if (left < right) {
    return -1;
  } else {
    return 0;
  }
}

function verion(left, right) {
  var leftVersion = toNumber(toVersion(left));
  var rightVersion = toNumber(toVersion(right));
  var result = compare(leftVersion, rightVersion);

  return result;
};

$(document).ready(function() {
  queryObj = Utils.getQueryObj();
  try {
    var fromid = localStorage.getItem('_share_fromid_' + window.shareC.eventName);

    if (queryObj && +queryObj.fid && +queryObj.fid !== +fromid) {
      if (queryObj.fid) {
        localStorage.setItem('_share_fromid_' + window.shareC.eventName, queryObj.fid);
      }
      localStorage.removeItem('_share_preid_' + window.shareC.eventName);
      fromid = queryObj.fid;
    }
    shareC && (shareC.fid = fromid);
  } catch (error) {
    console.log(error);
  }
  UFO.app = new Vue({
    el: '#mainContainer',
    data: {
      preid: 0,
      env: 'h5', // h5, app, miniprogram
      uid: 0,
      isShared: false,
      app_info: {
        uid: '0',
        app_version: '',
        client_type: '',
        session_key: ''
      },
      fromPage: 'iFP_SalesPromotion_h5',
      couponToken: {
        '2019-07-19': [
          'd27c2b94-b7fb-46f9-9ffd-d3a86775bcf4',
          '4571657e-929a-4d7e-9db3-03fd500a16c5',
          'b8cb3a92-3d40-4bba-88bc-2a2d58648eba'
        ],
        '2019-07-20': [
          '65e07092-1c66-407c-a36c-c63024bfcbef',
          '2c39ad8c-c98c-424e-9752-2cc549d105cf',
          'c8b430df-6501-492f-9d3a-f135933b95e5'
        ],
        '2019-07-21': [
          'ae52d919-27ff-4954-a533-748cd130c634',
          '62cacf6d-e39f-4a6d-a096-82010f2781e3',
          'a28e9b14-a6c3-40b0-bcbd-8539b08d0372'
        ],
        '2019-07-22': [
          '61e9f48b-9218-4bb3-82b8-6baddd494941',
          'a7120af1-0a06-439a-b447-03842e3f5e84',
          '7689f136-b316-4df0-85ff-330f958bd7e4'
        ],
        '2019-07-23': [
          '7e666967-5f32-4e69-8126-6bfad68b868b',
          '02fd13a7-9f8e-47d0-93fc-50752811508d',
          '0b4668e2-d756-424d-b544-f029a670efd5'
        ],
        '2019-07-24': [
          '5e185a9c-e447-4634-af7f-cf4565f7c149',
          'c58dff86-0b97-4d71-984f-5725ff2c37a2',
          '4ac38e96-811c-4e9d-b3c9-89b3ad318bf0'
        ],
      },
      productData: [],
      serverTime: '',
      serverTimeNumber: 0,
      isShowShare: false,
      isShowTips: false, // 显示提示框
      tipsMessage: '' // 提示文字
    },
    created: function() {
      this.productData = productData;
      if (navigator.userAgent.match(/ANDROID/i)) {
        this.fromPage = 'aFP_SalesPromotion_h5';
      }
    },
    mounted: function() {
      if (queryObj.uid) {
        this.uid = queryObj.uid;
      }
      if (navigator.userAgent.match(/yohobuy/i)) {
        this.env = 'app';
      } else if (navigator.userAgent.match(/miniprogram/i)) {
        window.__wxjs_environment = 'miniprogram';
        this.weixinJSReady();
      } else if (!window.WeixinJSBridge || !window.WeixinJSBridge.invoke) {
        document.addEventListener('WeixinJSBridgeReady', this.weixinJSReady, false);
      }
      this.yo_sdk = window.yo_sdk; // 初始化sdk
      this.getServerTime();
      this.preshare();
      this.getParams();
      this.getResource();
    },
    methods: {
      preshare() {
        try {
          var that = this;
          var preid = localStorage.getItem('_share_preid_' + window.shareC.eventName);

          if (preid) {
            this.preid = preid;
            this.setShareLink(preid);
            return;
          }
          $.ajax({
            url: '//action.yoho.cn/api/share/preshare?callback=?',
            dataType: 'jsonp',
            data:{
              eventName: shareC.eventName,
              shareUrl: shareC.shareUrl,
              fid: shareC.fid || 0
            },
            success: function(res) {
              if (res) {
                that.preid = res.preid || 0;
                localStorage.setItem('_share_preid_' + window.shareC.eventName, that.preid);
                that.setShareLink(res.preid);
              }
            }
          })
        } catch (error) {
          console.log(error);
        }
      },
      setShareLink(preid) {
        shareC.link = document.location.href.split('?')[0] + '?fid=' + preid;
        share && share();
      },
      weixinJSReady() {
        if (window.__wxjs_environment === 'miniprogram') {
          this.env = 'miniprogram';
        }
      },
      jumpToMiniapp(e) { // 跳转小程序
        let url = decodeURIComponent(e.currentTarget.dataset.url);

        let params = Utils.getQueryObj(url);
        // console.log(params);

        if (params['openby:yohobuy']) {
          if (this.env === 'miniprogram') {
            wx.miniProgram.navigateTo({url: '/pages/common/webback?url=' + encodeURIComponent(url)});
          }
        } else if(url.indexOf('mp.weixin.qq.com') > 0) {
          wx.miniProgram.navigateTo({url: '/pages/webview/webview?url=' + encodeURIComponent(url)});
        }
      },
      goLogin() { //  跳转登录
        // console.log('goLogin')

        let that = this;
        let appInterface = window.yohoInterface;

        if (this.env === 'app' && appInterface) { // app登录
          let params = {
            method: 'get.uid'
          };
          let success = function(uid) {
            if (!parseInt(uid, 10)) {
              if (that.yo_sdk.goLogin) {
                that.yo_sdk.goLogin();
              }
            } else {
              that.app_info.uid = that.uid = uid;
              appInterface.triggerEvent(function(sid) {
                that.app_info.session_key = sid;
                that.checkShared();
              }, function() {}, {
                method: 'get.sessionId'
              });
              that.setShareC(+uid);
            }
          };
          let fail = function() {};

          appInterface.triggerEvent(success, fail, params);
        } else { // 小程序或H5登录
          if (that.yo_sdk.goLogin) {
            that.yo_sdk.goLogin();
          }
        }
      },
      setShareC(uid) {
        shareC.uid = uid;
        if (this.preid) {
          $.ajax({
            url: '//action.yoho.cn/api/share/collect?callback=?',
            dataType: 'jsonp',
            data:{
              uid: uid,
              fid: this.preid
            },
          })
        } else {
          setTimeout(() => {
            this.setShareC(uid);
          }, 500);
        }
      },
      getParams() {
        let app_uid = this.uid = Utils.getCookie('app_uid') || queryObj.uid || '0';
        let app_session_key = Utils.getCookie('app_session_key') || queryObj.session_key || '';
        let app_version = Utils.getCookie('app_version') || queryObj.app_version || '';
        let app_client_type = Utils.getCookie('app_client_type') || queryObj.app_client_type || '';

        this.app_info = {
          uid: app_uid,
          session_key: app_session_key,
          app_version: app_version,
          client_type: app_client_type
        };

        if (app_uid && app_uid !== '0') {
          this.setShareC(+app_uid);
        }
      },
      checkShared() { // 检查是否分享
        let that = this;
        if (!this.isShared) {

          if (this.env === 'app') { // 唤起app分享
            let newShare = false;
            let app_vercode = Utils.getCookie('app_vercode');
            let app_version = Utils.getCookie('app_version') || queryObj.app_version || '';

            if (verion(app_version, '6.9.5') >= 0 || app_vercode) {
              newShare = true;
            }

            document.addEventListener('deviceready', function() {
              let appInterface = window.yohoInterface;

              window.successShare = function() {
                that.getCoupon()
              };
              appInterface.triggerEvent(
                function () {
                  // alert('success');
                },
                function () {
                  // alert('fail')
                },
                {
                  method: 'go.showshareaction',
                  arguments: {
                    title: '【有货UFO】冲冲冲!扫货必备600元优惠券一键到手',
                    desc: '专属大额优惠券一网打尽,用券最高减300元!买潮流好鞋就在有货UFO,超万件商品任你选!',
                    imgUrl: document.location.href.split('?')[0].replace(/\/index\d{0,}\.html/, '') + '/share/logo.jpg',
                    link: window.shareC && shareC.link || document.location.href.split('?')[0],
                    hideType: ['4', '5', '6', '7', '8', '9'],
                    isCareCallBack: '1'
                  }
                });

              if (!newShare) {
                setTimeout(that.getCoupon, 5000);
              }
            })
          } else {
            this.showShare();
            if (this.env === 'miniprogram') {
              setTimeout(this.getCoupon, 5000);
            }
          }
        } else {
          this.getCoupon();
        }
      },
      getCoupon() { // 领券
        let that = this;

        if (!this.serverTime) {
          this.showMessage('网络异常,请刷新重试');
          return;
        }
        if (!this.couponToken[this.serverTime]) {
          this.showMessage('活动已结束, 感谢您的参与');
          return;
        }

        let couponToken = this.couponToken[this.serverTime].join(',');

        if (this.app_info.uid) {
          let data = {
              token: couponToken,
              uid: this.uid,
              coupontype: 'ufo',
              app: this.app_info
          };
          $.ajax({
              url: getCouponApi + '?callback=?',
              type: 'get',
              dataType: 'jsonp',
              data: data,
              success: function (data) {
                // console.log(data);
                if (data.code === 200) {
                  that.showMessage('领券成功');
                } else if (data.code === 401) {
                  that.showMessage('已领完,请明日再来');
                } else if (data.code === 403) {
                  that.showMessage('目前券已领完,请明日再来');
                } else {
                  that.showMessage(data.message);
                }
                that.hideShare();
              }
          });
        }
      },
      getResource() { // 获取商品列表
        let that = this;
        $('.products-list a').each(function() {
          if (that.env === 'miniprogram') {
            $(this).on('click', that.jumpToMiniapp);
          } else {
            $(this).attr('href', $(this).data('url'));
            $(this).attr('target', '_blank');
          }
        })
      },
      getServerTime() {
        let that = this;
        $.ajax({
          url: getServerTimeApi + '?callback=?',
          type: 'get',
          dataType: 'jsonp',
          data:{},
          success: function(data) {
            if (data) {
              let time = new Date(data * 1000);
              that.serverTimeNumber = time.getTime();
              let year = time.getFullYear();
              let month = (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : time.getMonth();
              let day = time.getDate() < 10 ? '0' + (time.getDate()) : time.getDate();

              that.serverTime = [year, month, day].join('-');
            }
          },
          error: function(e) {
            let time = new Date();
            let year = time.getFullYear();
            let month = (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : time.getMonth();
            let day = time.getDate();

            that.serverTime = [year, month, day].join('-');
          }
        })
      },
      showMessage(msg) {
        this.tipsMessage = msg;
        this.isShowTips  = true;
      },
      showShare() {
        this.isShowShare = true;
      },
      hideShare() {
        this.isShowShare = false;
      },
      closeTips () {
        this.tipsMessage = '';
        this.isShowTips  = false;
      },
      refresh() {
        document.location.reload();
      }
    }
  })
});