login.js 1.22 KB
var common = require('../utils/common');
var base = require('./base');

function login(obj) {
    var _this = this;
    _this.env = obj.env;
    _this.url = obj.loginUrl;

    _this.goLogin = function () {
        if (_this.env === 'miniprogram') {
            wx.miniProgram.switchTab({url: _this.url});
        } else if (_this.env === 'app') {
            var loginButton = document.getElementById('yosdk-login');
            if (!loginButton) {
                loginButton = common.createLinkButton(_this.url, 'yosdk-login');
            }
            loginButton.click();
        } else {
            window.location.href = _this.url;
        }
        return false;
    };

    _this.auth = function () {
        base.getUser().then(function (user) {
            if (!user.uid) {
                var list = document.querySelectorAll('.auth');
                if (list.length) {
                    list.forEach(function (value) {
                        value.onclick = function (event) {
                            event.preventDefault();
                            _this.goLogin();
                        }
                    });
                    return true;
                }
            }
        });
    }
}

module.exports = login;