浙政钉的开发文档和专有钉钉一样:但想通过浙政钉扫一扫需向发浙政钉起申请获取应用信息
这里以专有钉钉为例
1.首先先注册一个专有钉钉账户
专有钉钉开放平台开发文档:https://dg-work.cn/
根据提示完成注册
2.在我的应用下创建应用信息
这里有App Key 和 App Secret
回调地址是可以访问到你后台方法的路径
开始编写程序
1.页面部分:下面iframe中的应用标识和回到地址改成自己对应的信息,post地址改成自己后端的访问地址
作用:生成二维码,获取临时code
<!DOCTYPE html>
<html>
<head>
<title>ddQRcode.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body style="text-align:center ">
<iframe style="border: medium none;" width="365" height="400" src="https://login-pro.ding.zj.gov.cn/oauth2/auth.htm?response_type=code&client_id=应用标识&redirect_uri=回调地址&scope=get_user_info&authType=QRCODE&embedMode=true"></iframe>
<script src="../js/jquery-1.11.2.min.js"></script>
<script type="application/javascript">
window.addEventListener('message', function(event) {
// 这里的event.data 就是登录成功的信息
// 数据格式:{ "code": "aaaa", "state": "bbbb" }
$.post("../zzdscan/getUserInfo",{"code":JSON.stringify(event.data)},function(res){
if(res.success==true){
var name = res.content.data.lastName;
window.location.href="./main.html?name="+encodeURIComponent(name);
}
});
});
</script>
</body>
</html>
2.java controller类下:替换******号内的内容
作用:获取access_token,通过code和access_token获取用户信息
package com.jtj.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("/zzdscan")
public class zzdController {
private static ExecutableClient executableClient;
static {
executableClient =ExecutableClient.getInstance();
//DomainName不同环境对应不同域名,示例为sass域名
//浙政钉域名:openplatform-pro.ding.zj.gov.cn
executableClient.setDomainName("openplatform.dg-work.cn");
executableClient.setProtocal("https");
//应用App Key
executableClient.setAccessKey("******");
//应用App Secret
executableClient.setSecretKey("******");
executableClient.init();
}
@RequestMapping(value = "/getUserInfo",produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String getUserInfo(String code) {
JSONObject StringCode = JSONObject.fromObject(code);
String codeStr = (String) StringCode.get("code");
String access_token= getToken();
String api ="/rpc/oauth2/getuserinfo_bycode.json";
PostClient postClient = executableClient.newPostClient(api);
//设置参数
postClient.addParameter("code", codeStr);
postClient.addParameter("access_token", access_token);
//调用API
String apiResult = postClient.post();
System.out.println(apiResult);
return apiResult;
}
public static String getToken() {
String api ="/gettoken.json";
PostClient postClient = executableClient.newPostClient(api);
//设置参数
postClient.addParameter("appkey", "******");
postClient.addParameter("appsecret", "");
//调用API
String apiResult = postClient.post();
//将字符串转为json对象
JSONObject jsonObj = JSONObject.fromObject(apiResult);
JSONObject jsonObject1 = jsonObj.getJSONObject("content");
JSONObject jsonObject2 = jsonObject1.getJSONObject("data");
String access_token = (String) jsonObject2.get("accessToken");
return access_token;
}
}
pom.xml 下对应maven包:自行提取有缺失根据控制台提示自行添加
<dependencies>
<!--spring-boot mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--添加tk-mapper依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- alibaba的Druid(德鲁伊)数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- sqlserver jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<!-- Document Elements -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<!-- @ResponseBody注解进行返回Response封装类 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- JSONObject对象依赖的jar包 开始 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
<!-- jdk版本 -->
</dependency>
<!-- Json依赖架包下载结束 -->
</dependencies>
这里是宁波浙政钉信息申请地址https://yida-pro.ding.zj.gov.cn/alibaba/web/APP_XG44H7NA6ZV5K05ZZ8RE/inst/homepage/#/
想找自己地区的自行联系客服小姐姐
到这里基本上就完成了。希望对你有所帮助。