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()); return ResponseEntity.badRequest().build(); } } /** * 微信小程序登录 */ @PostMapping("/wx-login") public ResponseEntity wxLogin(@RequestBody WxLoginRequest request) { try { WxLoginResponse response = authService.wxLogin(request); return ResponseEntity.ok(response); } catch (JsonProcessingException e) { return ResponseEntity.badRequest().build(); } } /** * 解密微信手机号(旧版API) */ @PostMapping("/decrypt-phone") public ResponseEntity 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 getPhoneNumberByCode(@RequestParam String code) { try { PhoneDecryptResponse response = authService.getPhoneNumberByCode(code); return ResponseEntity.ok(response); } catch (Exception e) { return ResponseEntity.badRequest().build(); } } }