Skip to content

Commit 8f5393c

Browse files
committed
Merge pull request #52 from leancloud/feature/2015.01.22
Feature/2015.01.22
2 parents 04f69bb + 0da0b71 commit 8f5393c

File tree

8 files changed

+425
-9440
lines changed

8 files changed

+425
-9440
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules/
22
dist/
3+
package/
4+
*tgz

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.tgz
2+
node_modules/
3+
dist/
4+
package/
5+
tests/
6+
tools/
7+
demo/
8+
lib/cloud.js
9+
*.sh

changelog.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# 0.4.7 日期: 2015 年 1 月 23 日
2+
* 修复页面跳转更新 user 导致 current user 属性丢失的 Bug。
3+
* 增加 `AV.User.updatePassword` 方法,根据老密码修改成新密码。
4+
*`AV.Object` 增加 `getObjectId, getUpdatedAt, getCreatedAt` 三个方法。
5+
* 增加 `AV.User#signUpOrlogInWithMobilePhone` 手机一键登录。
6+
* 一些内部改进和重构。
7+
8+
# 0.4.6 日期:2014 年 12 月 11 日
9+
* 添加新方法 `AV.File.createWithoutData(objectId)`,根据 objectId 构造 AV.File
10+
* 添加 `AV.Query.and` 方法用于复合查询
11+
* `AV.File` 支持 get/set ACL
12+
* 增加新方法 `AV.setProduction(boolean)` 用于设置生产环境或者测试环境。
13+
14+
# 0.4.5 日期:2014 年 10 月 29 日
15+
* CQL 查询支持占位符,AV.Query.doCloudQuery 方法增加三个参数版本
16+
* AV.Push 增加 cql 属性说明,可以通过 CQL 指定推送查询条件。
17+
* 部分内部代码重构。
18+
19+
# 0.4.4 日期:2014 年 10 月 14 日
20+
* 修复 node.js 下上传文件无法指定文件 mime type 的Bug
21+
* 添加 `AV.Object.new` 函数用来创建对象,避免代码压缩带来的问题。
22+
123
# 0.4.3
224

325
* 添加 CQL 查询支持,增加 `AV.Query.doCloudQuery` 方法。

lib/av.js

Lines changed: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
(function(root) {
1515
root.AV = root.AV || {};
16-
root.AV.VERSION = "js0.4.6";
16+
root.AV.VERSION = "js0.4.7";
1717
}(this));
1818
// Underscore.js 1.4.4
1919
// http://underscorejs.org
@@ -1409,14 +1409,14 @@
14091409
*Use china avoscloud API service:https://cn.avoscloud.com
14101410
*/
14111411
AV.useAVCloudCN = function(){
1412-
AV.serverURL = "https://cn.avoscloud.com";
1412+
AV.serverURL = "https://leancloud.cn";
14131413
};
14141414

14151415
/**
14161416
*Use USA avoscloud API service:https://us.avoscloud.com
14171417
*/
14181418
AV.useAVCloudUS = function(){
1419-
AV.serverURL = "https://us.avoscloud.com";
1419+
AV.serverURL = "https://avoscloud.us";
14201420
};
14211421

14221422
/**
@@ -1509,6 +1509,13 @@
15091509
}
15101510
};
15111511
xdr.onerror = xdr.ontimeout = function() {
1512+
// Let's fake a real error message.
1513+
var fakeResponse = {
1514+
responseText: JSON.stringify({
1515+
code: AV.Error.X_DOMAIN_REQUEST,
1516+
error: "IE's XDomainRequest does not supply error info."
1517+
})
1518+
};
15121519
promise.reject(xdr);
15131520
};
15141521
xdr.onprogress = function() {};
@@ -1517,14 +1524,25 @@
15171524
return promise;
15181525
};
15191526

1527+
AV._useXDomainRequest = function() {
1528+
if (typeof(XDomainRequest) !== "undefined") {
1529+
// We're in IE 8+.
1530+
if ('withCredentials' in new XMLHttpRequest()) {
1531+
// We're in IE 10+.
1532+
return false;
1533+
}
1534+
return true;
1535+
}
1536+
return false;
1537+
};
15201538

15211539
AV._ajax = function(method, url, data, success, error) {
15221540
var options = {
15231541
success: success,
15241542
error: error
15251543
};
15261544

1527-
if (typeof(XDomainRequest) !== "undefined") {
1545+
if (AV._useXDomainRequest()) {
15281546
return AV._ajaxIE8(method, url, data)._thenRunCallbacks(options);
15291547
}
15301548

@@ -1607,11 +1625,13 @@
16071625
route !== "requestSmsCode" &&
16081626
route !== "verifySmsCode" &&
16091627
route !== "users" &&
1628+
route !== "usersByMobilePhone" &&
16101629
route !== "cloudQuery" &&
16111630
route !== "qiniu" &&
16121631
route !== "statuses" &&
16131632
route !== 'subscribe/statuses/count' &&
16141633
route !== 'subscribe/statuses' &&
1634+
!(/users\/[^\/]+\/updatePassword/.test(route)) &&
16151635
!(/users\/[^\/]+\/friendship\/[^\/]+/.test(route))) {
16161636
throw "Bad route: '" + route + "'.";
16171637
}
@@ -2237,7 +2257,15 @@
22372257
* Twitter) is unsupported.
22382258
* @constant
22392259
*/
2240-
UNSUPPORTED_SERVICE: 252
2260+
UNSUPPORTED_SERVICE: 252,
2261+
/**
2262+
* Error code indicating a real error code is unavailable because
2263+
* we had to use an XDomainRequest object to allow CORS requests in
2264+
* Internet Explorer, which strips the body from HTTP responses that have
2265+
* a non-2XX status code.
2266+
* @constant
2267+
*/
2268+
X_DOMAIN_REQUEST: 602
22412269
});
22422270

22432271
}(this));
@@ -4528,6 +4556,30 @@
45284556
this._fetchWhenSave = enable;
45294557
},
45304558

4559+
/**
4560+
* Returns the object's objectId.
4561+
* @return {String} the objectId.
4562+
*/
4563+
getObjectId: function() {
4564+
return this.id;
4565+
},
4566+
4567+
/**
4568+
* Returns the object's createdAt attribute.
4569+
* @return {Date}
4570+
*/
4571+
getCreatedAt: function() {
4572+
return this.createdAt || this.get('createdAt');
4573+
},
4574+
4575+
/**
4576+
* Returns the object's updatedAt attribute.
4577+
* @return {Date}
4578+
*/
4579+
getUpdatedAt: function() {
4580+
return this.updatedAt || this.get('updatedAt');
4581+
},
4582+
45314583
/**
45324584
* Returns a JSON version of the object suitable for saving to AV.
45334585
* @return {Object}
@@ -5277,7 +5329,9 @@
52775329
route = "users";
52785330
className = null;
52795331
}
5280-
var request = AV._request(route, className, model.id, method, json);
5332+
//hook makeRequest in options.
5333+
var makeRequest = options._makeRequest || AV._request;
5334+
var request = makeRequest(route, className, model.id, method, json);
52815335

52825336
request = request.then(function(resp, status, xhr) {
52835337
var serverAttrs = model.parse(resp, status, xhr);
@@ -6894,6 +6948,69 @@
68946948
return this.save(attrs, newOptions);
68956949
},
68966950

6951+
/**
6952+
* Signs up a new user with mobile phone and sms code.
6953+
* You should call this instead of save for
6954+
* new AV.Users. This will create a new AV.User on the server, and
6955+
* also persist the session on disk so that you can access the user using
6956+
* <code>current</code>.
6957+
*
6958+
* <p>A username and password must be set before calling signUp.</p>
6959+
*
6960+
* <p>Calls options.success or options.error on completion.</p>
6961+
*
6962+
* @param {Object} attrs Extra fields to set on the new user, or null.
6963+
* @param {Object} options A Backbone-style options object.
6964+
* @return {AV.Promise} A promise that is fulfilled when the signup
6965+
* finishes.
6966+
* @see AV.User.signUpOrlogInWithMobilePhone
6967+
* @see AV.Cloud.requestSmsCode
6968+
*/
6969+
signUpOrlogInWithMobilePhone: function(attrs, options) {
6970+
var error;
6971+
options = options || {};
6972+
6973+
var mobilePhoneNumber = (attrs && attrs.mobilePhoneNumber) ||
6974+
this.get("mobilePhoneNumber");
6975+
if (!mobilePhoneNumber || (mobilePhoneNumber === "")) {
6976+
error = new AV.Error(
6977+
AV.Error.OTHER_CAUSE,
6978+
"Cannot sign up or login user by mobilePhoneNumber " +
6979+
"with an empty mobilePhoneNumber.");
6980+
if (options && options.error) {
6981+
options.error(this, error);
6982+
}
6983+
return AV.Promise.error(error);
6984+
}
6985+
6986+
var smsCode = (attrs && attrs.smsCode) || this.get("smsCode");
6987+
if (!smsCode || (smsCode === "")) {
6988+
error = new AV.Error(
6989+
AV.Error.OTHER_CAUSE,
6990+
"Cannot sign up or login user by mobilePhoneNumber " +
6991+
"with an empty smsCode.");
6992+
if (options && options.error) {
6993+
options.error(this, error);
6994+
}
6995+
return AV.Promise.error(error);
6996+
}
6997+
6998+
// Overridden so that the user can be made the current user.
6999+
var newOptions = _.clone(options);
7000+
newOptions._makeRequest = function(route, className, id, method, json) {
7001+
return AV._request('usersByMobilePhone', null, null, "POST", json);
7002+
};
7003+
newOptions.success = function(model) {
7004+
model._handleSaveResult(true);
7005+
delete model.attributes.smsCode;
7006+
delete model._serverData.smsCode;
7007+
if (options.success) {
7008+
options.success.apply(this, arguments);
7009+
}
7010+
};
7011+
return this.save(attrs, newOptions);
7012+
},
7013+
68977014
/**
68987015
* Logs in a AV.User. On success, this saves the session to localStorage,
68997016
* so you can retrieve the currently logged in user using
@@ -7026,6 +7143,24 @@
70267143
return AV.Object.prototype.fetch.call(this, newOptions);
70277144
},
70287145

7146+
/**
7147+
* Update user's new password safely based on old password.
7148+
* @param {String} oldPassword, the old password.
7149+
* @param {String} newPassword, the new password.
7150+
* @param {Object} An optional Backbone-like options object with
7151+
* success and error callbacks that will be invoked once the iteration
7152+
* has finished.
7153+
*/
7154+
updatePassword: function(oldPassword, newPassword, options) {
7155+
var route = 'users/' + this.id + '/updatePassword';
7156+
var params = {
7157+
old_password: oldPassword,
7158+
new_password: newPassword
7159+
};
7160+
var request = AV._request(route, null, null, 'PUT', params);
7161+
return request._thenRunCallbacks(options, this);
7162+
},
7163+
70297164
/**
70307165
* Returns true if <code>current</code> would return this user.
70317166
* @see AV.User#current
@@ -7199,6 +7334,31 @@
71997334
return user.logIn(options);
72007335
},
72017336

7337+
/**
7338+
* Sign up or logs in a user with a mobilePhoneNumber and smsCode.
7339+
* On success, this saves the session to disk, so you can retrieve the currently
7340+
* logged in user using <code>current</code>.
7341+
*
7342+
* <p>Calls options.success or options.error on completion.</p>
7343+
*
7344+
* @param {String} mobilePhoneNumber The user's mobilePhoneNumber.
7345+
* @param {String} smsCode The sms code sent by AV.Cloud.requestSmsCode
7346+
* @param {Object} attributes The user's other attributes such as username etc.
7347+
* @param {Object} options A Backbone-style options object.
7348+
* @return {AV.Promise} A promise that is fulfilled with the user when
7349+
* the login completes.
7350+
* @see AV.User#signUpOrlogInWithMobilePhone
7351+
* @see AV.Cloud.requestSmsCode
7352+
*/
7353+
signUpOrlogInWithMobilePhone: function(mobilePhoneNumber, smsCode, attrs, options) {
7354+
attrs = attrs || {};
7355+
attrs.mobilePhoneNumber = mobilePhoneNumber;
7356+
attrs.smsCode = smsCode;
7357+
var user = AV.Object._create("_User");
7358+
return user.signUpOrlogInWithMobilePhone(attrs, options);
7359+
},
7360+
7361+
72027362
/**
72037363
* Logs in a user with a mobile phone number and password. On success, this
72047364
* saves the session to disk, so you can retrieve the currently logged in
@@ -7428,7 +7588,8 @@
74287588
delete json._id;
74297589
AV.User._currentUser._sessionToken = json._sessionToken;
74307590
delete json._sessionToken;
7431-
AV.User._currentUser.set(json);
7591+
AV.User._currentUser._finishFetch(json);
7592+
//AV.User._currentUser.set(json);
74327593

74337594
AV.User._currentUser._synchronizeAllAuthData();
74347595
AV.User._currentUser._refreshCache();

0 commit comments

Comments
 (0)