Sfoglia il codice sorgente

7.25 一级电子市场 - 注册 - 登录 - 忘记密码

Mechrevo 2 anni fa
parent
commit
c588f7a007

+ 1 - 0
sp-core/sp-base/src/main/java/com/pj/enummj/People.java

@@ -20,4 +20,5 @@ public enum People {
 
     private String msg;
 
+
 }

+ 11 - 11
sp-service/level-one-server/src/main/java/com/pj/tb_enterprise/MethodEnterpriseService.java

@@ -111,29 +111,29 @@ public class MethodEnterpriseService {
                 tbEnterprise.setRegisterTime(dateValue);
             }
 
-            if(row.getCell(21) != null && !row.getCell(21).toString().trim().equals(""))
-                tbEnterprise.setPersonId(row.getCell(21).getStringCellValue());
+//            if(row.getCell(21) != null && !row.getCell(21).toString().trim().equals(""))
+//                tbEnterprise.setPersonId(row.getCell(21).getStringCellValue());
 
             //公共字段
-            HSSFCell cell2 = row.getCell(22);
+            HSSFCell cell2 = row.getCell(21);
             if (cell2 != null && cell2.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell2)) {
                 tbEnterprise.setCreateTime(cell2.getDateCellValue());
             }
 
+            if(row.getCell(22) != null && !row.getCell(22).toString().trim().equals(""))
+                tbEnterprise.setCreateBy(row.getCell(22).getStringCellValue());
+
             if(row.getCell(23) != null && !row.getCell(23).toString().trim().equals(""))
-                tbEnterprise.setCreateBy(row.getCell(23).getStringCellValue());
+                tbEnterprise.setCreateName(row.getCell(23).getStringCellValue());
 
             if(row.getCell(24) != null && !row.getCell(24).toString().trim().equals(""))
-                tbEnterprise.setCreateName(row.getCell(24).getStringCellValue());
-
+                tbEnterprise.setUpdateBy(row.getCell(24).getStringCellValue());
             if(row.getCell(25) != null && !row.getCell(25).toString().trim().equals(""))
-                tbEnterprise.setUpdateBy(row.getCell(25).getStringCellValue());
-            if(row.getCell(26) != null && !row.getCell(26).toString().trim().equals(""))
-                tbEnterprise.setUpdateName(row.getCell(26).getStringCellValue());
+                tbEnterprise.setUpdateName(row.getCell(25).getStringCellValue());
 
-            HSSFCell cell3 = row.getCell(27);
+            HSSFCell cell3 = row.getCell(26);
             if (cell3 != null && cell3.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell3)) {
-                tbEnterprise.setUpdateTime(row.getCell(27).getDateCellValue());
+                tbEnterprise.setUpdateTime(cell3.getDateCellValue());
             }
             //删除状态,默认可用
             tbEnterprise.setDeleteStatus(1);

+ 9 - 0
sp-service/sp-admin/src/main/java/com/pj/SpAdminApplication.java

@@ -5,7 +5,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.ApplicationPidFileWriter;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.Bean;
 import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
 import com.pj.current.SpCloudUtil;
@@ -34,4 +37,10 @@ public class SpAdminApplication {
 		// 测试服务调用  
 	}
 
+	/** 密码加密模式 */
+	@Bean
+	public PasswordEncoder passwordEncoder(){
+//        return NoOpPasswordEncoder.getInstance();
+		return new BCryptPasswordEncoder();
+	}
 }

+ 9 - 1
sp-service/sp-admin/src/main/java/com/pj/project/app_user/AppUser.java

@@ -109,9 +109,17 @@ public class AppUser extends Model<AppUser> implements Serializable {
 	/**
 	 * 更新时间 
 	 */
-	private Date updateTime;	
+	private Date updateTime;
 
+	/**
+	 * 逻辑删除
+	 */
+	private Integer deleteStatus;
 
+	/**
+	 * 密码
+	 */
+	private String password;
 
 
 

+ 26 - 0
sp-service/sp-admin/src/main/java/com/pj/project/app_user/AppUserController.java

@@ -1,9 +1,13 @@
 package com.pj.project.app_user;
 
 import java.util.List;
+
+import com.pj.project.app_user.dto.ForgetPasswordDto;
+import com.pj.project.app_user.dto.RegisterDto;
 import com.pj.utils.so.SoMap;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import com.pj.utils.sg.*;
@@ -76,7 +80,29 @@ public class AppUserController {
 		return AjaxJson.getPageData(so.getDataCount(), list);
 	}
 
+	/** 用户注册 */
+	@RequestMapping("register")
+	public AjaxJson register(@Validated @RequestBody RegisterDto registerDto) {
+		boolean register = appUserService.register(registerDto);
+		if(!register)return AjaxJson.getError("注册失败!");
+		return AjaxJson.getSuccess(register + "");
+	}
+
+	/** 用户登录 */
+	@RequestMapping("login")
+	public AjaxJson login(@RequestParam("phone")String phone,@RequestParam("password")String password) {
+		return appUserService.login(phone,password);
+	}
+
+	/** 忘记密码 */
+	@RequestMapping("forgetPassword")
+	public AjaxJson forgetPassword(ForgetPasswordDto forgetPasswordDto) {
+		return appUserService.forgetPassword(forgetPasswordDto);
+	}
+
+
 
+	/*-----               正常业务接口👆👆    不越界     rpc远程调用 👇👇                     -----*/
 
 
 	/** 启/停边民的app账号登陆限制 */

+ 151 - 0
sp-service/sp-admin/src/main/java/com/pj/project/app_user/AppUserService.java

@@ -1,10 +1,22 @@
 package com.pj.project.app_user;
 
+import java.util.Date;
 import java.util.List;
+import java.util.stream.Collectors;
 
+import cn.dev33.satoken.stp.StpUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.pj.current.satoken.StpUserUtil;
+import com.pj.project.app_user.dto.ForgetPasswordDto;
+import com.pj.project.app_user.dto.RegisterDto;
+import com.pj.project.re_role_menu.ReRoleMenu;
+import com.pj.project.re_role_menu.ReRoleMenuMapper;
+import com.pj.utils.cache.RedisUtil;
+import com.pj.utils.sg.AjaxJson;
 import com.pj.utils.so.SoMap;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -22,6 +34,16 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
 	/** 底层 Mapper 对象 */
 	@Autowired
 	AppUserMapper appUserMapper;
+	/** 密码加密模式 */
+	@Autowired
+	private PasswordEncoder passwordEncoder;
+	//权限字符
+	@Autowired
+	private ReRoleMenuMapper reRoleMenuMapper;
+	/** 方法抽取 */
+	@Autowired
+	private MethodAppUserService methodAppUserService;
+
 
 
 	/** 增 */
@@ -65,4 +87,133 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
 		return i;
 	}
 
+	/** 注册 */
+	boolean register(RegisterDto registerDto){
+		if(registerDto == null)return false;
+		//手机号去重
+		String phone = registerDto.getPhone();
+		if(appUserMapper.selectList(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone,phone).eq(AppUser::getDeleteStatus,1)).size() != 0)
+			throw new RuntimeException("当前手机号已被注册!");
+		//开始进行实际注册
+		//1. 查询数据库内是否存在逻辑删除
+		List<AppUser> appUserList = appUserMapper.selectList(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, phone).eq(AppUser::getDeleteStatus, 0));
+		if(appUserList.size() != 0){
+			//删除旧数据
+			appUserList.forEach(item -> {
+				appUserMapper.deleteById(item);
+			});
+		}
+		//2.todo:手机验证码
+
+
+		//3.开始新增
+		AppUser appUser = new AppUser();
+		//手机号
+		appUser.setPhone(phone);
+		//注册身份
+		appUser.setUserType(registerDto.getType().toString());
+		//加密并设置登陆密码
+		String password = registerDto.getPassword();
+		String encode = passwordEncoder.encode(password);
+		appUser.setPassword(encode);
+		//昵称
+		appUser.setName(registerDto.getNickName());
+		//创建时间
+		appUser.setCreateTime(new Date());
+		//默认可用
+		appUser.setStatus("1");
+		appUser.setDeleteStatus(1);
+		//保存
+		int insert = appUserMapper.insert(appUser);
+
+		return insert == 1;
+	}
+
+	/** 用户登录 */
+	AjaxJson login(String phone, String password){
+		if(!StringUtils.isNotBlank(phone) || !StringUtils.isNotBlank(password))
+			return AjaxJson.getError("错误登录!");
+		//登陆查询
+		LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
+		queryWrapper.eq(AppUser::getPhone,phone);
+		queryWrapper.eq(AppUser::getDeleteStatus,1); //未被逻辑删除
+		List<AppUser> userList = appUserMapper.selectList(queryWrapper);
+		if(userList.size() == 0)return AjaxJson.getError("该用户不存在,请先注册!");
+		//判断用户是否存在
+		if(userList.size() > 1){
+			log.debug("该手机号登录用户存在多个活动账号,手机号: " + phone);
+			return AjaxJson.getError("该账户存在问题,请联系客服进行处理!");
+		}
+		//获取登录用户
+		AppUser appUser = userList.get(0);
+		//比对密码
+		String userPassword = appUser.getPassword();
+		boolean matches = passwordEncoder.matches(password, userPassword);
+		if(!matches)return AjaxJson.getError("密码校验错误,请检查!");
+		//判断是否停用
+		if(appUser.getStatus().equals("0"))return AjaxJson.getError("该账户 " + appUser.getPhone() + " 已停用!");
+		//开始执行登录
+		StpUtil.login(appUser.getId());
+		// 组织返回参数,直接拿过来用的,然后自己改吧改吧
+		SoMap map = new SoMap();
+		//敏感信息置空
+		appUser.setPassword(null);
+		//执行
+		map.put("appUser", appUser);
+		//获取当前用户角色权限字符
+		List<ReRoleMenu> reRoleMenus = reRoleMenuMapper.selectList(new LambdaQueryWrapper<ReRoleMenu>().eq(ReRoleMenu::getAppRoleId, appUser.getUserType()));
+		//使用stream流对其reRoleMenus集合的权限字符进行过滤顺便转成String类型
+		List<String> per_list = reRoleMenus.stream().map(ReRoleMenu::getAppMenuId).collect(Collectors.toList()).stream().map(String::valueOf).collect(Collectors.toList());
+		if(reRoleMenus.size() == 0)return AjaxJson.getError("当前职务暂无权限!");
+		//过滤出权限字符
+		map.put("per_list", per_list);
+		map.put("tokenInfo", StpUtil.getTokenInfo());
+		StpUserUtil.cachePerList(per_list);
+
+		return AjaxJson.getSuccessData(map);
+	}
+
+
+	/** 忘记密码 */
+	AjaxJson forgetPassword(ForgetPasswordDto forgetPasswordDto){
+		//进行查询
+		List<AppUser> appUserList = appUserMapper.selectList
+				  (new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, forgetPasswordDto.getPhone())
+													.eq(AppUser::getDeleteStatus, 1)
+													.eq(AppUser::getStatus, 1));
+		if(appUserList.size() != 1)return AjaxJson.getError("当前账户存在异常,请联系客服进行处理!");
+		//获取当前手机号对象
+		AppUser appUser = appUserList.get(0);
+		//获取短信验证码发送结果
+		boolean sendResult = methodAppUserService.getSmsCode(forgetPasswordDto.getPhone());
+		if(!sendResult)return AjaxJson.getError("验证码发送失败!");
+		//从缓存中获取验证码
+		String smsCode = RedisUtil.get(forgetPasswordDto.getPhone());
+		if(smsCode == null)return AjaxJson.getError("验证码发送失败!");
+		//比对用户输入的验证码
+		boolean result = smsCode.equals(forgetPasswordDto.getSmsCode());
+		//进行验证码校验完成后的流程
+		if(result){
+			//密码校验
+			boolean equals = forgetPasswordDto.getPassword().equals(forgetPasswordDto.getRePassword());
+			//验证码匹配成功,执行修改密码逻辑
+			//1.判断其密码验证
+			if(equals){
+				//1.1 密码校验正确,设置新密码
+				String encode = passwordEncoder.encode(forgetPasswordDto.getRePassword());
+				appUser.setPassword(encode);
+				//1.2  执行保存
+				int i = appUserMapper.updateById(appUser);
+				if(i != 1)return AjaxJson.getError("密码重置失败!");
+				return AjaxJson.getSuccess("密码找回成功!");
+			}else {
+				return AjaxJson.getError("两次密码校验不匹配,请重新尝试!");
+			}
+
+		}
+
+		//验证码匹配错误
+		return AjaxJson.getError("验证码输入错误,请检查!");
+	}
+
 }

+ 39 - 0
sp-service/sp-admin/src/main/java/com/pj/project/app_user/MethodAppUserService.java

@@ -0,0 +1,39 @@
+package com.pj.project.app_user;
+
+import com.pj.utils.cache.RedisUtil;
+import org.omg.CORBA.TIMEOUT;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Author Mechrevo
+ * @Date 2023 07 25 15 05
+ **/
+@Service
+@Transactional(rollbackFor = Exception.class)
+public class MethodAppUserService {
+
+    /** App User mapper */
+    @Autowired
+    private AppUserMapper appUserMapper;
+
+
+    /** 获取手机验证码 */
+    boolean getSmsCode(String phone){
+        //todo: 发送短信,其短信商回调验证码
+        String smsCode = "123";
+        //todo: 可能存在短信发送失败情况
+//        if(false)return null;
+//        return false;
+
+
+        //todo: 保存到缓存,默认1分钟
+        RedisUtil.setByMINUTES(phone,smsCode, 1);
+
+        return true;
+    }
+
+}

+ 27 - 0
sp-service/sp-admin/src/main/java/com/pj/project/app_user/dto/ForgetPasswordDto.java

@@ -0,0 +1,27 @@
+package com.pj.project.app_user.dto;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @Author Mechrevo
+ * @Date 2023 07 25 15 45
+ **/
+@Data
+public class ForgetPasswordDto {
+
+    /** 手机号码 */
+    @NotNull
+    private String phone;
+    /** 短信验证码 */
+    @NotNull
+    private String smsCode;
+    /** 密码 */
+    @NotNull
+    private String password;
+    /** 再次输入密码 */
+    @NotNull
+    private String rePassword;
+
+}

+ 28 - 0
sp-service/sp-admin/src/main/java/com/pj/project/app_user/dto/RegisterDto.java

@@ -0,0 +1,28 @@
+package com.pj.project.app_user.dto;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * 注册的用户选择角色,填写手机号码、获取短信认证,填写密码进行注册
+ * @Author Mechrevo
+ * @Date 2023 07 25 10 43
+ **/
+@Data
+public class RegisterDto {
+
+    /** 注册身份 1=边民 2= 3= 4= */
+    @NotNull(message = "身份选择不能为空!")
+    private Integer type;
+    /** 手机号码 */
+    @NotNull(message = "手机号不能为空")
+    private String phone;
+    /** 手机验证码 */
+    private Integer smsCode;
+    /** 登陆密码 */
+    @NotNull(message = "密码不能为空!")
+    private String password;
+    /** 昵称 */
+    private String nickName;
+}

+ 6 - 0
sp-service/transport-server/src/main/java/com/pj/TransportServerApplication.java

@@ -6,7 +6,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.ApplicationPidFileWriter;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.Bean;
 import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.crypto.password.NoOpPasswordEncoder;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
 import java.io.File;
@@ -28,4 +32,6 @@ public class TransportServerApplication {
         SpCloudUtil.printCurrentServiceInfo();
     }
 
+
+
 }