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

相关推荐
MarkGosling42 分钟前
【开源项目】网络诊断告别命令行!NetSonar:开源多协议网络诊断利器
运维·后端·自动化运维
congvee43 分钟前
springboot 学习第1期 - 创建工程
spring boot
Codebee1 小时前
OneCode3.0 VFS分布式文件管理API速查手册
后端·架构·开源
_新一1 小时前
Go 调度器(二):一个线程的执行流程
后端
estarlee1 小时前
腾讯云轻量服务器创建镜像免费API接口教程
后端
风流 少年1 小时前
Cursor创建Spring Boot项目
java·spring boot·后端
毕设源码_钟学姐2 小时前
计算机毕业设计springboot宿舍管理信息系统 基于Spring Boot的高校宿舍管理平台设计与实现 Spring Boot框架下的宿舍管理系统开发
spring boot·后端·课程设计
军军君012 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
前端·javascript·spring boot·spring·微信小程序·前端框架·集成学习
方圆想当图灵3 小时前
ScheduledFutureTask 踩坑实录
后端
全栈凯哥3 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端