forked from hnauto/Autojs-bmob-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autojs-bmob-sdk.js
217 lines (208 loc) · 10 KB
/
autojs-bmob-sdk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* @Author: autojs.xyz Bmob后端云
* @version 2.2.3
* @blog: blog.csdn.net/vip http://doc.bmobapp.com/data/restful/index.html
* @github: github.com/hnauto/Autojs-bmob-sdk
* @Last Modified time: 2023-03-15 11:00:00
* @Description: Bmob数据库autojs SDK
* @Memo: 部分代码参考原作者 家 QQ:203118908,后因业务调整,部分代码由Bmob后端云官方修改
*/
//参见http://doc.bmobapp.com/data/restful/index.html
const APPID = '';// APPID
const REST_ID = ''; //REST ID
/*------------bmob数据库的增删改查*------------*/
const Bmob = (function () {
function Bmob(url, appId, restKey) {
this.baseUrl = url;
this.appId = appId;
this.restKey = restKey;
}
Bmob.prototype.makeRequest = function (method, url, json, sessionToken, callback) {
url = this.baseUrl + url;
var options = {};
options.contentType = "application/json";
options.method = method;
if (json) {
options.body = JSON.stringify(json);
}
sessionToken = sessionToken || "";
options.headers = {
"X-Bmob-Application-Id": this.appId,
"X-Bmob-REST-API-Key": this.restKey,
"Content-Type": "application/json",
// 加入X-Bmob-Session-Token
"X-Bmob-Session-Token": sessionToken
}
return http.request(url, options, callback);
}
//获取服务器时间
Bmob.prototype.timestamp = function () {
return this.makeRequest("GET", "/timestamp", null).body.json();
}
/*------------------------用户表------------------------------*/
//添加用户
Bmob.prototype.userCreate = function (username,password,email,phone,info) {
let user = {"username": username,"password": password,
// "email": email,
// "mobilePhoneNumber": phone,
// "info": info
}
return this.makeRequest("POST", "/users/", user).body.json();
}
//重置为你自己的备案域名
Bmob.prototype.resetDomain = function (url) {
this.baseUrl = url;
}
//添加手机用户
Bmob.prototype.userMobileCreate = function (phoneNum,smsCode) {
let user = {"mobilePhoneNumber":phoneNum,"smsCode":smsCode}
return this.makeRequest("POST", "/users/", user).body.json();
}
//用户登录
Bmob.prototype.userLogin = function (username,password) {
let url = encodeURI(util.format('/login?username=%s&password=%s', username, password));
return this.makeRequest("GET", url, null).body.json();
}
//手机用户登录
Bmob.prototype.userMobileLogin = function (phoneNum,smsCode) {
let url = encodeURI(util.format('/login?username=%s&password=%s', phoneNum,smsCode));
return this.makeRequest("GET", url, null).body.json();
}
//用户查询
Bmob.prototype.userQuery = function (username) {
let url = encodeURI(util.format('/users?where={"username":"%s"}', username));
return this.makeRequest("GET", url, null).body.json();
}
//当前用户
Bmob.prototype.userCurrent = function (objectId) {
return this.makeRequest("GET", "/users/"+objectId, null).body.json();
}
//检查session token是否过期 ?在哪些情况下{ msg: 'ok' }现验证为{ msg: 'fail' }
Bmob.prototype.userSession = function (objectId) {
return this.makeRequest("GET", "/checkSession/"+objectId, null).body.json();
}
//更新用户
Bmob.prototype.userUpdate= function (objectId, sessionToken, data) {
return this.makeRequest("PUT", "/users/"+objectId, data, sessionToken).body.json();
}
//删除用户
Bmob.prototype.userDelete = function (objectId, sessionToken) {
return this.makeRequest("DELETE", "/users/"+objectId, null, sessionToken).body.json();
}
//用户列表
Bmob.prototype.userList = function () {
return this.makeRequest("GET", "/users/", null).body.json();
}
//密码重置
Bmob.prototype.userPasswordReset = function (type, data ,data2 ,sessionToken) {
/*data格式*email {"email":emailAdress} sms {"password": "new password"} oldNew {"oldPassword": "用户的老密码","newPassword": "用户的新密码"} */
var url = ""; var data = data || ""; var data2 = data2 || ""; var method = "PUT"; var sessionToken = sessionToken|| "";
switch (type) {
case "email":
method = "POST";
url = "/requestPasswordReset";
break;
case "sms":
url = "/resetPasswordBySmsCode/"+data2; //smsCode
break;
case "oldNew":
url = "/updateUserPassword/"+data2; //objectId
break;
default:
break;
}
return this.makeRequest(method, url, data, sessionToken).body.json();
}
//Email验证
Bmob.prototype.userEmailVerify = function (email) {
let data = {"email":email};
return this.makeRequest("POST", "/requestEmailVerify/", data, null).body.json();
}
//获取手机验证码
Bmob.prototype.requestSmsCode = function (phoneNum,template) {
let data = {"mobilePhoneNumber": phoneNum,"template": templateName||""}
/*data = {"mobilePhoneNumber": phoneNum,"template": templateName} */
return this.makeRequest("POST", "/requestSmsCode/", data).body.json();
}
/*------------------------第三方帐号用户账户连接----------------*/
//后续增加
/*------------------------数据表------------------------------*/
// 添加数据
Bmob.prototype.createObject = function (className, data) {
return this.makeRequest("POST", "/classes/" + className, data).body.json();
}
//批量增加数据
// https://你的备案域名地址/8/classes/TableName
Bmob.prototype.createObjects = function (className, items) {
//数据样例 className 为表名 items = [{"category": "类别","word": "value1"},{"category": "类别","word": "value2"}]
let data = {"requests": []};
items.forEach(item=> {
let tmp = {
"method": "POST",
"path": "/1/classes/" + className,
"body": item,
};
data.requests.push(tmp);
});
return this.makeRequest("POST", "/batch/", data).body.json();
}
// 查询表数据
Bmob.prototype.getObjects = function (className) {
return this.makeRequest("GET", "/classes/" + className).body.json();
}
// 查询ID数据
Bmob.prototype.getObject = function (className, id) {
return this.makeRequest("GET", "/classes/" + className + "/" + id).body.json();
}
//条件查询
Bmob.prototype.queryObject = function (className, data) {
let url = encodeURI(util.format('/classes/%s/?where=%j', className, data));
return this.makeRequest("GET", url, null).body.json();
}
// 更新数据
Bmob.prototype.updateObject = function (className, objectId, data) {
return this.makeRequest("PUT", "/classes/" + className + "/" + objectId, data).body.json();
}
// 删除数据
Bmob.prototype.deleteObject = function (className, data) {
var id = typeof (data) == "string" ? data : data.objectId;
return this.makeRequest("DELETE", "/classes/" + className + "/" + id).body.json();
}
//BQL查询
Bmob.prototype.BQL = function (bql) {
let url = encodeURI(util.format('/cloudQuery?bql=%s', bql));
return this.makeRequest("GET", url).body.json();
}
return Bmob;
})();
if (!APPID || !REST_ID) {
alert("需要注册Bmob并填入app id和rest id");
app.openUrl("http://doc.bmobapp.com/data/restful/index.html");
exit();
}
//注意:http://open.faasapi.net/ 这个域名仅供国内域名备案未完成前的测试使用,请勿用于违法操作。备案完毕之后,请重新切回使用自己的备案域名。
var bmob = new Bmob("https://api.faasapi.net/1", APPID, REST_ID);
// log(bmob.timestamp()); //时间戳
// log(bmob.userCreate("test1","123456")); //添加用户
// log(bmob.userLogin("test1","123456")); //用户登录
// log(bmob.requestSmsCode("13939999999")); //获取手机验证码
// log(bmob.userMobileCreate("13939999999","000000")); //注册手机用户
// log(bmob.userMobileLogin("13939999999","000000")); //手机用户登录
// log(bmob.userQuery("test1")); //查询用户
// log(bmob.userCurrent('c470e3740d')); //当前用户 objectId
// log(bmob.userUpdate("c470e3740d",'43c59ec540879cee8051110483f9dfb8',{"mobilePhoneNumber":"13939999999"}));
// log(bmob.userSession("c470e3740d")); //用户seesion token
// log(bmob.userDelete("c470e3740d",'43c59ec540879cee8051110483f9dfb8'));//删除用户
// log(bmob.userList()); //用户列表
// log(bmob.userPasswordReset("email",{"email":"[email protected]"})); //密码重置邮箱方式
// log(bmob.userPasswordReset("sms",{"password":"123456"},smsCode)); //密码重置短信方式
// log(bmob.userPasswordReset("oldNew",{"oldPassword": "654321","newPassword": "123456"},'f8b98e11ac','132efc77400780b380f33e94651652d7')); //更改密码
// log(bmob.userEmailVerify("[email protected]"));
// log(bmob.createObject("ClassName",{"category":"category","value":"value"})); //添加数据
// log(bmob.createObjects("ClassName", [{"category": "category","value": "value1"},{"category": "category","value": "value2"}])); //批量添加数据
// log(bmob.getObjects("ClassName")); //查询表数据
// log(bmob.getObject("ClassName",'cd7bc2eefa')); //查询指定objectId数据
// log(bmob.queryObject("ClassName",{"category": "category","value": "value1"})); 条件查询数据
// log(bmob.updateObject("ClassName",'cd7bc2eefa',{"category": 'category2',"value": 'value11'})) 更新数据
// log(bmob.deleteObject("ClassName",'cd7bc2eefa')); //删除指定objectId数据
// log(bmob.BQL("select * from ClassName limit 0,100 order by category")) //BQL查询