jinlin
2025-03-18 d30e385951ce03335a5023f0775fd144da3c0b88
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.example.client;
 
import com.example.client.service.SetUIFontService;
import com.example.client.utils.GBC;
import com.example.server.user.model.SysUser;
import com.example.server.user.service.UserService;
import com.example.server.utils.CacheUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
 
@Service
public class Login extends JFrame {
    @Autowired
    SetUIFontService setUIFontService;
    @Autowired
    UserService userService;
    @Autowired
    Main main;
    JLabel label, label2;
    JButton loginButton, exitButton;
    JTextField jTextField, ipTextField;
    JPasswordField passwordField;
    JFrame frame;
    int count = 0;
    @Value("${data.imgDir}")
    private String imgPath;
    @Value("${data.config-path}")
    private String configPath;
    Boolean exist;
    Integer role;
    SysUser user;
    Properties properties = new Properties();
 
 
    public void Start() {
        String userName = "admin";
        String password = "admin";
        String site = "";
        String teamGroup = "";
        String team = "";
 
        String path = Login.class.getClassLoader().getResource("config.properties").getPath();
        InputStream inStream = null;
        File dir = new File(path);
        if (dir.exists()) {
            inStream = Login.class.getClassLoader().getResourceAsStream("config.properties");
        } else {
            try {
                inStream = new FileInputStream(configPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        Reader reader = null;
        try {
            reader = new InputStreamReader(inStream, StandardCharsets.UTF_8);
            // 使用Properties.load(Reader)加载属性文件
            properties.load(reader);
 
            userName = properties.get("userName").toString();
            password = properties.get("password").toString();
            site = properties.get("site").toString();
            teamGroup = properties.get("teamGroup").toString();
            team = properties.get("team").toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        frame = new JFrame("用户登录界面");
        frame.setSize(560, 300);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout layout = new GridBagLayout();
        frame.setLayout(layout);
        try {
            Image image = ImageIO.read(new File(imgPath + "/user.png"));
            frame.setIconImage(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
 
 
        Font font = new Font("宋体", Font.BOLD, 20);
        setUIFontService.setFont(font);
 
        //ipLabel = new JLabel("服务端地址");
        label = new JLabel("用户名");
        label2 = new JLabel("密码");
        loginButton = new JButton("登录");
        exitButton = new JButton("退出");
        jTextField = new JTextField(16);//设置文本框的长度
        jTextField.setText(userName);
        passwordField = new JPasswordField(16);//设置密码框
        passwordField.setText(password);
       /* ipTextField = new JTextField(35);
        ipTextField.setEditable(false);*/
 
 
        //frame.add(ipLabel, new GBC(0, 0).setAnchor(GBC.SOUTHEAST).setInsets(5));
        //frame.add(ipTextField, new GBC(1, 0, 4, 1).setAnchor(GBC.SOUTHWEST).setWeight(0, 0).setInsets(5));
 
        frame.add(label, new GBC(0, 0).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame.add(jTextField, new GBC(1, 0, 2, 1).setAnchor(GBC.SOUTHWEST).setWeight(0, 0).setInsets(5));
 
        frame.add(label2, new GBC(0, 1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame.add(passwordField, new GBC(1, 1, 2, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        frame.add(loginButton, new GBC(1, 2, 1, 1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame.add(exitButton, new GBC(2, 2, 1, 1).setAnchor(GBC.SOUTHWEST).setInsets(5).setWeight(0, 0));
 
        frame.setVisible(true);//设置可见
 
        String finalSite = site;
        String finalTeamGroup = teamGroup;
        String finalTeam = team;
        String finalTeamGroup1 = teamGroup;
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = jTextField.getText();
                char[] password = passwordField.getPassword();
                String str = String.valueOf(password); //将char数组转化为string类型
 
 
                user = userService.login(name, str);
                exist = user.getExist();
                Boolean exit2 = true;
                if (StringUtils.isNotBlank(user.getTeamgroup())) {
                    if (StringUtils.isNotBlank(finalTeamGroup1) && !finalTeamGroup1.equals("null")) {
                        if (!finalTeamGroup1.contains(user.getTeamgroup())) {
                            exit2 = false;
                        }
                    }
                }
 
                if (exist) {
                    if (!exit2) {
                        JOptionPane.showMessageDialog(null, "当前机器不支持该专业用户登录", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    try {
                        String path = Login.class.getClassLoader().getResource("config.properties").getPath();
                        OutputStream outputStream = null;
                        File dir = new File(path);
                        if (dir.exists()) {
                            outputStream = new FileOutputStream(path);
                        } else {
                            try {
                                outputStream = new FileOutputStream(configPath);
                            } catch (FileNotFoundException ex) {
                                ex.printStackTrace();
                            }
                        }
 
                        properties.setProperty("userName", name);
                        properties.setProperty("password", str);
                        properties.store(outputStream, "rxkj");
                        outputStream.close();
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ep) {
                        ep.printStackTrace();
                    }
                    user.setBoatfleet(finalTeam);
                    CacheUtils.put("user", "user", user);
                    CacheUtils.put("site", "site", finalSite);
                    CacheUtils.put("teamGroup", "teamGroup", finalTeamGroup);
                    main.Start(role, user.getId());
                    frame.dispose();
                } else {
                    count++;
                    JOptionPane.showMessageDialog(null, "用户名密码错误,还有" + (3 - count) + "次机会", "提示", JOptionPane.WARNING_MESSAGE);
                    if (count == 3) {
                        System.exit(0);
                    }
                }
            }
        });
 
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
    }
}