Skip to content

Commit 92e3fa2

Browse files
committed
feat(user): unionId support
1 parent 5a0d40b commit 92e3fa2

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

src/user.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ const getWeappLoginCode = () => {
2121
});
2222
};
2323

24+
const mergeUnionDataIntoAuthData = (authData, unionId, { unionIdPlatform = 'weixin', asMainAccount = false } = {}) => {
25+
if (typeof unionId !== 'string') throw new AVError(AVError.OTHER_CAUSE, 'unionId is not a string');
26+
if (typeof unionIdPlatform !== 'string') throw new AVError(AVError.OTHER_CAUSE, 'unionIdPlatform is not a string');
27+
28+
return _.extend({}, authData, {
29+
platform: unionIdPlatform,
30+
unionid: unionId,
31+
main_account: Boolean(asMainAccount),
32+
});
33+
};
34+
2435
module.exports = function(AV) {
2536
/**
2637
* @class
@@ -180,6 +191,33 @@ module.exports = function(AV) {
180191
return this._linkWith(platform, authData);
181192
},
182193

194+
/**
195+
* Associate the user with a third party authData and unionId.
196+
* @since 3.5.0
197+
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
198+
* @param {string} platform Available platform for sign up.
199+
* @param {string} unionId
200+
* @param {Object} [unionLoginOptions]
201+
* @param {string} [unionLoginOptions.unionIdPlatform = 'weixin'] unionId platform
202+
* @param {boolean} [unionLoginOptions.asMainAccount = false]
203+
* @return {Promise<AV.User>} A promise that is fulfilled with the user when completed.
204+
* @example user.associateWithAuthData({
205+
* openid: 'abc123',
206+
* access_token: '123abc',
207+
* expires_in: 1382686496
208+
* }, 'weixin', 'union123', {
209+
* unionIdPlatform: 'weixin',
210+
* asMainAccount: false,
211+
* }).then(function(user) {
212+
* //Access user here
213+
* }).catch(function(error) {
214+
* //console.error("error: ", error);
215+
* });
216+
*/
217+
associateWithAuthDataAndUnionId(authData, platform, unionId, unionLoginOptions) {
218+
return this._linkWith(platform, mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions));
219+
},
220+
183221
/**
184222
* 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。
185223
* 仅在小程序中可用。
@@ -804,7 +842,11 @@ module.exports = function(AV) {
804842
* @param {string} platform Available platform for sign up.
805843
* @return {Promise} A promise that is fulfilled with the user when
806844
* the login completes.
807-
* @example AV.User.signUpOrlogInWithAuthData(authData, platform).then(function(user) {
845+
* @example AV.User.signUpOrlogInWithAuthData({
846+
* openid: 'abc123',
847+
* access_token: '123abc',
848+
* expires_in: 1382686496
849+
* }, 'weixin').then(function(user) {
808850
* //Access user here
809851
* }).catch(function(error) {
810852
* //console.error("error: ", error);
@@ -815,6 +857,33 @@ module.exports = function(AV) {
815857
return AV.User._logInWith(platform, authData);
816858
},
817859

860+
/**
861+
* Sign up or logs in a user with a third party authData and unionId.
862+
* @since 3.5.0
863+
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
864+
* @param {string} platform Available platform for sign up.
865+
* @param {string} unionId
866+
* @param {Object} [unionLoginOptions]
867+
* @param {string} [unionLoginOptions.unionIdPlatform = 'weixin'] unionId platform
868+
* @param {boolean} [unionLoginOptions.asMainAccount = false]
869+
* @return {Promise<AV.User>} A promise that is fulfilled with the user when completed.
870+
* @example user.associateWithAuthData({
871+
* openid: 'abc123',
872+
* access_token: '123abc',
873+
* expires_in: 1382686496
874+
* }, 'weixin', 'union123', {
875+
* unionIdPlatform: 'weixin',
876+
* asMainAccount: false,
877+
* }).then(function(user) {
878+
* //Access user here
879+
* }).catch(function(error) {
880+
* //console.error("error: ", error);
881+
* });
882+
*/
883+
signUpOrlogInWithAuthDataAndUnionId(authData, platform, unionId, unionLoginOptions) {
884+
return this.signUpOrlogInWithAuthData(mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions), platform);
885+
},
886+
818887
/**
819888
* 使用当前使用小程序的微信用户身份注册或登录,成功后用户的 session 会在设备上持久化保存,之后可以使用 AV.User.current() 获取当前登录用户。
820889
* 仅在小程序中可用。

storage.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@ export class Role extends Object {
537537
setName(name: string): Role;
538538
}
539539

540+
interface UnionLoginOptions {
541+
unionIdPlatform?: string;
542+
asMainAccount?: boolean;
543+
}
544+
540545
/**
541546
* @class
542547
*
@@ -558,6 +563,7 @@ export class User extends Object {
558563
static logInWithMobilePhone(mobilePhone: string, password: string, options?: AuthOptions): Promise<User>;
559564
static logInWithMobilePhoneSmsCode(mobilePhone: string, smsCode: string, options?: AuthOptions): Promise<User>;
560565
static signUpOrlogInWithAuthData(data: any, platform: string, options?: AuthOptions): Promise<User>;
566+
static signUpOrlogInWithAuthDataAndUnionId(authData: Object, platform: string, unionId: string, unionLoginOptions?: UnionLoginOptions): Promise<User>;
561567
static signUpOrlogInWithMobilePhone(mobilePhoneNumber: string, smsCode: string, attributes?: any, options?: AuthOptions): Promise<User>;
562568
static requestEmailVerify(email: string, options?: AuthOptions): Promise<User>;
563569
static requestLoginSmsCode(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
@@ -576,8 +582,9 @@ export class User extends Object {
576582
isAuthenticated(): Promise<boolean>;
577583
isCurrent(): boolean;
578584

579-
associateWithAuthData(authData: Object, platform?: string): Promise<User>;
580-
dissociateAuthData(platform?: string): Promise<User>;
585+
associateWithAuthData(authData: Object, platform: string): Promise<User>;
586+
associateWithAuthDataAndUnionId(authData: Object, platform: string, unionId: string, unionLoginOptions?: UnionLoginOptions): Promise<User>;
587+
dissociateAuthData(platform: string): Promise<User>;
581588

582589
getEmail(): string;
583590
setEmail(email: string, options?: AuthOptions): boolean;

test/user.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@ describe("User", function() {
187187
});
188188
});
189189

190+
describe('authData and unionId', () => {
191+
it('should login as the same user', () => {
192+
const now = Date.now();
193+
return AV.User.signUpOrlogInWithAuthDataAndUnionId({
194+
openid: 'openid1' + now,
195+
access_token: 'access_token',
196+
expires_in: 1382686496,
197+
}, 'weixin_1', 'unionid' + now, {
198+
asMainAccount: true,
199+
}).then((user1) => {
200+
return AV.User.signUpOrlogInWithAuthDataAndUnionId({
201+
openid: 'openid2' + now,
202+
access_token: 'access_token',
203+
expires_in: 1382686496,
204+
}, 'weixin_2', 'unionid' + now).then(user2 => {
205+
user2.id.should.be.eql(user1.id);
206+
});
207+
});
208+
});
209+
});
210+
190211
describe('associate with authData', function() {
191212
it('logIn an user, and associate with authData', function() {
192213
var username = Date.now().toString(36);

0 commit comments

Comments
 (0)