1.登录
1.定义结构体
struct user{
uint id;
string name;
string password;
string account;
}
uint256 cIId;
2.定义mapping映射
mapping(uint256 => confirm) public Confirmmapping;
3.编写方法
function register(string memory _name,string memory _password,string memory _confirm,string memory _account) public returns(uint256){
bytes32 str1Hash = keccak256(abi.encode(_password));
bytes32 str2Hash = keccak256(abi.encode(_confirm));
require(str1Hash == str2Hash,"Password inconsistency");
user storage information = Usermapping[Id];
information.name = _name;
information.password = _password;
information.account = _account;
information.id = 0;
cIId += 1;
information.id = cIId;
return cIId;
}
2.注册
1.编写方法
function login(string memory laccount,string memory lpassword) public view returns(string memory){
bytes32 str1Hash = keccak256(abi.encode(laccount));
bytes32 str2Hash = keccak256(abi.encode(lpassword));
for (uint i; i <= Id; i++) {
if(str1Hash == keccak256(abi.encode(Usermapping[i].name))){
require(str2Hash == keccak256(abi.encode(Usermapping[i].password)),"Password wrong!");
return '1';
}
}
return "false";
}
登录成功则会返回1,可以在前端使用web3获得返回值来判断语句来看自己是否登录成功。