解决并发问题
const baseURL = "https://api2.bmob.cn/1/classes/data/", headers = {
"X-Bmob-Application-Id": "***",
"X-Bmob-REST-API-Key": "***",
"Content-Type": "application/json"
}
function add(data) {
log({
account: data.account,
password: data.password,
token: data.token,
state: data.state || false
})
return !(res = getA(data.account)) ?
http.postJson(baseURL, {
account: data.account,
password: data.password,
token: data.token,
state: data.state || false
}, {
headers: headers
}).body.json() : res[0].objectId
}
function getA(account) {
let data = get('{ "account": "' + account + '"}')
return data.length > 0 ? data : false
}
function getT() {
state = false
if ((data = get('{ "state": ' + state + ' }')).length > 0) {
if (state || !getS(data[0].objectId)) {
if (DELETE(data[0].objectId)) {
data[0].state = !state
log(data[0].token)
add(data[0])
} else {
sleep(rand = random(0, 200))
getT()
}
}
} else {
toastLog("无账号")
}
}
function getS(objectId) {
return http.get(baseURL + objectId, {
headers: headers
}).body.json().state
}
function get(where) {
return http.get(baseURL + '?where=' + where, {
headers: headers
}).body.json().results
}
function DELETE(objectId) {
return http.request(baseURL + objectId, {
method: "DELETE",
headers: headers,
}).body.json();
}
function update(objectId, body) {
return http.request(baseURL + objectId, {
method: "PUT",
headers: headers,
body: body
}).body.json();
}