Skip to content

Commit 0bc00c8

Browse files
committed
feat(User): support unionId for weapp
- loginWithWeapp add options.preferUnionId - add [login|associate]WithWeappWithUnionId
1 parent 5ac79e6 commit 0bc00c8

File tree

2 files changed

+132
-16
lines changed

2 files changed

+132
-16
lines changed

src/user.js

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ const getWeappLoginCode = () => {
2525
});
2626
};
2727

28+
const getWeappAuthData = (
29+
code,
30+
{ preferUnionId, unionIdPlatform = 'weixin', asMainAccount = true } = {}
31+
) =>
32+
preferUnionId
33+
? {
34+
platform: unionIdPlatform,
35+
main_account: asMainAccount,
36+
code,
37+
}
38+
: { code };
39+
2840
const mergeUnionDataIntoAuthData = (
2941
authData,
3042
unionId,
@@ -237,23 +249,59 @@ module.exports = function(AV) {
237249
authData,
238250
platform,
239251
unionId,
240-
unionLoginOptions
252+
unionOptions
241253
) {
242254
return this._linkWith(
243255
platform,
244-
mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions)
256+
mergeUnionDataIntoAuthData(authData, unionId, unionOptions)
257+
);
258+
},
259+
260+
/**
261+
* 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。
262+
* 仅在小程序中可用。
263+
*
264+
* @since 3.13.0
265+
* @param {Object} [options]
266+
* @param {boolean} [options.preferUnionId] 当用户满足 {@link https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html 获取 UnionId 的条件} 时,是否将 UnionId 保存在用户账号中。
267+
* @param {string} [options.unionIdPlatform = 'weixin'] (only take effect when preferUnionId) unionId platform
268+
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
269+
* @return {Promise<AV.User>}
270+
*/
271+
associateWithWeapp(options) {
272+
return getWeappLoginCode().then(code =>
273+
this._linkWith(PLATFORM_WEAPP, getWeappAuthData(code, options))
274+
);
275+
},
276+
277+
/**
278+
* @deprecated renamed to {@link AV.User#associateWithWeapp}
279+
* @return {Promise<AV.User>}
280+
*/
281+
linkWithWeapp(options) {
282+
console.warn(
283+
'DEPRECATED: User#linkWithWeapp 已废弃,请使用 User#associateWithWeapp 代替'
245284
);
285+
return this.associateWithWeapp(options);
246286
},
247287

248288
/**
249289
* 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。
250290
* 仅在小程序中可用。
251291
*
292+
* @since 3.13.0
293+
* @param {string} unionId
294+
* @param {Object} [unionOptions]
295+
* @param {string} [unionOptions.unionIdPlatform = 'weixin'] unionId platform
296+
* @param {boolean} [unionOptions.asMainAccount = false] If true, the unionId will be associated with the user.
252297
* @return {Promise<AV.User>}
253298
*/
254-
linkWithWeapp() {
299+
associateWithWeappWithUnionId(unionId, unionOptions) {
255300
return getWeappLoginCode().then(code =>
256-
this._linkWith(PLATFORM_WEAPP, { code })
301+
this._linkWith(
302+
PLATFORM_WEAPP,
303+
mergeUnionDataIntoAuthData({ code }, unionId, unionOptions)
304+
)
257305
);
258306
},
259307

@@ -468,10 +516,33 @@ module.exports = function(AV) {
468516
/**
469517
* The same with {@link AV.User.loginWithWeapp}, except that you can set attributes before login.
470518
* @since 3.7.0
519+
* @param {Object} [options]
520+
* @param {boolean} [options.failOnNotExist] If true, the login request will fail when no user matches this authData exists.
521+
* @param {boolean} [options.preferUnionId] 当用户满足 {@link https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html 获取 UnionId 的条件} 时,是否使用 UnionId 登录。(since 3.13.0)
522+
* @param {string} [options.unionIdPlatform = 'weixin'] (only take effect when preferUnionId) unionId platform
523+
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
471524
*/
472525
loginWithWeapp(options) {
473526
return getWeappLoginCode().then(code =>
474-
this.loginWithAuthData({ code }, PLATFORM_WEAPP, options)
527+
this.loginWithAuthData(
528+
getWeappAuthData(code, options),
529+
PLATFORM_WEAPP,
530+
options
531+
)
532+
);
533+
},
534+
535+
/**
536+
* The same with {@link AV.User.loginWithWeappWithUnionId}, except that you can set attributes before login.
537+
* @since 3.13.0
538+
*/
539+
loginWithWeappWithUnionId(unionId, unionLoginOptions) {
540+
return getWeappLoginCode().then(code =>
541+
this.loginWithAuthData(
542+
mergeUnionDataIntoAuthData({ code }, unionId, unionLoginOptions),
543+
PLATFORM_WEAPP,
544+
unionLoginOptions
545+
)
475546
);
476547
},
477548

@@ -1030,12 +1101,39 @@ module.exports = function(AV) {
10301101
*
10311102
* @since 2.0.0
10321103
* @param {Object} [options]
1104+
* @param {boolean} [options.preferUnionId] 当用户满足 {@link https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html 获取 UnionId 的条件} 时,是否使用 UnionId 登录。(since 3.13.0)
1105+
* @param {string} [options.unionIdPlatform = 'weixin'] (only take effect when preferUnionId) unionId platform
1106+
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
10331107
* @param {boolean} [options.failOnNotExist] If true, the login request will fail when no user matches this authData exists. (since v3.7.0)
10341108
* @return {Promise.<AV.User>}
10351109
*/
10361110
loginWithWeapp(options) {
10371111
return getWeappLoginCode().then(code =>
1038-
this.loginWithAuthData({ code }, PLATFORM_WEAPP, options)
1112+
this.loginWithAuthData(
1113+
getWeappAuthData(code, options),
1114+
PLATFORM_WEAPP,
1115+
options
1116+
)
1117+
);
1118+
},
1119+
1120+
/**
1121+
* 使用当前使用小程序的微信用户身份注册或登录,
1122+
* 仅在小程序中可用。
1123+
*
1124+
* @since 3.13.0
1125+
* @param {Object} [unionLoginOptions]
1126+
* @param {string} [unionLoginOptions.unionIdPlatform = 'weixin'] unionId platform
1127+
* @param {boolean} [unionLoginOptions.asMainAccount = false] If true, the unionId will be associated with the user.
1128+
* @param {boolean} [unionLoginOptions.failOnNotExist] If true, the login request will fail when no user matches this authData exists. * @return {Promise.<AV.User>}
1129+
*/
1130+
loginWithWeappWithUnionId(unionId, unionLoginOptions) {
1131+
return getWeappLoginCode().then(code =>
1132+
this.loginWithAuthData(
1133+
mergeUnionDataIntoAuthData({ code }, unionId, unionLoginOptions),
1134+
PLATFORM_WEAPP,
1135+
unionLoginOptions
1136+
)
10391137
);
10401138
},
10411139

storage.d.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ interface CaptchaOptions {
4949

5050
interface FileSaveOptions extends AuthOptions {
5151
keepFileName?: boolean;
52-
onprogress?: (
53-
event: {
54-
loaded: number;
55-
total: number;
56-
percent: number;
57-
}
58-
) => any;
52+
onprogress?: (event: {
53+
loaded: number;
54+
total: number;
55+
percent: number;
56+
}) => any;
5957
}
6058

6159
export interface WaitOption {
@@ -621,6 +619,12 @@ interface UnionOptions {
621619

622620
interface UnionLoginOptions extends OAuthLoginOptions, UnionOptions {}
623621

622+
interface WeappOptions extends UnionOptions {
623+
preferUnion: boolean;
624+
}
625+
626+
interface WeappLoginOptions extends OAuthLoginOptions, WeappOptions {}
627+
624628
/**
625629
* @class
626630
*
@@ -644,7 +648,11 @@ export class User extends Object {
644648
static become(sessionToken: string): Promise<User>;
645649

646650
static loginAnonymously(): Promise<User>;
647-
static loginWithWeapp(options?: OAuthLoginOptions): Promise<User>;
651+
static loginWithWeapp(options?: WeappLoginOptions): Promise<User>;
652+
static loginWithWeappWithUnionId(
653+
unionId: string,
654+
unionLoginOptions?: UnionLoginOptions
655+
): Promise<User>;
648656
static logInWithMobilePhone(
649657
mobilePhone: string,
650658
password: string
@@ -711,7 +719,11 @@ export class User extends Object {
711719
static followerQuery(userObjectId: string): FriendShipQuery;
712720
static followeeQuery(userObjectId: string): FriendShipQuery;
713721

714-
loginWithWeapp(options?: OAuthLoginOptions): Promise<User>;
722+
loginWithWeapp(options?: WeappLoginOptions): Promise<User>;
723+
loginWithWeappWithUnionId(
724+
unionId: string,
725+
unionLoginOptions?: UnionLoginOptions
726+
): Promise<User>;
715727
loginWithAuthData(
716728
authData: object,
717729
platform: string,
@@ -731,12 +743,18 @@ export class User extends Object {
731743
isAnonymous(): boolean;
732744
isCurrent(): boolean;
733745

746+
associateWithWeapp(options?: WeappOptions): Promise<User>;
747+
associateWithWeappWithUnionId(
748+
unionId: string,
749+
unionOptions?: UnionOptions
750+
): Promise<User>;
751+
734752
associateWithAuthData(authData: object, platform: string): Promise<User>;
735753
associateWithAuthDataAndUnionId(
736754
authData: object,
737755
platform: string,
738756
unionId: string,
739-
unionLoginOptions?: UnionOptions
757+
unionOptions?: UnionOptions
740758
): Promise<User>;
741759
dissociateAuthData(platform: string): Promise<User>;
742760

0 commit comments

Comments
 (0)