|
|
package Service;
|
|
|
|
|
|
import Base.User;
|
|
|
import Send_Email.Deal_i_code;
|
|
|
import Send_Email.Send_email;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class User_service {
|
|
|
private ArrayList<User> users;
|
|
|
private final String base_dir="用户/";
|
|
|
|
|
|
public User_service() {
|
|
|
if (!load_users()){
|
|
|
users=new ArrayList<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String register(String email){
|
|
|
Deal_i_code.clean_all_codes();
|
|
|
if (find_user(email)!=null){
|
|
|
return "邮箱已被注册";
|
|
|
}
|
|
|
String judge_result=Send_email.judge_email_address(email);
|
|
|
if (judge_result.equals("邮箱格式正确")){
|
|
|
String code=Deal_i_code.generate_code(email);
|
|
|
if (!Send_email.send_email(email,code)){
|
|
|
return "邮件发送失败";
|
|
|
}
|
|
|
return "验证码已发送";
|
|
|
}
|
|
|
else{
|
|
|
return judge_result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String check_register(String email,String input_code,String pwd,String id){
|
|
|
if (Deal_i_code.judge_code(email,input_code)){
|
|
|
if (!User.check_password(pwd)){
|
|
|
return "密码长6-20位,至少包含大小写字母和数字";
|
|
|
}
|
|
|
User new_one=new User(email);
|
|
|
new_one.set_password(User.hash_pwd(pwd));
|
|
|
new_one.set_id(id);
|
|
|
if (find_user_i(id)!=null){
|
|
|
return "用户名重复";
|
|
|
}
|
|
|
users.add(new_one);
|
|
|
Deal_i_code.clean_all_codes();
|
|
|
if (!save_users()){
|
|
|
return "注册失败,请重试";
|
|
|
}
|
|
|
return "注册成功";
|
|
|
}
|
|
|
else {
|
|
|
return "验证码不存在或不正确";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String login(String id,String pwd){
|
|
|
User u=find_user_i(id);
|
|
|
if (u==null){
|
|
|
return "请先注册";
|
|
|
}
|
|
|
if (User.check_hash_pwd(pwd,u.get_password())){
|
|
|
return "登陆成功";
|
|
|
}
|
|
|
return "密码有误";
|
|
|
}
|
|
|
|
|
|
public String change_pwd(String email, String oldPassword, String newPassword){
|
|
|
User user=find_user(email);
|
|
|
if (user == null) {
|
|
|
return "用户不存在";
|
|
|
}
|
|
|
if (!User.check_hash_pwd(oldPassword,user.get_password())) {
|
|
|
return "原密码不正确";
|
|
|
}
|
|
|
if (oldPassword.equals(newPassword)){
|
|
|
return "新密码与原密码一致";
|
|
|
}
|
|
|
if (!User.check_password(newPassword)) {
|
|
|
return "密码长6-20位,至少包含大小写字母和数字";
|
|
|
}
|
|
|
user.set_password(newPassword);
|
|
|
if (save_users()) {
|
|
|
return "修改成功";
|
|
|
}
|
|
|
return "修改失败";
|
|
|
}
|
|
|
|
|
|
public String Unregister(String email){
|
|
|
User user=find_user(email);
|
|
|
if (user!=null) {
|
|
|
if (users.remove(user)) {
|
|
|
if (save_users()) {
|
|
|
return "删除成功";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return "删除失败";
|
|
|
}
|
|
|
|
|
|
public User find_user(String email){
|
|
|
for (User user : users) {
|
|
|
if (user.get_email().equals(email)) {
|
|
|
return user;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public User find_user_i(String id){
|
|
|
for (User user : users) {
|
|
|
if (user.get_id().equals(id)) {
|
|
|
return user;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private boolean save_users(){
|
|
|
File baseDir = new File(base_dir);
|
|
|
if (!baseDir.exists()) {
|
|
|
if (!baseDir.mkdirs()){
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
String file_path=base_dir+"用户信息.txt";
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(file_path))) {
|
|
|
if (users.isEmpty()){
|
|
|
return true;
|
|
|
}
|
|
|
for (int i = 0; i < users.size(); i++) {
|
|
|
writer.println(users.get(i).get_email()+" "+users.get(i).get_password()+" "+users.get(i).get_id());
|
|
|
if (i < users.size() - 1) {
|
|
|
writer.println();
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
} catch (IOException e) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean load_users(){
|
|
|
users=new ArrayList<>();
|
|
|
String file_path=base_dir+"用户信息.txt";
|
|
|
File file=new File(file_path);
|
|
|
if (file.exists()){
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
|
String line;
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
if (!line.trim().isEmpty()) {
|
|
|
String l=line.trim();
|
|
|
String[] parts=l.split(" ");
|
|
|
if (parts.length==3){
|
|
|
User temp=new User(parts[0]);
|
|
|
temp.set_password(parts[1]);
|
|
|
temp.set_id(parts[2]);
|
|
|
users.add(temp);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
} catch (IOException e) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
}
|