springboot 企业微信 网页授权

html 引入jquery

     $(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

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

控制层

    @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. 网页授权步骤说明

    2024-01-10 19:48:01       93 阅读
  2. 授权登录获取用户openid

    2024-01-10 19:48:01       17 阅读
  3. 基于SpringBoot小程序网上购物商城

    2024-01-10 19:48:01       53 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 19:48:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 19:48:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 19:48:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 19:48:01       20 阅读

热门阅读

  1. OceanBase CentOS7集群部署

    2024-01-10 19:48:01       44 阅读
  2. 面试 React 框架八股文十问十答第四期

    2024-01-10 19:48:01       38 阅读
  3. MySQL面试题(下)

    2024-01-10 19:48:01       33 阅读
  4. 面试 React 框架八股文十问十答第一期

    2024-01-10 19:48:01       40 阅读
  5. glusterFS

    glusterFS

    2024-01-10 19:48:01      34 阅读
  6. linux系统关于nginx服务

    2024-01-10 19:48:01       42 阅读
  7. 修改 Git 仓库的远程地址

    2024-01-10 19:48:01       37 阅读
  8. C#语言练手小功能

    2024-01-10 19:48:01       37 阅读
  9. 计算几何学(工程版)

    2024-01-10 19:48:01       40 阅读
  10. H5语言-入门教程

    2024-01-10 19:48:01       28 阅读
  11. CE322 Algorithmic Game Theory

    2024-01-10 19:48:01       31 阅读
  12. 【打卡】牛客网:BM75 编辑距离(一)

    2024-01-10 19:48:01       39 阅读