Fancy
2025-01-24 82721cf9b8c5c1b9527379ef618a1b1fa2a12662
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
package com.dy.pmsWechat.login;
 
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
@RestController
@RequestMapping("/wechat")
public class AuthController {
 
    private WeChatService weChatService;
 
    @Autowired
    public void setWechatService(WeChatService weChatUserService){
        this.weChatService = weChatUserService;
    }
 
    @PostMapping("/login")
    public BaseResponse<Boolean> login(@RequestBody QueryVo vo) {
        try {
            JSONObject wxResponse = weChatService.login(vo.code);
            if (wxResponse.containsKey("token")) {
                // 成功获取到了用户的openid,session_key和其他信息
                // 这里可以进行更多业务逻辑操作,例如生成自定义token返回给客户端
                return BaseResponseUtils.buildSuccess(wxResponse);
            } else {
                // 如果有错误信息,直接返回
                return BaseResponseUtils.buildError(wxResponse);
            }
        } catch (Exception e) {
            return BaseResponseUtils.buildError(e.getMessage());
        }
    }
}