文章目录
- 一、快速入门
- 1. 开发文档
- 2. 管控台
- 3. 接口文档
- 4. 参数获取
- 5. api调试
- 二、java实战
- 2.1. 入口
- 2.2. 核心方法
- 2.3. 核心配置
- 2.3. RestUtils 工具类
- 2.5. token缓存策略
- 三、源码分享
- 3.1. 后端源码
- 3.2. 前端源码
一、快速入门
1. 开发文档
企业内部开发文档:
https://developer.work.weixin.qq.com/document/path/91039
2. 管控台
企业微信管控台:
https://work.weixin.qq.com/wework_admin/frame#apps
3. 接口文档
获取access_token接口文档
4. 参数获取
corpid->获取企业ID
corpsecret->应用secret
5. api调试
- postman调试
- 官网调试
https://developer.work.weixin.qq.com/resource/devtool
二、java实战
2.1. 入口
// 1.通过corpId获取AccessTokenString accessToken = getAccessToken(corpId);
2.2. 核心方法
/*** 通过corpId获取AccessToken** @param corpId 企业ID* @return*/public String getAccessToken(String corpId) {String result = "";String accessTokenUrl = String.format(QywxInnerConfig.ACCESS_TOKEN_URL, corpId, AGENT_SECRET);Map response = RestUtils.get(accessTokenUrl);//获取错误日志if (response.containsKey("errcode") && (Integer) response.get("errcode") != 0) {logger.error(response.toString());} else {result = (String) response.get("access_token");}return result;}
2.3. 核心配置
package com.gblfy.qywxin.config;/*** 企业微信常量配置类** @author gblfy* @date 2022-01-12*/
public class QywxInnerConfig {public static final String BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin/";//获取access_token//https://open.work.weixin.qq.com/api/doc/90000/90135/91039public static final String ACCESS_TOKEN_URL = BASE_URL + "gettoken?corpid=%s&corpsecret=%s";
}
@Value("${qywx.agentSecret}")private String AGENT_SECRET;
2.3. RestUtils 工具类
package com.gblfy.qywxin.utils;import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;@Configuration
public class RestUtils {private static final RestTemplate restTemplate = new RestTemplate();public static JSONObject get(String url, Map<String,String> urlParams){return get(urlToUri(url,urlParams));}//在处理企业微信某些参数时有问题public static JSONObject get(String url){return get(URI.create(url));}private static JSONObject get(URI uri){ResponseEntity<JSONObject> responseEntity =restTemplate.getForEntity(uri,JSONObject.class);serverIsRight(responseEntity); //判断服务器返回状态码return responseEntity.getBody();}public static JSONObject post(String url,Map<String,String> urlParams,JSONObject json){//组装urlreturn post(urlToUri(url,urlParams),json);}public static JSONObject post(String url,JSONObject json){//组装urLreturn post(URI.create(url),json);}private static JSONObject post(URI uri,JSONObject json){//组装url//设置提交json格式数据HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);HttpEntity<JSONObject> request = new HttpEntity(json, headers);ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(uri,request,JSONObject.class);serverIsRight(responseEntity); //判断服务器返回状态码return responseEntity.getBody();}private static URI urlToUri(String url,Map<String,String> urlParams){//设置提交json格式数据UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url);for(Map.Entry<String,String> entry : urlParams.entrySet()) {uriBuilder.queryParam((String)entry.getKey(), (String) entry.getValue()) ;}return uriBuilder.build(true).toUri();}public static JSONObject upload(String url,MultiValueMap formParams){//设置表单提交HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.MULTIPART_FORM_DATA);HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(formParams, headers);ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url,request,JSONObject.class);serverIsRight(responseEntity); //判断服务器返回状态码return responseEntity.getBody();}public static String download(String url,String targetPath) throws IOException {ResponseEntity<byte[]> rsp = restTemplate.getForEntity(url, byte[].class);if(rsp.getStatusCode() != HttpStatus.OK){System.out.println("文件下载请求结果状态码:" + rsp.getStatusCode());}// 将下载下来的文件内容保存到本地Files.write(Paths.get(targetPath), Objects.requireNonNull(rsp.getBody()));return targetPath;}public static byte[] dowload(String url){ResponseEntity<byte[]> rsp = restTemplate.getForEntity(url, byte[].class);return rsp.getBody();}private static void serverIsRight(ResponseEntity responseEntity){if(responseEntity.getStatusCodeValue()==200){
// System.out.println("服务器请求成功:{}"+responseEntity.getStatusCodeValue());}else {System.out.println("服务器请求异常:{}"+responseEntity.getStatusCodeValue());}}
}
2.5. token缓存策略
建议使用redis,说一下具体实现流程:
- 1.获取token,根据token_key 查询redis中是否存在
- 2.如果存在,就获取token直接用
- 3.如果不存在,则通过企业ID获取token存储redis过期时间设置为2小时
三、源码分享
3.1. 后端源码
后端:https://gitee.com/gblfy/qywx-inner-java
3.2. 前端源码
前端:https://gitee.com/gblfy/qywx-vuejs