feat: 企业微信接口
This commit is contained in:
parent
cff92f2399
commit
5ff781e133
|
@ -0,0 +1,54 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>com.hzya.frame</groupId>
|
||||||
|
<artifactId>kangarooDataCenterV3</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>fw-weixin</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>${revision}</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hzya.frame</groupId>
|
||||||
|
<artifactId>base-service</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>${mysql-connector-java}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>none</mainClass> <!-- 取消查找本项目下的Main方法:为了解决Unable to find main class的问题 -->
|
||||||
|
<classifier>execute</classifier> <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.hzya.frame;public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello world!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.hzya.frame.wecom.service;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
|
||||||
|
import com.hzya.frame.web.entity.JsonResultEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 企业微信service
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/9/23 14:23
|
||||||
|
**/
|
||||||
|
public interface IWeComService {
|
||||||
|
/**
|
||||||
|
* 获取accessToken
|
||||||
|
* 该方法会缓存accessToken
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
|
||||||
|
* @param jsonObject
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
JSONObject accessToken(JSONObject jsonObject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送消息
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN
|
||||||
|
* @param jsonObject
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
JSONObject messageSend(JSONObject jsonObject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据授权码获取用户信息
|
||||||
|
* 单点登录的时候用
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
|
||||||
|
* @param jsonObject
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
JSONObject getUserInfoByAuthCode(JSONObject jsonObject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据userid读取成员信息
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID
|
||||||
|
* @param jsonObject
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
JSONObject getUserInfoByUserId(JSONObject jsonObject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据手机号获取userid
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=ACCESS_TOKEN
|
||||||
|
* @param jsonObject
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
JSONObject getUserIdByMobile(JSONObject jsonObject);
|
||||||
|
}
|
|
@ -0,0 +1,243 @@
|
||||||
|
package com.hzya.frame.wecom.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
|
||||||
|
import com.hzya.frame.web.entity.BaseResult;
|
||||||
|
import com.hzya.frame.web.entity.JsonResultEntity;
|
||||||
|
import com.hzya.frame.wecom.service.IWeComService;
|
||||||
|
import com.hzya.frame.wecom.util.WeComAccessToken;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 企业微信service
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/9/23 14:24
|
||||||
|
**/
|
||||||
|
@Service(value = "weComServiceImpl")
|
||||||
|
public class WeComServiceImpl implements IWeComService {
|
||||||
|
static Logger logger = LoggerFactory.getLogger(WeComServiceImpl.class);
|
||||||
|
/**
|
||||||
|
* 获取accessToken
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
|
||||||
|
* @param json
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public JSONObject accessToken(JSONObject json) {
|
||||||
|
JSONObject jsonObject = json.getJSONObject("jsonStr");
|
||||||
|
if (null == jsonObject){
|
||||||
|
return this.error("参数不能为空");
|
||||||
|
}
|
||||||
|
String corpid = jsonObject.getString("corpid");
|
||||||
|
String corpsecret = jsonObject.getString("corpsecret");
|
||||||
|
if (StrUtil.isEmpty(corpid)) {
|
||||||
|
return this.error("corpid不能为空");
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(corpsecret)) {
|
||||||
|
return this.error("corpsecret不能为空");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String accessToken = WeComAccessToken.getAccessToken(corpid, corpsecret);
|
||||||
|
return this.ok(accessToken);
|
||||||
|
}catch(Exception e){
|
||||||
|
logger.error("获取accessToken出错",e);
|
||||||
|
}
|
||||||
|
return this.error("系统异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送消息
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN
|
||||||
|
* @param json
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public JSONObject messageSend(JSONObject json) {
|
||||||
|
JSONObject jsonObject = json.getJSONObject("jsonStr");
|
||||||
|
if (null == jsonObject){
|
||||||
|
return this.error("参数不能为空");
|
||||||
|
}
|
||||||
|
String agentid = jsonObject.getString("agentid");
|
||||||
|
String text = jsonObject.getString("text");
|
||||||
|
String access_token = jsonObject.getString("access_token");
|
||||||
|
if (StrUtil.isEmpty(agentid)){
|
||||||
|
return this.error("agentid不能为空");
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(text)){
|
||||||
|
return this.error("消息内容不能为空");
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(access_token)){
|
||||||
|
return this.error("access_token不能为空");
|
||||||
|
}
|
||||||
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token;
|
||||||
|
jsonObject.remove("access_token");
|
||||||
|
String param = jsonObject.toString();
|
||||||
|
String res = HttpRequest.post(url).body(param).timeout(30000).execute().body();
|
||||||
|
if (StrUtil.isNotEmpty(res)){
|
||||||
|
JSONObject msgResponse = JSONObject.parseObject(res);
|
||||||
|
String errcode = msgResponse.getString("errcode");
|
||||||
|
String errmsg = msgResponse.getString("errmsg");
|
||||||
|
if ("0".equals(errcode)){
|
||||||
|
return ok();
|
||||||
|
}else {
|
||||||
|
return error(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.error("操作失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据授权码获取用户信息
|
||||||
|
* 单点登录的时候用
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
|
||||||
|
*
|
||||||
|
* @param json
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public JSONObject getUserInfoByAuthCode(JSONObject json) {
|
||||||
|
JSONObject jsonObject = json.getJSONObject("jsonStr");
|
||||||
|
if (null == jsonObject){
|
||||||
|
return this.error("参数不能为空");
|
||||||
|
}
|
||||||
|
String access_token = jsonObject.getString("access_token");
|
||||||
|
String code = jsonObject.getString("code");
|
||||||
|
if (StrUtil.isEmpty(access_token)){
|
||||||
|
return error("access_token不能为空");
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(code)){
|
||||||
|
return error("code不能为空");
|
||||||
|
}
|
||||||
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token="+access_token+"&code="+code;
|
||||||
|
String res = HttpRequest.get(url).timeout(30000).execute().body();
|
||||||
|
if (StrUtil.isNotEmpty(res)){
|
||||||
|
JSONObject msgResponse = JSONObject.parseObject(res);
|
||||||
|
String errcode = msgResponse.getString("errcode");
|
||||||
|
String errmsg = msgResponse.getString("errmsg");
|
||||||
|
String userid = msgResponse.getString("userid");
|
||||||
|
if ("0".equals(errcode)){
|
||||||
|
return ok(userid);
|
||||||
|
}else {
|
||||||
|
return error(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据userid读取成员信息
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID
|
||||||
|
*
|
||||||
|
* @param json
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public JSONObject getUserInfoByUserId(JSONObject json) {
|
||||||
|
JSONObject jsonObject = json.getJSONObject("jsonStr");
|
||||||
|
if (null == jsonObject){
|
||||||
|
return this.error("参数不能为空");
|
||||||
|
}
|
||||||
|
String access_token = jsonObject.getString("access_token");
|
||||||
|
String userid = jsonObject.getString("userid");
|
||||||
|
if (StrUtil.isEmpty(access_token)){
|
||||||
|
return error("access_token不能为空");
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(userid)){
|
||||||
|
return error("userid不能为空");
|
||||||
|
}
|
||||||
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+access_token+"&userid="+userid;
|
||||||
|
String res = HttpRequest.get(url).timeout(30000).execute().body();
|
||||||
|
if (StrUtil.isNotEmpty(res)){
|
||||||
|
JSONObject msgResponse = JSONObject.parseObject(res);
|
||||||
|
String errcode = msgResponse.getString("errcode");
|
||||||
|
String errmsg = msgResponse.getString("errmsg");
|
||||||
|
if ("0".equals(errcode)){
|
||||||
|
return ok(res);
|
||||||
|
}else {
|
||||||
|
return error(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据手机号获取userid
|
||||||
|
* https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=ACCESS_TOKEN
|
||||||
|
*
|
||||||
|
* @param json
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public JSONObject getUserIdByMobile(JSONObject json) {
|
||||||
|
JSONObject jsonObject = json.getJSONObject("jsonStr");
|
||||||
|
if (null == jsonObject){
|
||||||
|
return this.error("参数不能为空");
|
||||||
|
}
|
||||||
|
String access_token = jsonObject.getString("access_token");
|
||||||
|
String mobile = jsonObject.getString("mobile");
|
||||||
|
if (StrUtil.isEmpty(access_token)){
|
||||||
|
return error("access_token不能为空");
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(mobile)){
|
||||||
|
return error("mobile不能为空");
|
||||||
|
}
|
||||||
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token="+access_token;
|
||||||
|
jsonObject.remove("access_token");
|
||||||
|
String param = jsonObject.toString();
|
||||||
|
String res = HttpRequest.post(url).body(param).timeout(30000).execute().body();
|
||||||
|
if (StrUtil.isNotEmpty(res)){
|
||||||
|
JSONObject msgResponse = JSONObject.parseObject(res);
|
||||||
|
String errcode = msgResponse.getString("errcode");
|
||||||
|
String errmsg = msgResponse.getString("errmsg");
|
||||||
|
String userid = msgResponse.getString("userid");
|
||||||
|
if ("0".equals(errcode)){
|
||||||
|
return ok(userid);
|
||||||
|
}else {
|
||||||
|
return error(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static JSONObject ok(){
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("code","200");
|
||||||
|
jsonObject.put("msg","成功");
|
||||||
|
jsonObject.put("data","");
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功
|
||||||
|
* @param data 返回数据
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static JSONObject ok(String data){
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("code","200");
|
||||||
|
jsonObject.put("msg","成功");
|
||||||
|
jsonObject.put("data",data);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败
|
||||||
|
* @param msg 失败原因
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static JSONObject error(String msg){
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("code","500");
|
||||||
|
jsonObject.put("msg",msg);
|
||||||
|
jsonObject.put("data","");
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.hzya.frame.wecom.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 获取企业微信accesToken
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2024/9/23 14:08
|
||||||
|
**/
|
||||||
|
public class WeComAccessToken {
|
||||||
|
|
||||||
|
static Logger logger = LoggerFactory.getLogger(WeComAccessToken.class);
|
||||||
|
//token
|
||||||
|
private static String accessToken;
|
||||||
|
//过期时间
|
||||||
|
private static Instant expireTime;
|
||||||
|
private static final Long CACHE_EXPIRY_TIME = 7000L; // 缓存有效时间(秒)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取accessToken
|
||||||
|
*
|
||||||
|
* @param corpid 企业ID
|
||||||
|
* @param corpsecret 应用的凭证密钥
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getAccessToken(String corpid,String corpsecret) {
|
||||||
|
//判断是否过期 如果没过期直接返回
|
||||||
|
if (null != accessToken && expireTime != null && Instant.now().isBefore(expireTime)) {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
//获取新的accessToken
|
||||||
|
accessToken = fetchNewAccessToken(corpid,corpsecret);
|
||||||
|
//过期时间设置成当前事件+7000s,预留200s的时间
|
||||||
|
expireTime = Instant.now().plusSeconds(CACHE_EXPIRY_TIME);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取信的token
|
||||||
|
* @param corpid
|
||||||
|
* @param corpsecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String fetchNewAccessToken(String corpid, String corpsecret) {
|
||||||
|
String url = " https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;
|
||||||
|
String response = HttpRequest.get(url).timeout(30000).execute().body();
|
||||||
|
if (StrUtil.isNotEmpty(response)){
|
||||||
|
JSONObject json = JSONObject.parseObject(response);
|
||||||
|
String accessToken = json.getString("access_token");
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i=0; i<2; i++){
|
||||||
|
String accessToken1 = WeComAccessToken.getAccessToken("wwb46c3f5e6ffe3e2b", "oON2ELxNVyl7wc37LeA9bNOsv_jyuFXdrvD9e0yogbQ");
|
||||||
|
System.out.println(accessToken1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue