Skip to content

Commit e5e0886

Browse files
committed
feat(User): support QQApp login
1 parent 192d5a4 commit e5e0886

File tree

2 files changed

+178
-35
lines changed

2 files changed

+178
-35
lines changed

src/user.js

Lines changed: 158 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ const Promise = require('./promise');
66

77
const PLATFORM_ANONYMOUS = 'anonymous';
88
const PLATFORM_WEAPP = 'lc_weapp';
9+
const PLATFORM_QQAPP = 'lc_qqapp';
910

10-
const getWeappLoginCode = () => {
11+
const getMiniappLoginCode = () => {
1112
if (typeof wx === 'undefined' || typeof wx.login !== 'function') {
12-
throw new Error('Weapp Login is only available in Weapp');
13+
throw new Error('wx.login is not a function(当前平台不支持一键登录)');
1314
}
1415
return new Promise((resolve, reject) => {
1516
wx.login({
@@ -20,14 +21,18 @@ const getWeappLoginCode = () => {
2021
reject(new Error(errMsg));
2122
}
2223
},
23-
fail: () => reject(new Error('wx.login 失败')),
24+
fail: () => reject(new Error('login 失败')),
2425
});
2526
});
2627
};
2728

28-
const getWeappAuthData = (
29+
const getMiniappAuthData = (defaultUnionIdPlatform = 'weixin') => (
2930
code,
30-
{ preferUnionId, unionIdPlatform = 'weixin', asMainAccount = true } = {}
31+
{
32+
preferUnionId,
33+
unionIdPlatform = defaultUnionIdPlatform,
34+
asMainAccount = true,
35+
} = {}
3136
) =>
3237
preferUnionId
3338
? {
@@ -37,10 +42,10 @@ const getWeappAuthData = (
3742
}
3843
: { code };
3944

40-
const mergeUnionDataIntoAuthData = (
45+
const mergeUnionDataIntoAuthData = (defaultUnionIdPlatform = 'weixin') => (
4146
authData,
4247
unionId,
43-
{ unionIdPlatform = 'weixin', asMainAccount = false } = {}
48+
{ unionIdPlatform = defaultUnionIdPlatform, asMainAccount = false } = {}
4449
) => {
4550
if (typeof unionId !== 'string')
4651
throw new AVError(AVError.OTHER_CAUSE, 'unionId is not a string');
@@ -253,13 +258,33 @@ module.exports = function(AV) {
253258
) {
254259
return this._linkWith(
255260
platform,
256-
mergeUnionDataIntoAuthData(authData, unionId, unionOptions)
261+
mergeUnionDataIntoAuthData()(authData, unionId, unionOptions)
257262
);
258263
},
259264

260265
/**
261-
* 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。
262-
* 仅在小程序中可用。
266+
* 将用户与 QQ 小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用 QQ 小程序的微信帐号。
267+
* 仅在 QQ 小程序中可用。
268+
*
269+
* @since 4.2.0
270+
* @param {Object} [options]
271+
* @param {boolean} [options.preferUnionId] 如果服务端在登录时获取到了用户的 UnionId,是否将 UnionId 保存在用户账号中。
272+
* @param {string} [options.unionIdPlatform = 'qq'] (only take effect when preferUnionId) unionId platform
273+
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
274+
* @return {Promise<AV.User>}
275+
*/
276+
associateWithQQApp(options) {
277+
return getMiniappLoginCode().then(code =>
278+
this._linkWith(
279+
PLATFORM_QQAPP,
280+
getMiniappAuthData('qq')(code, options)
281+
)
282+
);
283+
},
284+
285+
/**
286+
* 将用户与微信小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用微信小程序的微信帐号。
287+
* 仅在微信小程序中可用。
263288
*
264289
* @since 3.13.0
265290
* @param {Object} [options]
@@ -269,8 +294,8 @@ module.exports = function(AV) {
269294
* @return {Promise<AV.User>}
270295
*/
271296
associateWithWeapp(options) {
272-
return getWeappLoginCode().then(code =>
273-
this._linkWith(PLATFORM_WEAPP, getWeappAuthData(code, options))
297+
return getMiniappLoginCode().then(code =>
298+
this._linkWith(PLATFORM_WEAPP, getMiniappAuthData()(code, options))
274299
);
275300
},
276301

@@ -286,8 +311,28 @@ module.exports = function(AV) {
286311
},
287312

288313
/**
289-
* 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。
290-
* 仅在小程序中可用。
314+
* 将用户与 QQ 小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用 QQ 小程序的 QQ 帐号。
315+
* 仅在 QQ 小程序中可用。
316+
*
317+
* @since 4.2.0
318+
* @param {string} unionId
319+
* @param {Object} [unionOptions]
320+
* @param {string} [unionOptions.unionIdPlatform = 'qq'] unionId platform
321+
* @param {boolean} [unionOptions.asMainAccount = false] If true, the unionId will be associated with the user.
322+
* @return {Promise<AV.User>}
323+
*/
324+
associateWithQQAppWithUnionId(unionId, unionOptions) {
325+
return getMiniappLoginCode().then(code =>
326+
this._linkWith(
327+
PLATFORM_QQAPP,
328+
mergeUnionDataIntoAuthData('qq')({ code }, unionId, unionOptions)
329+
)
330+
);
331+
},
332+
333+
/**
334+
* 将用户与微信小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用微信小程序的微信帐号。
335+
* 仅在微信小程序中可用。
291336
*
292337
* @since 3.13.0
293338
* @param {string} unionId
@@ -297,10 +342,10 @@ module.exports = function(AV) {
297342
* @return {Promise<AV.User>}
298343
*/
299344
associateWithWeappWithUnionId(unionId, unionOptions) {
300-
return getWeappLoginCode().then(code =>
345+
return getMiniappLoginCode().then(code =>
301346
this._linkWith(
302347
PLATFORM_WEAPP,
303-
mergeUnionDataIntoAuthData({ code }, unionId, unionOptions)
348+
mergeUnionDataIntoAuthData()({ code }, unionId, unionOptions)
304349
)
305350
);
306351
},
@@ -507,7 +552,7 @@ module.exports = function(AV) {
507552
unionLoginOptions
508553
) {
509554
return this.loginWithAuthData(
510-
mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions),
555+
mergeUnionDataIntoAuthData()(authData, unionId, unionLoginOptions),
511556
platform,
512557
unionLoginOptions
513558
);
@@ -523,9 +568,9 @@ module.exports = function(AV) {
523568
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
524569
*/
525570
loginWithWeapp(options) {
526-
return getWeappLoginCode().then(code =>
571+
return getMiniappLoginCode().then(code =>
527572
this.loginWithAuthData(
528-
getWeappAuthData(code, options),
573+
getMiniappAuthData()(code, options),
529574
PLATFORM_WEAPP,
530575
options
531576
)
@@ -537,9 +582,46 @@ module.exports = function(AV) {
537582
* @since 3.13.0
538583
*/
539584
loginWithWeappWithUnionId(unionId, unionLoginOptions) {
540-
return getWeappLoginCode().then(code =>
585+
return getMiniappLoginCode().then(code =>
541586
this.loginWithAuthData(
542-
mergeUnionDataIntoAuthData({ code }, unionId, unionLoginOptions),
587+
mergeUnionDataIntoAuthData()({ code }, unionId, unionLoginOptions),
588+
PLATFORM_WEAPP,
589+
unionLoginOptions
590+
)
591+
);
592+
},
593+
594+
/**
595+
* The same with {@link AV.User.loginWithQQApp}, except that you can set attributes before login.
596+
* @since 4.2.0
597+
* @param {Object} [options]
598+
* @param {boolean} [options.failOnNotExist] If true, the login request will fail when no user matches this authData exists.
599+
* @param {boolean} [options.preferUnionId] 如果服务端在登录时获取到了用户的 UnionId,是否将 UnionId 保存在用户账号中。
600+
* @param {string} [options.unionIdPlatform = 'qq'] (only take effect when preferUnionId) unionId platform
601+
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
602+
*/
603+
loginWithQQApp(options) {
604+
return getMiniappLoginCode().then(code =>
605+
this.loginWithAuthData(
606+
getMiniappAuthData('qq')(code, options),
607+
PLATFORM_WEAPP,
608+
options
609+
)
610+
);
611+
},
612+
613+
/**
614+
* The same with {@link AV.User.loginWithQQAppWithUnionId}, except that you can set attributes before login.
615+
* @since 4.2.0
616+
*/
617+
loginWithQQAppWithUnionId(unionId, unionLoginOptions) {
618+
return getMiniappLoginCode().then(code =>
619+
this.loginWithAuthData(
620+
mergeUnionDataIntoAuthData('qq')(
621+
{ code },
622+
unionId,
623+
unionLoginOptions
624+
),
543625
PLATFORM_WEAPP,
544626
unionLoginOptions
545627
)
@@ -1096,7 +1178,7 @@ module.exports = function(AV) {
10961178
unionLoginOptions
10971179
) {
10981180
return this.loginWithAuthData(
1099-
mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions),
1181+
mergeUnionDataIntoAuthData()(authData, unionId, unionLoginOptions),
11001182
platform,
11011183
unionLoginOptions
11021184
);
@@ -1114,8 +1196,8 @@ module.exports = function(AV) {
11141196
},
11151197

11161198
/**
1117-
* 使用当前使用小程序的微信用户身份注册或登录,成功后用户的 session 会在设备上持久化保存,之后可以使用 AV.User.current() 获取当前登录用户。
1118-
* 仅在小程序中可用
1199+
* 使用当前使用微信小程序的微信用户身份注册或登录,成功后用户的 session 会在设备上持久化保存,之后可以使用 AV.User.current() 获取当前登录用户。
1200+
* 仅在微信小程序中可用
11191201
*
11201202
* @since 2.0.0
11211203
* @param {Object} [options]
@@ -1126,18 +1208,18 @@ module.exports = function(AV) {
11261208
* @return {Promise.<AV.User>}
11271209
*/
11281210
loginWithWeapp(options) {
1129-
return getWeappLoginCode().then(code =>
1211+
return getMiniappLoginCode().then(code =>
11301212
this.loginWithAuthData(
1131-
getWeappAuthData(code, options),
1213+
getMiniappAuthData()(code, options),
11321214
PLATFORM_WEAPP,
11331215
options
11341216
)
11351217
);
11361218
},
11371219

11381220
/**
1139-
* 使用当前使用小程序的微信用户身份注册或登录
1140-
* 仅在小程序中可用
1221+
* 使用当前使用微信小程序的微信用户身份注册或登录
1222+
* 仅在微信小程序中可用
11411223
*
11421224
* @since 3.13.0
11431225
* @param {Object} [unionLoginOptions]
@@ -1146,15 +1228,61 @@ module.exports = function(AV) {
11461228
* @param {boolean} [unionLoginOptions.failOnNotExist] If true, the login request will fail when no user matches this authData exists. * @return {Promise.<AV.User>}
11471229
*/
11481230
loginWithWeappWithUnionId(unionId, unionLoginOptions) {
1149-
return getWeappLoginCode().then(code =>
1231+
return getMiniappLoginCode().then(code =>
11501232
this.loginWithAuthData(
1151-
mergeUnionDataIntoAuthData({ code }, unionId, unionLoginOptions),
1233+
mergeUnionDataIntoAuthData()({ code }, unionId, unionLoginOptions),
11521234
PLATFORM_WEAPP,
11531235
unionLoginOptions
11541236
)
11551237
);
11561238
},
11571239

1240+
/**
1241+
* 使用当前使用 QQ 小程序的 QQ 用户身份注册或登录,成功后用户的 session 会在设备上持久化保存,之后可以使用 AV.User.current() 获取当前登录用户。
1242+
* 仅在 QQ 小程序中可用。
1243+
*
1244+
* @since 4.2.0
1245+
* @param {Object} [options]
1246+
* @param {boolean} [options.preferUnionId] 如果服务端在登录时获取到了用户的 UnionId,是否将 UnionId 保存在用户账号中。
1247+
* @param {string} [options.unionIdPlatform = 'qq'] (only take effect when preferUnionId) unionId platform
1248+
* @param {boolean} [options.asMainAccount = false] (only take effect when preferUnionId) If true, the unionId will be associated with the user.
1249+
* @param {boolean} [options.failOnNotExist] If true, the login request will fail when no user matches this authData exists. (since v3.7.0)
1250+
* @return {Promise.<AV.User>}
1251+
*/
1252+
loginWithQQApp(options) {
1253+
return getMiniappLoginCode().then(code =>
1254+
this.loginWithAuthData(
1255+
getMiniappAuthData('qq')(code, options),
1256+
PLATFORM_QQAPP,
1257+
options
1258+
)
1259+
);
1260+
},
1261+
1262+
/**
1263+
* 使用当前使用 QQ 小程序的 QQ 用户身份注册或登录,
1264+
* 仅在 QQ 小程序中可用。
1265+
*
1266+
* @since 4.2.0
1267+
* @param {Object} [unionLoginOptions]
1268+
* @param {string} [unionLoginOptions.unionIdPlatform = 'qq'] unionId platform
1269+
* @param {boolean} [unionLoginOptions.asMainAccount = false] If true, the unionId will be associated with the user.
1270+
* @param {boolean} [unionLoginOptions.failOnNotExist] If true, the login request will fail when no user matches this authData exists. * @return {Promise.<AV.User>}
1271+
*/
1272+
loginWithQQAppWithUnionId(unionId, unionLoginOptions) {
1273+
return getMiniappLoginCode().then(code =>
1274+
this.loginWithAuthData(
1275+
mergeUnionDataIntoAuthData('qq')(
1276+
{ code },
1277+
unionId,
1278+
unionLoginOptions
1279+
),
1280+
PLATFORM_QQAPP,
1281+
unionLoginOptions
1282+
)
1283+
);
1284+
},
1285+
11581286
/**
11591287
* Only use for DI in tests to produce deterministic IDs.
11601288
*/

storage.d.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,11 @@ interface UnionOptions {
650650

651651
interface UnionLoginOptions extends OAuthLoginOptions, UnionOptions {}
652652

653-
interface WeappOptions extends UnionOptions {
653+
interface MiniappOptions extends UnionOptions {
654654
preferUnionId: boolean;
655655
}
656656

657-
interface WeappLoginOptions extends OAuthLoginOptions, WeappOptions {}
657+
interface MiniappLoginOptions extends OAuthLoginOptions, MiniappOptions {}
658658

659659
/**
660660
* @class
@@ -679,11 +679,16 @@ export class User extends Object {
679679
static become(sessionToken: string): Promise<User>;
680680

681681
static loginAnonymously(): Promise<User>;
682-
static loginWithWeapp(options?: WeappLoginOptions): Promise<User>;
682+
static loginWithWeapp(options?: MiniappLoginOptions): Promise<User>;
683683
static loginWithWeappWithUnionId(
684684
unionId: string,
685685
unionLoginOptions?: UnionLoginOptions
686686
): Promise<User>;
687+
static loginWithQQApp(options?: MiniappLoginOptions): Promise<User>;
688+
static loginWithQQAppWithUnionId(
689+
unionId: string,
690+
unionLoginOptions?: UnionLoginOptions
691+
): Promise<User>;
687692
static logInWithMobilePhone(
688693
mobilePhone: string,
689694
password: string
@@ -751,11 +756,16 @@ export class User extends Object {
751756
static followerQuery<T extends User>(userObjectId: string): Query<T>;
752757
static followeeQuery<T extends User>(userObjectId: string): Query<T>;
753758

754-
loginWithWeapp(options?: WeappLoginOptions): Promise<User>;
759+
loginWithWeapp(options?: MiniappLoginOptions): Promise<User>;
755760
loginWithWeappWithUnionId(
756761
unionId: string,
757762
unionLoginOptions?: UnionLoginOptions
758763
): Promise<User>;
764+
loginWithQQApp(options?: MiniappLoginOptions): Promise<User>;
765+
loginWithQQAppWithUnionId(
766+
unionId: string,
767+
unionLoginOptions?: UnionLoginOptions
768+
): Promise<User>;
759769
loginWithAuthData(
760770
authData: object,
761771
platform: string,
@@ -775,11 +785,16 @@ export class User extends Object {
775785
isAnonymous(): boolean;
776786
isCurrent(): boolean;
777787

778-
associateWithWeapp(options?: WeappOptions): Promise<User>;
788+
associateWithWeapp(options?: MiniappOptions): Promise<User>;
779789
associateWithWeappWithUnionId(
780790
unionId: string,
781791
unionOptions?: UnionOptions
782792
): Promise<User>;
793+
associateWithQQApp(options?: MiniappOptions): Promise<User>;
794+
associateWithQQAppWithUnionId(
795+
unionId: string,
796+
unionOptions?: UnionOptions
797+
): Promise<User>;
783798

784799
associateWithAuthData(authData: object, platform: string): Promise<User>;
785800
associateWithAuthDataAndUnionId(

0 commit comments

Comments
 (0)