Blame view

app/pages/webview/webview.js 1.74 KB
邱骏 authored
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
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))
    });
  }
});