From 4e6071cd832b54bcbd393cd16b8b83c7cdaa1781 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 3 Apr 2019 20:56:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(runtime):=20mp=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-mp-weixin/dist/index.js | 88 +++++++++++++++++++++++++++- packages/uni-mp-weixin/package.json | 2 +- src/core/runtime/wrapper/util.js | 88 +++++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 7 deletions(-) diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index c1836bc36..82f12fa57 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -385,13 +385,84 @@ function wrapper$1 (event) { return event } -function processEventArgs (event, args = [], isCustom, methodName) { +function getExtraValue (vm, dataPathsArray) { + let context = vm; + dataPathsArray.forEach(dataPathArray => { + const dataPath = dataPathArray[0]; + const value = dataPathArray[2]; + if (dataPath || typeof value !== 'undefined') { // ['','',index,'disable'] + const propPath = dataPathArray[1]; + const valuePath = dataPathArray[3]; + + const vFor = dataPath ? vm.__get_value(dataPath, context) : context; + + if (Number.isInteger(vFor)) { + context = value; + } else if (!propPath) { + context = vFor[value]; + } else { + if (Array.isArray(vFor)) { + context = vFor.find(vForItem => { + return vm.__get_value(propPath, vForItem) === value + }); + } else if (isPlainObject(vFor)) { + context = Object.keys(vFor).find(vForKey => { + return vm.__get_value(propPath, vFor[vForKey]) === value + }); + } else { + console.error('v-for 暂不支持循环数据:', vFor); + } + } + + if (valuePath) { + context = vm.__get_value(valuePath, context); + } + } + }); + return context +} + +function processEventExtra (vm, extra) { + const extraObj = {}; + + if (Array.isArray(extra) && extra.length) { + /** + *[ + * ['data.items', 'data.id', item.data.id], + * ['metas', 'id', meta.id] + *], + *[ + * ['data.items', 'data.id', item.data.id], + * ['metas', 'id', meta.id] + *], + *'test' + */ + extra.forEach((dataPath, index) => { + if (typeof dataPath === 'string') { + if (!dataPath) { // model,prop.sync + extraObj['$' + index] = vm; + } else { + extraObj['$' + index] = vm.__get_value(dataPath); + } + } else { + extraObj['$' + index] = getExtraValue(vm, dataPath); + } + }); + } + + return extraObj +} + +function processEventArgs (vm, event, args = [], extra = [], isCustom, methodName) { if (isCustom && !args.length) { // 无参数,直接传入 detail 数组 if (!Array.isArray(event.detail)) { // 应该是使用了 wxcomponent 原生组件,为了向前兼容,传递原始 event 对象 return [event] } return event.detail } + + const extraObj = processEventExtra(vm, extra); + const ret = []; args.forEach(arg => { if (arg === '$event') { @@ -401,7 +472,11 @@ function processEventArgs (event, args = [], isCustom, methodName) { ret.push(isCustom ? event.detail[0] : event); } } else { - ret.push(arg); + if (typeof arg === 'string' && hasOwn(extraObj, arg)) { + ret.push(extraObj[arg]); + } else { + ret.push(arg); + } } }); @@ -444,7 +519,14 @@ function handleEvent (event) { } handler.once = true; } - handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom, methodName)); + handler.apply(this.$vm, processEventArgs( + this.$vm, + event, + eventArray[1], + eventArray[2], + isCustom, + methodName + )); }); } }); diff --git a/packages/uni-mp-weixin/package.json b/packages/uni-mp-weixin/package.json index 6d7970e23..f8973c987 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.923", + "version": "0.0.924", "description": "uni-app mp-weixin", "main": "dist/index.js", "scripts": { diff --git a/src/core/runtime/wrapper/util.js b/src/core/runtime/wrapper/util.js index 0a6e1d1ec..2da8dd97b 100644 --- a/src/core/runtime/wrapper/util.js +++ b/src/core/runtime/wrapper/util.js @@ -128,13 +128,84 @@ function wrapper (event) { return event } -function processEventArgs (event, args = [], isCustom, methodName) { +function getExtraValue (vm, dataPathsArray) { + let context = vm + dataPathsArray.forEach(dataPathArray => { + const dataPath = dataPathArray[0] + const value = dataPathArray[2] + if (dataPath || typeof value !== 'undefined') { // ['','',index,'disable'] + const propPath = dataPathArray[1] + const valuePath = dataPathArray[3] + + const vFor = dataPath ? vm.__get_value(dataPath, context) : context + + if (Number.isInteger(vFor)) { + context = value + } else if (!propPath) { + context = vFor[value] + } else { + if (Array.isArray(vFor)) { + context = vFor.find(vForItem => { + return vm.__get_value(propPath, vForItem) === value + }) + } else if (isPlainObject(vFor)) { + context = Object.keys(vFor).find(vForKey => { + return vm.__get_value(propPath, vFor[vForKey]) === value + }) + } else { + console.error('v-for 暂不支持循环数据:', vFor) + } + } + + if (valuePath) { + context = vm.__get_value(valuePath, context) + } + } + }) + return context +} + +function processEventExtra (vm, extra) { + const extraObj = {} + + if (Array.isArray(extra) && extra.length) { + /** + *[ + * ['data.items', 'data.id', item.data.id], + * ['metas', 'id', meta.id] + *], + *[ + * ['data.items', 'data.id', item.data.id], + * ['metas', 'id', meta.id] + *], + *'test' + */ + extra.forEach((dataPath, index) => { + if (typeof dataPath === 'string') { + if (!dataPath) { // model,prop.sync + extraObj['$' + index] = vm + } else { + extraObj['$' + index] = vm.__get_value(dataPath) + } + } else { + extraObj['$' + index] = getExtraValue(vm, dataPath) + } + }) + } + + return extraObj +} + +function processEventArgs (vm, event, args = [], extra = [], isCustom, methodName) { if (isCustom && !args.length) { // 无参数,直接传入 detail 数组 if (!Array.isArray(event.detail)) { // 应该是使用了 wxcomponent 原生组件,为了向前兼容,传递原始 event 对象 return [event] } return event.detail } + + const extraObj = processEventExtra(vm, extra) + const ret = [] args.forEach(arg => { if (arg === '$event') { @@ -144,7 +215,11 @@ function processEventArgs (event, args = [], isCustom, methodName) { ret.push(isCustom ? event.detail[0] : event) } } else { - ret.push(arg) + if (typeof arg === 'string' && hasOwn(extraObj, arg)) { + ret.push(extraObj[arg]) + } else { + ret.push(arg) + } } }) @@ -187,7 +262,14 @@ export function handleEvent (event) { } handler.once = true } - handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom, methodName)) + handler.apply(this.$vm, processEventArgs( + this.$vm, + event, + eventArray[1], + eventArray[2], + isCustom, + methodName + )) }) } }) -- GitLab