整个九月闲了一个月,然后10月就开始忙的晕头转向,在连续加班2个星期的情况下,总算把微信公众号后台开发完成了。现在忙里偷闲,总结一波先。
项目需求的背景是源于微信公众号一配置服务器地址后,自动回复和菜单管理两个模块就不可用了,所以需要这两个模块单独拎出来做,感觉就是模仿公众号平台的操作页面,再做一遍。
一开始我是按照开发者文档,自己用HTTP请求调接口去实现,域名问题用本机ngrok内网穿透解决,资源自己百度,然后先申请一个未认证的个人号再申请一个测试号即可(不知道为什么测试号没有操作台很不直观而且感觉接口限制很多),流程就是,请求素材接口,然后组装菜单数据直接推送到公众号,当然自动回复模块的内容都要自己保存,微信回调到我们系统时按文档组装成微信需要的XML格式返回给微信端,这里对接微信,理解文档,定义相关实体类,表结构等等老花时间还做的不忍直视。好吧,我想应该不会只有我一个开发这种平台,果然一百度出来一堆插件,一开始只是用了部分fastweixin的实体类和部分实现方式去改善自己的代码,后来干脆直接接入weixin-java-tool,和微信端的事情就都交给这个SDK了。然后重写demo里的redis的实现,存储token到redis,其实可以就用demo里存在内存的实现,毕竟不是分布式。然后就是调用SDK实现类实现业务功能
企业微信也是一样,不过要注意的是weixin-java-tool调用查询部门成员详情接口时少了个is_leader_in_dept字段,需要自己去调接口实现,如下:
departments.add(new Department(depart.getId(), depart.getName(), depart.getParentId(), 1, depart.getOrder()));
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=" + depart.getId();
String params = "&fetch_child=" + "1";
String responseContent = wxCpService.get(url, params);
List<WxUser> wxUserList = JSON.parseObject(responseContent, WxUserListResult.class).getUserlist();
首先要在pom文件引入weixin-java-tool,weixin-java-cp是企业微信,weixin-java-mp是微信公众平台:
<!--weixin-java-tool-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-common</artifactId>
<version>3.3.7.B</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>3.3.7.B</version>
</dependency>
然后是配置:
import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class WxCpConfigStorage extends WxCpInMemoryConfigStorage {
@Autowired
private IRedisTemplateLoader redisTemplateLoader ;
private static final String ACCESS_TOKEN_KEY = "work_wechat_access_token_";
private static final String JSAPI_TICKET_KEY = "work_wechat_jsapi_ticket_";
private String accessTokenKey = ACCESS_TOKEN_KEY;
private String jsapiTicketKey = JSAPI_TICKET_KEY;
@Override
public String getAccessToken() {
return redisTemplateLoader.get(this.accessTokenKey);
}
@Override
public boolean isAccessTokenExpired() {
return StringUtils.isEmpty(redisTemplateLoader.get(this.accessTokenKey));
}
@Override
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
redisTemplateLoader.set(this.accessTokenKey, accessToken, expiresInSeconds);
}
@Override
public void expireAccessToken() {
redisTemplateLoader.set(this.accessTokenKey, "", 1);
}
@Override
public String getJsapiTicket() {
return redisTemplateLoader.get(this.jsapiTicketKey);
}
@Override
public boolean isJsapiTicketExpired() {
return StringUtils.isEmpty(redisTemplateLoader.get(this.jsapiTicketKey));
}
@Override
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
redisTemplateLoader.set(this.jsapiTicketKey, jsapiTicket, expiresInSeconds);
}
@Override
public void expireJsapiTicket() {
redisTemplateLoader.set(this.jsapiTicketKey, "", 0);
}
import com.google.common.collect.Maps;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.stream.Collectors;
@Configuration
@EnableConfigurationProperties(WxCpProperties.class)
public class WxCpConfiguration {
private WxCpProperties properties;
@Autowired
public WxCpConfiguration(WxCpProperties properties) {
this.properties = properties;
}
@Autowired
WxCpConfigStorage configStorage;
private static Map<String, WxCpService> cpServices = Maps.newHashMap();
public static WxCpService getCpService(String agentId) {
return cpServices.get(agentId);
}
@Bean
public Object services() {
cpServices = this.properties.getConfigs()
.stream()
.map(a -> {
configStorage.setCorpId(a.getCorpId());
configStorage.setCorpSecret(a.getCorpSecret());
configStorage.setAgentId(Integer.parseInt(a.getAgentId()));
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
WxCpService service = new WxCpServiceImpl();
service.setWxCpConfigStorage(configStorage);
return service;
}).collect(Collectors.toMap(s -> s.getWxCpConfigStorage().getAgentId() + "", a -> a));
return Boolean.TRUE;
}
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonUtils {
public static String toJson(Object obj) {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
return gson.toJson(obj);
}
}
import com.uniubi.internal.util.JsonUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@ConfigurationProperties(prefix = "wx.cp")
public class WxCpProperties {
private List<CpConfig> configs;
public List<CpConfig> getConfigs() {
return configs;
}
public void setConfigs(List<CpConfig> configs) {
this.configs = configs;
}
public static class CpConfig {
/**
* 设置企业微信的corpId
*/
private String corpId;
/**
* 设置企业微信的corp secret
*/
private String corpSecret;
/**
* 设置微信企业号应用ID
*/
private String agentId;
/**
* 设置微信公众号的token
*/
private String token;
/**
* 设置微信公众号的EncodingAESKey
*/
private String aesKey;
public String getCorpId() {
return corpId;
}
public void setCorpId(String corpId) {
this.corpId = corpId;
}
public String getCorpSecret() {
return corpSecret;
}
public void setCorpSecret(String corpSecret) {
this.corpSecret = corpSecret;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getAesKey() {
return aesKey;
}
public void setAesKey(String aesKey) {
this.aesKey = aesKey;
}
}
@Override
public String toString() {
return JsonUtils.toJson(this);
}
}
最后在application.yml里添上如下配置:
配置.png
就可以愉快的在代码里调用SDK了:
调用.png
网友评论