longyvfengyun
2023-04-12 ef4f300d8fe1843ad73da14ae421148eaab9a442
src/App.vue
@@ -1,28 +1,431 @@
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
    <div class="card-container">
      <el-card>
        <div slot="header" class="clearfix">
          <span>UKey信息</span>
          <el-button @click="getSM2PubKey" style="float: right; padding: 3px 0" type="text">刷新</el-button>
        </div>
        <div class="card-content">
          <div class="card-item">
            <el-input placeholder="请插入ukey,获取uKey的ID" v-model="uKey.id" :disabled="true">
              <template slot="prepend">UKey Id</template>
            </el-input>
          </div>
          <div class="card-item">
            <el-input placeholder="请输入内容" v-model="uKey.x">
              <template slot="prepend">PublicX</template>
            </el-input>
          </div>
          <div class="card-item">
            <el-input placeholder="请输入内容" v-model="uKey.y">
              <template slot="prepend">PublicY</template>
            </el-input>
          </div>
        </div>
          <div class="card-footer">
<!--              <el-button @click="starGenKeyPair" type="primary">更新为<span class="f-weight">(平台)</span>密钥对</el-button>-->
<!--              <el-button @click="updateDev" type="primary">更新为<span class="f-weight">(设备)</span>密钥对</el-button>-->
<!--              <el-button @click="showOwnDialog" type="primary">自定义密钥对</el-button>-->
              <el-button type="primary" icon="el-icon-edit" @click="setOwnKey">写入密钥</el-button>
              <el-button type="success" @click="getSM2PubKey" icon="el-icon-refresh">刷新</el-button>
          </div>
      </el-card>
    </div>
      <el-dialog
        title="自定义密钥对"
        width="auto"
        :visible.sync="ownKeyDialog"
        :close-on-click-modal="false"
        top="0"
        class="dialog-center"
        :modal-append-to-body="false">
        <div class="params-container">
            <div class="card-content">
                <div class="card-item">
                    <el-input placeholder="请输入内容" v-model="ownKey.pId">
                        <template slot="prepend"> 私 钥 </template>
                    </el-input>
                </div>
                <div class="card-item">
                    <el-input placeholder="请输入内容" v-model="ownKey.name">
                        <template slot="prepend">用户身份</template>
                    </el-input>
                </div>
                <div class="card-item">
                    <el-input placeholder="请输入内容" v-model="ownKey.x">
                        <template slot="prepend">PublicX</template>
                    </el-input>
                </div>
                <div class="card-item">
                    <el-input placeholder="请输入内容" v-model="ownKey.y">
                        <template slot="prepend">PublicY</template>
                    </el-input>
                </div>
            </div>
            <div class="dialog-footer">
                <el-button type="primary" @click="setOwnKey">确定</el-button>
                <el-button type="danger" @click="closeOwnDialog">取消</el-button>
            </div>
        </div>
      </el-dialog>
  </div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import SoftKey3W from "./assets/js/Syunew3";
export default {
  name: 'App',
  components: {
    HelloWorld
  data() {
    return {
      isIn: false,
      bConnect: 0,
      uKey: {
        id: "",
        x: "",
        y: ""
      },
      ownKey: {
        pId: "",
        x: "",
        y: "",
        name: "",
      },
      ownKeyDialog: false,
    };
  },
  methods: {
    uKeyLoad() {
      //如果是IE10及以下浏览器,则跳过不处理
      if(navigator.userAgent.indexOf("MSIE")>0 && !navigator.userAgent.indexOf("opera") > -1) return false;
      try {
        let _this = this;
        let s_pnp=new SoftKey3W();//创建UK类
        _this.isIn = false;
        s_pnp.Socket_UK.onopen = function() {
          _this.bConnect = 1;   //代表已经连接,用于判断是否安装了客户端服务
          _this.initUKey();
        };
        //在使用事件插拨时,注意,一定不要关掉Sockey,否则无法监测事件插拨
        s_pnp.Socket_UK.onmessage =function got_packet(Msg) {
          let PnpData = JSON.parse(Msg.data);
          if(PnpData.type==="PnpEvent")//如果是插拨事件处理消息
          {
            if(PnpData.IsIn){
              _this.isIn = true;
              _this.bConnect = 1;
              _this.initUKey();
              _this.getSM2PubKey();
            }else{
              _this.isIn = false;
              _this.bConnect = 0;
              _this.initUKey();
            }
          }
        }
      }catch (e) {
        alert(e.name + ": " + e.message);
        return false;
      }
    },
    initUKey(){
      this.uKey.id = "";
      this.uKey.x = "";
      this.uKey.y = "";
    },
    getSM2PubKey() {
      let _this = this;
      if(this.bConnect === 0) {
        window.alert("未能连接服务程序,请确定服务程序是否安装。");
        return false;
      }
      try {
        let DevicePath;
        let s_pnp=new SoftKey3W();//创建UK类
        s_pnp.Socket_UK.onopen = function() {
          s_pnp.ResetOrder();
        };
        s_pnp.Socket_UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          if(UK_Data.type!=="Process") return false;//如果不是流程处理消息,则跳过
          switch(UK_Data.order) {
            case 0:
              s_pnp.FindPort(0);
              break;
            case 1:
              if(UK_Data.LastError!==0){
                window.alert ("未发现加密锁,请插入加密锁");
                s_pnp.Socket_UK.close();
                return false;
              }
              DevicePath = UK_Data.return_value;
              s_pnp.GetChipID(DevicePath)
              break;
            case 2:
              if(UK_Data.LastError!==0){
                window.alert ("未发现加密锁,请插入加密锁");
                s_pnp.Socket_UK.close();
                return false;
              }
              _this.uKey.id = UK_Data.return_value;
              s_pnp.GetPubKeyX(DevicePath);
              break;
            case 3:
              if(UK_Data.LastError!==0){
                window.alert ("获取用公钥X失败");
                s_pnp.Socket_UK.close();
                return false;
              }
              _this.uKey.x = UK_Data.return_value;
              s_pnp.GetPubKeyY(DevicePath);
              break;
            case 4:
              if(UK_Data.LastError!==0){
                window.alert ("获取用公钥Y失败");
                s_pnp.Socket_UK.close();
                return false;
              }
              _this.uKey.y = UK_Data.return_value;
              s_pnp.Socket_UK.close()
              break;
          }
        }
      }catch (e) {
        console.log(e);
      }
    },
    starGenKeyPair() {
      let _this = this;
      if(this.bConnect === 0) {
        window.alert("未能连接服务程序,请确定服务程序是否安装。");
        return false;
      }
      try {
        let DevicePath, PrivateKey, PublicX, PublicY;
        let s_pnp=new SoftKey3W();//创建UK类
        s_pnp.Socket_UK.onopen = function() {
          s_pnp.ResetOrder()
        }
        s_pnp.Socket_UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          if(UK_Data.type!=="Process") return false;//如果不是流程处理消息,则跳过
          switch(UK_Data.order) {
            case 0:
              s_pnp.FindPort(0);
              break;
            case 1:
              if(UK_Data.LastError!==0){
                window.alert ("未发现加密锁,请插入加密锁");
                s_pnp.Socket_UK.close();
                return false;
              }
              DevicePath = UK_Data.return_value;
              s_pnp.StarGenKeyPair(DevicePath);
              break;
            case 2:
              if(UK_Data.LastError!==0){
                window.alert ("产生密钥对出现错误,错误码为:"+UK_Data.LastError.toString());
                s_pnp.Socket_UK.close();
                return false;
              }
              s_pnp.GenPriKey();
              break;
            case 3:
              if(UK_Data.LastError!==0){
                window.alert ("获取生成的私钥时错误,错误码为:"+UK_Data.LastError.toString());
                s_pnp.Socket_UK.close();
                return false;
              }
              PrivateKey = UK_Data.return_value;
              s_pnp.GenPubKeyX();
              break;
            case 4:
              if(UK_Data.LastError!==0){
                window.alert ("获取生成的公钥X时错误,错误码为:"+UK_Data.LastError.toString());
                s_pnp.Socket_UK.close();
                return false;
              }
              PublicX = UK_Data.return_value;
              s_pnp.GenPubKeyY();
              break;
            case 5:
              if(UK_Data.LastError!==0){
                window.alert ("获取生成的公钥Y时错误,错误码为:"+UK_Data.LastError.toString());
                s_pnp.Socket_UK.close();
                return false;
              }
              PublicY = UK_Data.return_value;
              var name = "武汉源畅科技有限公司";
              s_pnp.Set_SM2_KeyPair(PrivateKey, PublicX, PublicY, name, DevicePath);
              break;
            case 6:
              if(UK_Data.LastError!==0){
                window.alert ("设置密钥对及用户身份时出现错误");
                s_pnp.Socket_UK.close();
                return false;
              }
              _this.getSM2PubKey();
              s_pnp.Socket_UK.close();
              break;
          }
        }
      }catch (e) {
        console.log(e);
      }
    },
    updateDev() {
      let _this = this;
      if(this.bConnect === 0) {
        window.alert("未能连接服务程序,请确定服务程序是否安装。");
        return false;
      }
      try {
        let DevicePath;
        let s_pnp=new SoftKey3W();//创建UK类
        s_pnp.Socket_UK.onopen = function() {
          s_pnp.ResetOrder()
        }
        s_pnp.Socket_UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          if(UK_Data.type!=="Process") return false;//如果不是流程处理消息,则跳过
          switch(UK_Data.order) {
            case 0:
              s_pnp.FindPort(0);
              break;
            case 1:
              if(UK_Data.LastError!==0){
                window.alert ("未发现加密锁,请插入加密锁");
                s_pnp.Socket_UK.close();
                return false;
              }
              DevicePath = UK_Data.return_value;
              var oldTime = new Date("2023-3-3 00:00:00").getTime();
              var nowTime = (new Date()).getTime();
              var PrivateKey = "A3A96FD399FB5CD5761D1624571E4158FC8D6E896A7754D62E3EA47DC766BF9B";
              var PublicX = "129BDA6338262E3914704DB177955436A2183284CFAA69DAEB7432D591D0CF62";
              var PublicY = "EC2FC8397C31530159EF4F9223DA40D4039203AA1D49AEECDC9366973CF7AE20";
              if(nowTime<oldTime) {
                PrivateKey = "C1B18CCB03B1DEEDEE9DEDB95C456F55CA24CD0C478ACCFBE57371BA4A7E2883";
                PublicX = "FE7C0F1805BA9F263BCE6EB198DE02B52C6706AF5C9FBC24C8B0E19545B01595";
                PublicY = "6471C138A584BD6EEAFE1FCD7628531A7DE8E49FFD205857AE1F82D84016F3F3";
              }
              s_pnp.Set_SM2_KeyPair(PrivateKey, PublicX, PublicY, "123", DevicePath);
              break;
            case 2:
              if(UK_Data.LastError!==0){
                window.alert ("设置密钥对及用户身份时出现错误");
                s_pnp.Socket_UK.close();
                return false;
              }
              _this.getSM2PubKey();
              s_pnp.Socket_UK.close();
              break
          }
        }
      }catch(e) {
        console.log(e);
      }
    },
    showOwnDialog() {
      this.ownKeyDialog = true;
    },
    setOwnKey() {
      let _this = this;
      if(this.bConnect === 0) {
        window.alert("未能连接服务程序,请确定服务程序是否安装。");
        return false;
      }
      try {
        let DevicePath;
        let s_pnp=new SoftKey3W();//创建UK类
        s_pnp.Socket_UK.onopen = function() {
          s_pnp.ResetOrder()
        }
        s_pnp.Socket_UK.onmessage = function got_packet(Msg) {
          let UK_Data = JSON.parse(Msg.data);
          if(UK_Data.type!=="Process") return false;//如果不是流程处理消息,则跳过
          switch(UK_Data.order) {
            case 0:
              s_pnp.FindPort(0);
              break;
            case 1:
              if(UK_Data.LastError!==0){
                window.alert ("未发现加密锁,请插入加密锁");
                s_pnp.Socket_UK.close();
                return false;
              }
              DevicePath = UK_Data.return_value;
              var PrivateKey = "A3A96FD399FB5CD5761D1624571E4158FC8D6E896A7754D62E3EA47DC766BF9B";
              var PublicX = _this.uKey.x.trim();
              var PublicY = _this.uKey.y.trim();
              var name = "123";
              s_pnp.Set_SM2_KeyPair(PrivateKey, PublicX, PublicY, name, DevicePath);
              break;
            case 2:
              if(UK_Data.LastError!==0){
                window.alert ("设置密钥对及用户身份时出现错误");
                s_pnp.Socket_UK.close();
                return false;
              }
              _this.closeOwnDialog()
              s_pnp.Socket_UK.close();
              break
          }
        }
      }catch(e) {
        console.log(e);
      }
    },
    closeOwnDialog(){
      this.ownKeyDialog = false;
      if(this.isIn) {
        this.getSM2PubKey();
      }
    }
  },
  mounted() {
    this.uKeyLoad();
  }
}
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
.card-container {
    position: absolute;
    width: 800px;
    top: 100px;
    left: 50%;
    margin-left: -400px;
}
.card-item {
    margin-bottom: 16px;
}
.f-weight {
    font-size: 18px;
    font-weight: bold;
}
.params-container {
    width: 800px;
}
.dialog-center {
    display: flex;
    align-items: center;
    justify-content: center;
}
.dialog-center .el-dialog {
    margin-bottom: 0;
}
.dialog-center .el-dialog__header {
    text-align: left;
}
.dialog-footer {
    text-align: right;
}
.card-footer {
    text-align: right;
}
</style>