webview.js 3.83 KB
import accountModel from '../../models/account/index';
import saleModel from '../../models/activity/sale/index';
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 { tapGoBind } from '../../common/login';

Page({

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

    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        if (options.scene) {
            accountModel.getMiniAppRealParam(decodeURIComponent(options.scene || ''))
                .then(query => {
                    global.router.go('webView', query);
                });
        } else {
            if (!options.limitId) {
                this.setData({
                    activityExist: '2'
                });
                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);
                        });
                        tapGoBind();
                    } else {
                        this.generateUrl(options);
                    }
                }
            } else { // 限定C类公众号文章
                saleModel.queryLimitActivityDetail({id: options.limitId}).then(res => {
                    // 不存在的活动
                    if (!res.data || res.data.code === 400) {
                        return this.setData({
                            activityExist: '1'
                        });
                    } else {
                        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);
                                });
                                tapGoBind();
                            } else {
                                this.generateUrl(options);
                            }
                        }

                        this.setData({
                            activityExist: '2'
                        });
                    }
                });
            }
        }
    },

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

        return app && app.getUid() ? app.getUid() : 0;
    },


    /**
   * 生成链接
   */
    generateUrl: function(options) {
        wx.showLoading({
            title: '正在加载中...'
        });

        let app = getApp();
        let param = {
            app_version: config.apiParams.app_version,
            session_key: app && app.getSessionKey() || '',
            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))
        }, wx.hideLoading);
    },
    checkOther: function() {
        global.router.go('activityIndex');
    }
});