提交 020faa4e 编写于 作者: L liuxiaohang 提交者: qiang

fix(mp-toutiao): 修复 字节小程序$emit不触发 fixed #2774

# Conflicts:
#	packages/uni-mp-toutiao/dist/index.js
上级 58ff9525
import Vue from 'vue';
function b64DecodeUnicode (str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}
function getCurrentUserInfo () {
const token = ( tt).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()
};
function b64DecodeUnicode (str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}
function getCurrentUserInfo () {
const token = ( tt).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;
......@@ -581,44 +581,44 @@ var previewImage = {
}
};
const UUID_KEY = '__DC_STAT_UUID';
let deviceId;
function addUuid (result) {
deviceId = deviceId || tt.getStorageSync(UUID_KEY);
if (!deviceId) {
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7);
tt.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.windowHeight - safeArea.bottom
};
}
}
var getSystemInfo = {
returnValue: function (result) {
addUuid(result);
addSafeAreaInsets(result);
}
const UUID_KEY = '__DC_STAT_UUID';
let deviceId;
function addUuid (result) {
deviceId = deviceId || tt.getStorageSync(UUID_KEY);
if (!deviceId) {
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7);
tt.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.windowHeight - safeArea.bottom
};
}
}
var getSystemInfo = {
returnValue: function (result) {
addUuid(result);
addSafeAreaInsets(result);
}
};
const oName = 'getUserInfo';
const nName = 'getUserProfile';
var getUserProfile = {
name: tt.canIUse(nName) ? nName : oName
const oName = 'getUserInfo';
const nName = 'getUserProfile';
var getUserProfile = {
name: tt.canIUse(nName) ? nName : oName
};
// 不支持的 API 列表
......@@ -978,96 +978,96 @@ var eventApi = /*#__PURE__*/Object.freeze({
$emit: $emit
});
function createMediaQueryObserver () {
const mediaQueryObserver = {};
const {
windowWidth,
windowHeight
} = tt.getSystemInfoSync();
const orientation = windowWidth < windowHeight ? 'portrait' : 'landscape';
mediaQueryObserver.observe = (options, callback) => {
let matches = true;
for (const item in options) {
const itemValue = item === 'orientation' ? options[item] : Number(options[item]);
if (options[item] !== '') {
if (item === 'width') {
if (itemValue === windowWidth) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'minWidth') {
if (windowWidth >= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'maxWidth') {
if (windowWidth <= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'height') {
if (itemValue === windowHeight) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'minHeight') {
if (windowHeight >= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'maxHeight') {
if (windowHeight <= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'orientation') {
if (options[item] === orientation) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
}
}
callback(matches);
return matches
};
mediaQueryObserver.disconnect = () => {
};
return mediaQueryObserver
function createMediaQueryObserver () {
const mediaQueryObserver = {};
const {
windowWidth,
windowHeight
} = tt.getSystemInfoSync();
const orientation = windowWidth < windowHeight ? 'portrait' : 'landscape';
mediaQueryObserver.observe = (options, callback) => {
let matches = true;
for (const item in options) {
const itemValue = item === 'orientation' ? options[item] : Number(options[item]);
if (options[item] !== '') {
if (item === 'width') {
if (itemValue === windowWidth) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'minWidth') {
if (windowWidth >= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'maxWidth') {
if (windowWidth <= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'height') {
if (itemValue === windowHeight) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'minHeight') {
if (windowHeight >= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'maxHeight') {
if (windowHeight <= itemValue) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
if (item === 'orientation') {
if (options[item] === orientation) {
matches = true;
} else {
matches = false;
callback(matches);
return matches
}
}
}
}
callback(matches);
return matches
};
mediaQueryObserver.disconnect = () => {
};
return mediaQueryObserver
}
var api = /*#__PURE__*/Object.freeze({
......@@ -1091,7 +1091,11 @@ function initTriggerEvent (mpInstance) {
};
}
function initHook (name, options) {
function initHook (name, options, isComponent) {
{
// fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created
isComponent && (options = options.lifetimes);
}
const oldHook = options[name];
if (!oldHook) {
options[name] = function () {
......@@ -1113,7 +1117,7 @@ if (!MPPage.__$wrappered) {
Page.after = MPPage.after;
Component = function (options = {}) {
initHook('created', options);
initHook('created', options, true);
return MPComponent(options)
};
}
......@@ -2297,23 +2301,23 @@ function createSubpackageApp (vm) {
return vm
}
function createPlugin (vm) {
const appOptions = parseApp(vm);
if (isFn(appOptions.onShow) && tt.onAppShow) {
tt.onAppShow((...args) => {
appOptions.onShow.apply(vm, args);
});
}
if (isFn(appOptions.onHide) && tt.onAppHide) {
tt.onAppHide((...args) => {
appOptions.onHide.apply(vm, args);
});
}
if (isFn(appOptions.onLaunch)) {
const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync();
appOptions.onLaunch.call(vm, args);
}
return vm
function createPlugin (vm) {
const appOptions = parseApp(vm);
if (isFn(appOptions.onShow) && tt.onAppShow) {
tt.onAppShow((...args) => {
appOptions.onShow.apply(vm, args);
});
}
if (isFn(appOptions.onHide) && tt.onAppHide) {
tt.onAppHide((...args) => {
appOptions.onHide.apply(vm, args);
});
}
if (isFn(appOptions.onLaunch)) {
const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync();
appOptions.onLaunch.call(vm, args);
}
return vm
}
todos.forEach(todoApi => {
......
......@@ -24,7 +24,11 @@ function initTriggerEvent (mpInstance) {
}
}
function initHook (name, options) {
function initHook (name, options, isComponent) {
if (__PLATFORM__ === 'mp-toutiao') {
// fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created
isComponent && (options = options.lifetimes)
}
const oldHook = options[name]
if (!oldHook) {
options[name] = function () {
......@@ -46,7 +50,7 @@ if (!MPPage.__$wrappered) {
Page.after = MPPage.after
Component = function (options = {}) {
initHook('created', options)
initHook('created', options, true)
return MPComponent(options)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册