diff --git a/fw-weixin/pom.xml b/fw-weixin/pom.xml new file mode 100644 index 00000000..5a3c515b --- /dev/null +++ b/fw-weixin/pom.xml @@ -0,0 +1,54 @@ + + + + com.hzya.frame + kangarooDataCenterV3 + ${revision} + + + 4.0.0 + fw-weixin + jar + ${revision} + + + + com.hzya.frame + base-service + ${revision} + + + mysql + mysql-connector-java + ${mysql-connector-java} + + + + + 8 + 8 + UTF-8 + + + + + org.springframework.boot + spring-boot-maven-plugin + + none + execute + true + + + + + repackage + + + + + + + \ No newline at end of file diff --git a/fw-weixin/src/main/java/com/hzya/frame/Main.java b/fw-weixin/src/main/java/com/hzya/frame/Main.java new file mode 100644 index 00000000..1817eb3e --- /dev/null +++ b/fw-weixin/src/main/java/com/hzya/frame/Main.java @@ -0,0 +1,5 @@ +package com.hzya.frame;public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/fw-weixin/src/main/java/com/hzya/frame/wecom/service/IWeComService.java b/fw-weixin/src/main/java/com/hzya/frame/wecom/service/IWeComService.java new file mode 100644 index 00000000..aec0dd6d --- /dev/null +++ b/fw-weixin/src/main/java/com/hzya/frame/wecom/service/IWeComService.java @@ -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); +} diff --git a/fw-weixin/src/main/java/com/hzya/frame/wecom/service/impl/WeComServiceImpl.java b/fw-weixin/src/main/java/com/hzya/frame/wecom/service/impl/WeComServiceImpl.java new file mode 100644 index 00000000..f8b36791 --- /dev/null +++ b/fw-weixin/src/main/java/com/hzya/frame/wecom/service/impl/WeComServiceImpl.java @@ -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; + } +} diff --git a/fw-weixin/src/main/java/com/hzya/frame/wecom/util/WeComAccessToken.java b/fw-weixin/src/main/java/com/hzya/frame/wecom/util/WeComAccessToken.java new file mode 100644 index 00000000..78b6dc19 --- /dev/null +++ b/fw-weixin/src/main/java/com/hzya/frame/wecom/util/WeComAccessToken.java @@ -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); + } + } +} diff --git a/fw-weixin/target/classes/com/hzya/frame/Main.class b/fw-weixin/target/classes/com/hzya/frame/Main.class new file mode 100644 index 00000000..fcf604dc Binary files /dev/null and b/fw-weixin/target/classes/com/hzya/frame/Main.class differ diff --git a/fw-weixin/target/classes/com/hzya/frame/wecom/Test.class b/fw-weixin/target/classes/com/hzya/frame/wecom/Test.class new file mode 100644 index 00000000..32e87025 Binary files /dev/null and b/fw-weixin/target/classes/com/hzya/frame/wecom/Test.class differ diff --git a/fw-weixin/target/classes/com/hzya/frame/wecom/service/IWeComService.class b/fw-weixin/target/classes/com/hzya/frame/wecom/service/IWeComService.class new file mode 100644 index 00000000..4914de8e Binary files /dev/null and b/fw-weixin/target/classes/com/hzya/frame/wecom/service/IWeComService.class differ diff --git a/fw-weixin/target/classes/com/hzya/frame/wecom/service/impl/WeComServiceImpl.class b/fw-weixin/target/classes/com/hzya/frame/wecom/service/impl/WeComServiceImpl.class new file mode 100644 index 00000000..cab0982e Binary files /dev/null and b/fw-weixin/target/classes/com/hzya/frame/wecom/service/impl/WeComServiceImpl.class differ diff --git a/fw-weixin/target/classes/com/hzya/frame/wecom/util/WeComAccessToken.class b/fw-weixin/target/classes/com/hzya/frame/wecom/util/WeComAccessToken.class new file mode 100644 index 00000000..c899d211 Binary files /dev/null and b/fw-weixin/target/classes/com/hzya/frame/wecom/util/WeComAccessToken.class differ