|
|
@@ -0,0 +1,361 @@
|
|
|
+package com.ruoyi.weChat.service;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+
|
|
|
+import com.ruoyi.common.core.redis.RedisCache;
|
|
|
+import com.ruoyi.common.utils.http.HttpUtils;
|
|
|
+import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+public class WeChatService {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(WeChatService.class);
|
|
|
+
|
|
|
+
|
|
|
+ @Value(value = "${wx.appId}")
|
|
|
+ private String appId;
|
|
|
+ @Value(value = "${wx.appSecret}")
|
|
|
+ private String appSecret;
|
|
|
+ @Value(value = "${wx.state}")
|
|
|
+ private String wxState;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private SysUserMapper sysUserMapper;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公出待审核通知
|
|
|
+ */
|
|
|
+ private static final String PUBLIC_RELEASE_EXAMINE ="j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I";
|
|
|
+ /**
|
|
|
+ * 公出审核结果通知
|
|
|
+ */
|
|
|
+ private static final String PUBLIC_RELEASE_EXAMINE_RESULT="pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI";
|
|
|
+ /**
|
|
|
+ * 公出待审核通知
|
|
|
+ */
|
|
|
+ private static final String EXPENSE_RELEASE_EXAMINE ="0e2Te3WUa0fDPQnyE8TLmN_K06WLsB4fqm5qpo6npNo";
|
|
|
+ /**
|
|
|
+ * 公出审核结果通知
|
|
|
+ */
|
|
|
+ private static final String EXPENSE_RELEASE_EXAMINE_RESULT="rRPWzRcGY8BGwlk0xyjwR1RKdv3iRFXaU3bcK1300gw";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送地址
|
|
|
+ */
|
|
|
+ private static String send_url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String getOpenId(String code) {
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+ url=url+"appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code";
|
|
|
+
|
|
|
+ JSONObject js= HttpUtils.sendGetToJson(url);
|
|
|
+ String openid=js.getString("openid");
|
|
|
+ if (openid==null){
|
|
|
+ log.error("openId 获取异常");
|
|
|
+ }else {
|
|
|
+
|
|
|
+ return openid;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getAccessToken() {
|
|
|
+ Date date = new Date();
|
|
|
+ String access_token = redisTemplate.opsForValue().get("access_token");
|
|
|
+ if (access_token!=null) {
|
|
|
+ log.debug("缓存读取access_token="+access_token);
|
|
|
+ return access_token;
|
|
|
+ } else {
|
|
|
+ return setSessionAccessToken();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String setSessionAccessToken() {
|
|
|
+ String url =String.format(
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId,
|
|
|
+ appSecret);
|
|
|
+ JSONObject js= HttpUtils.sendGetToJson(url);
|
|
|
+ String errcode =js.getString("errcode");
|
|
|
+ Long expires_in = js.getLong("expires_in");
|
|
|
+ String access_token=null;
|
|
|
+ if (errcode!=null) {
|
|
|
+ log.error("获取AccessToken失败!");
|
|
|
+ }else {
|
|
|
+ access_token = js.getString("access_token");
|
|
|
+ redisTemplate.opsForValue().set("access_token", access_token,expires_in, TimeUnit.SECONDS);
|
|
|
+ log.debug("获取并存入AccessToken成功,accesstoken="+access_token);
|
|
|
+ }
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 推送微信订阅信息,跳转为公出信息
|
|
|
+// * @param openid 发送对象微信openid
|
|
|
+// * @param type 状态 0驳回 1发起 2同意
|
|
|
+// * @param date 日期
|
|
|
+// * @param aname 发送者名称
|
|
|
+// * @param remarks 发送信息,不能大于十五个字
|
|
|
+// */
|
|
|
+// public Integer addNotice(String openid ,Integer type,Integer id, Date date, String aname,String remarks) {
|
|
|
+// Map<String, Object> map=new HashMap<String, Object>();
|
|
|
+// String accessToken=getAccessToken();
|
|
|
+// String url=send_url.replace("ACCESS_TOKEN", accessToken);
|
|
|
+// map.put("access_token", accessToken);
|
|
|
+// map.put("touser", openid);
|
|
|
+// map.put("page", "pages/egressDetails/index?id="+id);
|
|
|
+// map.put("miniprogram_state", wxState);
|
|
|
+// if (type==1) {
|
|
|
+// map.put("template_id", PUBLIC_RELEASE_EXAMINE);
|
|
|
+// map.put("data",getPublicData(type,date, aname,remarks));
|
|
|
+// }else {
|
|
|
+// map.put("template_id", PUBLIC_RELEASE_EXAMINE_RESULT);
|
|
|
+// map.put("data",getPublicData(type,date, aname,remarks));
|
|
|
+// }
|
|
|
+// return SendHttpPost(map, url);
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// private Integer SendHttpPost(String params, String url) {
|
|
|
+// String res= HttpUtils.sendPost(url,params);
|
|
|
+// if (res.get("errcode")!=null) {
|
|
|
+// log.error(String.format("errcode={%s}",res.getInteger("errcode").toString()));
|
|
|
+// return res.getInteger("errcode");
|
|
|
+// }else {
|
|
|
+// return 1;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 推送微信订阅信息,跳转为报销信息
|
|
|
+// * @param openid 发送对象微信openid
|
|
|
+// * @param type 状态 0驳回 1发起 2同意
|
|
|
+// * @param date 日期
|
|
|
+// * @param aname 发送者名称
|
|
|
+// * @param remarks 发送信息,不能大于十五个字
|
|
|
+// */
|
|
|
+// public Integer addExpenseAccountWeChatNotice(String openid ,Integer type,Integer id, Date date, String aname,String remarks) {
|
|
|
+// if (openid==null )log.error("openId不能为空");
|
|
|
+// Map<String, Object> map=new HashMap<String, Object>();
|
|
|
+// String accessToken=getExpenseAccountAccessToken();
|
|
|
+// String url=send_url.replace("ACCESS_TOKEN", accessToken);
|
|
|
+// map.put("access_token", accessToken);
|
|
|
+// map.put("touser", openid);
|
|
|
+// //这里要替换成当前的
|
|
|
+// map.put("page", "pages/egressDetails/index?id="+id);
|
|
|
+// map.put("miniprogram_state", wxState);
|
|
|
+// if (type==1) {
|
|
|
+// map.put("template_id", EXPENSE_RELEASE_EXAMINE);
|
|
|
+// map.put("data",getExpenseData(type,date, aname,remarks));
|
|
|
+// }else {
|
|
|
+// map.put("template_id", EXPENSE_RELEASE_EXAMINE_RESULT);
|
|
|
+// map.put("data",getExpenseData(type,date, aname,remarks));
|
|
|
+// }
|
|
|
+// return SendHttpPost(map, url);
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 公出订阅消息组装
|
|
|
+// * @param type
|
|
|
+// * @param date
|
|
|
+// * @param aname
|
|
|
+// * @param remarks
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// private Map<String, Object> getPublicData(Integer type,Date date, String aname, String remarks) {
|
|
|
+// Map<String, Object> we=new HashMap<String, Object>();
|
|
|
+// String strDate=DateUtils.formatDate(date, AFTConstants.YMDHMS_CHINESE);
|
|
|
+// if (remarks.length()>20)remarks=remarks.substring(0,17)+"...";
|
|
|
+// if (type==1) {
|
|
|
+// Map<String, Object> v1=new HashMap<String, Object>();
|
|
|
+// v1.put("value",strDate);
|
|
|
+// we.put("time5", v1);
|
|
|
+// Map<String, Object> v2=new HashMap<String, Object>();
|
|
|
+// v2.put("value", aname);
|
|
|
+// we.put("name3", v2);
|
|
|
+// Map<String, Object> v3=new HashMap<String, Object>();
|
|
|
+// v3.put("value", remarks);
|
|
|
+// we.put("thing10", v3);
|
|
|
+// }else {
|
|
|
+// String thing2="";
|
|
|
+// if (type==0){
|
|
|
+// thing2="驳回";
|
|
|
+// }else if (type==2){
|
|
|
+// thing2="通过";
|
|
|
+// }
|
|
|
+// Map<String, Object> v1=new HashMap<String, Object>();
|
|
|
+// v1.put("value",strDate);
|
|
|
+// we.put("date3", v1);
|
|
|
+// Map<String, Object> v2=new HashMap<String, Object>();
|
|
|
+// v2.put("value", thing2);
|
|
|
+// we.put("thing2", v2);
|
|
|
+// Map<String, Object> v3=new HashMap<String, Object>();
|
|
|
+// v3.put("value", remarks);
|
|
|
+// we.put("thing6", v3);
|
|
|
+// }
|
|
|
+// return we;
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 报销订阅消息组装
|
|
|
+// * @param type
|
|
|
+// * @param date
|
|
|
+// * @param aname
|
|
|
+// * @param remarks
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// private Map<String, Object> getExpenseData(Integer type,Date date, String aname, String remarks) {
|
|
|
+// Map<String, Object> we=new HashMap<String, Object>();
|
|
|
+// String strDate=DateUtils.formatDate(date, AFTConstants.YMDHMS_CHINESE);
|
|
|
+// if (remarks.length()>20)remarks=remarks.substring(0,17)+"...";
|
|
|
+// if (type==1) {
|
|
|
+// Map<String, Object> v2=new HashMap<String, Object>();
|
|
|
+// we.put("thing4", v2);
|
|
|
+// v2.put("value", aname);
|
|
|
+// Map<String, Object> v1=new HashMap<String, Object>();
|
|
|
+// we.put("time6", v1);
|
|
|
+// v1.put("value",strDate);
|
|
|
+// Map<String, Object> v3=new HashMap<String, Object>();
|
|
|
+// we.put("thing2", v3);
|
|
|
+// v3.put("value", remarks);
|
|
|
+// }else {
|
|
|
+// String thing5="";
|
|
|
+// if (type==0){
|
|
|
+// thing5="报销驳回";
|
|
|
+// }else if (type==2){
|
|
|
+// thing5="报销通过";
|
|
|
+// }
|
|
|
+// Map<String, Object> v1=new HashMap<String, Object>();
|
|
|
+// we.put("time3", v1);
|
|
|
+// v1.put("value",strDate);
|
|
|
+// Map<String, Object> v2=new HashMap<String, Object>();
|
|
|
+// we.put("thing5", v2);
|
|
|
+// v2.put("value", thing5);
|
|
|
+// Map<String, Object> v3=new HashMap<String, Object>();
|
|
|
+// we.put("thing4", v3);
|
|
|
+// v3.put("value", remarks);
|
|
|
+// }
|
|
|
+// return we;
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// public String getAccessToken() {
|
|
|
+// Date date = new Date();
|
|
|
+// if (redisUtil.presence("access_token")||redisUtil.presence("expires_in")) {
|
|
|
+// String access_token=redisUtil.getString("access_token");
|
|
|
+// String expires_in=redisUtil.getString("expires_in");
|
|
|
+// if (date.getTime() < Long.valueOf(expires_in)) {
|
|
|
+// LoggerUtils.debug(getClass(), "redis中获取accestoken");
|
|
|
+// return access_token;
|
|
|
+// } else {
|
|
|
+// LoggerUtils.debug(getClass(), "accestoken超时重新获取");
|
|
|
+// redisUtil.deleteString("access_token");
|
|
|
+// redisUtil.deleteString("expires_in");
|
|
|
+// return setSessionAccessToken(date,0);
|
|
|
+// }
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// return setSessionAccessToken(date,0);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// public String getExpenseAccountAccessToken() {
|
|
|
+// Date date = new Date();
|
|
|
+// if (redisUtil.presence("expense_access_token")||redisUtil.presence("expense_expires_in")) {
|
|
|
+// String access_token=redisUtil.getString("expense_access_token");
|
|
|
+// String expires_in=redisUtil.getString("expense_expires_in");
|
|
|
+// if (date.getTime() < Long.valueOf(expires_in)) {
|
|
|
+// LoggerUtils.debug(getClass(), "redis中获取accestoken");
|
|
|
+// return access_token;
|
|
|
+// } else {
|
|
|
+// LoggerUtils.debug(getClass(), "accestoken超时重新获取");
|
|
|
+// redisUtil.deleteString("expense_access_token");
|
|
|
+// redisUtil.deleteString("expense_expires_in");
|
|
|
+// return setSessionAccessToken(date,1);
|
|
|
+// }
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// return setSessionAccessToken(date,1);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// /**
|
|
|
+// *
|
|
|
+// * @param date 参数
|
|
|
+// * @param type 0公出小程序 1报销小程序
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// private String setSessionAccessToken(Date date,Integer type) {
|
|
|
+// String url =null;
|
|
|
+// if (type==0){
|
|
|
+// url=String.format(
|
|
|
+// "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId,
|
|
|
+// appSecret);
|
|
|
+// }else if(type==1){
|
|
|
+// url=String.format(
|
|
|
+// "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", expenseAppId,
|
|
|
+// expenseAppSecret);
|
|
|
+// }
|
|
|
+//
|
|
|
+// JSONObject js = HttpUtils.httpGet(url);
|
|
|
+// String errcode =js.getString("errcode");
|
|
|
+// if (errcode!=null) {
|
|
|
+// throw new BusinessException("获取AccessToken失败!");
|
|
|
+// }else {
|
|
|
+// LoggerUtils.debug(AsyncUtils.class,"获取并存入AccessToken成功,accesstoken="+js.getString("access_token"));
|
|
|
+// }
|
|
|
+// String access_token = js.getString("access_token");
|
|
|
+// Long expires_in = js.getLong("expires_in");
|
|
|
+// //返回时间为秒需要计算换成毫秒
|
|
|
+// expires_in=expires_in*1000;
|
|
|
+// expires_in+=date.getTime();
|
|
|
+// if (type==0){
|
|
|
+// redisUtil.setString("access_token", access_token);
|
|
|
+// redisUtil.setString("expires_in", expires_in.toString());
|
|
|
+// }else if (type==1){
|
|
|
+// redisUtil.setString("expense_access_token", access_token);
|
|
|
+// redisUtil.setString("expense_expires_in", expires_in.toString());
|
|
|
+// }
|
|
|
+//
|
|
|
+// return access_token;
|
|
|
+// }
|
|
|
+//
|
|
|
+
|
|
|
+}
|