package com.rongyichuang.auth.controller;
|
|
import com.rongyichuang.auth.dto.LoginRequest;
|
import com.rongyichuang.auth.dto.LoginResponse;
|
import com.rongyichuang.auth.dto.PhoneDecryptResponse;
|
import com.rongyichuang.auth.dto.WxLoginRequest;
|
import com.rongyichuang.auth.dto.WxLoginResponse;
|
import com.rongyichuang.auth.service.AuthService;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.validation.annotation.Validated;
|
import jakarta.validation.Valid;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
/**
|
* RESTful认证控制器
|
*/
|
@RestController
|
@RequestMapping("/auth")
|
@CrossOrigin(origins = "*")
|
@Validated
|
public class AuthController {
|
|
private static final Logger logger = LoggerFactory.getLogger(AuthController.class);
|
|
@Autowired
|
private AuthService authService;
|
|
/**
|
* Web端用户登录
|
*/
|
@PostMapping("/web-login")
|
public ResponseEntity<?> webLogin(@Valid @RequestBody LoginRequest request) {
|
logger.info("收到Web登录请求,手机号: {}", request.getPhone());
|
try {
|
LoginResponse response = authService.login(request);
|
logger.info("Web登录成功,手机号: {}", request.getPhone());
|
return ResponseEntity.ok(response);
|
} catch (Exception e) {
|
logger.error("Web登录失败,手机号: {}, 错误: {}", request.getPhone(), e.getMessage());
|
// 返回包含错误信息的JSON响应
|
return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
|
}
|
}
|
|
// 错误响应类
|
public static class ErrorResponse {
|
private String message;
|
|
public ErrorResponse(String message) {
|
this.message = message;
|
}
|
|
public String getMessage() {
|
return message;
|
}
|
|
public void setMessage(String message) {
|
this.message = message;
|
}
|
}
|
|
/**
|
* 微信小程序登录
|
*/
|
@PostMapping("/wx-login")
|
public ResponseEntity<WxLoginResponse> wxLogin(@RequestBody WxLoginRequest request) {
|
logger.info("收到微信登录请求,openid: {}", request.getWxOpenid());
|
try {
|
WxLoginResponse response = authService.wxLogin(request);
|
logger.info("微信登录成功,openid: {}", request.getWxOpenid());
|
return ResponseEntity.ok(response);
|
} catch (JsonProcessingException e) {
|
logger.error("微信登录JSON处理异常,openid: {}, 错误: {}", request.getWxOpenid(), e.getMessage());
|
return ResponseEntity.badRequest().build();
|
} catch (Exception e) {
|
logger.error("微信登录失败,openid: {}, 错误: {}", request.getWxOpenid(), e.getMessage(), e);
|
return ResponseEntity.status(500).build();
|
}
|
}
|
|
/**
|
* 解密微信手机号(旧版API)
|
*/
|
@PostMapping("/decrypt-phone")
|
public ResponseEntity<PhoneDecryptResponse> decryptPhoneNumber(
|
@RequestParam String encryptedData,
|
@RequestParam String iv,
|
@RequestParam String sessionKey) {
|
try {
|
PhoneDecryptResponse response = authService.decryptPhoneNumber(encryptedData, iv, sessionKey);
|
return ResponseEntity.ok(response);
|
} catch (Exception e) {
|
return ResponseEntity.badRequest().build();
|
}
|
}
|
|
/**
|
* 获取微信手机号(新版API)
|
*/
|
@PostMapping("/get-phone-by-code")
|
public ResponseEntity<PhoneDecryptResponse> getPhoneNumberByCode(@RequestParam String code) {
|
try {
|
PhoneDecryptResponse response = authService.getPhoneNumberByCode(code);
|
return ResponseEntity.ok(response);
|
} catch (Exception e) {
|
return ResponseEntity.badRequest().build();
|
}
|
}
|
}
|