|
@@ -13,6 +13,7 @@ import com.szwl.model.bo.R;
|
|
//import com.szwl.model.dto.WechatDTO;
|
|
//import com.szwl.model.dto.WechatDTO;
|
|
import com.szwl.model.entity.TAdmin;
|
|
import com.szwl.model.entity.TAdmin;
|
|
import com.szwl.model.entity.TWechat;
|
|
import com.szwl.model.entity.TWechat;
|
|
|
|
+import com.szwl.model.param.WxBindParam;
|
|
import com.szwl.model.utils.DateUtils;
|
|
import com.szwl.model.utils.DateUtils;
|
|
import com.szwl.model.utils.HttpClientUtils;
|
|
import com.szwl.model.utils.HttpClientUtils;
|
|
import com.szwl.service.TAdminService;
|
|
import com.szwl.service.TAdminService;
|
|
@@ -84,115 +85,201 @@ public class TWechatController {
|
|
private String http;
|
|
private String http;
|
|
|
|
|
|
@ApiOperation(value = "绑定微信")
|
|
@ApiOperation(value = "绑定微信")
|
|
- @PostMapping("/bindWechat")
|
|
|
|
- public Map<String, Object> bindWechat(@RequestBody Map<String, Object> params) throws Exception {
|
|
|
|
- Long adminId = (Long) params.get("adminId");
|
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
|
- if (adminId==null) {
|
|
|
|
- result.put("code", 400);
|
|
|
|
- result.put("msg", "参数为空");
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
- TAdmin tAdmin = tAdminService.getById(adminId);
|
|
|
|
- TWechat tWechat = new TWechat();
|
|
|
|
- if (tAdmin != null) {
|
|
|
|
- List<TWechat> list = tWechatService
|
|
|
|
- .lambdaQuery()
|
|
|
|
- .eq(TWechat::getAdminId, adminId)
|
|
|
|
- .list();
|
|
|
|
- if (!list.isEmpty()) {
|
|
|
|
- tWechat = list.get(0);
|
|
|
|
- tWechatService.updateById(tWechat);
|
|
|
|
- } else {
|
|
|
|
- tWechat.setAdminId(String.valueOf(adminId));
|
|
|
|
- tWechatService.save(tWechat);
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- throw new MyException("用户不存在!");
|
|
|
|
- }
|
|
|
|
-// String path = http + "/tWechat/callback?";
|
|
|
|
- String path = http + "/SZWL-SERVER/tWechat/callback?";
|
|
|
|
|
|
+ @GetMapping("/bindWechat")
|
|
|
|
+ public R bindWechat(@RequestParam Long adminId) {
|
|
|
|
|
|
|
|
+ if (adminId == null) {
|
|
|
|
+ throw new MyException("参数为空");
|
|
|
|
+ }
|
|
|
|
+ String path = http + "/SZWL-SERVER/tWechat/callback";
|
|
|
|
+// String path = http + "/tWechat/callback";
|
|
try {
|
|
try {
|
|
- path = URLEncoder.encode(path, "UTF-8");
|
|
|
|
|
|
+ // redirectUrl 用于处理微信授权回调请求的页面
|
|
|
|
+ String redirectUrl = URLEncoder.encode(path, "UTF-8");
|
|
|
|
+ // 第一步:用户同意授权,获取code
|
|
|
|
+ String url = "http://szwlh.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
|
|
|
|
+// String url = "http://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
|
|
|
|
+// String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
|
|
|
|
+ + "appid=" + appid
|
|
|
|
+ + "&redirect_uri=" + redirectUrl
|
|
|
|
+ + "&response_type=code"
|
|
|
|
+ + "&scope=snsapi_userinfo"
|
|
|
|
+ + "&state=" + adminId
|
|
|
|
+ + "#wechat_redirect";
|
|
|
|
+ return R.ok(url);
|
|
|
|
+
|
|
} catch (UnsupportedEncodingException e) {
|
|
} catch (UnsupportedEncodingException e) {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
-
|
|
|
|
- // 第一步:用户同意授权,获取code
|
|
|
|
- String url = "http://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
|
|
|
|
-// String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
|
|
|
|
- + "appid=" + appid
|
|
|
|
- + "&redirect_uri=" + path
|
|
|
|
- + "&response_type=code"
|
|
|
|
- + "&scope=snsapi_userinfo"
|
|
|
|
- + "&state=" + adminId
|
|
|
|
- + "#wechat_redirect";
|
|
|
|
-// response.sendRedirect(url);
|
|
|
|
- result.put("code", 200);
|
|
|
|
- result.put("msg", "success");
|
|
|
|
- result.put("data", url);
|
|
|
|
- return result;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
@ApiOperation(value = "绑定微信回调")
|
|
@ApiOperation(value = "绑定微信回调")
|
|
@GetMapping("/callback")
|
|
@GetMapping("/callback")
|
|
- public void oauthCallback(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
|
|
+ public R oauthCallback(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
// 获取code
|
|
// 获取code
|
|
String code = request.getParameter("code");
|
|
String code = request.getParameter("code");
|
|
String adminId = request.getParameter("state");
|
|
String adminId = request.getParameter("state");
|
|
|
|
|
|
- // 第二步:通过code换取网页授权access_token
|
|
|
|
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
|
|
|
|
|
|
+ // 第二步:通过 code 换取网页授权 access_token 和 openid
|
|
|
|
+ String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
|
|
"appid=" + appid +
|
|
"appid=" + appid +
|
|
"&secret=" + appsecret +
|
|
"&secret=" + appsecret +
|
|
"&code=" + code +
|
|
"&code=" + code +
|
|
"&grant_type=authorization_code";
|
|
"&grant_type=authorization_code";
|
|
- JSONObject jsonObject = HttpClientUtils.get(url);
|
|
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = HttpClientUtils.get(openUrl);
|
|
String openid = jsonObject.getString("openid");
|
|
String openid = jsonObject.getString("openid");
|
|
String accessToken = jsonObject.getString("access_token");
|
|
String accessToken = jsonObject.getString("access_token");
|
|
// 第三步:刷新access_token(如果需要)
|
|
// 第三步:刷新access_token(如果需要)
|
|
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
|
|
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
|
|
- url = "https://api.weixin.qq.com/sns/userinfo?" +
|
|
|
|
|
|
+ String userUrl = "https://api.weixin.qq.com/sns/userinfo?" +
|
|
"access_token=" + accessToken +
|
|
"access_token=" + accessToken +
|
|
"&openid=" + openid +
|
|
"&openid=" + openid +
|
|
"&lang=zh_CN";
|
|
"&lang=zh_CN";
|
|
- JSONObject userInfo = HttpClientUtils.get(url);
|
|
|
|
|
|
+ JSONObject userInfo = HttpClientUtils.get(userUrl);
|
|
|
|
+ String nickname = userInfo.getString("nickname");
|
|
|
|
+ String headimgurl = userInfo.getString("headimgurl");
|
|
|
|
|
|
- LambdaQueryWrapper<TWechat> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
- wrapper.eq(TWechat::getAdminId, adminId);
|
|
|
|
- TWechat tWechat = tWechatService.getOne(wrapper);
|
|
|
|
-
|
|
|
|
- if (Objects.isNull(tWechat)) {
|
|
|
|
- response.sendError(HttpServletResponse.SC_NOT_FOUND, "tWechat is null"); // 返回错误状态码和错误信息给前端
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- String openId = tWechat.getOpenId();
|
|
|
|
-
|
|
|
|
- if (Objects.nonNull(openId)) {
|
|
|
|
- tWechat.setOpenId(userInfo.getString("openid"));
|
|
|
|
- tWechat.setNickName(userInfo.getString("nickname"));
|
|
|
|
- tWechat.setAvatarUrl(userInfo.getString("headimgurl"));
|
|
|
|
- tWechat.setModifyDate(new Date());
|
|
|
|
- tWechatService.updateById(tWechat);
|
|
|
|
|
|
+ if (StringUtils.isNotEmpty(openid)) {
|
|
|
|
+ TWechat list0 = tWechatService
|
|
|
|
+ .lambdaQuery()
|
|
|
|
+ .eq(TWechat::getOpenId, openid)
|
|
|
|
+ .one();
|
|
|
|
+ if (Objects.isNull(list0)) {
|
|
|
|
+ if (StringUtils.isNotEmpty(adminId)) {
|
|
|
|
+ TWechat wechat = tWechatService
|
|
|
|
+ .lambdaQuery()
|
|
|
|
+ .eq(TWechat::getAdminId, adminId)
|
|
|
|
+ .one();
|
|
|
|
+ if (Objects.isNull(wechat)) {
|
|
|
|
+ // 当前 adminId 首次绑定微信
|
|
|
|
+ TWechat tWechat = new TWechat();
|
|
|
|
+ tWechat.setOpenId(openid);
|
|
|
|
+ tWechat.setAdminId(adminId);
|
|
|
|
+ tWechat.setNickName(nickname);
|
|
|
|
+ tWechat.setAvatarUrl(headimgurl);
|
|
|
|
+ tWechat.setCreateDate(new Date());
|
|
|
|
+ tWechatService.save(tWechat);
|
|
|
|
+ } else {
|
|
|
|
+ // 更新绑定在当前 adminId 上的微信号
|
|
|
|
+ wechat.setOpenId(openid);
|
|
|
|
+ wechat.setAdminId(adminId);
|
|
|
|
+ wechat.setNickName(nickname);
|
|
|
|
+ wechat.setAvatarUrl(headimgurl);
|
|
|
|
+ wechat.setModifyDate(new Date());
|
|
|
|
+ tWechatService.updateById(wechat);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw new MyException("用户不存在!");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ String userId = list0.getAdminId();
|
|
|
|
+ if (Objects.equals(userId, adminId)) {
|
|
|
|
+ TWechat one = tWechatService
|
|
|
|
+ .lambdaQuery()
|
|
|
|
+ .eq(TWechat::getAdminId, userId)
|
|
|
|
+ .one();
|
|
|
|
+ one.setOpenId(openid);
|
|
|
|
+ one.setAdminId(adminId);
|
|
|
|
+ one.setNickName(nickname);
|
|
|
|
+ one.setAvatarUrl(headimgurl);
|
|
|
|
+ one.setModifyDate(new Date());
|
|
|
|
+ tWechatService.updateById(one);
|
|
|
|
+ } else {
|
|
|
|
+ return R.fail("当前微信号已绑定<" + userId + ">账户");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- tWechat.setOpenId(userInfo.getString("openid"));
|
|
|
|
- tWechat.setNickName(userInfo.getString("nickname"));
|
|
|
|
- tWechat.setAvatarUrl(userInfo.getString("headimgurl"));
|
|
|
|
- tWechat.setCreateDate(new Date());
|
|
|
|
- tWechatService.updateById(tWechat);
|
|
|
|
|
|
+ // 获取微信授权失败
|
|
|
|
+ return R.fail("微信授权失败,没有openid");
|
|
}
|
|
}
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
|
- result.put("code", 200);
|
|
|
|
- result.put("msg", "success");
|
|
|
|
- // 跳转到用户页面
|
|
|
|
- String userPageUrl = "http://szwltest.sunzee.com.cn/shenze/#/user";
|
|
|
|
- result.put("data", userPageUrl);
|
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
|
- response.getWriter().write(JSON.toJSONString(result));
|
|
|
|
|
|
+ String redirectUrl = http + "/shenze/#/user";
|
|
|
|
+ response.sendRedirect(redirectUrl);
|
|
|
|
+ return R.ok(userInfo);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @ApiOperation(value = "获取用户头像")
|
|
|
|
+ @GetMapping("/getAvatar")
|
|
|
|
+ public R getAvatar(@RequestParam("adminId") Long adminId) {
|
|
|
|
+ String avatarUrl = "";
|
|
|
|
+ if (adminId != null) {
|
|
|
|
+ TWechat wechat = tWechatService.lambdaQuery()
|
|
|
|
+ .eq(TWechat::getAdminId, adminId)
|
|
|
|
+ .one();
|
|
|
|
+ if (wechat != null) {
|
|
|
|
+ avatarUrl = wechat.getAvatarUrl();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return R.ok(avatarUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "绑定微信回调2")
|
|
|
|
+ @PostMapping("/auth")
|
|
|
|
+ public Map<String, Object> auth(@RequestBody WxBindParam wxBindParam) {
|
|
|
|
+ String adminId = wxBindParam.getState();
|
|
|
|
+ String code = wxBindParam.getCode();
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ // 第二步:通过 code 换取网页授权 access_token 和 openid
|
|
|
|
+ String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
|
|
|
|
+ "appid=" + appid +
|
|
|
|
+ "&secret=" + appsecret +
|
|
|
|
+ "&code=" + code +
|
|
|
|
+ "&grant_type=authorization_code";
|
|
|
|
+ System.out.println(openUrl);
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = HttpClientUtils.get(openUrl);
|
|
|
|
+ String openid = jsonObject.getString("openid");
|
|
|
|
+ String accessToken = jsonObject.getString("access_token");
|
|
|
|
+ // 第三步:刷新access_token(如果需要)
|
|
|
|
+ // 第四步:拉取用户信息(需scope为 snsapi_userinfo)
|
|
|
|
+ String userUrl = "https://api.weixin.qq.com/sns/userinfo?" +
|
|
|
|
+ "access_token=" + accessToken +
|
|
|
|
+ "&openid=" + openid +
|
|
|
|
+ "&lang=zh_CN";
|
|
|
|
+ JSONObject userInfo = HttpClientUtils.get(userUrl);
|
|
|
|
+ String nickname = userInfo.getString("nickname");
|
|
|
|
+ String headimgurl = userInfo.getString("headimgurl");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isNotEmpty(openid)) {
|
|
|
|
+ if (StringUtils.isNotEmpty(adminId)) {
|
|
|
|
+ TWechat wechat = tWechatService
|
|
|
|
+ .lambdaQuery()
|
|
|
|
+ .eq(TWechat::getAdminId, adminId)
|
|
|
|
+ .one();
|
|
|
|
+ if (Objects.isNull(wechat)) {
|
|
|
|
+ // 当前 adminId 首次绑定微信
|
|
|
|
+ TWechat tWechat = new TWechat();
|
|
|
|
+ tWechat.setOpenId(openid);
|
|
|
|
+ tWechat.setAdminId(adminId);
|
|
|
|
+ tWechat.setNickName(nickname);
|
|
|
|
+ tWechat.setAvatarUrl(headimgurl);
|
|
|
|
+ tWechat.setCreateDate(new Date());
|
|
|
|
+ tWechatService.save(tWechat);
|
|
|
|
+ } else {
|
|
|
|
+ // 更新绑定在当前 adminId 上的微信号
|
|
|
|
+ wechat.setOpenId(openid);
|
|
|
|
+ wechat.setAdminId(adminId);
|
|
|
|
+ wechat.setNickName(nickname);
|
|
|
|
+ wechat.setAvatarUrl(headimgurl);
|
|
|
|
+ wechat.setModifyDate(new Date());
|
|
|
|
+ tWechatService.updateById(wechat);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw new MyException("用户不存在!");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // 获取微信授权失败
|
|
|
|
+ result.put("message", "微信授权失败,没有openid");
|
|
|
|
+ }
|
|
|
|
+ result.put("success", true);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ result.put("success", false);
|
|
|
|
+ result.put("message", e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|