|
|
@@ -5,6 +5,10 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
+import com.goafanti.common.bo.Result;
|
|
|
+import com.goafanti.common.dao.AdminMapper;
|
|
|
+import com.goafanti.common.model.Admin;
|
|
|
+import com.goafanti.core.shiro.token.TokenManager;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -22,10 +26,17 @@ public class WeChatUtils {
|
|
|
private String appId;
|
|
|
@Value(value = "${wx.appSecret}")
|
|
|
private String appSecret;
|
|
|
+ @Value(value = "${wx.expense.appId}")
|
|
|
+ private String expenseAppId;
|
|
|
+ @Value(value = "${wx.expense.appSecret}")
|
|
|
+ private String expenseAppSecret;
|
|
|
@Value(value = "${wx.state}")
|
|
|
private String wxState;
|
|
|
@Autowired
|
|
|
private RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ private AdminMapper adminMapper;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 公出待审核通知
|
|
|
@@ -83,7 +94,7 @@ public class WeChatUtils {
|
|
|
private Integer SendHttpPost(Map<String, Object> map, String url) {
|
|
|
JSONObject res=HttpUtils.httpPost(url, map);
|
|
|
if (res.get("errcode")!=null) {
|
|
|
- LoggerUtils.debug(this.getClass(),res.getInteger("errcode").toString());
|
|
|
+ LoggerUtils.debug(this.getClass(),"errcode={%s},url={%s}",res.getInteger("errcode").toString(),url);
|
|
|
return res.getInteger("errcode");
|
|
|
}else {
|
|
|
return 1;
|
|
|
@@ -100,7 +111,7 @@ public class WeChatUtils {
|
|
|
*/
|
|
|
public Integer addExpenseAccountWeChatNotice(String openid ,Integer type,Integer id, Date date, String aname,String remarks) {
|
|
|
Map<String, Object> map=new HashMap<String, Object>();
|
|
|
- String accessToken=getAccessToken();
|
|
|
+ String accessToken=getExpenseAccountAccessToken();
|
|
|
String url=send_url.replace("ACCESS_TOKEN", accessToken);
|
|
|
map.put("access_token", accessToken);
|
|
|
map.put("touser", openid);
|
|
|
@@ -217,22 +228,57 @@ public class WeChatUtils {
|
|
|
LoggerUtils.debug(getClass(), "accestoken超时重新获取");
|
|
|
redisUtil.deleteString("access_token");
|
|
|
redisUtil.deleteString("expires_in");
|
|
|
- return setSessionAccessToken(date);
|
|
|
+ return setSessionAccessToken(date,0);
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
- return setSessionAccessToken(date);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
|
|
|
- private String setSessionAccessToken(Date date) {
|
|
|
- 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.httpGet(url);
|
|
|
String errcode =js.getString("errcode");
|
|
|
if (errcode!=null) {
|
|
|
@@ -245,9 +291,41 @@ public class WeChatUtils {
|
|
|
//返回时间为秒需要计算换成毫秒
|
|
|
expires_in=expires_in*1000;
|
|
|
expires_in+=date.getTime();
|
|
|
- redisUtil.setString("access_token", access_token);
|
|
|
- redisUtil.setString("expires_in", expires_in.toString());
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
+ public Integer setOpenId(String code, Integer type) {
|
|
|
+ if(type==null)type=0;
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?";
|
|
|
+ if (type==1){
|
|
|
+ url=url+"appid="+expenseAppId+"&secret="+expenseAppSecret+"&js_code="+code+"&grant_type=authorization_code";
|
|
|
+ }else{
|
|
|
+ url=url+"appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code";
|
|
|
+ }
|
|
|
+ JSONObject json= HttpUtils.httpGet(url);
|
|
|
+ System.out.println(json.toJSONString());
|
|
|
+ String openid=json.getString("openid");
|
|
|
+ if (StringUtils.isNotBlank(openid)) {
|
|
|
+ LoggerUtils.debug(getClass(), "获取"+(type==1?"报销":"公出")+"openid:"+openid);
|
|
|
+ Admin a=new Admin();
|
|
|
+ if (type!=null&&type==1){
|
|
|
+ a.setExpenseOpenId(openid);
|
|
|
+ }else {
|
|
|
+ a.setOpenId(openid);
|
|
|
+ }
|
|
|
+ a.setId(TokenManager.getAdminId());
|
|
|
+ adminMapper.updateByPrimaryKeySelective(a);
|
|
|
+ return 1;
|
|
|
+ }else {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|