1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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<LoginResponse> 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<WxLoginResponse> 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<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();
        }
    }
}