1
0
Gudge-ZL 2 лет назад
Родитель
Сommit
996b2d7b33

+ 11 - 0
src/main/java/com/seecoder/BlueWhale/Controller/UserController.java

@@ -3,10 +3,15 @@ package com.seecoder.BlueWhale.Controller;
 import com.seecoder.BlueWhale.Service.UserService;
 import com.seecoder.BlueWhale.VO.ResultVO;
 import com.seecoder.BlueWhale.VO.UserVO;
+
+import java.util.HashMap;
+import java.util.Map;
+
 import org.springframework.beans.factory.annotation.Autowired;
 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.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
@@ -21,4 +26,10 @@ public class UserController {
         return ResultVO.buildSuccess(userService.register(userVO));
     }
 
+    @PostMapping("/login")
+    public ResultVO<Map<String, String>> login(@RequestParam("phone") String phone, @RequestParam("password") String password){
+        Map<String, String> tokenMap = new HashMap<>();
+        tokenMap.put("token", userService.login(phone, password));
+        return ResultVO.buildSuccess(tokenMap);
+    }
 }

+ 4 - 0
src/main/java/com/seecoder/BlueWhale/Exception/BlueWhaleException.java

@@ -17,4 +17,8 @@ public class BlueWhaleException extends RuntimeException{
     public static BlueWhaleException notLogin(){
         return new BlueWhaleException("未登录!");
     }
+
+    public static BlueWhaleException phoneOrPasswordError(){
+        return new BlueWhaleException("手机号或密码错误!");
+    }
 }

+ 3 - 0
src/main/java/com/seecoder/BlueWhale/Mapper/UserMapper.java

@@ -3,5 +3,8 @@ package com.seecoder.BlueWhale.Mapper;
 import com.seecoder.BlueWhale.PO.User;
 import org.springframework.data.jpa.repository.JpaRepository;
 
+
 public interface UserMapper extends JpaRepository<User, Integer> {
+    User findByPhone(String phone);
+    User findByPhoneAndPassword(String phone, String password);
 }

+ 28 - 2
src/main/java/com/seecoder/BlueWhale/ServiceImpl/UserServiceImpl.java

@@ -2,26 +2,52 @@ package com.seecoder.BlueWhale.ServiceImpl;
 
 import com.seecoder.BlueWhale.Exception.BlueWhaleException;
 import com.seecoder.BlueWhale.Mapper.UserMapper;
+import com.seecoder.BlueWhale.PO.User;
 import com.seecoder.BlueWhale.Service.UserService;
+import com.seecoder.BlueWhale.Util.TokenUtil;
 import com.seecoder.BlueWhale.VO.UserVO;
+
+import java.util.Date;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+/**
+ * @Author: GaoZhaolong
+ * @Date: 14:46 2023/11/26
+ *
+ * 注册登录功能实现
+*/
 @Service
 public class UserServiceImpl implements UserService {
 
     @Autowired
     UserMapper userMapper;
+    
+    @Autowired
+    TokenUtil tokenUtil;
 
     @Override
     public Boolean register(UserVO userVO) {
-        throw BlueWhaleException.phoneAlreadyExists();
+        User user = userMapper.findByPhone(userVO.getPhone());
+        if (user != null) {
+            throw BlueWhaleException.phoneAlreadyExists();
+        }
+        User newUser = userVO.toPO();
+        newUser.setCreateTime(new Date());
+        userMapper.save(newUser);
+        return true;
         //return null;
     }
 
     @Override
     public String login(String phone, String password) {
-        return null;
+        User user = userMapper.findByPhoneAndPassword(phone, password);
+        if (user == null) {
+            throw BlueWhaleException.phoneOrPasswordError();
+        }
+        String token = tokenUtil.getToken(user);
+        return token;
     }
 
     @Override