|
1 | 1 | const _ = require('underscore'); |
2 | 2 | const AVError = require('./error'); |
3 | 3 | const AVRequest = require('./request').request; |
| 4 | +const Promise = require('./promise'); |
| 5 | + |
| 6 | +const getWeappLoginCode = () => { |
| 7 | + if (typeof wx === 'undefined' || typeof wx.login !== 'function') { |
| 8 | + throw new Error('Weapp Login is only available in Weapp'); |
| 9 | + } |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + wx.login({ |
| 12 | + success: ({ code, errMsg }) => { |
| 13 | + if (code) { |
| 14 | + resolve(code); |
| 15 | + } else { |
| 16 | + reject(new Error(errMsg)); |
| 17 | + } |
| 18 | + }, |
| 19 | + }); |
| 20 | + }); |
| 21 | +}; |
4 | 22 |
|
5 | 23 | module.exports = function(AV) { |
6 | 24 | /** |
@@ -108,9 +126,9 @@ module.exports = function(AV) { |
108 | 126 | // Some old version of leanengine-node-sdk will overwrite |
109 | 127 | // AV.User._saveCurrentUser which returns no Promise. |
110 | 128 | // So we need a Promise wrapper. |
111 | | - return AV.Promise.resolve(AV.User._saveCurrentUser(this)); |
| 129 | + return Promise.resolve(AV.User._saveCurrentUser(this)); |
112 | 130 | } else { |
113 | | - return AV.Promise.resolve(); |
| 131 | + return Promise.resolve(); |
114 | 132 | } |
115 | 133 | }, |
116 | 134 |
|
@@ -141,6 +159,16 @@ module.exports = function(AV) { |
141 | 159 | } |
142 | 160 | }, |
143 | 161 |
|
| 162 | + /** |
| 163 | + * 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。 |
| 164 | + * 仅在小程序中可用。 |
| 165 | + * |
| 166 | + * @return {AV.User} |
| 167 | + */ |
| 168 | + linkWithWeapp() { |
| 169 | + return getWeappLoginCode().then(code => this._linkWith('lc_weapp', { code })); |
| 170 | + }, |
| 171 | + |
144 | 172 | /** |
145 | 173 | * Unlinks a user from a service. |
146 | 174 | * @private |
@@ -735,6 +763,17 @@ module.exports = function(AV) { |
735 | 763 | return AV.User._logInWith(platform, authData); |
736 | 764 | }, |
737 | 765 |
|
| 766 | + /** |
| 767 | + * 使用当前使用小程序的微信用户身份注册或登录,成功后用户的 session 会在设备上持久化保存,之后可以使用 AV.User.current() 获取当前登录用户。 |
| 768 | + * 仅在小程序中可用。 |
| 769 | + * |
| 770 | + * @since 2.0.0 |
| 771 | + * @return {AV.User} |
| 772 | + */ |
| 773 | + loginWithWeapp() { |
| 774 | + return getWeappLoginCode().then(code => this.signUpOrlogInWithAuthData({ code }, 'lc_weapp')); |
| 775 | + }, |
| 776 | + |
738 | 777 | /** |
739 | 778 | * Associate a user with a third party auth data(AccessToken). |
740 | 779 | * |
@@ -764,7 +803,7 @@ module.exports = function(AV) { |
764 | 803 | logOut: function() { |
765 | 804 | if (AV._config.disableCurrentUser) { |
766 | 805 | console.warn('AV.User.current() was disabled in multi-user environment, call logOut() from user object instead https://leancloud.cn/docs/leanengine-node-sdk-upgrade-1.html'); |
767 | | - return AV.Promise.resolve(null); |
| 806 | + return Promise.resolve(null); |
768 | 807 | } |
769 | 808 |
|
770 | 809 | if (AV.User._currentUser !== null) { |
@@ -926,16 +965,16 @@ module.exports = function(AV) { |
926 | 965 | currentAsync: function() { |
927 | 966 | if (AV._config.disableCurrentUser) { |
928 | 967 | console.warn('AV.User.currentAsync() was disabled in multi-user environment, access user from request instead https://leancloud.cn/docs/leanengine-node-sdk-upgrade-1.html'); |
929 | | - return AV.Promise.resolve(null); |
| 968 | + return Promise.resolve(null); |
930 | 969 | } |
931 | 970 |
|
932 | 971 | if (AV.User._currentUser) { |
933 | | - return AV.Promise.resolve(AV.User._currentUser); |
| 972 | + return Promise.resolve(AV.User._currentUser); |
934 | 973 | } |
935 | 974 |
|
936 | 975 | if (AV.User._currentUserMatchesDisk) { |
937 | 976 |
|
938 | | - return AV.Promise.resolve(AV.User._currentUser); |
| 977 | + return Promise.resolve(AV.User._currentUser); |
939 | 978 | } |
940 | 979 |
|
941 | 980 |
|
@@ -1023,7 +1062,7 @@ module.exports = function(AV) { |
1023 | 1062 | promise = AV.User.logOut(); |
1024 | 1063 | } |
1025 | 1064 | else { |
1026 | | - promise = AV.Promise.resolve(); |
| 1065 | + promise = Promise.resolve(); |
1027 | 1066 | } |
1028 | 1067 | return promise.then(function() { |
1029 | 1068 | user._isCurrentUser = true; |
|
0 commit comments