提交 a5e78997 编写于 作者: Q qiang

Merge branch 'dev' into alpha

# Conflicts:
#	packages/uni-mp-weixin/dist/index.js
import Vue from 'vue';
import { initVueI18n } from '@dcloudio/uni-i18n';
let realAtob;
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
if (typeof atob !== 'function') {
realAtob = function (str) {
str = String(str).replace(/[\t\n\f\r ]+/g, '');
if (!b64re.test(str)) { throw new Error("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.") }
// Adding the padding if missing, for semplicity
str += '=='.slice(2 - (str.length & 3));
var bitmap; var result = ''; var r1; var r2; var i = 0;
for (; i < str.length;) {
bitmap = b64.indexOf(str.charAt(i++)) << 18 | b64.indexOf(str.charAt(i++)) << 12 |
(r1 = b64.indexOf(str.charAt(i++))) << 6 | (r2 = b64.indexOf(str.charAt(i++)));
result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
: r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
: String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
}
return result
};
} else {
// 注意atob只能在全局对象上调用,例如:`const Base64 = {atob};Base64.atob('xxxx')`是错误的用法
realAtob = atob;
}
function b64DecodeUnicode (str) {
return decodeURIComponent(realAtob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}
function getCurrentUserInfo () {
const token = ( wx).getStorageSync('uni_id_token') || '';
const tokenArr = token.split('.');
if (!token || tokenArr.length !== 3) {
return {
uid: null,
role: [],
permission: [],
tokenExpired: 0
}
}
let userInfo;
try {
userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1]));
} catch (error) {
throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message)
}
userInfo.tokenExpired = userInfo.exp * 1000;
delete userInfo.exp;
delete userInfo.iat;
return userInfo
}
function uniIdMixin (Vue) {
Vue.prototype.uniIDHasRole = function (roleId) {
const {
role
} = getCurrentUserInfo();
return role.indexOf(roleId) > -1
};
Vue.prototype.uniIDHasPermission = function (permissionId) {
const {
permission
} = getCurrentUserInfo();
return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1
};
Vue.prototype.uniIDTokenValid = function () {
const {
tokenExpired
} = getCurrentUserInfo();
return tokenExpired > Date.now()
};
let realAtob;
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
if (typeof atob !== 'function') {
realAtob = function (str) {
str = String(str).replace(/[\t\n\f\r ]+/g, '');
if (!b64re.test(str)) { throw new Error("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.") }
// Adding the padding if missing, for semplicity
str += '=='.slice(2 - (str.length & 3));
var bitmap; var result = ''; var r1; var r2; var i = 0;
for (; i < str.length;) {
bitmap = b64.indexOf(str.charAt(i++)) << 18 | b64.indexOf(str.charAt(i++)) << 12 |
(r1 = b64.indexOf(str.charAt(i++))) << 6 | (r2 = b64.indexOf(str.charAt(i++)));
result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
: r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
: String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
}
return result
};
} else {
// 注意atob只能在全局对象上调用,例如:`const Base64 = {atob};Base64.atob('xxxx')`是错误的用法
realAtob = atob;
}
function b64DecodeUnicode (str) {
return decodeURIComponent(realAtob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}
function getCurrentUserInfo () {
const token = ( wx).getStorageSync('uni_id_token') || '';
const tokenArr = token.split('.');
if (!token || tokenArr.length !== 3) {
return {
uid: null,
role: [],
permission: [],
tokenExpired: 0
}
}
let userInfo;
try {
userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1]));
} catch (error) {
throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message)
}
userInfo.tokenExpired = userInfo.exp * 1000;
delete userInfo.exp;
delete userInfo.iat;
return userInfo
}
function uniIdMixin (Vue) {
Vue.prototype.uniIDHasRole = function (roleId) {
const {
role
} = getCurrentUserInfo();
return role.indexOf(roleId) > -1
};
Vue.prototype.uniIDHasPermission = function (permissionId) {
const {
permission
} = getCurrentUserInfo();
return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1
};
Vue.prototype.uniIDTokenValid = function () {
const {
tokenExpired
} = getCurrentUserInfo();
return tokenExpired > Date.now()
};
}
const _toString = Object.prototype.toString;
......@@ -546,163 +546,164 @@ var previewImage = {
}
};
function _getDeviceBrand (model) {
if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' }
if (/windows/gi.test(model)) { return 'microsoft' }
return ''
}
const UUID_KEY = '__DC_STAT_UUID';
let deviceId;
function useDeviceId (result) {
deviceId = deviceId || wx.getStorageSync(UUID_KEY);
if (!deviceId) {
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7);
wx.setStorage({
key: UUID_KEY,
data: deviceId
});
}
result.deviceId = deviceId;
}
function addSafeAreaInsets (result) {
if (result.safeArea) {
const safeArea = result.safeArea;
result.safeAreaInsets = {
top: safeArea.top,
left: safeArea.left,
right: result.windowWidth - safeArea.right,
bottom: result.screenHeight - safeArea.bottom
};
}
}
function populateParameters (result) {
const {
brand = '', model = '', system = '',
language = '', theme, version,
hostName, platform, fontSizeSetting,
SDKVersion, pixelRatio, deviceOrientation,
environment
} = result;
const isQuickApp = "mp-weixin".indexOf('quickapp-webview') !== -1;
// osName osVersion
let osName = '';
let osVersion = '';
{
osName = system.split(' ')[0] || '';
osVersion = system.split(' ')[1] || '';
}
let hostVersion = version;
// deviceType
const deviceType = getGetDeviceType(result, model);
// deviceModel
const deviceBrand = getDeviceBrand(brand, model, isQuickApp);
// hostName
let _hostName = hostName || "mp-weixin".split('-')[1]; // mp-jd
{
if (environment) {
_hostName = environment;
} else if (result.host && result.host.env) {
_hostName = result.host.env;
}
}
// deviceOrientation
let _deviceOrientation = deviceOrientation; // 仅 微信 百度 支持
// devicePixelRatio
let _devicePixelRatio = pixelRatio;
// SDKVersion
let _SDKVersion = SDKVersion;
// wx.getAccountInfoSync
const parameters = {
appId: process.env.UNI_APP_ID,
appName: process.env.UNI_APP_NAME,
appVersion: process.env.UNI_APP_VERSION_NAME,
appVersionCode: process.env.UNI_APP_VERSION_CODE,
uniCompileVersion: process.env.UNI_COMPILER_VERSION,
uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
deviceBrand,
deviceModel: model,
deviceType,
devicePixelRatio: _devicePixelRatio,
deviceOrientation: _deviceOrientation,
osName: osName.toLocaleLowerCase(),
osVersion,
hostTheme: theme,
hostVersion,
hostLanguage: language.replace('_', '-'),
hostName: _hostName,
hostSDKVersion: _SDKVersion,
hostFontSizeSetting: fontSizeSetting,
windowTop: 0,
windowBottom: 0,
// TODO
osLanguage: undefined,
osTheme: undefined,
ua: undefined,
hostPackageName: undefined,
browserName: undefined,
browseVersion: undefined
};
Object.assign(result, parameters);
}
function getGetDeviceType (result, model) {
let deviceType = result.deviceType || 'phone';
{
const deviceTypeMaps = {
ipad: 'pad',
windows: 'pc',
mac: 'pc'
};
const deviceTypeMapsKeys = Object.keys(deviceTypeMaps);
const _model = model.toLocaleLowerCase();
for (let index = 0; index < deviceTypeMapsKeys.length; index++) {
const _m = deviceTypeMapsKeys[index];
if (_model.indexOf(_m) !== -1) {
deviceType = deviceTypeMaps[_m];
break
}
}
}
return deviceType
}
function getDeviceBrand (
brand,
model,
isQuickApp = false
) {
let deviceBrand = model.split(' ')[0].toLocaleLowerCase();
if (
isQuickApp
) {
deviceBrand = brand.toLocaleLowerCase();
} else {
deviceBrand = _getDeviceBrand(deviceBrand);
}
return deviceBrand
function _getDeviceBrand (model) {
if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' }
if (/windows/gi.test(model)) { return 'microsoft' }
return ''
}
const UUID_KEY = '__DC_STAT_UUID';
let deviceId;
function useDeviceId (result) {
deviceId = deviceId || wx.getStorageSync(UUID_KEY);
if (!deviceId) {
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7);
wx.setStorage({
key: UUID_KEY,
data: deviceId
});
}
result.deviceId = deviceId;
}
function addSafeAreaInsets (result) {
if (result.safeArea) {
const safeArea = result.safeArea;
result.safeAreaInsets = {
top: safeArea.top,
left: safeArea.left,
right: result.windowWidth - safeArea.right,
bottom: result.screenHeight - safeArea.bottom
};
}
}
function populateParameters (result) {
const {
brand = '', model = '', system = '',
language = '', theme, version,
hostName, platform, fontSizeSetting,
SDKVersion, pixelRatio, deviceOrientation,
environment
} = result;
const isQuickApp = "mp-weixin".indexOf('quickapp-webview') !== -1;
// osName osVersion
let osName = '';
let osVersion = '';
{
osName = system.split(' ')[0] || '';
osVersion = system.split(' ')[1] || '';
}
let hostVersion = version;
// deviceType
const deviceType = getGetDeviceType(result, model);
// deviceModel
const deviceBrand = getDeviceBrand(brand, model, isQuickApp);
// hostName
const _platform = 'WeChat' ;
let _hostName = hostName || _platform; // mp-jd
{
if (environment) {
_hostName = environment;
} else if (result.host && result.host.env) {
_hostName = result.host.env;
}
}
// deviceOrientation
let _deviceOrientation = deviceOrientation; // 仅 微信 百度 支持
// devicePixelRatio
let _devicePixelRatio = pixelRatio;
// SDKVersion
let _SDKVersion = SDKVersion;
// wx.getAccountInfoSync
const parameters = {
appId: process.env.UNI_APP_ID,
appName: process.env.UNI_APP_NAME,
appVersion: process.env.UNI_APP_VERSION_NAME,
appVersionCode: process.env.UNI_APP_VERSION_CODE,
uniCompileVersion: process.env.UNI_COMPILER_VERSION,
uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
deviceBrand,
deviceModel: model,
deviceType,
devicePixelRatio: _devicePixelRatio,
deviceOrientation: _deviceOrientation,
osName: osName.toLocaleLowerCase(),
osVersion,
hostTheme: theme,
hostVersion,
hostLanguage: language.replace('_', '-'),
hostName: _hostName,
hostSDKVersion: _SDKVersion,
hostFontSizeSetting: fontSizeSetting,
windowTop: 0,
windowBottom: 0,
// TODO
osLanguage: undefined,
osTheme: undefined,
ua: undefined,
hostPackageName: undefined,
browserName: undefined,
browseVersion: undefined
};
Object.assign(result, parameters);
}
function getGetDeviceType (result, model) {
let deviceType = result.deviceType || 'phone';
{
const deviceTypeMaps = {
ipad: 'pad',
windows: 'pc',
mac: 'pc'
};
const deviceTypeMapsKeys = Object.keys(deviceTypeMaps);
const _model = model.toLocaleLowerCase();
for (let index = 0; index < deviceTypeMapsKeys.length; index++) {
const _m = deviceTypeMapsKeys[index];
if (_model.indexOf(_m) !== -1) {
deviceType = deviceTypeMaps[_m];
break
}
}
}
return deviceType
}
function getDeviceBrand (
brand,
model,
isQuickApp = false
) {
let deviceBrand = model.split(' ')[0].toLocaleLowerCase();
if (
isQuickApp
) {
deviceBrand = brand.toLocaleLowerCase();
} else {
deviceBrand = _getDeviceBrand(deviceBrand);
}
return deviceBrand
}
var getSystemInfo = {
returnValue: function (result) {
useDeviceId(result);
addSafeAreaInsets(result);
populateParameters(result);
}
var getSystemInfo = {
returnValue: function (result) {
useDeviceId(result);
addSafeAreaInsets(result);
populateParameters(result);
}
};
var showActionSheet = {
......@@ -713,55 +714,55 @@ var showActionSheet = {
}
};
var getAppBaseInfo = {
returnValue: function (result) {
const { version, language, SDKVersion, theme } = result;
let _hostName = "mp-weixin".split('-')[1]; // mp-jd
{
if (result.host && result.host.env) {
_hostName = result.host.env;
}
}
Object.assign(result, {
hostVersion: version,
hostLanguage: language.replace('_', '-'),
hostName: _hostName,
hostSDKVersion: SDKVersion,
hostTheme: theme,
appId: process.env.UNI_APP_ID,
appName: process.env.UNI_APP_NAME,
appVersion: process.env.UNI_APP_VERSION_NAME,
appVersionCode: process.env.UNI_APP_VERSION_CODE
});
}
var getAppBaseInfo = {
returnValue: function (result) {
const { version, language, SDKVersion, theme } = result;
let _hostName = "mp-weixin".split('-')[1]; // mp-jd
{
if (result.host && result.host.env) {
_hostName = result.host.env;
}
}
Object.assign(result, {
hostVersion: version,
hostLanguage: language.replace('_', '-'),
hostName: _hostName,
hostSDKVersion: SDKVersion,
hostTheme: theme,
appId: process.env.UNI_APP_ID,
appName: process.env.UNI_APP_NAME,
appVersion: process.env.UNI_APP_VERSION_NAME,
appVersionCode: process.env.UNI_APP_VERSION_CODE
});
}
};
var getDeviceInfo = {
returnValue: function (result) {
const { brand, model } = result;
const deviceType = getGetDeviceType(result, model);
const deviceBrand = getDeviceBrand(brand, model);
useDeviceId(result);
Object.assign(result, {
deviceType,
deviceBrand,
deviceModel: model
});
}
var getDeviceInfo = {
returnValue: function (result) {
const { brand, model } = result;
const deviceType = getGetDeviceType(result, model);
const deviceBrand = getDeviceBrand(brand, model);
useDeviceId(result);
Object.assign(result, {
deviceType,
deviceBrand,
deviceModel: model
});
}
};
var getWindowInfo = {
returnValue: function (result) {
addSafeAreaInsets(result);
Object.assign(result, {
windowTop: 0,
windowBottom: 0
});
}
var getWindowInfo = {
returnValue: function (result) {
addSafeAreaInsets(result);
Object.assign(result, {
windowTop: 0,
windowBottom: 0
});
}
};
// import navigateTo from 'uni-helpers/navigate-to'
......@@ -1789,172 +1790,172 @@ function getEventChannel (id) {
return eventChannelStack.shift()
}
const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound',
'onThemeChange',
'onUnhandledRejection'
];
function initEventChannel () {
Vue.prototype.getOpenerEventChannel = function () {
// 微信小程序使用自身getOpenerEventChannel
{
return this.$scope.getOpenerEventChannel()
}
};
const callHook = Vue.prototype.__call_hook;
Vue.prototype.__call_hook = function (hook, args) {
if (hook === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
return callHook.call(this, hook, args)
};
}
function initScopedSlotsParams () {
const center = {};
const parents = {};
Vue.prototype.$hasScopedSlotsParams = function (vueId) {
const has = center[vueId];
if (!has) {
parents[vueId] = this;
this.$on('hook:destroyed', () => {
delete parents[vueId];
});
}
return has
};
Vue.prototype.$getScopedSlotsParams = function (vueId, name, key) {
const data = center[vueId];
if (data) {
const object = data[name] || {};
return key ? object[key] : object
} else {
parents[vueId] = this;
this.$on('hook:destroyed', () => {
delete parents[vueId];
});
}
};
Vue.prototype.$setScopedSlotsParams = function (name, value) {
const vueIds = this.$options.propsData.vueId;
if (vueIds) {
const vueId = vueIds.split(',')[0];
const object = center[vueId] = center[vueId] || {};
object[name] = value;
if (parents[vueId]) {
parents[vueId].$forceUpdate();
}
}
};
Vue.mixin({
destroyed () {
const propsData = this.$options.propsData;
const vueId = propsData && propsData.vueId;
if (vueId) {
delete center[vueId];
delete parents[vueId];
}
}
});
}
function parseBaseApp (vm, {
mocks,
initRefs
}) {
initEventChannel();
{
initScopedSlotsParams();
}
if (vm.$options.store) {
Vue.prototype.$store = vm.$options.store;
}
uniIdMixin(Vue);
Vue.prototype.mpHost = "mp-weixin";
Vue.mixin({
beforeCreate () {
if (!this.$options.mpType) {
return
}
this.mpType = this.$options.mpType;
this.$mp = {
data: {},
[this.mpType]: this.$options.mpInstance
};
this.$scope = this.$options.mpInstance;
delete this.$options.mpType;
delete this.$options.mpInstance;
if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
const app = getApp();
if (app.$vm && app.$vm.$i18n) {
this._i18n = app.$vm.$i18n;
}
}
if (this.mpType !== 'app') {
initRefs(this);
initMocks(this, mocks);
}
}
});
const appOptions = {
onLaunch (args) {
if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return
}
{
if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
}
}
this.$vm = vm;
this.$vm.$mp = {
app: this
};
this.$vm.$scope = this;
// vm 上也挂载 globalData
this.$vm.globalData = this.globalData;
this.$vm._isMounted = true;
this.$vm.__call_hook('mounted', args);
this.$vm.__call_hook('onLaunch', args);
}
};
// 兼容旧版本 globalData
appOptions.globalData = vm.$options.globalData || {};
// 将 methods 中的方法挂在 getApp() 中
const methods = vm.$options.methods;
if (methods) {
Object.keys(methods).forEach(name => {
appOptions[name] = methods[name];
});
}
initAppLocale(Vue, vm, wx.getSystemInfoSync().language || 'zh-Hans');
initHooks(appOptions, hooks);
return appOptions
const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound',
'onThemeChange',
'onUnhandledRejection'
];
function initEventChannel () {
Vue.prototype.getOpenerEventChannel = function () {
// 微信小程序使用自身getOpenerEventChannel
{
return this.$scope.getOpenerEventChannel()
}
};
const callHook = Vue.prototype.__call_hook;
Vue.prototype.__call_hook = function (hook, args) {
if (hook === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
return callHook.call(this, hook, args)
};
}
function initScopedSlotsParams () {
const center = {};
const parents = {};
Vue.prototype.$hasScopedSlotsParams = function (vueId) {
const has = center[vueId];
if (!has) {
parents[vueId] = this;
this.$on('hook:destroyed', () => {
delete parents[vueId];
});
}
return has
};
Vue.prototype.$getScopedSlotsParams = function (vueId, name, key) {
const data = center[vueId];
if (data) {
const object = data[name] || {};
return key ? object[key] : object
} else {
parents[vueId] = this;
this.$on('hook:destroyed', () => {
delete parents[vueId];
});
}
};
Vue.prototype.$setScopedSlotsParams = function (name, value) {
const vueIds = this.$options.propsData.vueId;
if (vueIds) {
const vueId = vueIds.split(',')[0];
const object = center[vueId] = center[vueId] || {};
object[name] = value;
if (parents[vueId]) {
parents[vueId].$forceUpdate();
}
}
};
Vue.mixin({
destroyed () {
const propsData = this.$options.propsData;
const vueId = propsData && propsData.vueId;
if (vueId) {
delete center[vueId];
delete parents[vueId];
}
}
});
}
function parseBaseApp (vm, {
mocks,
initRefs
}) {
initEventChannel();
{
initScopedSlotsParams();
}
if (vm.$options.store) {
Vue.prototype.$store = vm.$options.store;
}
uniIdMixin(Vue);
Vue.prototype.mpHost = "mp-weixin";
Vue.mixin({
beforeCreate () {
if (!this.$options.mpType) {
return
}
this.mpType = this.$options.mpType;
this.$mp = {
data: {},
[this.mpType]: this.$options.mpInstance
};
this.$scope = this.$options.mpInstance;
delete this.$options.mpType;
delete this.$options.mpInstance;
if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
const app = getApp();
if (app.$vm && app.$vm.$i18n) {
this._i18n = app.$vm.$i18n;
}
}
if (this.mpType !== 'app') {
initRefs(this);
initMocks(this, mocks);
}
}
});
const appOptions = {
onLaunch (args) {
if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return
}
{
if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
}
}
this.$vm = vm;
this.$vm.$mp = {
app: this
};
this.$vm.$scope = this;
// vm 上也挂载 globalData
this.$vm.globalData = this.globalData;
this.$vm._isMounted = true;
this.$vm.__call_hook('mounted', args);
this.$vm.__call_hook('onLaunch', args);
}
};
// 兼容旧版本 globalData
appOptions.globalData = vm.$options.globalData || {};
// 将 methods 中的方法挂在 getApp() 中
const methods = vm.$options.methods;
if (methods) {
Object.keys(methods).forEach(name => {
appOptions[name] = methods[name];
});
}
initAppLocale(Vue, vm, wx.getSystemInfoSync().language || 'zh-Hans');
initHooks(appOptions, hooks);
return appOptions
}
const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
......@@ -2242,10 +2243,10 @@ function parsePage (vuePageOptions) {
})
}
function createPage (vuePageOptions) {
{
return Component(parsePage(vuePageOptions))
}
function createPage (vuePageOptions) {
{
return Component(parsePage(vuePageOptions))
}
}
function createComponent (vueOptions) {
......@@ -2290,23 +2291,23 @@ function createSubpackageApp (vm) {
return vm
}
function createPlugin (vm) {
const appOptions = parseApp(vm);
if (isFn(appOptions.onShow) && wx.onAppShow) {
wx.onAppShow((...args) => {
vm.__call_hook('onShow', args);
});
}
if (isFn(appOptions.onHide) && wx.onAppHide) {
wx.onAppHide((...args) => {
vm.__call_hook('onHide', args);
});
}
if (isFn(appOptions.onLaunch)) {
const args = wx.getLaunchOptionsSync && wx.getLaunchOptionsSync();
vm.__call_hook('onLaunch', args);
}
return vm
function createPlugin (vm) {
const appOptions = parseApp(vm);
if (isFn(appOptions.onShow) && wx.onAppShow) {
wx.onAppShow((...args) => {
vm.__call_hook('onShow', args);
});
}
if (isFn(appOptions.onHide) && wx.onAppHide) {
wx.onAppHide((...args) => {
vm.__call_hook('onHide', args);
});
}
if (isFn(appOptions.onLaunch)) {
const args = wx.getLaunchOptionsSync && wx.getLaunchOptionsSync();
vm.__call_hook('onLaunch', args);
}
return vm
}
todos.forEach(todoApi => {
......
......@@ -16,15 +16,21 @@ import fr from './fr.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'
export const LOCALE_ZH_HANS = 'zh-Hans'
export const LOCALE_ZH_HANT = 'zh-Hant'
export const LOCALE_EN = 'en'
export const LOCALE_FR = 'fr'
export const LOCALE_ES = 'es'
const messages = {}
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'app-plus') {
Object.assign(messages, {
en,
es,
fr,
'zh-Hans': zhHans,
'zh-Hant': zhHant
[LOCALE_EN]: en,
[LOCALE_ES]: es,
[LOCALE_FR]: fr,
[LOCALE_ZH_HANS]: zhHans,
[LOCALE_ZH_HANT]: zhHant
})
}
......@@ -39,7 +45,7 @@ if (__PLATFORM__ === 'h5') {
locale = ''
}
} else {
locale = __GLOBAL__.getSystemInfoSync().language
locale = normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
}
function initI18nMessages () {
......@@ -95,7 +101,7 @@ export function initAppLocale (Vue, appVm, locale) {
}
Object.defineProperty(appVm, '$locale', {
get () {
return state.locale
return normalizeLocale(state.locale)
},
set (v) {
state.locale = v
......@@ -110,7 +116,7 @@ function getLocaleMessage () {
const locale = uni.getLocale()
const locales = __uniConfig.locales
return (
locales[locale] || locales[__uniConfig.fallbackLocale] || locales.en || {}
locales[locale] || locales[__uniConfig.fallbackLocale] || locales[LOCALE_EN] || {}
)
}
......@@ -190,6 +196,44 @@ export function initTabBarI18n (tabBar) {
return tabBar
}
function include (str, parts) {
return !!parts.find((part) => str.indexOf(part) !== -1)
}
function startsWith (str, parts) {
return parts.find((part) => str.indexOf(part) === 0)
}
export function normalizeLocale (locale, messages) {
if (!locale) {
return
}
locale = locale.trim().replace(/_/g, '-')
if (messages && messages[locale]) {
return locale
}
locale = locale.toLowerCase()
if (locale === 'chinese') {
// 支付宝
return LOCALE_ZH_HANS
}
if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS
}
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT
}
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
return LOCALE_ZH_HANT
}
return LOCALE_ZH_HANS
}
const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES])
if (lang) {
return lang
}
}
// export function initI18n() {
// const localeKeys = Object.keys(__uniConfig.locales || {})
// if (localeKeys.length) {
......@@ -197,4 +241,4 @@ export function initTabBarI18n (tabBar) {
// i18n.add(locale, __uniConfig.locales[locale])
// )
// }
// }
// }
import { normalizeLocale, LOCALE_EN } from '../helpers/i18n/index'
export function getLocale () {
// 优先使用 $locale
const app = getApp({
......@@ -6,7 +8,7 @@ export function getLocale () {
if (app && app.$vm) {
return app.$vm.$locale
}
return __GLOBAL__.getSystemInfoSync().language || 'zh-Hans'
return normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
}
export function setLocale (locale) {
......@@ -34,4 +36,4 @@ export function onLocaleChange (fn) {
if (typeof global !== 'undefined') {
global.getLocale = getLocale
}
}
......@@ -171,7 +171,7 @@ export function getBrowserInfo () {
var system = `${osname} ${osversion}`
var platform = osname.toLocaleLowerCase()
let browserName
let browserName = ''
let browseVersion = String(IEVersion())
if (browseVersion !== '-1') { browserName = 'IE' } else {
const browseVendors = ['Version', 'Firefox', 'Chrome', 'Edge{0,1}']
......
......@@ -66,7 +66,8 @@ export function populateParameters (result) {
const deviceBrand = getDeviceBrand(brand, model, isQuickApp)
// hostName
let _hostName = hostName || __PLATFORM__.split('-')[1] // mp-jd
const _platform = __PLATFORM__ === 'mp-weixin' ? 'WeChat' : __PLATFORM__.split('-')[1]
let _hostName = hostName || _platform // mp-jd
if (__PLATFORM__ === 'mp-weixin') {
if (environment) {
_hostName = environment
......
......@@ -6,7 +6,9 @@ import {
} from 'uni-wrapper/util'
import {
initAppLocale
initAppLocale,
normalizeLocale,
LOCALE_EN
} from 'uni-helpers/i18n/index'
import EventChannel from 'uni-helpers/EventChannel'
......@@ -185,7 +187,7 @@ export default function parseBaseApp (vm, {
})
}
initAppLocale(Vue, vm, __GLOBAL__.getSystemInfoSync().language || 'zh-Hans')
initAppLocale(Vue, vm, normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN)
initHooks(appOptions, hooks)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册