Angular 统一社会信用代码生成校验

地址

项目代码 https://files-cdn.cnblogs.com/files/linyisonger/webtoolbox.zip

展示效果

介绍

使用angular前端框架,采用 github actions技术,将项目打包成docker镜像,再连接自己的服务器更新镜像与容器,达到持续集成的效果。

主要代码

  • 行政区划码
    • 读取
  • 统一社会信用代码
    • 生成
    • 校验
行政区划码读取

行政区划代码 http://www.mca.gov.cn/article/sj/xzqh/2020/

2020年11月中华人民共和国县以上行政区划代码 http://www.mca.gov.cn/article/sj/xzqh/2020/202101/20210100031547.shtml

使用正则匹配读取静态html文件内容,插入到数组中去,方便之后维护。

regin.service.ts

export class ReginService {
  private _regins: IReginModel[] = []
  public get regins() {
    return this._regins;
  }
  constructor(private http: HttpClient) {
    this.initialization();
  }
  // 初始化
  async initialization() {
    const html = await this.http.get("/assets/202101041104.html", { responseType: "text" }).toPromise();
    const trs = html.match(/<tr height=19 style='mso-height-source:userset;height:14.25pt'>[\s\S]*?<\/tr>/g)
    if (trs instanceof Array) {
      for (const tr of trs) {
        const tds = tr.match(/<td.*>([\s\S]*?)<\/td>/g).join('');
        const name = tds.match(/[\u4e00-\u9fa5]+/)?.[0];
        const code = tds.match(/<td class=.*>([0-9]+)<\/td>/)?.[1]
        this._regins.push({ code, name })
      }
    }
  }
}
统一社会信用代码

中华人民共和国国家标准GB32100—2015http://c.gb688.cn/bzgk/gb/showGb?type=online&hcno=24691C25985C1073D3A7C85629378AC0

根据GB32100—2015标准生成以及校验(旧的统一社会信用代码,可能会校验失败)

unified-identifier.service.ts

export class UnifiedIdentifierService {
  // 登记管理部门代码标识  
  public registrationManagementDepartmentCode: IRegistrationManagementDepartmentCodeModel[] = [...]
  // 机构类别
  public institutionsCategory: IInstitutionsCategoryModel[] = [...]
  // 主体标识码
  public subjectIdentificationCode: ISubjectIdentificationCodeModel[] = [...]
  // 代码字符集对应数
  public codeCharacterSetNumber: ICodeCharacterSetNumberModel[] = [...]
  constructor(public reginService: ReginService) { }
  // 生成
  public async generate(institutionsCategory: IInstitutionsCategoryModel[] = this.institutionsCategory, regins: IReginModel[] = this.reginService.regins) {
    if (institutionsCategory?.length == 0) return "";
    if (regins?.length == 0) return "";
    // 登记管理部门代码1位 机构类别代码1位
    const ic = institutionsCategory[this.radomNumber(institutionsCategory.length)];
    // 登记管理机关行政区划码6位
    const rc = regins[this.radomNumber(regins.length)];
    // 主体标识码(组织机构代码)9位
    const sic: ISubjectIdentificationCodeModel[] = [];
    for (let i = 0; i < 9; i++) {
      sic.push(this.subjectIdentificationCode[this.radomNumber(this.subjectIdentificationCode.length)])
    }
    const code = ic.parentCode + ic.code + rc.code + sic.map(c => c.code).join("")
    return code + this.checkCode(code);
  }
  // 验证
  public async verification(code: string) {
    if (code.length != 18) return { message: "长度不正确", isSuccess: false };
    if (!/[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/g.test(code)) return { message: "规范不正确", isSuccess: false };
    if (this.checkCode(code.substring(0, 17)) != code.substring(17, 18)) return { message: "验证码错误", isSuccess: false };
    return { message: "验证成功", isSuccess: true };;
  }
  // 随机0~max的整数
  private radomNumber(max: number) {
    return Math.floor((Math.random() * max))
  }
  // 校验码 
  public checkCode(code: string) {
    // 积和 
    let ps = code.split("").map((char, i) => {
      // 字符数
      let cn = this.codeCharacterSetNumber.find(c => c.code == char).num;
      // 权重因子
      let wn = Math.pow(3, i) % 31;
      return cn * wn;
    }).reduce((prev, curr) => prev + curr);
    return this.codeCharacterSetNumber.find(c => c.num == (31 - ps % 31))?.code || 0;
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林一怂儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值