package com.szwl.controller; import cn.com.crbank.ommo.exception.MyException; import com.szwl.model.bo.R; import com.szwl.model.entity.TWechat; import com.szwl.model.param.WxBindParam; 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.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.*; /** *
* 前端控制器 *
* * @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}") private String http; @ApiOperation(value = "绑定微信") @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 { // 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) { throw new RuntimeException(e); } } @ApiOperation(value = "绑定微信回调") @GetMapping("/callback") public R oauthCallback(HttpServletRequest request, HttpServletResponse response) throws IOException { // 获取code String code = request.getParameter("code"); String adminId = request.getParameter("state"); // 第二步:通过 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"; 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)) { 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 { // 获取微信授权失败 return R.fail("微信授权失败,没有openid"); } 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