user.js 3.03 KB
import $ from 'jquery';
import jsonp from './jsonp';
import utils from './utils';
import cookies from './cookies';

let _noLoginUrl = function() {
    let referrer = location.href.split('?')[0];
    let domain = 'm.yohobuy.com';
    let url = utils.urlParse(referrer);

    if (url.host !== 'm.yohobuy.com' && url.host !== 'activity.yoho.cn' && url.host !== 'feature.yoho.cn') {
        domain = 'www.yohobuy.com';
    }

    if (/AliApp/i.test(navigator.userAgent)) {
        return `${location.protocol}//xianyu.yohobuy.com/xianyu/passport/login/taobao`;
    }

    return `//${domain}/signin.html?refer=` + encodeURIComponent(location.href) + '&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' + referrer.replace(/\//g, '\\/') + '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
};

export default {
    uid: '',
    noLoginUrl: _noLoginUrl,
    init() {
        let _this = this;
        return jsonp({
            url: '//m.yohobuy.com/passport/login/user?callback=?'
        }).then(function(res){
            if (res.code === 200) {
                _this.uid = Number(res.data) || 0;
            }
            if (!_this.uid && utils.isApp()) {
                _this.uid = Number(utils.queryString().uid) || 0;
            }
            if (res.unionType) {
                cookies.setCookie('unionTypeYas', res.unionType, {
                    path: '/'
                });
            }
            return $.Deferred().resolve(_this.uid);
        }, function() {
            utils.isApp() && (_this.uid = Number(utils.queryString().uid) || 0);
            return $.Deferred().resolve(_this.uid);
        });
    },
    auth() {
        if (!this.uid) {
            let noLoginUrl = _noLoginUrl();

            $('a.auth').attr('href', noLoginUrl);
        }
    },
    favout(skn) {
        if (!skn || !this.uid) {
            return;
        }

        jsonp({
            url: `//service.yoho.cn/activity/favorite/addFavorite?productSkn=${skn}&uid=${this.uid}&callback=?`,
        }).then(function(res) {
            if (res.code === 200) {
                utils.showTip({
                    content: '恭喜您,收藏成功',
                    close: true
                });
            } else {
                utils.showTip({
                    content: res.message,
                    close: true
                });
            }
        }, function(){
            utils.showTip({
                title: '收藏失败<br>请刷新重新领取',
                content: '如多次收藏失败,请联系客服人员<br>带来不便敬请谅解',
                close: false
            });
        });
    },
    getMktCode() {
        let mkt_code;

        if (this.uid) {
            if (Number(this.uid) % 1024 >= 512) {
                mkt_code = 100000000000195; // 默认的值
            } else {
                mkt_code = 100000000000193; // 个性化,动态接口数据
            }
        } else {
            mkt_code = 100000000000197; // 访客接口数据
        }
        return mkt_code;
    }
};