diff --git a/package.json b/package.json index 951742e6435b2046bd2546aec885d592a515b2ba..0e017e0ff76bc1a3ffb359e0e795924e42f03869 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,8 @@ "globals": { "App": true, "Page": true, - "Component": true, + "Component": true, + "Behavior":true, "getApp": true, "getCurrentPages": true, "plus": true, diff --git a/packages/uni-app-plus/dist/index.js b/packages/uni-app-plus/dist/index.js index 4758dae3d3b8f05b9ad5883c77950c87e82c04a5..5308b4af101ec0993f4890e7d1921fd7ea5d39be 100644 --- a/packages/uni-app-plus/dist/index.js +++ b/packages/uni-app-plus/dist/index.js @@ -19,7 +19,26 @@ function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } -function noop () {} +function noop () {} + +/** + * Create a cached version of a pure function. + */ +function cached (fn) { + const cache = Object.create(null); + return function cachedFn (str) { + const hit = cache[str]; + return hit || (cache[str] = fn(str)) + } +} + +/** + * Camelize a hyphen-delimited string. + */ +const camelizeRE = /-(\w)/g; +const camelize = cached((str) => { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +}); const SYNC_API_RE = /requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/; @@ -241,6 +260,49 @@ var api = /*#__PURE__*/Object.freeze({ requireNativePlugin: requireNativePlugin }); +const WXPage = Page; +const WXComponent = Component; + +const customizeRE = /:/g; + +const customize = cached((str) => { + return camelize(str.replace(customizeRE, '-')) +}); + +function initTriggerEvent (mpInstance) { + const oldTriggerEvent = mpInstance.triggerEvent; + mpInstance.triggerEvent = function (event, ...args) { + return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]) + }; +} + +Page = function (options = {}) { + const name = 'onLoad'; + const oldHook = options[name]; + if (!oldHook) { + options[name] = function () { + initTriggerEvent(this); + }; + } else { + options[name] = function (...args) { + initTriggerEvent(this); + return oldHook.apply(this, args) + }; + } + return WXPage(options) +}; + +const behavior = Behavior({ + created () { + initTriggerEvent(this); + } +}); + +Component = function (options = {}) { + (options.behaviors || (options.behaviors = [])).unshift(behavior); + return WXComponent(options) +}; + const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']; function initMocks (vm) { @@ -280,7 +342,7 @@ function getData (vueOptions, context) { } Object.keys(methods).forEach(methodName => { - if (!hasOwn(data, methodName)) { + if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) { data[methodName] = methods[methodName]; } }); @@ -534,7 +596,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onUniNViewMessage' ]; function createApp (vm) { @@ -564,7 +627,7 @@ function createApp (vm) { }); const appOptions = { - onLaunch (args) { + onLaunch (args) { this.$vm = vm; diff --git a/packages/uni-app-plus/package.json b/packages/uni-app-plus/package.json index 60cbe2eeb48a2b408d1f98427e25e24e9a9a701d..c25480d547e0e8b50507819b759b97b2a65a1f38 100644 --- a/packages/uni-app-plus/package.json +++ b/packages/uni-app-plus/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-app-plus", - "version": "0.0.207", + "version": "0.0.209", "description": "uni-app app-plus", "main": "dist/index.js", "scripts": { diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index e50cc4863c9825211ca7e6699e91ea8bc654a062..ce08e55dac0a52de7a04387dfebedf86302c43ae 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -19,7 +19,26 @@ function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } -function noop () {} +function noop () {} + +/** + * Create a cached version of a pure function. + */ +function cached (fn) { + const cache = Object.create(null); + return function cachedFn (str) { + const hit = cache[str]; + return hit || (cache[str] = fn(str)) + } +} + +/** + * Camelize a hyphen-delimited string. + */ +const camelizeRE = /-(\w)/g; +const camelize = cached((str) => { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +}); const SYNC_API_RE = /requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/; @@ -268,6 +287,49 @@ var api = /*#__PURE__*/Object.freeze({ }); +const WXPage = Page; +const WXComponent = Component; + +const customizeRE = /:/g; + +const customize = cached((str) => { + return camelize(str.replace(customizeRE, '-')) +}); + +function initTriggerEvent (mpInstance) { + const oldTriggerEvent = mpInstance.triggerEvent; + mpInstance.triggerEvent = function (event, ...args) { + return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]) + }; +} + +Page = function (options = {}) { + const name = 'onLoad'; + const oldHook = options[name]; + if (!oldHook) { + options[name] = function () { + initTriggerEvent(this); + }; + } else { + options[name] = function (...args) { + initTriggerEvent(this); + return oldHook.apply(this, args) + }; + } + return WXPage(options) +}; + +const behavior = Behavior({ + created () { + initTriggerEvent(this); + } +}); + +Component = function (options = {}) { + (options.behaviors || (options.behaviors = [])).unshift(behavior); + return WXComponent(options) +}; + const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']; function initMocks (vm) { @@ -307,7 +369,7 @@ function getData (vueOptions, context) { } Object.keys(methods).forEach(methodName => { - if (!hasOwn(data, methodName)) { + if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) { data[methodName] = methods[methodName]; } }); @@ -561,7 +623,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onUniNViewMessage' ]; function createApp (vm) { @@ -591,7 +654,7 @@ function createApp (vm) { }); const appOptions = { - onLaunch (args) { + onLaunch (args) { { if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上'); diff --git a/packages/uni-mp-weixin/package.json b/packages/uni-mp-weixin/package.json index 4b3c7a12a8faafadeda4c80a03c41d4d19f16701..e92913a1b1b2ccb3fc77f896a37a7d2c5f9c1808 100644 --- a/packages/uni-mp-weixin/package.json +++ b/packages/uni-mp-weixin/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-mp-weixin", - "version": "0.0.927", + "version": "0.0.928", "description": "uni-app mp-weixin", "main": "dist/index.js", "scripts": { diff --git a/src/core/runtime/wrapper/create-app.js b/src/core/runtime/wrapper/create-app.js index f81792ea309652a2668503fa79563c5259203028..d3a8a21ce25bf1fcbdb145b2c831eec8e08f4726 100644 --- a/src/core/runtime/wrapper/create-app.js +++ b/src/core/runtime/wrapper/create-app.js @@ -1,3 +1,5 @@ +import 'uni-platform/runtime/index' + import Vue from 'vue' import { @@ -10,7 +12,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onUniNViewMessage' ] export function createApp (vm) { @@ -40,7 +43,7 @@ export function createApp (vm) { }) const appOptions = { - onLaunch (args) { + onLaunch (args) { if (__PLATFORM__ === 'mp-weixin') { if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上') diff --git a/src/core/runtime/wrapper/util.js b/src/core/runtime/wrapper/util.js index ccdc61dc93c2bb99f70bdb35f24f27d37ea10913..5004db307932998edd65b0e131b607d57d91cb3f 100644 --- a/src/core/runtime/wrapper/util.js +++ b/src/core/runtime/wrapper/util.js @@ -44,7 +44,7 @@ export function getData (vueOptions, context) { } Object.keys(methods).forEach(methodName => { - if (!hasOwn(data, methodName)) { + if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) { data[methodName] = methods[methodName] } }) diff --git a/src/platforms/app-plus/runtime/index.js b/src/platforms/app-plus/runtime/index.js new file mode 100644 index 0000000000000000000000000000000000000000..5a843bc821374df4c1959470c611958c8b82d732 --- /dev/null +++ b/src/platforms/app-plus/runtime/index.js @@ -0,0 +1 @@ +import '../../mp-weixin/runtime/index' diff --git a/src/platforms/mp-alipay/runtime/index.js b/src/platforms/mp-alipay/runtime/index.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/platforms/mp-baidu/runtime/index.js b/src/platforms/mp-baidu/runtime/index.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/platforms/mp-toutiao/runtime/index.js b/src/platforms/mp-toutiao/runtime/index.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/platforms/mp-weixin/runtime/index.js b/src/platforms/mp-weixin/runtime/index.js new file mode 100644 index 0000000000000000000000000000000000000000..f722360df0f3fa97818d50280eebe4becd359979 --- /dev/null +++ b/src/platforms/mp-weixin/runtime/index.js @@ -0,0 +1,47 @@ +import { + cached, + camelize +} from 'uni-shared' + +const WXPage = Page +const WXComponent = Component + +const customizeRE = /:/g + +const customize = cached((str) => { + return camelize(str.replace(customizeRE, '-')) +}) + +function initTriggerEvent (mpInstance) { + const oldTriggerEvent = mpInstance.triggerEvent + mpInstance.triggerEvent = function (event, ...args) { + return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]) + } +} + +Page = function (options = {}) { + const name = 'onLoad' + const oldHook = options[name] + if (!oldHook) { + options[name] = function () { + initTriggerEvent(this) + } + } else { + options[name] = function (...args) { + initTriggerEvent(this) + return oldHook.apply(this, args) + } + } + return WXPage(options) +} + +const behavior = Behavior({ + created () { + initTriggerEvent(this) + } +}) + +Component = function (options = {}) { + (options.behaviors || (options.behaviors = [])).unshift(behavior) + return WXComponent(options) +} diff --git a/src/shared/util.js b/src/shared/util.js index 78f25f81b74300838fc64d98b804fe6fb7a05aac..048f8e3b7df519eda30aed49a45b036f81671e5b 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -27,6 +27,25 @@ export function toRawType (val) { return _toString.call(val).slice(8, -1) } +/** + * Create a cached version of a pure function. + */ +export function cached (fn) { + const cache = Object.create(null) + return function cachedFn (str) { + const hit = cache[str] + return hit || (cache[str] = fn(str)) + } +} + +/** + * Camelize a hyphen-delimited string. + */ +const camelizeRE = /-(\w)/g +export const camelize = cached((str) => { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +}) + export function setProperties (item, props, propsData) { props.forEach(function (name) { if (hasOwn(propsData, name)) { @@ -49,7 +68,7 @@ export function formatDateTime ({ } else { return date.getFullYear() + '-' + _completeValue(date.getMonth() + 1) + '-' + _completeValue(date.getDate()) } -} +} export function updateElementStyle (element, styles) { for (let attrName in styles) {