使用lua Cjson实现登陆注册(三)

博主在上上篇博客中写了登陆注册,但只是用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		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铸梦xy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值