base.js 3.26 KB
import jsonp from "../utils/jsonp";
import cookies from '../utils/cookie';
import {getQueryObj} from '../utils/common'
import Promise from 'promise-polyfill'

const parseUrl = (url) => {
    let query = {},
        hashs,
        hash,
        i;

    url = (url || '').split('?');
    hashs = (url[1] || '').split('&');

    if (hashs && hashs.length) {
        for (i = 0; i < hashs.length; i++) {
            hash = hashs[i].split('=');
            query[hash[0]] = hash[1];
        }
    }

    return {
        path: url[0],
        query: query
    }
};
let env = '';
export default {
    //evn
    env: function () {
        let envFlag = window.__wxjs_environment;
        if (!envFlag && navigator.userAgent.match(/yohobuy/i)) {
            document.addEventListener('deviceready', function () {
            });
            env = 'app';
        } else if ((!envFlag && navigator.userAgent.match(/miniProgram/i)) || (envFlag === 'miniprogram')) {
            env = 'miniprogram';
        } else if (location.origin === 'https://www.yohobuy.com') {
            env = 'pc'
        } else {
            env = 'h5'
        }
        return env
    },
    //url
    loginUrl: function () {
        let url, refer = location.href.split('?')[0];
        url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
        if (env === 'app') {
            refer = parseUrl(refer);
            if (/Android/i.test(navigator.userAgent || '')) {
                if (url.indexOf('?') < 0) {
                    url += '?appLogin=1';
                }
                if (location.href.indexOf('&openby') >= 0) {
                    url = url.substring(0, url.indexOf('&openby'));
                }
                url += '&';
            } else {
                if (location.href.indexOf('#openby') >= 0) {
                    url = url.substring(0, url.indexOf('#openby'));
                }
                url += '#';
            }
            url += 'openby:yohobuy=' + JSON.stringify({
                action: 'go.weblogin',
                params: {
                    priority: 'N',
                    jumpurl: {
                        url: refer.path,
                        param: refer.query
                    },
                    needlogout: 'Y'
                }
            });
        } else if (env === 'pc') {
            url = 'https://www.yoho/signin.html?refer=' + encodeURIComponent(location.href) + '&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' + refer.replace(/\//g, '\\/') + '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
        } else if (env === 'miniprogram') {
            url = '/pages/userCenter/userCenter'
        }
        return url;
    },
    //uid
    getUid: function () {
        return new Promise(function (resolve) {
            if (env === 'h5' || env === 'pc') {
                jsonp('https://m.yohobuy.com/passport/login/user').then(r => {
                    let uid = 0;
                    if (r.code === 200) {
                        uid = Number(r.data)
                    }
                    resolve(uid);
                });
            } else {
                let uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
                resolve(uid);
            }
        })
    }
}