diff --git a/Zframework/file.lua b/Zframework/file.lua index 9a167c40..c574c30e 100644 --- a/Zframework/file.lua +++ b/Zframework/file.lua @@ -11,13 +11,15 @@ function FILE.load(name) if s then setfenv(s,{}) return s() + else + LOG.print(name.." "..text.loadError,COLOR.red) end else - local _,res=json.decode(s) - if _ then + local res=json.decode(s) + if res then return res else - LOG.print(name.." "..text.loadError..(mes or"unknown error"),COLOR.red) + LOG.print(name.." "..text.loadError,COLOR.red) end end end @@ -27,10 +29,9 @@ function FILE.save(data,name,mode,luacode) name=name..".dat" local _,mes if not luacode then - _,data=json.encode(data) - if not _ then + res=json.encode(res) + if res then LOG.print(name.." "..text.saveError..(mes or"json error"),"error") - return end else data=dumpTable(data) diff --git a/Zframework/toolfunc.lua b/Zframework/toolfunc.lua index d056a9bc..677d36e3 100644 --- a/Zframework/toolfunc.lua +++ b/Zframework/toolfunc.lua @@ -304,7 +304,12 @@ do--json function json.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 ------------------------------------------------------------------------------- @@ -532,7 +537,12 @@ do--json end function json.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 diff --git a/parts/scenes/load.lua b/parts/scenes/load.lua index 75d4c294..6ac36f84 100644 --- a/parts/scenes/load.lua +++ b/parts/scenes/load.lua @@ -118,19 +118,17 @@ function Tmr.load() VOC.play("welcome_voc") httpRequest(TICK.httpREQ_launch,"/tech/api/v1/app/info") if ACCOUNT.auth_token then - local success,data=json.encode({ + local res=json.encode{ email=ACCOUNT.email, auth_token=ACCOUNT.auth_token, - }) - if not success then - LOG.print(text.jsonError,"warn") - else + } + if res then httpRequest( TICK.httpREQ_autoLogin, "/tech/api/v1/users", "GET", {["Content-Type"]="application/json"}, - data + res ) end end diff --git a/parts/scenes/login.lua b/parts/scenes/login.lua index 7a1cf7d3..ef2a0943 100644 --- a/parts/scenes/login.lua +++ b/parts/scenes/login.lua @@ -7,21 +7,19 @@ function keyDown.login(key) elseif #password==0 then LOG.print(text.noPassword)return end - local success,data=json.encode({ + local res=json.encode{ email=email, password=password, - }) - if not success then - LOG.print(text.jsonError,"warn") - return + } + if res then + httpRequest( + TICK.httpREQ_newLogin, + "/tech/api/v1/users", + "GET", + {["Content-Type"]="application/json"}, + res + ) end - httpRequest( - TICK.httpREQ_newLogin, - "/tech/api/v1/users", - "GET", - {["Content-Type"]="application/json"}, - data - ) elseif key=="escape"then SCN.back() else diff --git a/parts/scenes/register.lua b/parts/scenes/register.lua index 67c03510..d5abd82f 100644 --- a/parts/scenes/register.lua +++ b/parts/scenes/register.lua @@ -13,22 +13,20 @@ function keyDown.register(key) elseif password~=password2 then LOG.print(text.diffPassword)return end - local success,data=json.encode({ + local res=json.encode{ username=username, email=email, password=password, - }) - if not success then - LOG.print(text.jsonError,"warn") - return + } + if res then + httpRequest( + TICK.httpREQ_register, + "/tech/api/v1/users", + "POST", + {["Content-Type"]="application/json"}, + res + ) end - httpRequest( - TICK.httpREQ_register, - "/tech/api/v1/users", - "POST", - {["Content-Type"]="application/json"}, - data - ) elseif key=="escape"then SCN.back() else diff --git a/parts/tick.lua b/parts/tick.lua index 66b9bcf6..1cbcb630 100644 --- a/parts/tick.lua +++ b/parts/tick.lua @@ -89,24 +89,17 @@ end function Tick.httpREQ_launch(data) local response,request_error=client.poll(data.task) if response then - if response.code==200 then - local success,content=json.decode(response.body) - if success then - LOG.print(content.notice,360,COLOR.sky) - if VERSION_CODE==content.version_code then + local res=json.decode(response.body) + if res 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",content.version_name),"warn") + LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn") end else - LOG.print(text.jsonError,"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") + LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") end end return true @@ -123,19 +116,12 @@ end function Tick.httpREQ_register(data) local response,request_error=client.poll(data.task) if response then - if response.code==200 then - local success,content=json.decode(response.body) - if success then - LOG.print(text.registerSuccessed..": "..content.message) + local res=json.decode(response.body) + if res then + if response.code==200 then + LOG.print(text.registerSuccessed..": "..res.message) else - LOG.print(text.jsonError,"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") + LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") end end return true @@ -152,24 +138,16 @@ end function Tick.httpREQ_newLogin(data) local response,request_error=client.poll(data.task) if response then - if response.code==200 then - LOGIN=true - local success,content=json.decode(response.body) - if success then - LOG.print(text.loginSuccessed..": "..content.message) - ACCOUNT.email=_content.email - ACCOUNT.auth_token=content.auth_token + local res=json.decode(response.body) + LOGIN=response.code==200 + if res then + if LOGIN then + LOG.print(text.loginSuccessed..": "..res.message) + ACCOUNT.email=res.email + ACCOUNT.auth_token=res.auth_token FILE.save(ACCOUNT,"account","") else - LOG.print(text.jsonError,"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") + LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") end end return true @@ -188,19 +166,15 @@ function Tick.httpREQ_autoLogin(data) if response then if response.code==200 then LOGIN=true - local success,content=json.decode(response.body) - if success then - LOG.print(text.loginSuccessed..": "..content.message) - else - LOG.print(text.jsonError,"warn") + local res=json.decode(response.body) + if res then + LOG.print(text.loginSuccessed..": "..res.message) 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") + local err=json.decode(response.body) + if err then + LOG.print(text.netErrorCode..response.code..": "..err.message,"warn") end end return true