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

相关推荐
武子康41 分钟前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
椰椰椰耶2 小时前
【Spring】拦截器详解
java·后端·spring
brzhang3 小时前
我操,终于有人把 AI 大佬们 PUA 程序员的套路给讲明白了!
前端·后端·架构
G_whang5 小时前
jenkins使用Jenkinsfile部署springboot+docker项目
spring boot·docker·jenkins
wan_da_ren6 小时前
JVM监控及诊断工具-GUI篇
java·开发语言·jvm·后端
【本人】6 小时前
Django基础(一)———创建与启动
后端·python·django
lifallen6 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
你的人类朋友6 小时前
【✈️速通】什么是SIT,什么是UAT?
后端·单元测试·测试
程序无bug9 小时前
后端3行代码写出8个接口!
java·后端
绝无仅有9 小时前
使用LNMP一键安装包安装PHP、Nginx、Redis、Swoole、OPcache
后端·面试·github