wgt-node-utils 1.2.19 → 1.2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/bundle.js +1 -1
- package/package.json +1 -1
- package/src/index.js +43 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -796,5 +796,48 @@ class wgtNodeUtils {
|
|
|
796
796
|
}
|
|
797
797
|
}
|
|
798
798
|
|
|
799
|
+
/**
|
|
800
|
+
* @param {Array} languageCodeList - 语言代码列表
|
|
801
|
+
* @returns {Array} - 格式化后的语言代码列表
|
|
802
|
+
* */
|
|
803
|
+
formatLanguageCodeList = (languageCodeList) => {
|
|
804
|
+
return languageCodeList.map(str => typeof str === 'string' ? str.toLowerCase() : str);
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* 获取国家code
|
|
808
|
+
* @param {object} req - 请求对象
|
|
809
|
+
* @returns {string} - 国家code
|
|
810
|
+
* */
|
|
811
|
+
getCloudFrontViewerCountry = (req, LANGUAGE_CODE_LIST) => {
|
|
812
|
+
// 从header中获取国家code
|
|
813
|
+
const countryCode = req.headers['cloudfront-viewer-country'];
|
|
814
|
+
// 从header中获取accept-language
|
|
815
|
+
const acceptLanguage = req.headers['accept-language'];
|
|
816
|
+
|
|
817
|
+
let languageCode = countryCode || (acceptLanguage ? acceptLanguage.split(',')[0].split('-')[0].toLowerCase() : 'en');
|
|
818
|
+
return LANGUAGE_CODE_LIST.includes(languageCode) ? languageCode : 'en';
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* 获取网站语言
|
|
823
|
+
* @param {string} url - 网站语言获取接口
|
|
824
|
+
* @param {string} hostname - 网站域名
|
|
825
|
+
* @returns {Array} - 格式化后的语言代码列表
|
|
826
|
+
* */
|
|
827
|
+
getSiteLanguage = async (url) => {
|
|
828
|
+
return new Promise((resolve) => {
|
|
829
|
+
axios.get(url).then(res => {
|
|
830
|
+
let siteData = res.data.data;
|
|
831
|
+
// 如果未获取到数据,使用en做为默认数据
|
|
832
|
+
if (!siteData) {
|
|
833
|
+
siteData = ['en']
|
|
834
|
+
}
|
|
835
|
+
resolve(this.formatLanguageCodeList(siteData));
|
|
836
|
+
}).catch(err => {
|
|
837
|
+
resolve(this.formatLanguageCodeList(['en']));
|
|
838
|
+
});
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
|
|
799
842
|
}
|
|
800
843
|
module.exports = new wgtNodeUtils('cli_a538bc45e770d00b', 'EDK1uleQyLym6WWDISo00dwx4gssiskJ');
|