json库使用方式调整
This commit is contained in:
@@ -11,13 +11,15 @@ function FILE.load(name)
|
|||||||
if s then
|
if s then
|
||||||
setfenv(s,{})
|
setfenv(s,{})
|
||||||
return s()
|
return s()
|
||||||
|
else
|
||||||
|
LOG.print(name.." "..text.loadError,COLOR.red)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local _,res=json.decode(s)
|
local res=json.decode(s)
|
||||||
if _ then
|
if res then
|
||||||
return res
|
return res
|
||||||
else
|
else
|
||||||
LOG.print(name.." "..text.loadError..(mes or"unknown error"),COLOR.red)
|
LOG.print(name.." "..text.loadError,COLOR.red)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -27,10 +29,9 @@ function FILE.save(data,name,mode,luacode)
|
|||||||
name=name..".dat"
|
name=name..".dat"
|
||||||
local _,mes
|
local _,mes
|
||||||
if not luacode then
|
if not luacode then
|
||||||
_,data=json.encode(data)
|
res=json.encode(res)
|
||||||
if not _ then
|
if res then
|
||||||
LOG.print(name.." "..text.saveError..(mes or"json error"),"error")
|
LOG.print(name.." "..text.saveError..(mes or"json error"),"error")
|
||||||
return
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
data=dumpTable(data)
|
data=dumpTable(data)
|
||||||
|
|||||||
@@ -304,7 +304,12 @@ do--json
|
|||||||
|
|
||||||
function json.encode(val)
|
function json.encode(val)
|
||||||
local a,b=pcall(encode,val)
|
local a,b=pcall(encode,val)
|
||||||
if a then return b else return""end
|
if a then
|
||||||
|
return b
|
||||||
|
else
|
||||||
|
LOG.print(text.jsonError,"warn")
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
@@ -532,7 +537,12 @@ do--json
|
|||||||
end
|
end
|
||||||
function json.decode(str)
|
function json.decode(str)
|
||||||
local a,b=pcall(decode,str)
|
local a,b=pcall(decode,str)
|
||||||
if a then return b else return""end
|
if a then
|
||||||
|
return b
|
||||||
|
else
|
||||||
|
LOG.print(text.jsonError,"warn")
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -118,19 +118,17 @@ function Tmr.load()
|
|||||||
VOC.play("welcome_voc")
|
VOC.play("welcome_voc")
|
||||||
httpRequest(TICK.httpREQ_launch,"/tech/api/v1/app/info")
|
httpRequest(TICK.httpREQ_launch,"/tech/api/v1/app/info")
|
||||||
if ACCOUNT.auth_token then
|
if ACCOUNT.auth_token then
|
||||||
local success,data=json.encode({
|
local res=json.encode{
|
||||||
email=ACCOUNT.email,
|
email=ACCOUNT.email,
|
||||||
auth_token=ACCOUNT.auth_token,
|
auth_token=ACCOUNT.auth_token,
|
||||||
})
|
}
|
||||||
if not success then
|
if res then
|
||||||
LOG.print(text.jsonError,"warn")
|
|
||||||
else
|
|
||||||
httpRequest(
|
httpRequest(
|
||||||
TICK.httpREQ_autoLogin,
|
TICK.httpREQ_autoLogin,
|
||||||
"/tech/api/v1/users",
|
"/tech/api/v1/users",
|
||||||
"GET",
|
"GET",
|
||||||
{["Content-Type"]="application/json"},
|
{["Content-Type"]="application/json"},
|
||||||
data
|
res
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,21 +7,19 @@ function keyDown.login(key)
|
|||||||
elseif #password==0 then
|
elseif #password==0 then
|
||||||
LOG.print(text.noPassword)return
|
LOG.print(text.noPassword)return
|
||||||
end
|
end
|
||||||
local success,data=json.encode({
|
local res=json.encode{
|
||||||
email=email,
|
email=email,
|
||||||
password=password,
|
password=password,
|
||||||
})
|
}
|
||||||
if not success then
|
if res then
|
||||||
LOG.print(text.jsonError,"warn")
|
httpRequest(
|
||||||
return
|
TICK.httpREQ_newLogin,
|
||||||
|
"/tech/api/v1/users",
|
||||||
|
"GET",
|
||||||
|
{["Content-Type"]="application/json"},
|
||||||
|
res
|
||||||
|
)
|
||||||
end
|
end
|
||||||
httpRequest(
|
|
||||||
TICK.httpREQ_newLogin,
|
|
||||||
"/tech/api/v1/users",
|
|
||||||
"GET",
|
|
||||||
{["Content-Type"]="application/json"},
|
|
||||||
data
|
|
||||||
)
|
|
||||||
elseif key=="escape"then
|
elseif key=="escape"then
|
||||||
SCN.back()
|
SCN.back()
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -13,22 +13,20 @@ function keyDown.register(key)
|
|||||||
elseif password~=password2 then
|
elseif password~=password2 then
|
||||||
LOG.print(text.diffPassword)return
|
LOG.print(text.diffPassword)return
|
||||||
end
|
end
|
||||||
local success,data=json.encode({
|
local res=json.encode{
|
||||||
username=username,
|
username=username,
|
||||||
email=email,
|
email=email,
|
||||||
password=password,
|
password=password,
|
||||||
})
|
}
|
||||||
if not success then
|
if res then
|
||||||
LOG.print(text.jsonError,"warn")
|
httpRequest(
|
||||||
return
|
TICK.httpREQ_register,
|
||||||
|
"/tech/api/v1/users",
|
||||||
|
"POST",
|
||||||
|
{["Content-Type"]="application/json"},
|
||||||
|
res
|
||||||
|
)
|
||||||
end
|
end
|
||||||
httpRequest(
|
|
||||||
TICK.httpREQ_register,
|
|
||||||
"/tech/api/v1/users",
|
|
||||||
"POST",
|
|
||||||
{["Content-Type"]="application/json"},
|
|
||||||
data
|
|
||||||
)
|
|
||||||
elseif key=="escape"then
|
elseif key=="escape"then
|
||||||
SCN.back()
|
SCN.back()
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -89,24 +89,17 @@ end
|
|||||||
function Tick.httpREQ_launch(data)
|
function Tick.httpREQ_launch(data)
|
||||||
local response,request_error=client.poll(data.task)
|
local response,request_error=client.poll(data.task)
|
||||||
if response then
|
if response then
|
||||||
if response.code==200 then
|
local res=json.decode(response.body)
|
||||||
local success,content=json.decode(response.body)
|
if res then
|
||||||
if success then
|
if response.code==200 then
|
||||||
LOG.print(content.notice,360,COLOR.sky)
|
LOG.print(res.notice,360,COLOR.sky)
|
||||||
if VERSION_CODE==content.version_code then
|
if VERSION_CODE==res.version_code then
|
||||||
LOG.print(text.versionIsNew,360,COLOR.sky)
|
LOG.print(text.versionIsNew,360,COLOR.sky)
|
||||||
else
|
else
|
||||||
LOG.print(string.gsub(text.versionIsOld,"$1",content.version_name),"warn")
|
LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
LOG.print(text.jsonError,"warn")
|
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
|
||||||
end
|
|
||||||
else
|
|
||||||
local success,content=json.decode(response.body)
|
|
||||||
if success then
|
|
||||||
LOG.print(text.netErrorCode..response.code..": "..content.message,"warn")
|
|
||||||
else
|
|
||||||
LOG.print(text.netErrorCode..response.code,"warn")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
@@ -123,19 +116,12 @@ end
|
|||||||
function Tick.httpREQ_register(data)
|
function Tick.httpREQ_register(data)
|
||||||
local response,request_error=client.poll(data.task)
|
local response,request_error=client.poll(data.task)
|
||||||
if response then
|
if response then
|
||||||
if response.code==200 then
|
local res=json.decode(response.body)
|
||||||
local success,content=json.decode(response.body)
|
if res then
|
||||||
if success then
|
if response.code==200 then
|
||||||
LOG.print(text.registerSuccessed..": "..content.message)
|
LOG.print(text.registerSuccessed..": "..res.message)
|
||||||
else
|
else
|
||||||
LOG.print(text.jsonError,"warn")
|
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
|
||||||
end
|
|
||||||
else
|
|
||||||
local success,content=json.decode(response.body)
|
|
||||||
if success then
|
|
||||||
LOG.print(text.netErrorCode..response.code..": "..content.message,"warn")
|
|
||||||
else
|
|
||||||
LOG.print(text.netErrorCode..response.code,"warn")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
@@ -152,24 +138,16 @@ end
|
|||||||
function Tick.httpREQ_newLogin(data)
|
function Tick.httpREQ_newLogin(data)
|
||||||
local response,request_error=client.poll(data.task)
|
local response,request_error=client.poll(data.task)
|
||||||
if response then
|
if response then
|
||||||
if response.code==200 then
|
local res=json.decode(response.body)
|
||||||
LOGIN=true
|
LOGIN=response.code==200
|
||||||
local success,content=json.decode(response.body)
|
if res then
|
||||||
if success then
|
if LOGIN then
|
||||||
LOG.print(text.loginSuccessed..": "..content.message)
|
LOG.print(text.loginSuccessed..": "..res.message)
|
||||||
ACCOUNT.email=_content.email
|
ACCOUNT.email=res.email
|
||||||
ACCOUNT.auth_token=content.auth_token
|
ACCOUNT.auth_token=res.auth_token
|
||||||
FILE.save(ACCOUNT,"account","")
|
FILE.save(ACCOUNT,"account","")
|
||||||
else
|
else
|
||||||
LOG.print(text.jsonError,"warn")
|
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
|
||||||
end
|
|
||||||
else
|
|
||||||
LOGIN=false
|
|
||||||
local success,content=json.decode(response.body)
|
|
||||||
if success then
|
|
||||||
LOG.print(text.netErrorCode..response.code..": "..content.message,"warn")
|
|
||||||
else
|
|
||||||
LOG.print(text.netErrorCode..response.code,"warn")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
@@ -188,19 +166,15 @@ function Tick.httpREQ_autoLogin(data)
|
|||||||
if response then
|
if response then
|
||||||
if response.code==200 then
|
if response.code==200 then
|
||||||
LOGIN=true
|
LOGIN=true
|
||||||
local success,content=json.decode(response.body)
|
local res=json.decode(response.body)
|
||||||
if success then
|
if res then
|
||||||
LOG.print(text.loginSuccessed..": "..content.message)
|
LOG.print(text.loginSuccessed..": "..res.message)
|
||||||
else
|
|
||||||
LOG.print(text.jsonError,"warn")
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
LOGIN=false
|
LOGIN=false
|
||||||
local success,content=json.decode(response.body)
|
local err=json.decode(response.body)
|
||||||
if success then
|
if err then
|
||||||
LOG.print(text.netErrorCode..response.code..": "..content.message,"warn")
|
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
|
||||||
else
|
|
||||||
LOG.print(text.netErrorCode..response.code,"warn")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
|
|||||||
Reference in New Issue
Block a user