diff --git a/packages/uni-cli-shared/src/vue/utils.ts b/packages/uni-cli-shared/src/vue/utils.ts
index 37842feb7cf4f4bfe868d387e2d648fef8b00f3a..c814594e62266b05d8a8dcf74c1144b60ba9f68d 100644
--- a/packages/uni-cli-shared/src/vue/utils.ts
+++ b/packages/uni-cli-shared/src/vue/utils.ts
@@ -2,6 +2,8 @@ import { isComponentTag } from '@dcloudio/uni-shared'
import {
AttributeNode,
ComponentNode,
+ createSimpleExpression,
+ DirectiveNode,
ElementNode,
ElementTypes,
isCoreComponent,
@@ -66,3 +68,26 @@ export function addStaticClass(node: ElementNode, clazz: string) {
content: clazz,
}
}
+
+function createDirectiveNode(
+ name: string,
+ arg: string,
+ exp: string
+): DirectiveNode {
+ return {
+ type: NodeTypes.DIRECTIVE,
+ name,
+ modifiers: [],
+ loc: locStub,
+ arg: createSimpleExpression(arg, true),
+ exp: createSimpleExpression(exp, false),
+ }
+}
+
+export function createOnDirectiveNode(name: string, value: string) {
+ return createDirectiveNode('on', name, value)
+}
+
+export function createBindDirectiveNode(name: string, value: string) {
+ return createDirectiveNode('bind', name, value)
+}
diff --git a/packages/uni-mp-alipay/__tests__/vOn.spec.ts b/packages/uni-mp-alipay/__tests__/vOn.spec.ts
index cc2b50427d901865224616c305bbe53938e95f52..8227e2edb87e6cdc12aa0ff1edb385b59063c073 100644
--- a/packages/uni-mp-alipay/__tests__/vOn.spec.ts
+++ b/packages/uni-mp-alipay/__tests__/vOn.spec.ts
@@ -1,6 +1,15 @@
import { assert } from './testUtils'
describe('mp-alipay: transform v-on', () => {
+ test('getphonenumber', () => {
+ assert(
+ ``,
+ ``,
+ `(_ctx, _cache) => {
+ return { a: _o($event => _ctx.$onAliGetAuthorize('getPhoneNumber', $event)), b: _o($event => _ctx.$onAliAuthError('getPhoneNumber', $event)) }
+}`
+ )
+ })
test('basic', () => {
assert(
``,
diff --git a/packages/uni-mp-alipay/dist/uni.compiler.js b/packages/uni-mp-alipay/dist/uni.compiler.js
index b8225477c405ea18328293a2613ed7203de19af9..c7c08308b1b65f3b19dec9cc24ad43d69c4c7d10 100644
--- a/packages/uni-mp-alipay/dist/uni.compiler.js
+++ b/packages/uni-mp-alipay/dist/uni.compiler.js
@@ -87,6 +87,43 @@ const eventMap = {
animationfinish: 'animationEnd',
};
+function transformOpenType(node) {
+ var _a;
+ if (node.type !== 1 /* ELEMENT */ || node.tag !== 'button') {
+ return;
+ }
+ const openTypeProp = compilerCore.findProp(node, 'open-type');
+ if (!openTypeProp) {
+ return;
+ }
+ if (openTypeProp.type !== 6 /* ATTRIBUTE */ ||
+ ((_a = openTypeProp.value) === null || _a === void 0 ? void 0 : _a.content) !== 'getPhoneNumber') {
+ return;
+ }
+ openTypeProp.value.content = 'getAuthorize';
+ const { props } = node;
+ props.splice(props.indexOf(openTypeProp) + 1, 0, uniCliShared.createAttributeNode('scope', 'phoneNumber'));
+ let getPhoneNumberMethodName = '';
+ const getPhoneNumberPropIndex = props.findIndex((prop) => {
+ if (prop.type === 7 /* DIRECTIVE */ && prop.name === 'on') {
+ const { arg, exp } = prop;
+ if ((arg === null || arg === void 0 ? void 0 : arg.type) === 4 /* SIMPLE_EXPRESSION */ &&
+ (exp === null || exp === void 0 ? void 0 : exp.type) === 4 /* SIMPLE_EXPRESSION */ &&
+ arg.isStatic &&
+ arg.content === 'getphonenumber') {
+ getPhoneNumberMethodName = exp.content;
+ return true;
+ }
+ }
+ });
+ if (!getPhoneNumberMethodName) {
+ return;
+ }
+ props.splice(getPhoneNumberPropIndex, 1);
+ props.push(uniCliShared.createOnDirectiveNode('getAuthorize', `$onAliGetAuthorize('${getPhoneNumberMethodName}',$event)`));
+ props.push(uniCliShared.createOnDirectiveNode('error', `$onAliAuthError('${getPhoneNumberMethodName}',$event)`));
+}
+
const projectConfigFilename = 'mini.project.json';
const miniProgram = {
event,
@@ -103,6 +140,7 @@ const miniProgram = {
// TODO getPhoneNumber 等事件
const nodeTransforms = [
transformRef,
+ transformOpenType,
uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_ON_LINK, 6 /* ATTRIBUTE */),
];
const tags = [
diff --git a/packages/uni-mp-alipay/src/compiler/options.ts b/packages/uni-mp-alipay/src/compiler/options.ts
index 49453c600810c244641b7f3fb0dfcc008140a14c..ec1246d5101b84fd37409d3da0d8b6d0abe32545 100644
--- a/packages/uni-mp-alipay/src/compiler/options.ts
+++ b/packages/uni-mp-alipay/src/compiler/options.ts
@@ -9,6 +9,7 @@ import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite'
import source from './mini.project.json'
import { transformRef } from './transforms/transformRef'
import { event } from './event'
+import { transformOpenType } from './transforms/transformOpenType'
const projectConfigFilename = 'mini.project.json'
@@ -27,6 +28,7 @@ export const miniProgram: MiniProgramCompilerOptions = {
// TODO getPhoneNumber 等事件
export const nodeTransforms = [
transformRef,
+ transformOpenType,
createTransformComponentLink(COMPONENT_ON_LINK, NodeTypes.ATTRIBUTE),
]
export const tags = [
diff --git a/packages/uni-mp-alipay/src/compiler/transforms/transformOpenType.ts b/packages/uni-mp-alipay/src/compiler/transforms/transformOpenType.ts
new file mode 100644
index 0000000000000000000000000000000000000000..44b31ad78b99118233417acd4bc517146af0af71
--- /dev/null
+++ b/packages/uni-mp-alipay/src/compiler/transforms/transformOpenType.ts
@@ -0,0 +1,64 @@
+import {
+ findProp,
+ NodeTypes,
+ RootNode,
+ TemplateChildNode,
+} from '@vue/compiler-core'
+import {
+ createAttributeNode,
+ createOnDirectiveNode,
+} from '@dcloudio/uni-cli-shared'
+
+export function transformOpenType(node: RootNode | TemplateChildNode) {
+ if (node.type !== NodeTypes.ELEMENT || node.tag !== 'button') {
+ return
+ }
+ const openTypeProp = findProp(node, 'open-type')
+ if (!openTypeProp) {
+ return
+ }
+ if (
+ openTypeProp.type !== NodeTypes.ATTRIBUTE ||
+ openTypeProp.value?.content !== 'getPhoneNumber'
+ ) {
+ return
+ }
+ openTypeProp.value.content = 'getAuthorize'
+ const { props } = node
+ props.splice(
+ props.indexOf(openTypeProp) + 1,
+ 0,
+ createAttributeNode('scope', 'phoneNumber')
+ )
+ let getPhoneNumberMethodName = ''
+ const getPhoneNumberPropIndex = props.findIndex((prop) => {
+ if (prop.type === NodeTypes.DIRECTIVE && prop.name === 'on') {
+ const { arg, exp } = prop
+ if (
+ arg?.type === NodeTypes.SIMPLE_EXPRESSION &&
+ exp?.type === NodeTypes.SIMPLE_EXPRESSION &&
+ arg.isStatic &&
+ arg.content === 'getphonenumber'
+ ) {
+ getPhoneNumberMethodName = exp.content
+ return true
+ }
+ }
+ })
+ if (!getPhoneNumberMethodName) {
+ return
+ }
+ props.splice(getPhoneNumberPropIndex, 1)
+ props.push(
+ createOnDirectiveNode(
+ 'getAuthorize',
+ `$onAliGetAuthorize('${getPhoneNumberMethodName}',$event)`
+ )
+ )
+ props.push(
+ createOnDirectiveNode(
+ 'error',
+ `$onAliAuthError('${getPhoneNumberMethodName}',$event)`
+ )
+ )
+}
diff --git a/packages/uni-mp-compiler/src/ast.ts b/packages/uni-mp-compiler/src/ast.ts
index 9eed6fa0ff40de4198528550f08fc0cf1287d484..8ed984648af2d6f32da0ea716c3c7b3c50b87e68 100644
--- a/packages/uni-mp-compiler/src/ast.ts
+++ b/packages/uni-mp-compiler/src/ast.ts
@@ -27,12 +27,8 @@ import {
} from '@babel/types'
import {
createCompilerError,
- createSimpleExpression,
- DirectiveNode,
ErrorCodes,
ExpressionNode,
- locStub,
- NodeTypes,
} from '@vue/compiler-core'
import { CodegenScope, CodegenVIfScope } from './options'
import { TransformContext } from './transform'
@@ -150,26 +146,3 @@ export function parseStringLiteral(
}
return stringLiteral('')
}
-
-function createDirectiveNode(
- name: string,
- arg: string,
- exp: string
-): DirectiveNode {
- return {
- type: NodeTypes.DIRECTIVE,
- name,
- modifiers: [],
- loc: locStub,
- arg: createSimpleExpression(arg, true),
- exp: createSimpleExpression(exp, false),
- }
-}
-
-export function createOnDirectiveNode(name: string, value: string) {
- return createDirectiveNode('on', name, value)
-}
-
-export function createBindDirectiveNode(name: string, value: string) {
- return createDirectiveNode('bind', name, value)
-}
diff --git a/packages/uni-mp-compiler/src/transforms/transformComponent.ts b/packages/uni-mp-compiler/src/transforms/transformComponent.ts
index 645c1a1990f85575ddb2220632e71821cca02013..3f964f13c10479c38f4cdc17c7061084ea0995b6 100644
--- a/packages/uni-mp-compiler/src/transforms/transformComponent.ts
+++ b/packages/uni-mp-compiler/src/transforms/transformComponent.ts
@@ -1,7 +1,10 @@
import { ComponentNode } from '@vue/compiler-core'
-import { createAttributeNode, isUserComponent } from '@dcloudio/uni-cli-shared'
+import {
+ createAttributeNode,
+ createBindDirectiveNode,
+ isUserComponent,
+} from '@dcloudio/uni-cli-shared'
import { isVForScope, NodeTransform, TransformContext } from '../transform'
-import { createBindDirectiveNode } from '../ast'
import { ATTR_VUE_ID } from './utils'
export const transformComponent: NodeTransform = (node, context) => {
diff --git a/packages/uni-mp-compiler/src/transforms/vModel.ts b/packages/uni-mp-compiler/src/transforms/vModel.ts
index ecb39c17ee7caa7eb4a6b9e2fbf19ad8b5d34a11..d6420170463e8aae3a616e42130028884fb9f528 100644
--- a/packages/uni-mp-compiler/src/transforms/vModel.ts
+++ b/packages/uni-mp-compiler/src/transforms/vModel.ts
@@ -1,3 +1,4 @@
+import { camelize } from '@vue/shared'
import {
Property,
transformModel as baseTransform,
@@ -13,9 +14,12 @@ import {
TransformContext as VueTransformContext,
} from '@vue/compiler-core'
import { DOMErrorCodes, createDOMCompilerError } from '@vue/compiler-dom'
-import { camelize } from '@vue/shared'
-import { V_ON } from '..'
-import { createBindDirectiveNode, createOnDirectiveNode } from '../ast'
+
+import {
+ createBindDirectiveNode,
+ createOnDirectiveNode,
+} from '@dcloudio/uni-cli-shared'
+import { V_ON } from '../runtimeHelpers'
import { genExpr } from '../codegen'
import { TransformContext } from '../transform'
import { DirectiveTransformResult } from './transformElement'
diff --git a/packages/uni-mp-compiler/src/transforms/vSlot.ts b/packages/uni-mp-compiler/src/transforms/vSlot.ts
index 9d80b6de59e70b784c3c3243f83689e7b5f8cd58..0ea125ea00613fc5ce143f601aec754b323f8f13 100644
--- a/packages/uni-mp-compiler/src/transforms/vSlot.ts
+++ b/packages/uni-mp-compiler/src/transforms/vSlot.ts
@@ -25,9 +25,12 @@ import {
TemplateChildNode,
TemplateNode,
} from '@vue/compiler-core'
-import { isUserComponent } from '@dcloudio/uni-cli-shared'
+import {
+ createBindDirectiveNode,
+ isUserComponent,
+} from '@dcloudio/uni-cli-shared'
import { WITH_SCOPED_SLOT } from '../runtimeHelpers'
-import { createBindDirectiveNode, parseExpr } from '../ast'
+import { parseExpr } from '../ast'
import { genExpr } from '../codegen'
import { CodegenScope, CodegenVForScope } from '../options'
import { isVForScope, NodeTransform, TransformContext } from '../transform'