webview.js 1.74 KB
import {parse, stringify} from '../../vendors/query-stringify';
import config from '../../common/config';
import udid from '../../common/udid';
import event from '../../common/event';
import { verify } from '../../common/api';
import { tapGoLogin } from '../../common/login';

Page({

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

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

    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.on('user-login-success', () => {
          this.generateUrl(options);
        });
        tapGoLogin();
      } 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;
  },


  /**
   * 生成链接
   */
  generateUrl: function(options) {
    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);


    this.setData({
      url: uri + '?' + stringify(Object.assign({}, params, qs))
    });
  }
});