左晓为主开发手持机充值管理机
zuoxiao
2023-11-14 5a843ba11b991722f8f2af386f0e546e2769f7c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.dayu.recharge.card;
 
import com.dayu.recharge.tools.BcdUtil;
import com.dayu.recharge.tools.HexUtil;
import com.dayu.recharge.utils.MornyUtil;
 
import java.io.Serializable;
import java.net.IDN;
import java.util.Calendar;
 
/**
 * Copyright (C), 2023,
 * Author: zuo
 * Date: 2023-11-08 11:47
 * Description:设置域名卡:
 */
public class DomainCard implements Serializable {
 
 
    public String cardType = "C1";//卡类型
    public String domainNumber;//域名序号 (BCD格式)
    public int domainLength;//域名长度
 
    //端口号为5个字节,不足5位高位补0。端口号与域名之间用”,”隔开。端口号最后以”#”结束
    public String domainName;//域名
    public String port;//端口
 
    private byte[] asciiByte;//当前域名和端口号数组
 
    public byte[] getZeroByte() {
        Zero zero = new Zero();
        return zero.toByte();
    }
 
    public byte[] getOneByte() {
        One one = new One();
        return one.toBytes();
    }
 
    public byte[] getTwoByte() {
        Two two = new Two();
        return two.toBytes();
    }
 
 
    /**
     * 用户卡0块
     */
    public class Zero extends BaseCard {
        public byte[] toByte() {
            byte[] data = new byte[16];
            data[0] = HexUtil.hexToByte(cardType);
            byte[] domainNumbers = BcdUtil.strToBcd(domainNumber);
            data[1] = domainNumbers[0];
            data[2] = HexUtil.hexToByte(HexUtil.get10to16(domainLength));
            String ascii = IDN.toASCII(domainName + "," + port + "#");
            asciiByte = ascii.getBytes();
            if (asciiByte.length < 12) {
                System.arraycopy(asciiByte, 0, data, 3, asciiByte.length);
            } else {
                System.arraycopy(asciiByte, 0, data, 3, 12);
            }
            data[15] = getByteSum(data);
            return data;
        }
 
    }
 
    /**
     * 用户卡1块
     */
    public class One extends BaseCard {
 
        public byte[] toBytes() {
            byte[] data = new byte[16];
            if (asciiByte.length > 12) {
                if (asciiByte.length - 12 < 15) {
                    System.arraycopy(asciiByte, 12, data, 3, asciiByte.length - 12);
                } else {
                    System.arraycopy(asciiByte, 12, data, 3, 15);
                }
            }
            data[15] = getByteSum(data);
            return data;
        }
 
 
    }
 
    /**
     * 用户卡2块
     */
    public class Two extends BaseCard {
        public byte[] toBytes() {
            byte[] data = new byte[16];
            if (asciiByte.length > (12 + 15)) {
                if (asciiByte.length - (12 + 15) < 15) {
                    System.arraycopy(asciiByte, 12, data, 3, asciiByte.length - (12 + 15));
                } else {
                    System.arraycopy(asciiByte, 12, data, 3, 15);
                }
            }
            data[15] = getByteSum(data);
            return data;
        }
    }
}