webview.js 5.44 KB
import { APP_VERSION, CLIENT_TYPE, PRIVATE_KEY } from '../../libs/config';
import { sign_body} from '../../libs/request';
import { getQRCodeSource } from '../../libs/miniQRCodeRoute.js'
import { getYHStorageSync } from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:'',
    title:'',
    shareUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let hideShareMenu = options.hideShareMenu;
    if (hideShareMenu) {
      wx.hideShareMenu();
    }
    if (options && options.scene && options.scene.length > 0) {
      let code = options.scene;
      getQRCodeSource(code).then(json => {
        if (json) {
          this.loadElement(json);
        }
      })
        .catch(error => {
        });
    } else {
      this.loadElement(options);
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      let h5backReload = wx.getStorageSync('h5backReload');
      
      if (h5backReload) {
          wx.removeStorage({key: 'h5backReload'});
          this.loadElement({url: h5backReload});
      }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    let newURL = this.data.url.split('?');
    let path = '/pages/webview/webview?url=' + this.data.url;

    if (newURL[1]) {
      if (newURL[1].indexOf('&app_version') > 0) {
        let newParams = newURL[1].split('&app_version');
        if (newParams[0]) {
          path = '/pages/webview/webview?url=' + newURL[0] + encodeURIComponent('?' + newParams[0]);
        }
      } else if (newURL[1].indexOf('app_version') === 0) {
        path = '/pages/webview/webview?url=' + newURL[0];
      }
    }

    if (this.data.shareUrl) {
      if (this.data.shareUrl.indexOf('http') >= 0) {
        path = '/pages/webview/webview?url=' + this.data.shareUrl;
      } else {
        let idx = newURL[0].lastIndexOf('/');
        let url = newURL[0].substring(0, idx) + '/' + this.data.shareUrl;
        path = '/pages/webview/webview?url=' + url;
      }
    }
    
    return {
      title: decodeURIComponent(this.data.title),
      path: path
    }
  },

  /**
   * 取得URL中的参数
   */
  getQueryObj(link) {
    let loc = decodeURIComponent(link);
    let variables = '';
    let variableArr = [];
    let finalArr = [];

    if (loc.indexOf('?') > 0) {
      variables = loc.split('?')[1];
    }

    if (variables.length > 0) {
      variableArr = variables.split('#')[0].split('&');
    }

    for (let i = 0; i < variableArr.length; i++) {
      let obj = {};

      obj.name = variableArr[i].split('=')[0];
      obj.value = variableArr[i].split('=')[1];
      finalArr.push(obj);
    }

    let query_obj = {};

    for (let i = 0; i < finalArr.length; i++) {
      query_obj[finalArr[i].name] = finalArr[i].value;
    }

    return query_obj;
  },

  loadElement: function(options){
    console.log(options);
    let isSign = true;
    if (options.url) {
      let queryObj = this.getQueryObj(options.url);

      if (queryObj.title) {
        this.setData({
          title: queryObj.title
        })
      }
      if(queryObj.shareURL) {
        this.setData({
          shareUrl: queryObj.shareURL
        })
      }
      if(queryObj.noSign) {
        isSign = false;
      }
    }

    let app = getApp()

    //解析渠道号
    let unionType = options.union_type ? options.union_type : '';
    if (unionType) {
      app.updateUnionType(unionType);
    }

    if (options && options.url) {
      let param = {}
      let sessionkey = app && app.globalData && app.globalData.sessionkey ? app.globalData.sessionkey : '';
      let udid = app && app.globalData && app.globalData.udid ? app.globalData.udid : "";
      if (udid.length == 0) {
        udid = getYHStorageSync('udid', 'webview');
      }

      let uid = app && app.globalData && app.globalData.userInfo && app.globalData.userInfo.uid ? app.globalData.userInfo.uid : 0;
      param.uid = uid;


      let res = wx.getSystemInfoSync();
      let screen_size = res.windowWidth + 'x' + res.windowHeight;
      let os_version = res.version;


      param.app_version = APP_VERSION;
      param.session_key = sessionkey;
      param.client_type = CLIENT_TYPE;
      param.screen_size = screen_size;
      param.os_version = os_version;
      param.udid = udid;
      param.uid = uid;

      let response = sign_body(param, decodeURIComponent(options.url));
      let url = response.url;
      console.log('response:', response);
      if (response.signString) {
        url = url + "&client_secret=" + response.signString
      }

      if (!isSign) {
        url = decodeURIComponent(options.url) + '&uid=' + param.uid + '&client_secret=' + response.signString;
      }

      console.log("webview-url:",url)
      this.setData({
        url: url
      })
    }
  },

  bindGetMsg: function (e) {
    if (e.detail && e.detail.data && e.detail.data[0] && e.detail.data[0].title){
      this.setData({
        title: e.detail.data[0].title,
      })
    }
  },
})