WebWXUserController.java
4.22 KB
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
package com.lhcredit.project.webbusiness.controller;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.common.utils.http.HttpUtil;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.service.IFrontUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;
/**
* 用户信息对外接口
*
* @author lhcredit
* @date 2023-11-28
*/
@RestController
@RequestMapping("/web/wxUser")
@RequiredArgsConstructor
@Slf4j
public class WebWXUserController {
@Value(value = "${wx.appId}")
private String appId;
@Value(value = "${wx.path}")
private String pathUrl;
@Value(value = "${wx.secret}")
private String secret;
@Autowired
private IFrontUserService frontUserService;
/**
* 第一步:用户同意授权,获取code
*
* @param response
*/
@RequestMapping("/oauth")
public void oauth(HttpServletResponse response) throws IOException {
// 项目服务器url
String path = pathUrl +"/";
try {
path = URLEncoder.encode(path, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=" + appId +
"&redirect_uri=" + path +
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=comi" +
"#wechat_redirect";
log.info("url===" + url);
response.sendRedirect(url);
}
/**
* 第二步:用户同意授权,页面跳转至/invoke
*
* @param request
*/
@RequestMapping("/invoke")
public AjaxResult invoke(HttpServletRequest request) {
String code = request.getParameter("code");
log.info("code=====" + code);
//通过code获取ACCESS_TOKEN
//认证服务器
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=" + appId +
"&secret=" + secret +
"&code=" + code +
"&grant_type=authorization_code";
String object = HttpUtil.doGet(url);
JSONObject jsonObject = JSONObject.parseObject(object);
log.info("jsonObject====" + jsonObject);
String access_token = jsonObject.getString("access_token");
String openid = jsonObject.getString("openid");
//第三步:拉取用户信息
String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?" +
"access_token=" + access_token +
"&openid=" + openid +
"&lang=zh_CN";
String userInfo = HttpUtil.doGet(userInfoUrl);
JSONObject userJson = JSONObject.parseObject(userInfo);
log.info("userJson=====" + userJson);
String openid1 = userJson.getString("openid");
AjaxResult frontUser = frontUserService.getFrontUserByOpenId(request,openid1);
return AjaxResult.success(frontUser);
}
@RequestMapping("/login")
public AjaxResult webLogin(HttpServletRequest request, @RequestBody FrontUserMon frontUserMon){
HttpSession session = request.getSession(true);
Object verObj=session.getAttribute("verCode");
if (null==verObj){
return AjaxResult.other(4003,"验证码过期");
}
String verCode =verObj.toString();
String code = frontUserMon.getCode();
if (!verCode.equalsIgnoreCase(code)) {
return AjaxResult.loginError("验证码错误!");
}
return frontUserService.webWxLogin(request, frontUserMon);
}
}