using LitJson;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class Login
{
public static Dictionary<string ,string > allUserAccount = new Dictionary<string, string>();
public static bool UserRegister(string rename,string repwd,string repwd2)
{
if (rename.Equals(string.Empty)||repwd.Equals(string.Empty))
{
Debug.Log("用户名密码不能为空");
}
else if(allUserAccount.ContainsKey(rename))
{
Debug.Log("用户名已被注册");
}
else if (repwd != repwd2)
{
Debug.Log("两次密码不匹配");
}
else
{
Debug.Log("注册成功!");
allUserAccount.Add(rename, repwd);
Save();
return true;
}
return false;
}
public static bool UserLogin(string rename, string repwd)
{
if (rename.Equals(string.Empty)||repwd.Equals(string.Empty))
{
Debug .Log("用户名密码不能为空");
}
else if (allUserAccount.ContainsKey(rename) ==false )
{
Debug.Log("用户名不存在 ");
}
else if (allUserAccount.ContainsKey(rename))
{
if (repwd==allUserAccount[rename])
{
Debug.Log("登陆成功!");
return true;
}else
{
Debug.Log("密码错误");
}
}
return false;
}
/// <summary>
/// 保存注册信息
/// 路径path、文件信息FileInfo、写入流FileStream、编码byte uif-8、存入byte数组write
/// </summary>
public static void Save()
{
string path =Application.dataPath + "/AppConfig/allUserAccount.txt";
// 获取文件路径信息
// 判断文件是否存在
FileInfo file = new FileInfo(path);
if (file.Exists ==false)
{
Debug.Log("/AppConfig/allUserAccount.txt文件不存在");
return;
}
/// 使用FileStream写入数据 并打开文件FileMode.OpenOrCreate
/// 将string类型字符转换为Json类型写入allUserAccount文件
/// 缓冲,使用utf-8编码模式
/// 将内容fs存入byte的数组中
using (FileStream fs = new FileStream(path,FileMode.OpenOrCreate))
{
string js =JsonMapper.ToJson(allUserAccount);
byte[] bs =System.Text.Encoding.UTF8.GetBytes(js);
fs.Write(bs, 0, bs.Length);
Debug.Log("保存成功!");
}
}
/// <summary>
/// 读取注册信息
/// 路径path、文件信息FileInfo、读取StreamReader、string遍历ReadToEnd、JsonMapper解析返回数据
/// </summary>
public static void Start()
{
string path =Application.dataPath + "/Appconfig/allUserAccount.txt";
FileInfo file = new FileInfo(path);
if (file.Exists ==false )
{
Debug.Log("/Appconfig/allUserAccount.txt文件不存在");
return;
}
// 读取路径文件StreamReaader
// string类型获取文件遍历读取到的内容
// JsonMapper解析后返回数据
StreamReader reader = new StreamReader(path);
string str =reader.ReadToEnd();
allUserAccount =JsonMapper.ToObject<Dictionary<string, string>>(str);
}
}
usingSystem.Collections.Generic;
[System.Serializable]
public class UserAccount : SaveBase
{
publicDictionary<string, string> allUserAccount= new Dictionary<string, string>();
public void Add(string s,string s1)
{
allUserAccount.Add(s, s1);
}
}
public class DataMgr :Singleton<DataMgr>
{
publicUserAccount accounts;
}