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

let _noLoginUrl = function() {
    if (!utils.isApp()) {
        return '//m.yohobuy.com/signin.html?refer=' + encodeURIComponent(location.href);
    } else {
        let referrer = location.href.split('?')[0];
        return '//m.yohobuy.com/signin.html?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;

        if (!utils.isApp()) {
            // 获取当前登录用户信息
            return jsonp({
                url: '//m.yohobuy.com/passport/login/user?callback=?'
            }).then(function(res){
                if (res.code === 200) {
                    _this.uid = res.data;
                }
                return $.Deferred().resolve(_this.uid);
            }, function() {
                return $.Deferred().resolve(_this.uid);
            });
        } else {
            _this.uid = utils.queryString().uid;
            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
            });
        });
    }
};