5 changed files with 166 additions and 0 deletions
@ -0,0 +1,102 @@ |
|||
package com.yxt.yythmall.biz.wx; |
|||
|
|||
import com.alibaba.fastjson.JSONObject; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
public class WxConfig { |
|||
|
|||
private static final Logger L = LoggerFactory.getLogger(WxConfig.class); |
|||
|
|||
private static final long token_expires_in = 5400 * 1000; |
|||
private static final String WX_URL_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; |
|||
|
|||
//获取 ticket
|
|||
private static final String ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_card"; |
|||
private static String wx_ticket; |
|||
|
|||
private static final long ticket_expires_in = 5400 * 1000; |
|||
|
|||
private static long wx_ticket_create_timestamp; |
|||
|
|||
private static String wx_access_token; |
|||
private static long wx_token_create_timestamp; |
|||
private static String mp_access_token; |
|||
private static long mp_token_create_timestamp; |
|||
|
|||
public static final String WX_APPID = "wxc64c6cf0a06880e5"; |
|||
private static final String WX_APPSECRET = "eb3a0794da05f12a94c92134030246f1"; |
|||
public static final String MP_APPID = "wx05604ce2a8bede05"; |
|||
private static final String MP_APPSECRET = "3d36e8a61212cf773a2fa4e6c9a83334"; |
|||
|
|||
|
|||
/** |
|||
* 使用AppId和AppSecret获取AccessToken(公众号和小程序通用) |
|||
* |
|||
* @param appid |
|||
* @param secret |
|||
* @return |
|||
*/ |
|||
public static String getAccessToken(String appid, String secret) { |
|||
String url = WX_URL_ACCESS_TOKEN.replace("APPID", appid).replace("APPSECRET", secret); |
|||
String res = new RestTemplate().getForObject(url, String.class); |
|||
JSONObject jsonObject = JSONObject.parseObject(res); |
|||
String token = jsonObject.getString("access_token"); |
|||
return token; |
|||
} |
|||
|
|||
/** |
|||
* 获取公众号的AccessToken |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String wxAccessToken() { |
|||
if (StringUtils.isBlank(wx_access_token) || System.currentTimeMillis() > (wx_token_create_timestamp + token_expires_in)) { |
|||
wx_access_token = getAccessToken(WX_APPID, WX_APPSECRET); |
|||
wx_token_create_timestamp = System.currentTimeMillis(); |
|||
} |
|||
return wx_access_token; |
|||
} |
|||
|
|||
/** |
|||
* 获取小程序的AccessToken |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String mpAccessToken() { |
|||
if (StringUtils.isBlank(mp_access_token) || System.currentTimeMillis() > (mp_token_create_timestamp + token_expires_in)) { |
|||
mp_access_token = getAccessToken(MP_APPID, MP_APPSECRET); |
|||
mp_token_create_timestamp = System.currentTimeMillis(); |
|||
} |
|||
return mp_access_token; |
|||
} |
|||
|
|||
/** |
|||
* 获取ticket |
|||
* @param appid |
|||
* @param secret |
|||
* @return |
|||
*/ |
|||
public static String getTicket(String appid, String secret) { |
|||
String url = ticket.replace("ACCESS_TOKEN",wxAccessToken()); |
|||
String res = new RestTemplate().getForObject(url, String.class); |
|||
JSONObject jsonObject = JSONObject.parseObject(res); |
|||
String ticket = jsonObject.getString("ticket"); |
|||
return ticket; |
|||
} |
|||
|
|||
/** |
|||
* 获取公众号的ticket |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String wxTicket() { |
|||
if (StringUtils.isBlank(wx_ticket) || System.currentTimeMillis() > (wx_ticket_create_timestamp + ticket_expires_in)) { |
|||
wx_ticket = getTicket(WX_APPID, WX_APPSECRET); |
|||
wx_ticket_create_timestamp = System.currentTimeMillis(); |
|||
} |
|||
return wx_ticket; |
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
package com.yxt.yythmall.biz.wx.invoice; |
|||
|
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.yxt.common.core.result.ResultBean; |
|||
import com.yxt.yythmall.biz.wx.WxConfig; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author wangpengfei |
|||
* @date 2024/3/15 16:09 |
|||
*/ |
|||
public class ElectronicInvoice { |
|||
private static final String AUTHORIZATION_PAGE="https://api.weixin.qq.com/card/invoice/getauthurl?access_token=ACCESS_TOKEN"; |
|||
|
|||
public ResultBean getAuthorizationPage(String appid, String secret) { |
|||
ResultBean rb= ResultBean.fireFail(); |
|||
String url = AUTHORIZATION_PAGE.replace("ACCESS_TOKEN", WxConfig.wxAccessToken()); |
|||
Map<String, String> map = new HashMap<>(); |
|||
RequestMes res = new RestTemplate().postForObject(url,map,RequestMes.class); |
|||
if(!res.getErrcode().equals("0")){ |
|||
return rb; |
|||
} |
|||
return rb; |
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.yxt.yythmall.biz.wx.invoice; |
|||
|
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author wangpengfei |
|||
* @date 2024/3/15 17:26 |
|||
*/ |
|||
@Data |
|||
public class RequestMes { |
|||
private String errcode; |
|||
private String errmsg; |
|||
private String auth_url; |
|||
private String appid; |
|||
} |
Loading…
Reference in new issue