@@ -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+
2435module . 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 * 仅在小程序中可用。
0 commit comments