123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package com.szwl.controller;
- import cn.com.crbank.ommo.exception.MyException;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.sun.org.apache.regexp.internal.RE;
- import com.szwl.constant.ResponseCodesEnum;
- import com.szwl.model.bo.R;
- //import com.szwl.model.dto.WechatDTO;
- import com.szwl.model.entity.TAdmin;
- import com.szwl.model.entity.TWechat;
- import com.szwl.model.utils.DateUtils;
- import com.szwl.model.utils.HttpClientUtils;
- import com.szwl.service.TAdminService;
- import com.szwl.service.TWechatService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiModelProperty;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.apache.http.Header;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpHeaders;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.LaxRedirectStrategy;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.format.annotation.DateTimeFormat;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.servlet.view.RedirectView;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.security.MessageDigest;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author wuhs
- * @since 2023-05-19
- */
- @Slf4j
- @Api(value = "/WeChatController", tags = {"微信绑定接口"})
- @RestController
- @RequestMapping("/tWechat")
- public class TWechatController {
- @Autowired
- private TAdminService tAdminService;
- @Autowired
- private TWechatService tWechatService;
- // 从 yml 文件中获取
- @Value("${oauth.wx.appid}")
- private String appid;
- @Value("${oauth.wx.appsecret}")
- private String appsecret;
- @Value("${oauth.callback.http:http://szwltest.sunzee.com.cn:49002}")
- private String http;
- @ApiOperation(value = "绑定微信")
- @GetMapping("/bindWechat")
- public R bindWechat(@RequestParam Long adminId) throws Exception {
- if (adminId==null) {
- throw new MyException("参数为空");
- }
- 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?";
- try {
- path = URLEncoder.encode(path, "UTF-8");
- } catch (UnsupportedEncodingException 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);
- return R.ok(url);
- }
- // 绑定微信回调
- @GetMapping("/callback")
- @CrossOrigin(value = "https://api.weixin.qq.com/")
- public JSONObject oauthCallback(HttpServletRequest request) throws IOException {
- // 获取code
- String code = request.getParameter("code");
- String adminId = request.getParameter("state");
- // 第二步:通过code换取网页授权access_token
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
- "appid=" + appid +
- "&secret=" + appsecret +
- "&code=" + code +
- "&grant_type=authorization_code";
- JSONObject jsonObject = HttpClientUtils.get(url);
- String openid = jsonObject.getString("openid");
- String accessToken = jsonObject.getString("access_token");
- // 第三步:刷新access_token(如果需要)
- // 第四步:拉取用户信息(需scope为 snsapi_userinfo)
- url = "https://api.weixin.qq.com/sns/userinfo?" +
- "access_token=" + accessToken +
- "&openid=" + openid +
- "&lang=zh_CN";
- JSONObject userInfo = HttpClientUtils.get(url);
- List<TWechat> list = tWechatService.lambdaQuery()
- .eq(TWechat::getAdminId, adminId)
- .list();
- if (Objects.nonNull(list)) {
- TWechat tWechat = list.get(0);
- tWechat.setOpenId(userInfo.getString("openid"));
- tWechat.setNickName(userInfo.getString("nickname"));
- tWechat.setAvatarUrl(userInfo.getString("headimgurl"));
- tWechat.setModifyDate(new Date());
- tWechatService.updateById(tWechat);
- } else {
- TWechat tWechat = new TWechat();
- tWechat.setOpenId(userInfo.getString("openid"));
- tWechat.setNickName(userInfo.getString("nickname"));
- tWechat.setAvatarUrl(userInfo.getString("headimgurl"));
- tWechat.setCreateDate(new Date());
- tWechatService.save(tWechat);
- }
- return userInfo;
- }
- }
|