提交 8cb77614 编写于 作者: L liuxiaohang

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

上级 d5466f64
import Vue from 'vue'; import Vue from 'vue';
function b64DecodeUnicode (str) { function b64DecodeUnicode (str) {
return decodeURIComponent(atob(str).split('').map(function (c) { return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
...@@ -48,8 +48,8 @@ function uniIdMixin (Vue) { ...@@ -48,8 +48,8 @@ function uniIdMixin (Vue) {
} = getCurrentUserInfo(); } = getCurrentUserInfo();
return tokenExpired > Date.now() return tokenExpired > Date.now()
}; };
} }
const _toString = Object.prototype.toString; const _toString = Object.prototype.toString;
const hasOwnProperty = Object.prototype.hasOwnProperty; const hasOwnProperty = Object.prototype.hasOwnProperty;
...@@ -88,8 +88,8 @@ function cached (fn) { ...@@ -88,8 +88,8 @@ function cached (fn) {
const camelizeRE = /-(\w)/g; const camelizeRE = /-(\w)/g;
const camelize = cached((str) => { const camelize = cached((str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
}); });
const HOOKS = [ const HOOKS = [
'invoke', 'invoke',
'success', 'success',
...@@ -192,7 +192,7 @@ function queue (hooks, data) { ...@@ -192,7 +192,7 @@ function queue (hooks, data) {
} }
if (res === false) { if (res === false) {
return { return {
then () {} then () { }
} }
} }
} }
...@@ -272,16 +272,20 @@ const promiseInterceptor = { ...@@ -272,16 +272,20 @@ const promiseInterceptor = {
if (!isPromise(res)) { if (!isPromise(res)) {
return res return res
} }
return res.then(res => { return new Promise((resolve, reject) => {
return res[1] res.then(res => {
}).catch(res => { if (res[0]) {
return res[0] reject(res[0]);
} else {
resolve(res[1]);
}
});
}) })
} }
}; };
const SYNC_API_RE = const SYNC_API_RE =
/^\$|Window$|WindowStyle$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; /^\$|Window$|WindowStyle$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale/;
const CONTEXT_API_RE = /^create|Manager$/; const CONTEXT_API_RE = /^create|Manager$/;
...@@ -350,8 +354,8 @@ function promisify (name, api) { ...@@ -350,8 +354,8 @@ function promisify (name, api) {
}), ...params); }), ...params);
}))) })))
} }
} }
const EPS = 1e-4; const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750; const BASE_DEVICE_WIDTH = 750;
let isIOS = false; let isIOS = false;
...@@ -392,20 +396,20 @@ function upx2px (number, newDeviceWidth) { ...@@ -392,20 +396,20 @@ function upx2px (number, newDeviceWidth) {
} }
} }
return number < 0 ? -result : result return number < 0 ? -result : result
} }
const interceptors = { const interceptors = {
promiseInterceptor promiseInterceptor
}; };
var baseApi = /*#__PURE__*/Object.freeze({ var baseApi = /*#__PURE__*/Object.freeze({
__proto__: null, __proto__: null,
upx2px: upx2px, upx2px: upx2px,
addInterceptor: addInterceptor, addInterceptor: addInterceptor,
removeInterceptor: removeInterceptor, removeInterceptor: removeInterceptor,
interceptors: interceptors interceptors: interceptors
}); });
class EventChannel { class EventChannel {
constructor (id, events) { constructor (id, events) {
this.id = id; this.id = id;
...@@ -472,8 +476,8 @@ class EventChannel { ...@@ -472,8 +476,8 @@ class EventChannel {
type type
}); });
} }
} }
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
...@@ -509,8 +513,8 @@ var navigateTo = { ...@@ -509,8 +513,8 @@ var navigateTo = {
returnValue (fromRes, toRes) { returnValue (fromRes, toRes) {
fromRes.eventChannel = getEventChannel(); fromRes.eventChannel = getEventChannel();
} }
}; };
function findExistsPageIndex (url) { function findExistsPageIndex (url) {
const pages = getCurrentPages(); const pages = getCurrentPages();
let len = pages.length; let len = pages.length;
...@@ -521,8 +525,8 @@ function findExistsPageIndex (url) { ...@@ -521,8 +525,8 @@ function findExistsPageIndex (url) {
} }
} }
return -1 return -1
} }
var redirectTo = { var redirectTo = {
name (fromArgs) { name (fromArgs) {
if (fromArgs.exists === 'back' && fromArgs.delta) { if (fromArgs.exists === 'back' && fromArgs.delta) {
...@@ -541,8 +545,8 @@ var redirectTo = { ...@@ -541,8 +545,8 @@ var redirectTo = {
} }
} }
} }
}; };
var previewImage = { var previewImage = {
args (fromArgs) { args (fromArgs) {
let currentIndex = parseInt(fromArgs.current); let currentIndex = parseInt(fromArgs.current);
...@@ -575,8 +579,8 @@ var previewImage = { ...@@ -575,8 +579,8 @@ var previewImage = {
loop: false loop: false
} }
} }
}; };
const UUID_KEY = '__DC_STAT_UUID'; const UUID_KEY = '__DC_STAT_UUID';
let deviceId; let deviceId;
function addUuid (result) { function addUuid (result) {
...@@ -608,15 +612,15 @@ var getSystemInfo = { ...@@ -608,15 +612,15 @@ var getSystemInfo = {
addUuid(result); addUuid(result);
addSafeAreaInsets(result); addSafeAreaInsets(result);
} }
}; };
const oName = 'getUserInfo'; const oName = 'getUserInfo';
const nName = 'getUserProfile'; const nName = 'getUserProfile';
var getUserProfile = { var getUserProfile = {
name: tt.canIUse(nName) ? nName : oName name: tt.canIUse(nName) ? nName : oName
}; };
// 不支持的 API 列表 // 不支持的 API 列表
const todos = [ const todos = [
'preloadPage', 'preloadPage',
...@@ -788,8 +792,8 @@ const protocols = { ...@@ -788,8 +792,8 @@ const protocols = {
digestAlgorithm: false digestAlgorithm: false
} }
} }
}; };
const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
function processCallback (methodName, method, returnValue) { function processCallback (methodName, method, returnValue) {
...@@ -874,8 +878,8 @@ function wrapper (methodName, method) { ...@@ -874,8 +878,8 @@ function wrapper (methodName, method) {
} }
} }
return method return method
} }
const todoApis = Object.create(null); const todoApis = Object.create(null);
const TODOS = [ const TODOS = [
...@@ -902,15 +906,15 @@ function createTodoApi (name) { ...@@ -902,15 +906,15 @@ function createTodoApi (name) {
TODOS.forEach(function (name) { TODOS.forEach(function (name) {
todoApis[name] = createTodoApi(name); todoApis[name] = createTodoApi(name);
}); });
var providers = { var providers = {
oauth: ['toutiao'], oauth: ['toutiao'],
share: ['toutiao'], share: ['toutiao'],
payment: ['toutiao'], payment: ['toutiao'],
push: ['toutiao'] push: ['toutiao']
}; };
function getProvider ({ function getProvider ({
service, service,
success, success,
...@@ -932,18 +936,18 @@ function getProvider ({ ...@@ -932,18 +936,18 @@ function getProvider ({
isFn(fail) && fail(res); isFn(fail) && fail(res);
} }
isFn(complete) && complete(res); isFn(complete) && complete(res);
} }
var extraApi = /*#__PURE__*/Object.freeze({ var extraApi = /*#__PURE__*/Object.freeze({
__proto__: null, __proto__: null,
getProvider: getProvider getProvider: getProvider
}); });
const getEmitter = (function () { const getEmitter = (function () {
let Emitter; let Emitter;
return function getUniEmitter () { return function getUniEmitter () {
if (!Emitter) { if (!Emitter) {
Emitter = new Vue(); Emitter = new Vue();
} }
return Emitter return Emitter
} }
...@@ -964,16 +968,16 @@ function $once () { ...@@ -964,16 +968,16 @@ function $once () {
} }
function $emit () { function $emit () {
return apply(getEmitter(), '$emit', [...arguments]) return apply(getEmitter(), '$emit', [...arguments])
} }
var eventApi = /*#__PURE__*/Object.freeze({ var eventApi = /*#__PURE__*/Object.freeze({
__proto__: null, __proto__: null,
$on: $on, $on: $on,
$off: $off, $off: $off,
$once: $once, $once: $once,
$emit: $emit $emit: $emit
}); });
function createMediaQueryObserver () { function createMediaQueryObserver () {
const mediaQueryObserver = {}; const mediaQueryObserver = {};
const { const {
...@@ -1064,13 +1068,13 @@ function createMediaQueryObserver () { ...@@ -1064,13 +1068,13 @@ function createMediaQueryObserver () {
}; };
return mediaQueryObserver return mediaQueryObserver
} }
var api = /*#__PURE__*/Object.freeze({ var api = /*#__PURE__*/Object.freeze({
__proto__: null, __proto__: null,
createMediaQueryObserver: createMediaQueryObserver createMediaQueryObserver: createMediaQueryObserver
}); });
const MPPage = Page; const MPPage = Page;
const MPComponent = Component; const MPComponent = Component;
...@@ -1088,8 +1092,10 @@ function initTriggerEvent (mpInstance) { ...@@ -1088,8 +1092,10 @@ function initTriggerEvent (mpInstance) {
} }
function initHook (name, options, isComponent) { function initHook (name, options, isComponent) {
// fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created {
isComponent && (options = options.lifetimes) // fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created
isComponent && (options = options.lifetimes);
}
const oldHook = options[name]; const oldHook = options[name];
if (!oldHook) { if (!oldHook) {
options[name] = function () { options[name] = function () {
...@@ -1114,8 +1120,8 @@ if (!MPPage.__$wrappered) { ...@@ -1114,8 +1120,8 @@ if (!MPPage.__$wrappered) {
initHook('created', options, true); initHook('created', options, true);
return MPComponent(options) return MPComponent(options)
}; };
} }
const PAGE_EVENT_HOOKS = [ const PAGE_EVENT_HOOKS = [
'onPullDownRefresh', 'onPullDownRefresh',
'onReachBottom', 'onReachBottom',
...@@ -1657,8 +1663,8 @@ function handleEvent (event) { ...@@ -1657,8 +1663,8 @@ function handleEvent (event) {
) { ) {
return ret[0] return ret[0]
} }
} }
const hooks = [ const hooks = [
'onShow', 'onShow',
'onHide', 'onHide',
...@@ -1818,8 +1824,8 @@ function parseBaseApp (vm, { ...@@ -1818,8 +1824,8 @@ function parseBaseApp (vm, {
initHooks(appOptions, hooks); initHooks(appOptions, hooks);
return appOptions return appOptions
} }
function findVmByVueId (vm, vuePid) { function findVmByVueId (vm, vuePid) {
const $children = vm.$children; const $children = vm.$children;
// 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200) // 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200)
...@@ -1860,8 +1866,8 @@ function handleLink (event) { ...@@ -1860,8 +1866,8 @@ function handleLink (event) {
} }
vueOptions.parent = parentVm; vueOptions.parent = parentVm;
} }
const mocks = ['__route__', '__webviewId__', '__nodeid__', '__nodeId__']; const mocks = ['__route__', '__webviewId__', '__nodeid__', '__nodeId__'];
function isPage () { function isPage () {
...@@ -1961,8 +1967,8 @@ function handleLink$1 ({ ...@@ -1961,8 +1967,8 @@ function handleLink$1 ({
vm._isMounted = true; vm._isMounted = true;
vm.__call_hook('mounted'); vm.__call_hook('mounted');
vm.__call_hook('onReady'); vm.__call_hook('onReady');
} }
function parseApp (vm) { function parseApp (vm) {
Vue.prototype._$fallback = true; // 降级(调整原 vue 的部分生命周期,如 created,beforeMount,inject,provide) Vue.prototype._$fallback = true; // 降级(调整原 vue 的部分生命周期,如 created,beforeMount,inject,provide)
...@@ -1989,13 +1995,13 @@ function parseApp (vm) { ...@@ -1989,13 +1995,13 @@ function parseApp (vm) {
mocks, mocks,
initRefs: function () {} // attached 时,可能查询不到 initRefs: function () {} // attached 时,可能查询不到
}) })
} }
function createApp (vm) { function createApp (vm) {
App(parseApp(vm)); App(parseApp(vm));
return vm return vm
} }
const encodeReserveRE = /[!'()*]/g; const encodeReserveRE = /[!'()*]/g;
const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16); const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16);
const commaRE = /%2C/g; const commaRE = /%2C/g;
...@@ -2037,8 +2043,8 @@ function stringifyQuery (obj, encodeStr = encode) { ...@@ -2037,8 +2043,8 @@ function stringifyQuery (obj, encodeStr = encode) {
return encodeStr(key) + '=' + encodeStr(val) return encodeStr(key) + '=' + encodeStr(val)
}).filter(x => x.length > 0).join('&') : null; }).filter(x => x.length > 0).join('&') : null;
return res ? `?${res}` : '' return res ? `?${res}` : ''
} }
function parseBaseComponent (vueComponentOptions, { function parseBaseComponent (vueComponentOptions, {
isPage, isPage,
initRelation initRelation
...@@ -2129,8 +2135,8 @@ function parseBaseComponent (vueComponentOptions, { ...@@ -2129,8 +2135,8 @@ function parseBaseComponent (vueComponentOptions, {
return componentOptions return componentOptions
} }
return [componentOptions, VueComponent] return [componentOptions, VueComponent]
} }
const components = []; const components = [];
function parseComponent (vueOptions) { function parseComponent (vueOptions) {
...@@ -2183,8 +2189,8 @@ function parseComponent (vueOptions) { ...@@ -2183,8 +2189,8 @@ function parseComponent (vueOptions) {
componentOptions.methods.__l = handleLink$1; componentOptions.methods.__l = handleLink$1;
return componentOptions return componentOptions
} }
const hooks$1 = [ const hooks$1 = [
'onShow', 'onShow',
'onHide', 'onHide',
...@@ -2213,8 +2219,8 @@ function parseBasePage (vuePageOptions, { ...@@ -2213,8 +2219,8 @@ function parseBasePage (vuePageOptions, {
}; };
return pageOptions return pageOptions
} }
function parsePage (vuePageOptions) { function parsePage (vuePageOptions) {
const pageOptions = parseBasePage(vuePageOptions, { const pageOptions = parseBasePage(vuePageOptions, {
isPage, isPage,
...@@ -2231,34 +2237,34 @@ function parsePage (vuePageOptions) { ...@@ -2231,34 +2237,34 @@ function parsePage (vuePageOptions) {
} else { } else {
this.is && console.warn(this.is + ' is not ready'); this.is && console.warn(this.is + ' is not ready');
} }
}; };
pageOptions.lifetimes.detached = function detached () { pageOptions.lifetimes.detached = function detached () {
this.$vm && this.$vm.$destroy(); this.$vm && this.$vm.$destroy();
// 清理 // 清理
const webviewId = this.__webviewId__; const webviewId = this.__webviewId__;
webviewId && Object.keys(instances).forEach(key => { webviewId && Object.keys(instances).forEach(key => {
if (key.indexOf(webviewId + '_') === 0) { if (key.indexOf(webviewId + '_') === 0) {
delete instances[key]; delete instances[key];
} }
}); });
}; };
return pageOptions return pageOptions
} }
function createPage (vuePageOptions) { function createPage (vuePageOptions) {
{ {
return Component(parsePage(vuePageOptions)) return Component(parsePage(vuePageOptions))
} }
} }
function createComponent (vueOptions) { function createComponent (vueOptions) {
{ {
return Component(parseComponent(vueOptions)) return Component(parseComponent(vueOptions))
} }
} }
function createSubpackageApp (vm) { function createSubpackageApp (vm) {
const appOptions = parseApp(vm); const appOptions = parseApp(vm);
const app = getApp({ const app = getApp({
...@@ -2293,8 +2299,8 @@ function createSubpackageApp (vm) { ...@@ -2293,8 +2299,8 @@ function createSubpackageApp (vm) {
vm.__call_hook('onLaunch', args); vm.__call_hook('onLaunch', args);
} }
return vm return vm
} }
function createPlugin (vm) { function createPlugin (vm) {
const appOptions = parseApp(vm); const appOptions = parseApp(vm);
if (isFn(appOptions.onShow) && tt.onAppShow) { if (isFn(appOptions.onShow) && tt.onAppShow) {
...@@ -2312,8 +2318,8 @@ function createPlugin (vm) { ...@@ -2312,8 +2318,8 @@ function createPlugin (vm) {
appOptions.onLaunch.call(vm, args); appOptions.onLaunch.call(vm, args);
} }
return vm return vm
} }
todos.forEach(todoApi => { todos.forEach(todoApi => {
protocols[todoApi] = false; protocols[todoApi] = false;
}); });
...@@ -2396,7 +2402,7 @@ tt.createComponent = createComponent; ...@@ -2396,7 +2402,7 @@ tt.createComponent = createComponent;
tt.createSubpackageApp = createSubpackageApp; tt.createSubpackageApp = createSubpackageApp;
tt.createPlugin = createPlugin; tt.createPlugin = createPlugin;
var uni$1 = uni; var uni$1 = uni;
export default uni$1; export default uni$1;
export { createApp, createComponent, createPage, createPlugin, createSubpackageApp }; export { createApp, createComponent, createPage, createPlugin, createSubpackageApp };
...@@ -24,7 +24,11 @@ function initTriggerEvent (mpInstance) { ...@@ -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] const oldHook = options[name]
if (!oldHook) { if (!oldHook) {
options[name] = function () { options[name] = function () {
...@@ -46,7 +50,7 @@ if (!MPPage.__$wrappered) { ...@@ -46,7 +50,7 @@ if (!MPPage.__$wrappered) {
Page.after = MPPage.after Page.after = MPPage.after
Component = function (options = {}) { Component = function (options = {}) {
initHook('created', options) initHook('created', options, true)
return MPComponent(options) return MPComponent(options)
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册