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;
|
Boolean exit3 = true;
|
if (StringUtils.isNotBlank(user.getTeamgroup())) {
|
if (StringUtils.isNotBlank(finalTeamGroup1) && !finalTeamGroup1.equals("null")) {
|
if (!finalTeamGroup1.contains(user.getTeamgroup())) {
|
exit2 = false;
|
}
|
}
|
}
|
if (user.getNickName().equals("工作组")){
|
if(!"工作组".equals(finalSite)){
|
exit3 = false;
|
}
|
}
|
|
if (exist) {
|
if (!exit2) {
|
JOptionPane.showMessageDialog(null, "当前机器不支持该专业用户登录", "提示", JOptionPane.WARNING_MESSAGE);
|
return;
|
}
|
if (!exit3) {
|
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();
|
}
|
});
|
}
|
}
|