springboot 企业微信 网页授权

html 引入jquery

javascript 复制代码
     $(function () {
        // alert("JQ onready");
        // 当前企业的 corp_id
        const corp_id = 'xxxxxx';
        // 重定向 URL → 最终打开的画面地址,域名是在企业微信上配置好的域名
        const redirect_uri = encodeURI('http://xxxxx.cn');
        //企业的agentId 每个应用都不一样
        const agentId = 1000002;
        //获取当前路径的code(有code代表已经回调过了)
        let url = new URL(window.location.href)
        let code = new URLSearchParams(url.search).get("code");;
        //是否存在code
        if(code === undefined || code == null || code === "") {
            //不存在就打开地址进行授权,这里是默认授权(没写手动授权咋写),授权后直接跳转到redirect_uri 设置的地址,地址后会有回调的code参数值
            //必须是encodeURIComponent(路径)
            // snsapi_base:静默授权,可获取成员的基础信息(UserId与DeviceId);
            window.location.href =
                // `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corp_id}&redirect_uri=${encodeURIComponent(redirect_uri)}&response_type=code&scope=snsapi_base&state=STATE123456&agentid=${agentId}#wechat_redirect`
                `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corp_id}&redirect_uri=${encodeURIComponent(redirect_uri)}&response_type=code&scope=snsapi_privateinfo&state=STATE123456&agentid=${agentId}#wechat_redirect`
            // snsapi_privateinfo:手动授权,可获取成员的详细信息,包含头像、二维码等敏感信息。
        }else {
            alert(code);
            $.get("/test/user/"+code,
                function (data) {
                console.info(data);
                }, "json");
        }
    });

JAVA端

引入pom

xml 复制代码
  <dependency>
      <groupId>com.github.binarywang</groupId>
      <artifactId>wx-java-cp-spring-boot-starter</artifactId>
      <version>4.5.8.B</version>
  </dependency>

控制层

java 复制代码
    @ApiOperation("获取用户信息")
    @GetMapping("/user/{code}")
    public R userInfo(@PathVariable(value = "code") String code) throws WxErrorException {
        final WxCpService wxCpService = WxCpConfiguration.getCpService(1000002);
        WxCpOAuth2Service oauth2Service = wxCpService.getOauth2Service();
        WxCpOauth2UserInfo userInfo = oauth2Service.getUserInfo(code);
        String userTicket = userInfo.getUserTicket();
        WxCpUserDetail userDetail = oauth2Service.getUserDetail(userTicket);
        return R.ok(userDetail);
    }

企业微信配置

java get请求完成校验

企业微信API

https://developer.work.weixin.qq.com/document/path/91022

相关推荐
语落心生1 分钟前
Mcp+Agent - 自动化BI报表实现方案探索
后端
shark_chili12 分钟前
来聊聊JVM中安全点的概念
后端
无奈何杨12 分钟前
事件时间驱动的策略版本管理:风控系统中的关键设计抉择
java·后端·架构
这里有鱼汤14 分钟前
一文读懂量化交易中最常用的5种均线,附源码,建议收藏
后端·python
风象南1 小时前
SpringBoot的5种签到打卡实现方案
java·spring boot·后端
追逐时光者1 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 41 期(2025年6.1-6.8)
后端·.net
追逐时光者1 小时前
不写一行代码 .NET 使用 FluentCMS 快速构建现代化内容管理系统(CMS)
后端·.net·cms
星辰离彬1 小时前
Java 高级泛型实战:8 个场景化编程技巧
java·开发语言·后端·程序人生
懒虫虫~5 小时前
基于SpringBoot利用死信队列解决RabbitMQ业务队列故障重试无效场景问题
spring boot·rabbitmq
烛阴10 小时前
bignumber.js深度解析:驾驭任意精度计算的终极武器
前端·javascript·后端