@@ -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+
2840const 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
0 commit comments