2020年12月27日 升级代码,修复一些bug。
增加了2个方法。
pragma solidity >=0.5.0 <0.7.0;
contract certificate {
struct student{ //建立学员结构体
uint number;
}
mapping(address => student)students; //地址到学员的映射
struct IssuingAuthority{ //建立制证机构结构体
uint number; //已发证书的数量
uint permit ;//是否有权限制证
}
mapping(address => IssuingAuthority)IssuingAuthoritys; //地址到机构的映射
struct Certificate {
string student; //持证人
address addrid; //持证人钱包地址
string department;//发证人
string content; //证书内容
uint time; //发证时间
}
uint number = 1; //证书编号
mapping (uint => Certificate) Certificates;//编号是与证书一一对应的关系
address admin = 0x1D28AD27E51ccc304639F92a96E6332FF9B6cCa7;
modifier adminOnly(){ //只有管理员才能进行一些操作。
if(msg.sender != admin) revert();
else _;
}
function inIssuingAuthority(address _addr) public adminOnly{
//require(IssuingAuthoritys[_addr].number > 1,"The IssuingAuthority has been registered!");
IssuingAuthoritys[_addr].permit = 1; //管理员给机构赋予制证权限
IssuingAuthoritys[_addr].number = 0;//初始发证数为0个
}
function inputcer(address _id,string memory _student,string memory _content, string memory _department, uint _time)
public {
require (IssuingAuthoritys[msg.sender].permit == 1); //确定制证机构有权限
require(students[_id].number < 1);
Certificates[number].student = _student; // 学员姓名
Certificates[number].addrid = _id; //学员地址
Certificates[number].department =_department; //机构名称
Certificates[number].content = _content; //证书内容
Certificates[number].time = _time; //证书时间
students[_id].number= number; //把证书编号映射给学员
IssuingAuthoritys[msg.sender].number++;
number++;
}
function studentIndexes(address _studentid)public view returns(uint){
return students[_studentid].number;
}
//输入地址可以查询到证书编号
//输入证书编号,输出该编号对应的证书
function outputceri(uint _f)public view returns(string memory,address,string memory,string memory,uint){
return (Certificates[_f].student,Certificates[_f].addrid,Certificates[_f].department,Certificates[_f].content,Certificates[_f].time);
}
function getnum()public view returns(uint,uint)//输出下一个证书编号,输出当前机构制证数量。
{
return(number,IssuingAuthoritys[msg.sender].number);
}
function getIssuingAuthority ()public view returns (uint)//增加方法,判断操作地址权限,返回“1”可以制证,返回“0”不可以制证
{
return(IssuingAuthoritys[msg.sender].permit);
}
}