From ea6ee3af977e2755c25a6a671c593d7f6835d06b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A7=9C=E7=8E=89=E7=90=A6?=
<7507756+jiang_yuqi@user.noreply.gitee.com>
Date: Sun, 31 Mar 2024 23:41:45 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../main/resources/wxstatic/oQUTTivnSQ.txt | 1 +
ruoyi-ui/src/views/wxsetting/wxAuth/index.vue | 71 ++++++++++++++
.../wxstatic/MP_verify_CIDdpE5WbJ0acBKs.txt | 1 +
.../controller/WxBindingController.java | 93 +++++++++++++++++++
4 files changed, 166 insertions(+)
create mode 100644 ruoyi-admin/src/main/resources/wxstatic/oQUTTivnSQ.txt
create mode 100644 ruoyi-ui/src/views/wxsetting/wxAuth/index.vue
create mode 100644 ruoyi-wechat/src/main/resources/wxstatic/MP_verify_CIDdpE5WbJ0acBKs.txt
create mode 100644 yanzhu-jh/src/main/java/com/yanzhu/jh/wxsetting/controller/WxBindingController.java
diff --git a/ruoyi-admin/src/main/resources/wxstatic/oQUTTivnSQ.txt b/ruoyi-admin/src/main/resources/wxstatic/oQUTTivnSQ.txt
new file mode 100644
index 00000000..eaec9df5
--- /dev/null
+++ b/ruoyi-admin/src/main/resources/wxstatic/oQUTTivnSQ.txt
@@ -0,0 +1 @@
+29aef62cd1316f1635063c5b86a3ffee
\ No newline at end of file
diff --git a/ruoyi-ui/src/views/wxsetting/wxAuth/index.vue b/ruoyi-ui/src/views/wxsetting/wxAuth/index.vue
new file mode 100644
index 00000000..5f565d69
--- /dev/null
+++ b/ruoyi-ui/src/views/wxsetting/wxAuth/index.vue
@@ -0,0 +1,71 @@
+
+
+
+
+
+
+
diff --git a/ruoyi-wechat/src/main/resources/wxstatic/MP_verify_CIDdpE5WbJ0acBKs.txt b/ruoyi-wechat/src/main/resources/wxstatic/MP_verify_CIDdpE5WbJ0acBKs.txt
new file mode 100644
index 00000000..24661151
--- /dev/null
+++ b/ruoyi-wechat/src/main/resources/wxstatic/MP_verify_CIDdpE5WbJ0acBKs.txt
@@ -0,0 +1 @@
+CIDdpE5WbJ0acBKs
\ No newline at end of file
diff --git a/yanzhu-jh/src/main/java/com/yanzhu/jh/wxsetting/controller/WxBindingController.java b/yanzhu-jh/src/main/java/com/yanzhu/jh/wxsetting/controller/WxBindingController.java
new file mode 100644
index 00000000..b3f5fe99
--- /dev/null
+++ b/yanzhu-jh/src/main/java/com/yanzhu/jh/wxsetting/controller/WxBindingController.java
@@ -0,0 +1,93 @@
+package com.yanzhu.jh.wxsetting.controller;
+
+import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.core.controller.BaseController;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.api.WxConsts;
+import me.chanjar.weixin.common.exception.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
+import me.chanjar.weixin.mp.bean.result.WxMpUser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+/**
+ * 微信openId绑定
+ *
+ * @author JiangYuQi
+ * @date 2023-08-24
+ */
+@Slf4j
+@Controller
+@RequestMapping("/wxAuth")
+public class WxBindingController extends BaseController {
+
+ @Autowired
+ private WxMpService wxMpService;
+
+ @GetMapping("/auth")
+ @ResponseBody
+ public String auth(@RequestParam(value = "echostr", defaultValue = "没有获取到") String echostr) {
+ return echostr;
+ }
+
+ /**
+ * @author :tao
+ * @param: :
+ * @return: 重定向到获取用户信息的类
+ * 微信授权登录
+ */
+ @GetMapping("/authorize")
+ public String authorize(@RequestParam(value = "returnUrl", defaultValue = "https://szgc.jhncidg.com/jhapi/") String returnUrl) throws UnsupportedEncodingException {
+ log.info("【微信网页授权】进来了,参数={}", returnUrl);
+ //1. 配置
+ //2. 调用方法
+ String url = RuoYiConfig.getProjectUrl() + "wxAuth/userInfo";
+ /*
+ * 相当于这种形式
+ * URLEncoder.decode(returnUrl,"UTF-8"
+ * https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
+ */
+ String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl, "utf-8"));
+ log.info("【微信网页授权】获取code,result={}", redirectUrl);
+ return "redirect:" + redirectUrl;
+ }
+
+ /**
+ * @author :tao
+ * @param: :
+ * @return: 重定向
+ * 获取用户信息类,最后重定向到指定url
+ */
+ @GetMapping("/userInfo")
+ public String userInfo(@RequestParam("code") String code,
+ @RequestParam("state") String returnUrl) throws WxErrorException {
+ /*当用户同意授权后,会回调所设置的url并把authorization code传过来,
+ 然后用这个code获得access token,其中也包含用户的openid等信息*/
+ WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
+ try {
+ //获取access token
+ wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
+ log.info("【AccessToken:】{}", wxMpOAuth2AccessToken.getAccessToken());
+ } catch (WxErrorException e) {
+ log.error("【微信网页授权】{}", e);
+ }
+
+ // 拿到openid
+ String openId = wxMpOAuth2AccessToken.getOpenId();
+ log.info("【openid:】{}", openId);
+ log.info("【我是前端要回调的地址:】{}", returnUrl + "&openid=" + openId);
+ // 顺便获取一下用户信息
+ WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, "zh_CN");
+ log.info("【用户信息:】{}", wxMpUser.toString());
+
+ //注意拼接参数,第一个参数需要加问号,之后参数使用&拼接的问题
+ //return "redirect:" + returnUrl + "/#/?openid=" + openId;
+ return "redirect:" + returnUrl + "&openid=" + openId;
+ }
+
+}