global.js 1.1 KB
/**
 * 全局引用js
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2017/03/15
 */
const $ = require('yoho-jquery');
const cookie = require('yoho-cookie');
const qs = require('yoho-qs');
const tip = require('plugin/tip');
const yoho = require('./yoho-app');


// App 的信息需要存下来
if (yoho.isApp && qs.uid && qs.app_version && qs.client_type) {
    const options = {
        path: '/'
    };

    cookie.set('app_uid', qs.uid, options);
    cookie.set('app_version', qs.app_version, options);
    cookie.set('app_client_type', qs.client_type, options);
}

// 注册ajaxError处理服务端401状态
$(document).ajaxError((event, xhr) => {
    if (xhr.status === 401) {
        if (xhr.responseJSON && xhr.responseJSON.lowVersion) {
            tip.show(xhr.responseJSON.message);
        } else {
            cookie.remove('_UID');
            cookie.remove('_TOKEN');

            if (yoho.isApp) {
                yoho.goLogin(window.location.href);
            } else {
                window.location.href = `/signin.html?refer=${encodeURIComponent(window.location.href)}`;
            }
        }
    }
});