再次整理/简化网络请求相关代码

This commit is contained in:
MrZ626
2021-01-31 00:36:07 +08:00
parent 7c4b69753a
commit e170c16d02
11 changed files with 71 additions and 90 deletions

View File

@@ -10,17 +10,15 @@ local function tick_httpREQ_launch(task)
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res.message=="OK"then
if response.code==200 then
LOG.print(res.notice,360,COLOR.sky)
if VERSION_CODE>=res.version_code then
LOG.print(text.versionIsNew,360,COLOR.sky)
else
LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn")
end
if res.message=="OK"and response.code==200 then
LOG.print(res.notice,360,COLOR.sky)
if VERSION_CODE>=res.version_code then
LOG.print(text.versionIsNew,360,COLOR.sky)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn")
end
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then
@@ -40,20 +38,13 @@ local function tick_httpREQ_autoLogin(task)
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
local res=json.decode(response.body)
if res.message=="OK"then
LOGIN=true
LOG.print(text.loginSuccessed)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
local res=json.decode(response.body)
if response.code==200 and res.message=="OK"then
LOGIN=true
LOG.print(text.loginSuccessed)
else
LOGIN=false
local err=json.decode(response.body)
if err then
LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn")
end
LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn")
end
return
elseif request_error then

View File

@@ -4,32 +4,30 @@ local function tick_httpREQ_newLogin(task)
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
local res=json.decode(response.body)
if res.message=="OK"then
LOGIN=true
USER.email=res.email
USER.auth_token=res.auth_token
USER.name=res.name
USER.id=res.id
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
local res=json.decode(response.body)
if response.code==200 and res.message=="OK"then
LOGIN=true
USER.email=res.email
USER.auth_token=res.auth_token
USER.name=res.name
USER.id=res.id
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
httpRequest(
TICK_httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
auth_token=USER.auth_token,
}
)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
httpRequest(
TICK_httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
auth_token=USER.auth_token,
}
)
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then

View File

@@ -1,6 +1,6 @@
local gc=love.graphics
local function tick_httpREQ_checkAccessToken(task)
local function tick_httpREQ_manualAutoLogin(task)
local time=0
while true do
coroutine.yield()
@@ -23,7 +23,7 @@ local function tick_httpREQ_checkAccessToken(task)
else
local err=json.decode(response.body)
if err then
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
LOG.print(text.httpCode..response.code..": "..err.message,"warn")
end
end
return
@@ -111,7 +111,7 @@ scene.widgetList={
if LOGIN then
if USER.access_token then
httpRequest(
tick_httpREQ_checkAccessToken,
tick_httpREQ_manualAutoLogin,
PATH.api..PATH.access,
"GET",
{["Content-Type"]="application/json"},

View File

@@ -11,13 +11,11 @@ local function task_fetchRooms(task)
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
local res=json.decode(response.body)
if res.message=="OK"then
rooms=res.room_list
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
local res=json.decode(response.body)
if response.code==200 and res.message=="OK"then
rooms=res.room_list
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then

View File

@@ -4,19 +4,17 @@ local function tick_httpREQ_register(task)
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
local res=json.decode(response.body)
if res.message=="OK"then
LOGIN=true
USER.name=res.name
USER.id=res.id
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user","q")
LOG.print(text.registerSuccessed..": "..res.message)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
local res=json.decode(response.body)
if response.code==200 and res.message=="OK"then
LOGIN=true
USER.name=res.name
USER.id=res.id
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user","q")
LOG.print(text.registerSuccessed..": "..res.message)
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then