Commit 6a6b73cf authored by Quxl's avatar Quxl

x

parent 55d1e124
package com.egolm.shop.api.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public interface QiyeWxService {
long DevTime = 1000*60*15;
String PrefixRedisTokenKey = "AccessTokenRedis_";
String UrlGetToken = "/gettoken?corpid={0}&corpsecret={1}";
String UrlSendMsg = "/message/send?access_token={0}";
String getTokenString();
void refreshToken();
void sendMessage(WxMessage wxMessage);
public static class WxMessage {
String touser;
String msgtype = "miniprogram_notice";
MiniprogramNotice miniprogram_notice;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public MiniprogramNotice getMiniprogram_notice() {
return miniprogram_notice;
}
public void setMiniprogram_notice(MiniprogramNotice miniprogram_notice) {
this.miniprogram_notice = miniprogram_notice;
}
public static class MiniprogramNotice {
String appid;
String page;
String title;
String description;
Boolean emphasis_first_item = true;
List<Map<String, String>> content_item = new ArrayList<Map<String, String>>();
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getEmphasis_first_item() {
return emphasis_first_item;
}
public void setEmphasis_first_item(Boolean emphasis_first_item) {
this.emphasis_first_item = emphasis_first_item;
}
public List<Map<String, String>> getContent_item() {
return content_item;
}
public void setContent_item(List<Map<String, String>> content_item) {
this.content_item = content_item;
}
}
}
}
package com.egolm.shop.api.service;
public interface WeixinTokenService {
}
package com.egolm.shop.api.service.impl;
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egolm.common.DateUtil;
import com.egolm.common.StringUtil;
import com.egolm.shop.api.service.QiyeWxService;
import com.egolm.shop.common.utils.HttpUtil;
import com.egolm.shop.common.utils.HttpUtil.HttpReqObject;
import com.egolm.shop.common.utils.HttpUtil.HttpRespObject;
import com.egolm.shop.common.utils.HttpUtil.JsonReqObject;
import com.egolm.shop.common.utils.HttpUtil.SSLVersion;
public class QiyeWxServiceImpl implements QiyeWxService {
@Value("${qiyewx.corpid}")
private String corpid;
@Value("${qiyewx.corpsecret}")
private String corpsecret;
@Value("${qiyewx.baseUrl}")
private String baseUrl;
@Autowired
private RedisTemplate<String, String> redis;
@Override
public String getTokenString() {
String redisCacheKey = PrefixRedisTokenKey + corpid;
Long systemTime = System.currentTimeMillis();
String tokenObjectString = redis.opsForValue().get(redisCacheKey);
if(StringUtil.isNotBlank(tokenObjectString)) {
JSONObject tokenObject = JSON.parseObject(tokenObjectString);
Integer errcode = tokenObject.getInteger("errcode");
if(errcode == 0) {
Long expires_in = tokenObject.getLong("expires_in");
String loadTimeString = tokenObject.getString("loadTime");
if(expires_in != null && StringUtil.isNotBlank(loadTimeString)) {
Date loadTime = DateUtil.parse(loadTimeString, "yyyy-MM-dd HH:mm:ss");
Long expireTime = loadTime.getTime() + (expires_in*1000L);
if((expireTime - DevTime) > systemTime) {
String access_token = tokenObject.getString("access_token");
if(StringUtil.isNotBlank(access_token)) {
return access_token;
}
}
}
}
}
String requestUrl = baseUrl + MessageFormat.format(UrlGetToken, corpid, corpsecret);
HttpRespObject resp = HttpUtil.newInstance().setSSLVersion(SSLVersion.SSLv3).post(new HttpReqObject(requestUrl, null, null));
if(resp.getHttpCode() == 200) {
String text = resp.getResponseBody();
JSONObject tokenObject = JSON.parseObject(text);
Integer code = tokenObject.getInteger("errcode");
if(code == 0) {
tokenObject.put("loadTime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
redis.opsForValue().set(redisCacheKey, tokenObject.toJSONString());
return tokenObject.getString("access_token");
}
}
return null;
}
@Override
public void refreshToken() {
String redisCacheKey = PrefixRedisTokenKey + corpid;
redis.delete(redisCacheKey);
}
@Override
public void sendMessage(WxMessage wxMessage) {
String tokenString = this.getTokenString();
String requestUrl = baseUrl + MessageFormat.format(UrlSendMsg, tokenString);
Map<String, String> headers = new HashMap<String, String>();
headers.put("content-type", "application/x-www-form-urlencoded");
HttpRespObject resp = HttpUtil.newInstance().setSSLVersion(SSLVersion.SSLv3).postJson(new JsonReqObject(requestUrl, wxMessage, headers));
Integer httpCode = resp.getHttpCode();
if(httpCode == 200) {
}
}
}
package com.egolm.shop.api.service.impl;
import com.egolm.shop.api.service.WeixinTokenService;
public class WeixinTokenServiceImpl implements WeixinTokenService {
}
This diff is collapsed.
...@@ -76,3 +76,9 @@ redis.guest.key=B2B_Guest ...@@ -76,3 +76,9 @@ redis.guest.key=B2B_Guest
redis.sms.code.key=B2B_Sms redis.sms.code.key=B2B_Sms
qiyewx.corpid=ww0e86cf527e3e5218
qiyewx.corpsecret=n6weRHp134mpOF3AUPuyEDrUW8zdoG22wuk1hyfXuZAz
qiyewx.baseUrl=https://qyapi.weixin.qq.com/cgi-bin
qiyewx.msg.redirect.appid=wxa573a87e45933b56
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment