【浙政钉】第一篇:企业内应用免登

发布于:2023-01-04 ⋅ 阅读:(1770) ⋅ 点赞:(1)

专有钉钉为浙政钉的测试平台,接下来为大家介绍如何对接浙政钉。

版权:本文提供大家参考学习,如需转载,请附上本文链接。 

目录

介绍

一 、专有钉钉

二、准备工作

开发阶段

一、开发环境

二、参数配置

三、代码开发

1、获取token

2、获取临时授权码

三、获取用户信息


介绍

一 、专有钉钉

专有钉钉是什么?专有钉钉是个开放的saas平台,用于对接浙政钉的测试平台,接口等和浙政钉正式环境一样。

二、准备工作

对接浙政钉之前,需要申请专有钉钉的ISV账号,这个账号是以单位为主进行创建。

传送门:专有钉钉门户 (dg-work.cn)

创建好账号后,可以创建一个项目,如图:

钉钉专有版开放平台开发者后台 (dg-work.cn)

 然后就是对创建的项目进行配置,如图:

好了,到此就正式介绍完毕了。

开发阶段

一、开发环境

 开发工具:idea   开发语言:Java   使用框架:SpringBoot

依赖:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
       
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>
        <dependency>
            <groupId>com.oracel</groupId>
            <artifactId>zwdd-sdk-java</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dingtalk</artifactId>
            <version>1.3.23</version>
        </dependency>
       
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
        </dependency>
       
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>

注意:依赖这个依赖是我自己生成在maven中的,你们可以根据自己maven仓库生成到具体文件夹

<dependency>
            <groupId>com.oracel</groupId>
            <artifactId>zwdd-sdk-java</artifactId>
            <version>1.2.0</version>
</dependency>

sdk(jar包)下载:专有钉钉门户 (dg-work.cn)

 jar生成maven依赖命令,我这边以为上面说的依赖来


mvn install:install-file -Dfile=D:\jar\zwdd-sdk-java-1.2.0.jar -DgroupId=com.oracel -DartifactId=zwdd-sdk-java
-Dversion=1.2.0 -Dpackaging=jar

路径:D:\jar\zwdd-sdk-java-1.2.0.jar 我是下载jar包的路径,可以根据自己的来。

二、参数配置

先在yml文件中做好配置:

#专有钉钉参数
zzdingTalk :
  appKey : xxxxxxxxxxxxxxx
  appSecret : xxxxxxxxxxxxxxxxx
  domainName : openplatform.dg-work.cn
  protocal : https
  tenantId : 1111111

参数说明:

appKey 、appSecret : 为创建应用的秘钥

 domainName:调用服务的域名

服务端 域名
浙政钉
openplatform-pro.ding.zj.gov.cn
专有钉钉   openplatform.dg-work.cn

tenantId :为租户id,简单点说就是你登陆的账号id

如何知道你的tenantId呢?你可以在钉钉专有版开放平台开发者后台 (dg-work.cn)这个页面右击查看源代码,如图找到tenantId。

三、代码开发

1、获取token

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xxpt.gateway.shared.api.request.OapiMoziOrganizationListOrganizationsByCodesRequest;
import com.alibaba.xxpt.gateway.shared.api.response.OapiMoziOrganizationListOrganizationsByCodesResponse;
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
import com.alibaba.xxpt.gateway.shared.client.http.IntelligentPostClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/***
 * 浙政钉通讯
 */
@Service
@Slf4j
public class DingTalkService {

    @Value("${zzdingTalk.domainName}")
    private String domainName;
    @Value("${zzdingTalk.appKey}")
    private String appKey;
    @Value("${zzdingTalk.appSecret}")
    private String appSecret;
    @Value("${zzdingTalk.protocal}")
    private String protocal;
    @Value("${zzdingTalk.tenantId}")
    private String tenantId;

    @Autowired
    private RedisTemplate redisTemplate;


    /***
     * 获取浙政钉token
     * @return
     */
    public String getToken() {
        if (redisTemplate.hasKey("dingTalkToken")) {
            return redisTemplate.opsForValue().get("dingTalkToken").toString();
        }
        ExecutableClient executableClient = ExecutableClient.getInstance();
        executableClient.setDomainName(domainName);
        executableClient.setProtocal(protocal);
        executableClient.setAccessKey(appKey);
        executableClient.setSecretKey(appSecret);
        executableClient.init();
        //executableClient要单例,并且使用前要初始化,只需要初始化一次
        String api = "/gettoken.json";
        GetClient getClient = executableClient.newGetClient(api);
        //调用API
        String apiResult = getClient.get();
        log.info("获取浙政钉token:{}", apiResult);
        JSONObject jsonObject = JSONObject.parseObject(apiResult);
        String token = "";
        Integer expiresIn = 0;
        if (jsonObject.getBoolean("success")) {
            token = jsonObject.getJSONObject("content").getJSONObject("data").getString("accessToken");
            expiresIn = jsonObject.getJSONObject("content").getJSONObject("data").getInteger("expiresIn");
            redisTemplate.opsForValue().set("dingTalkToken", token, expiresIn, TimeUnit.SECONDS);
        }
        return token;
    }
}

返回参数:

log.info("获取浙政钉token:{}", apiResult);返回如下:

{
    "success":true,
    "content":{
        "data":{
            "accessToken":"c139fe44362f41b6b84862ec82ab84d9",
            "expiresIn":"7200"
        },
        "requestId":"df04428415724925400701038d663a",
        "responseMessage":"OK",
        "responseCode":"0",
        "success": true
    }
}

我的代码中已经解析了json,所以最后只返回token字符串了。

2、获取临时授权码

这个就涉及到前端了,前端可以调用如下代码,新建个页面,然后在专有钉钉内打开你页面的地址。(其他容器打开获取不到),点击 专有钉钉门户 (dg-work.cn)进行查看api。

小程序获取免登授权码
使用说明
npm install gdt-jsapi
推荐使用 npm 包形式按需引入,小程序可以通过以下方式进行引入

import dd from 'gdt-jsapi';

dd.getAuthCode({}).then(res =>{
    console.log(res)
}).
catch(err =>{})

3、获取用户信息

/***
     * 获取用户信息
     * @param code 临时授权码
     * @return
     */
    public JSONObject getDingTalk(String code) {
        String accessToken = getToken();
        ExecutableClient executableClient = ExecutableClient.getInstance();
        executableClient.setDomainName(domainName);
        executableClient.setProtocal(protocal);
        executableClient.setAccessKey(appKey);
        executableClient.setSecretKey(appSecret);
        executableClient.init();
        String api = "/rpc/oauth2/dingtalk_app_user.json";
        PostClient postClient = executableClient.newPostClient(api);
        postClient.addParameter("access_token", accessToken);
        postClient.addParameter("auth_code", code);
        String apiResult = postClient.post();
        log.info("获取浙政钉获取用户信息:{}", apiResult);
        return JSONObject.parseObject(apiResult);
    }

返回参数:

{
    "success":true,
    "content":{
        "data":{
            "accountId":93,
            "lastName":"洪阳",
            "clientId":"mozi-developer-center",
            "realmId":57,
            "openid":"6f1a885a4020f3624b71570b74925d7b",
            "realmName":"绣花针测试租户",
            "namespace":"local",
            "nickNameCn":"洪阳",
            "tenantUserId":"57$93",
            "account":"pishi.hy",
            "employeeCode":"pishi.hy"
        },
        "responseMessage":"成功",
        "responseCode":"0",
        "success":true
    }
}

到此处,已经获取到用户信息了,接下来就是对用户的入库,生成对应的token给前端,然后就是一些业务了。

按照如上操作,就已经实现了企业内免登操作了。

本文含有隐藏内容,请 开通VIP 后查看