/**
* 微信授权
*
* @param response
* @return
* @throws IOException
*/
@GetMapping("/getWeChat")
public ResultMap getWeChat(HttpServletResponse response) throws IOException {
// 1、redirect_uri设置
String redict = URLEncoder.encode(weChatConfig.authCallBack, "UTF-8");
// 2、获取code的url
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+weChatConfig.getAppID()+"&redirect_uri="+redict+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
// 3、重定向去微信获取code,之后去回掉接口/getWxAccessToken.html
// return "redirect:" + url;
//response.sendRedirect(url);
return ResultMap.ok().put("RedirectURL",url);
}
// 以上系统中对应的参数都是通过系统配置文件中进行配置
下面是对应的参数读取类:
package com.ssdz.gwxb.wxPay.config;
import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.InputStream;
@Component
@ConfigurationProperties(prefix="wxchat")
public class WxPayAppConfig implements WXPayConfig {
/**
* appID
*/
private String appID;
/**
* 商户号
*/
private String mchID;
/**
* API 密钥
*/
private String key;
/**
* API证书绝对路径 (本项目放在了 resources/cert/wxpay/apiclient_cert.p12")
*/
private String certPath;
/**
* HTTP(S) 连接超时时间,单位毫秒
*/
private int httpConnectTimeoutMs = 8000;
/**
* HTTP(S) 读数据超时时间,单位毫秒
*/
private int httpReadTimeoutMs = 10000;
/**
* 微信支付异步通知地址
*/
private String payNotifyUrl;
/**
* 微信退款异步通知地址
*/
private String refundNotifyUrl;
@Value("${wxchat.appSecret}")
public String appSecret;
@Value("${wxchat.serverIP}")
private String serverIP;
@Value("${wxchat.apiAuth}")
public String apiAuth;
@Value("${wxchat.apiAuthAccessToken}")
public String apiAuthAccessToken;
@Value("${wxchat.apiUerInfo}")
public String apiUerInfo;
@Value("${wxchat.authCallBack}")
public String authCallBack;
@Value("${wxchat.isTest}")
public boolean isTest;
/**
* 获取商户证书内容(这里证书需要到微信商户平台进行下载)
*
* @return 商户证书内容
*/
@Override
public InputStream getCertStream() {
InputStream certStream =getClass().getClassLoader().getResourceAsStream(certPath);
return certStream;
}
public String getServerIP() {
return serverIP;
}
public void setServerIP(String serverip) {
this.serverIP = serverip;
}
public String getAppID() {
return appID;
}
public void setAppID(String appID) {
this.appID = appID;
}
public String getMchID() {
return mchID;
}
public void setMchID(String mchID) {
this.mchID = mchID;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getCertPath() {
return certPath;
}
public void setCertPath(String certPath) {
this.certPath = certPath;
}
public int getHttpConnectTimeoutMs() {
return httpConnectTimeoutMs;
}
public void setHttpConnectTimeoutMs(int httpConnectTimeoutMs) {
this.httpConnectTimeoutMs = httpConnectTimeoutMs;
}
public int getHttpReadTimeoutMs() {
return httpReadTimeoutMs;
}
public void setHttpReadTimeoutMs(int httpReadTimeoutMs) {
this.httpReadTimeoutMs = httpReadTimeoutMs;
}
public String getPayNotifyUrl() {
return payNotifyUrl;
}
public void setPayNotifyUrl(String payNotifyUrl) {
this.payNotifyUrl = payNotifyUrl;
}
public String getRefundNotifyUrl() {
return refundNotifyUrl;
}
public void setRefundNotifyUrl(String refundNotifyUrl) {
this.refundNotifyUrl = refundNotifyUrl;
}
}