base.js 5.32 KB
var jsonp = require('../utils/jsonp');
var cookies = require('../utils/cookie');
var common = require('../utils/common');
var Promise = require('../utils/promise');
var getQueryObj = common.getQueryObj;
var parseUrl = function (url) {
    var 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
    }
};

var webCacheUser;
var env = '';

var _appGetUid = function () {
    return new Promise(function (resolve) {
        common.invokeMethod({
            method: 'get.uid',
            arguments: {},
            success: function (uid) {
                resolve(parseInt(uid));
            },
            fail: function () {
                var uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
                resolve(uid);
            }
        });
    })
};
var _appGetSessionKey = function () {
    return new Promise(function (resolve) {
        common.invokeMethod({
            method: 'get.sessionId',
            arguments: {},
            success: function (sessionKey) {
                resolve(sessionKey);
            },
            fail: function () {
                var sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || '';
                resolve(sessionKey);
            }
        });
    })
};

module.exports = {
    //evn
    env: function () {
        var envFlag = window.__wxjs_environment;
        if (!envFlag && navigator.userAgent.match(/yohobuy/i)) {
            env = 'app';
            document.addEventListener('deviceready', function () {
            });
        } 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 () {
        var url, refer = location.href;
        if (env === 'app') {
            url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
            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);
        } else if (env === 'miniprogram') {
            url = '/pages/userCenter/userCenter'
        } else {
            if (refer.indexOf('?') < 0) {
                refer += "?skcall=1"
            } else {
                refer += "&skcall=1"
            }
            url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
        }
        return url;
    },
    //uid
    getUser: function (force) {
        return new Promise(function (resolve) {
            var obj = {
                sessionType: cookies.cookie('app_client_type') || getQueryObj().app_client_type || '',
                appVersion: cookies.cookie('app_version') || getQueryObj().app_version || ''
            };
            if (env === 'h5' || env === 'pc') {
                if (!force && webCacheUser) {
                    return resolve(webCacheUser);
                };

                jsonp({url: '//m.yohobuy.com/passport/login/user', jsonp: 'callback'}).then(function (r) {
                    obj.uid = 0;
                    obj.sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || '';

                    if (r.code === 200 || r.code === 403) {
                        obj.uid = Number(r.data) || 0;
                        webCacheUser = obj;
                    }

                    resolve(obj);
                });
            } else if (env === 'app') {
                document.addEventListener('deviceready',function() {
                    return Promise.all([_appGetUid(), _appGetSessionKey()]).then(function (r) {
                        obj.uid = r[0];
                        obj.sessionKey = r[1];
                        resolve(obj);
                    })
                })
            } else {
                obj.uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
                obj.sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || '';
                resolve(obj);
            }
        })
    }
};