webview.js 3.44 KB
import {
  parse,
  stringify
} from '../../vendors/query-string.js';

// import event from '../../utils/event.js';

Page({

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    options.url = decodeURIComponent(options.url || '');

    if (options && options.scene && options.scene.length > 0) {
      let code = options.scene;

      // getQRCodeSource(code).then(json => {
      //   console.log('json:', json);
      //   if (json) {
      //     this.generateUrl(json);
      //   }
      // }).catch(error => { });
    } else {
      if (options.url) {
        let uid = this.getUid();

        const search = options.url.split('?')[1] || '';
        const qs = parse(search);

        if (Number(qs.needLogin) === 1 && uid === 0) {
          // event.emit('user-is-login', () => {

          // }, () => {
          //   this.generateUrl(options);
          // });
          this.generateUrl(options);
        } else {
          this.generateUrl(options);
        }
      }
    }
  },

  /**
   * 获取 UID
   */
  getUid: function () {
    const app = getApp() || {};

    return app.globalData &&
      app.globalData.userInfo &&
      app.globalData.userInfo.uid ?
      app.globalData.userInfo.uid : 0;
  },

  /**
   * 取得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;
  },


  /**
   * 生成链接
   */
  generateUrl: function (options) {
    console.log(options);
    let isSign = true;

    if (options.url) {
      let queryObj = this.getQueryObj(options.url);

      console.log('query=', queryObj);
      if (queryObj.title) {
        this.setData({
          title: queryObj.title
        });
      }
      if (queryObj.shareURL) {
        this.setData({
          shareUrl: queryObj.shareURL
        });
      }
      if (queryObj.noSign) {
        isSign = false;
      }
    }

    console.log('option.url=', decodeURIComponent(options.url));

    // const app = getApp() || {};
    // let param = {
    //   app_version: config.apiParams.app_version,
    //   session_key: app.globalData && app.globalData.sessionKey || '',
    //   client_type: config.apiParams.client_type,
    //   udid: udid.get()
    // };
    // let uid = this.getUid();

    // if (uid) {
    //   param.uid = uid;
    // }

    let url = options.url;
    // let params = verify.computeSecret(param);

    const [uri, search] = url.split('?');
    const qs = parse(search);

    url = uri + '?' + stringify(Object.assign({}, qs));
    // 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,
      });
    }
  },
});