am-editor-001/api/app/extend/helper.js

37 lines
882 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

exports.isMobile = (ctx) => {
const source = ctx.get('user-agent') || '';
let isMobile = false;
if (/mobile|android|iphone|ipad|phone/i.test(source)) {
isMobile = true;
}
return isMobile;
};
exports.parseCookie = (ctx) => {
let cookies = ctx.get('cookie');
if (!cookies) {
return [];
}
cookies = cookies.split(';');
const res = {};
for (const item of cookies) {
const kv = item.split('=');
if (kv && kv.length > 0) {
res[kv[0].trim()] = decodeURIComponent(kv[1]);
}
}
return res;
};
exports.parseNavLang = (ctx) => {
// 服务端无法获取navigator.language所以只能通过Accept-Language来判断浏览器语言。
let navigatorLang;
const clientLang = ctx.get('Accept-Language');
if (clientLang.startsWith('zh')) {
navigatorLang = 'zh-CN';
} else if (clientLang.startsWith('en')) {
navigatorLang = 'en-US';
}
return navigatorLang;
};