|
@@ -0,0 +1,79 @@
|
|
|
+package com.fjhx.xmhjc.controller.open;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.fjhx.xmhjc.anno.LoginValid;
|
|
|
+import com.fjhx.xmhjc.constants.LoginConstant;
|
|
|
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersDto;
|
|
|
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
|
|
|
+import com.fjhx.xmhjc.service.website.WebsiteUsersService;
|
|
|
+import com.fjhx.xmhjc.utils.WebsiteUserUtil;
|
|
|
+import com.ruoyi.common.core.redis.RedisCache;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.sign.Md5Utils;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author hj
|
|
|
+ * @date 2023年11月17日 9:24
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/open/website/user")
|
|
|
+public class OpenWesiteUserController {
|
|
|
+ @Resource
|
|
|
+ private WebsiteUsersService websiteUsersService;
|
|
|
+ @Resource
|
|
|
+ private RedisCache redisCache;
|
|
|
+ /**
|
|
|
+ * 注册
|
|
|
+ * @author hj
|
|
|
+ * @date 2023/11/17 9:25
|
|
|
+ */
|
|
|
+ @PostMapping("/register")
|
|
|
+ public void register(@RequestBody WebsiteUsersDto websiteUsersDto){
|
|
|
+ if(StrUtil.isBlank(websiteUsersDto.getUserName()) || StrUtil.isBlank(websiteUsersDto.getPassword())){
|
|
|
+ throw new ServiceException("参数错误");
|
|
|
+ }
|
|
|
+ if(!websiteUsersDto.getPassword().equals(websiteUsersDto.getConfirmPassword())){
|
|
|
+ throw new ServiceException("两次密码不一致");
|
|
|
+ }
|
|
|
+ websiteUsersService.getByUserName(websiteUsersDto.getUserName()).ifPresent(o->{
|
|
|
+ throw new ServiceException("用户名已存在");
|
|
|
+ });
|
|
|
+ websiteUsersService.register(websiteUsersDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录
|
|
|
+ * @param websiteUsersDto
|
|
|
+ */
|
|
|
+ @PostMapping("/login")
|
|
|
+ public String login(@RequestBody WebsiteUsersDto websiteUsersDto){
|
|
|
+ if(StrUtil.isBlank(websiteUsersDto.getUserName()) || StrUtil.isBlank(websiteUsersDto.getPassword())){
|
|
|
+ throw new ServiceException("参数错误");
|
|
|
+ }
|
|
|
+ WebsiteUsers websiteUsers = websiteUsersService.getByUserName(websiteUsersDto.getUserName()).orElseThrow(()->new ServiceException("用户名不存在"));
|
|
|
+ if(!websiteUsers.getPassword().equals(Md5Utils.hash(LoginConstant.SOLID+websiteUsersDto.getPassword()))){
|
|
|
+ throw new ServiceException("密码错误");
|
|
|
+ }
|
|
|
+ String token = IdUtil.fastSimpleUUID();
|
|
|
+ redisCache.setCacheObject(LoginConstant.TOKEN_PREFIX+token, websiteUsers, LoginConstant.LOGIN_EXPIRED, TimeUnit.HOURS);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @LoginValid
|
|
|
+ @PostMapping("/logout")
|
|
|
+ public void logout(){
|
|
|
+ String websiteUserToken = WebsiteUserUtil.getWebsiteUserToken();
|
|
|
+ redisCache.deleteObject(websiteUserToken);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|