博主在上上篇博客中写了登陆注册,但只是用Io流存储到本地文件,那么这次就实现把账户对象的表转成Cjson字符串储存到本地,贴出源码:
注册
--注册按钮
function RegisterCtrl.SureRegisterButton(obj)
--所有的用户信息
local AllUserDataList={}
--是否有相同账号
local boolean isoverFind=false
if(RegisterPanel.RegAccountInputField:GetComponent("Text").text=="" or RegisterPanel.RegPsaaWordInputField:GetComponent("Text").text=="")
then
RegisterPanel.text:GetComponent("Text").text="账号密码不能为空!"
else
local datafile=io.open("D:\\Alluserdata.txt","w+")
local userdataJsonString=datafile:read("*a")
datafile.close()
--解析json
if(#userdataJsonString>0)then
--把JSon转成对象列表
local userdataList= cjson.decode(userdataJsonString)
for k,v in pairs(userdataList) do
--把上次用户的信息写进表里(为了以个JSON字符串储存所有用户信息)
table.insert(AllUserDataList,userdataList[k])
--如果账号相同
if(RegisterPanel.RegAccountInputField:GetComponent("Text").text==userdataList[k].Account)then
--有相同用户名 注册失败
isoverFind=false
break
else
isoverFind=true
end
end
end
if(isoverFind==true or #userdataJsonString==0) then--注册成功
--注册成功 开始记录账号密码
currentUserData={Account=RegisterPanel.RegAccountInputField:GetComponent("Text").text,PassWord=RegisterPanel.RegPsaaWordInputField:GetComponent("Text").text}
--把对象写入列表
table.insert(AllUserDataList,currentUserData)
--把储存所有用户信息的表 转成Json字符串 等待其他地方解析使用
AlluserCjsonString=cjson.encode(AllUserDataList)
--把JSON字符串以io形式储存到本地
local alldatafile=io.open("D:\\Alluserdata.txt","w")
--把json字符串写入文件
alldatafile:write(AlluserCjsonString)
--关闭文件
alldatafile:close()
RegisterPanel.text:GetComponent("Text").text="注册成功!"
else
RegisterPanel.text:GetComponent("Text").text="注册失败!"
end
end
end
登陆
function LoginCtrl.ButtonClick(obj)
--账户是否正确
local boolean Accountifcorrectbool=false
if(LoginPanel.AccountInputFild:GetComponent("Text").text=="" or LoginPanel.PassWordInputFild:GetComponent("Text").text=="")
then
LoginPanel.text:GetComponent("Text").text="账号密码不能为空!"
else
--打开本地文件
local AlluserDatafile =io.open("D:\\Alluserdata.txt","a+");
--读取json字符串
local alluserdataString =AlluserDatafile:read("*a")
AlluserDatafile:close()
--如果字符串不为空
if(#alluserdataString>0)then
--把储存账号密码的json字符串表转成对象
local alluserdatalist= cjson.decode(alluserdataString)
logWarn("JSon="..alluserdataString)
for k,v in pairs(alluserdatalist) do
--如果账号密码都正确
if(LoginPanel.AccountInputFild:GetComponent("Text").text==alluserdatalist[k].Account and LoginPanel.PassWordInputFild:GetComponent("Text").text==alluserdatalist[k].PassWord)
then
logWarn("登陆成功!")
--准备跳转界面
Tootil.LogionPnaelGb:SetActive(false)
Tootil.GamePanelGb:SetActive(true)
else
LoginPanel.text:GetComponent("Text").text="该账号或密码有误!"
end
end
else
LoginPanel.text:GetComponent("Text").text="请前往注册账号!"
end
end
end