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

相关推荐
一 乐9 分钟前
校园实验室|基于springboot + vue校园实验室管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
Lisonseekpan20 分钟前
Spring Boot Email 邮件发送完全指南
java·spring boot·后端·log4j
sheji341625 分钟前
【开题答辩全过程】以 基于Springboot的体检中心信息管理系统设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
天河归来29 分钟前
本地windows环境升级dify到1.11.1版本
java·spring boot·docker
天天向上10241 小时前
go 配置热更新
开发语言·后端·golang
甜鲸鱼1 小时前
【Spring AOP】操作日志的完整实现与原理剖析
java·spring boot·spring
狗头大军之江苏分军1 小时前
年底科技大考:2025 中国前端工程师的 AI 辅助工具实战盘点
java·前端·后端
一 乐2 小时前
酒店客房预订|基于springboot + vue酒店客房预订系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
计算机毕设指导62 小时前
基于Spring Boot的防诈骗管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
开心就好20252 小时前
IOScer 开发环境证书包括哪些,证书、描述文件与 App ID 的协同管理实践
后端