sgj
3 天以前 241d327e57cbfe504aa806c61aa22e6205706098
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.fzzy.appwx.controller;
 
 
import com.fzzy.appwx.constant.WeChatConst;
import com.fzzy.appwx.manager.WeChatManager;
import com.fzzy.appwx.param.WeChatBaseParam;
import com.fzzy.igds.data.ConfigData;
import com.fzzy.igds.data.PageResponse;
import com.ruoyi.common.core.domain.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import java.net.URLEncoder;
 
@Slf4j
@Controller
@RequestMapping("/wx")
public class WeChatController {
 
    @Resource
    private ConfigData configData;
    @Resource
    private WeChatManager weChatManager;
 
 
    /**
     * 微信菜单重定向获取code
     *
     * @param tag
     * @return
     * @throws Exception
     */
    @RequestMapping
    public String redirect(
            @RequestParam(value = "tag", required = false) String tag) throws Exception {
 
        String url = configData.getWxServeUrl() + "/wx/view-gateway";
 
        if (StringUtils.isEmpty(tag)) {
            tag = "home";
        }
//
        String newUrl = WeChatConst.USER_AUTH_UPR
                .replace("APPID", configData.getWxAppId())
                .replace("REDIRECT_URI", URLEncoder.encode(url, "UTF-8"))
                .replace("STATE", tag);
 
        return "redirect:" + newUrl;
//        return "redirect:" + url + "?state=" + tag;
    }
 
 
    /**
     * 页面分发
     *
     * @param
     * @return
     */
    @RequestMapping("/view-gateway")
    public ModelAndView viewGateway(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "state") String state,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "companyId", required = false) String companyId,
            @RequestParam(value = "loginType", required = false) String loginType,
            @RequestParam(value = "userName", required = false) String userName) {
 
        ModelAndView mv = new ModelAndView();
 
        //code不为空则获取openid
        if (StringUtils.isNotEmpty(code)) {
            String openid = weChatManager.getOpenid(code);
            mv.addObject("openid", openid);
            SysUser user = weChatManager.getUser(openid);
            if (StringUtils.isEmpty(userName)) {
                if (user != null) {
                    userName = user.getUserName();
                }
            }
 
        }
 
        if (StringUtils.isNotEmpty(id)) {
            mv.addObject("id", id);
            mv.addObject("companyId", companyId);
        }
        //判断是公众号还是APP,默认为公众号
        if (StringUtils.isEmpty(loginType)) {
            loginType = WeChatConst.LOGIN_TYPE_WECHAT;
        }
        mv.addObject("loginType", loginType);
 
        if (StringUtils.isNotEmpty(userName)) {
            mv.addObject("userName", userName);
        }
        mv.setViewName("/wx/" + state);
 
//        //测试代码开启
//        String openid = "testOpenId";
//        mv.addObject("openid", openid);
//        SysUser user = weChatManager.getUser(openid);
//        if (StringUtils.isEmpty(userName)) {
//            if (user != null) {
//                userName = user.getUserName();
//            }
//        }
//        //测试代码结束
 
        return mv;
    }
 
 
    /**
     * 获取用户的绑定信息
     *
     * @param param
     * @author sgj
     * @since 2026/04/11
     */
    @PostMapping(value = "/getUserBindInfo")
    @ResponseBody
    public PageResponse<SysUser> getUserBindInfo(  @RequestBody WeChatBaseParam param) {
        return weChatManager.getUserBindInfo(param);
    }
 
 
    /**
     * 绑定openId
     *
     * @param param
     * @author sgj
     * @since 2026/04/11
     */
    @PostMapping(value = "/bandOpenId")
    @ResponseBody
    public PageResponse<String> bandOpenId(  @RequestBody WeChatBaseParam param) {
        return weChatManager.bandOpenId(param);
    }
 
    /**
     * 解绑openId
     *
     * @param param
     * @author sgj
     * @since 2026/04/11
     */
    @PostMapping(value = "/unBandOpenId")
    @ResponseBody
    public PageResponse<String> unBandOpenId(  @RequestBody WeChatBaseParam param) {
        return weChatManager.unBandOpenId(param);
    }
 
}