diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js index f37ee8127494cf2a95c564ef7fbd6ad56b0d4f5e..43bc3a950ac0d77997062a32ccb1aa4cbf628181 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js @@ -1,14 +1,14 @@ const compiler = require('../lib') -function assertCodegen (template, templateCode, renderCode = 'with(this){}', options = {}) { - const res = compiler.compile(template, { +function assertCodegen (template, templateCode, renderCode = 'with(this){}', mpOptions = {}, baseOptions = {}) { + const res = compiler.compile(template, Object.assign({ resourcePath: 'test.wxml', mp: Object.assign({ minified: true, isTest: true, platform: 'mp-alipay' - }, options) - }) + }, mpOptions) + }, baseOptions)) expect(res.template).toBe(templateCode) expect(res.render).toBe(renderCode) @@ -45,6 +45,15 @@ describe('mp:compiler-mp-alipay', () => { '', '' ) + assertCodegen( + 'text', + 'text', + undefined, + undefined, + { + wxComponents: { component1: '/mycomponents/component1' } + } + ) }) it('generate default slot', () => { assertCodegen( diff --git a/packages/uni-template-compiler/lib/script/traverse/data/index.js b/packages/uni-template-compiler/lib/script/traverse/data/index.js index 2999c6ba7ce7205d7a2be750a8a5903bb6fa50e5..f1936216860004d9e9cf11849b5e78ccd29e8baa 100644 --- a/packages/uni-template-compiler/lib/script/traverse/data/index.js +++ b/packages/uni-template-compiler/lib/script/traverse/data/index.js @@ -44,6 +44,14 @@ module.exports = function traverseData (path, state, tagName) { t.stringLiteral('wx') ) ) + if (state.options.platform.name === 'mp-alipay') { + addAttrProperties.push( + t.objectProperty( + t.stringLiteral('ref'), + t.stringLiteral('__r') + ) + ) + } } if (addAttrProperties.length) { @@ -56,4 +64,4 @@ module.exports = function traverseData (path, state, tagName) { ) } } -} +} diff --git a/src/platforms/mp-alipay/runtime/wrapper/util.js b/src/platforms/mp-alipay/runtime/wrapper/util.js index ef741753af74147df2349f6337d214791c5be885..01e9a52f6cd44c3496540f0a6ef091b5c42b30ca 100644 --- a/src/platforms/mp-alipay/runtime/wrapper/util.js +++ b/src/platforms/mp-alipay/runtime/wrapper/util.js @@ -112,6 +112,36 @@ export function handleRef (ref) { if (!ref) { return } + if (ref.props['data-com-type'] === 'wx') { + const eventProps = {} + let refProps = ref.props + // 初始化支付宝小程序组件事件 + Object.keys(refProps).forEach(key => { + const handler = refProps[key] + const res = key.match(/^on([A-Z])(\S*)/) + if (res && typeof handler === 'function' && handler.name === 'bound handleEvent') { + const event = res && (res[1].toLowerCase() + res[2]) + refProps[key] = eventProps[key] = function () { + const props = Object.assign({}, refProps) + props[key] = handler + // 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式 + delete props['data-com-type'] + triggerEvent.bind({ props })(event, { + __args__: [...arguments] + }) + } + } + }) + // 处理 props 重写 + Object.defineProperty(ref, 'props', { + get () { + return refProps + }, + set (value) { + refProps = Object.assign(value, eventProps) + } + }) + } const refName = ref.props['data-ref'] const refInForName = ref.props['data-ref-in-for'] if (refName) { @@ -129,11 +159,13 @@ export function triggerEvent (type, detail, options) { const eventOpts = this.props['data-event-opts'] const eventParams = this.props['data-event-params'] + const comType = this.props['data-com-type'] const target = { dataset: { eventOpts, - eventParams + eventParams, + comType } }