login.js 1.17 KB
import wx from 'weixin-js-sdk';
import {createLinkButton} from "../utils/common";
import base from './base';

export default class {
    constructor(obj) {
        this.env = obj.env;
        this.url = obj.loginUrl;
    }

    auth() {
        base.getUid().then((uid) => {
            if (!uid) {
                let list = document.querySelectorAll('.auth');
                if (list.length) {
                    list.forEach((value) => {
                        value.onclick = (event) => {
                            event.preventDefault();
                            this.goLogin();
                        }
                    });
                    return true;
                }
            }
        });
    }

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