【微信小程序开发】用户一键登录 + 获取手机号 + 微信支付V3
一、技术点摘要
- 基于Springboot搭建微信小程序后端框架
- 获取并缓存微信api调用凭证AccessToken
- 封装微信一键登录、获取手机号API
- 封装微信支付客户端
- 封装函数式接口,统一捕获微信支付异常,减少重复代码
二、微信支付pom依赖
<!-- 微信支付 -->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.11</version>
</dependency>
三、微信相关配置
3.1 配置文件
ai-wxapp:
wechat:
# 应用id(小程序id)
appid: xxxxx
# 应用秘钥(小程序秘钥)
secret: xxxxxx
wechat-pay:
# 商户号
merchantId: xxxxxx
# 商户API私钥路径
privateKeyPath: D:\\Develop\\Wechat\\apiclient_key.pem
# 商户证书序列号
merchantSerialNumber: xxxxxx
# 商户APIv3密钥
apiV3Key: xxxxxx
# 支付成功回调url,可公网https访问的
payNotifyUrl: https://xxxxxx/wx/pay/payCallback
# 退款成功回调url,可公网https访问的
refundNotifyUrl: xxx
3.2 配置类
@Component
@ConfigurationProperties(prefix = "ai-wxapp.wechat")
@Data
public class WechatAppConfig {
private String appid;
private String secret;
}
@Data
@Component
@ConfigurationProperties(prefix = "ai-wxapp.wechat-pay")
public class WechatPayConfig {
/**
* 商户id
*/
private String merchantId;
/**
* 商户API私钥
*/
private String privateKeyPath;
/**
* 商户证书序列号
*/
private String merchantSerialNumber;
/**
* 商户APIv3密钥
*/
private String apiV3Key;
/**
* 支付成功回调url,可公网https访问的
*/
private String payNotifyUrl;
/**
* 退款成功回调url,可公网https访问的
*/
private String refundNotifyUrl;
}
四、微信一键登录+获取手机号
4.1 缓存微信api调用凭证:AccessToken
微信开放文档:获取接口调用凭据
@Slf4j
@Component
public class WechatAccessTokenService {
@Resource
private RedisUtils redisUtils;
@Resource
private WechatAppConfig wechatConfig;
private final String getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
private final String access_token = "access_token";
private final String expires_in = "expires_in";
/**
* 获取微信api调用凭证,可用于后续获取手机号等
*
* @return AccessToken
* @author shn 2024/05/24 12:33
*/
@Nullable
public String getAccessToken() {
String key = RedisKeys.WX_ACCESS_TOKEN;
// 先查缓存
if (redisUtils.hasKey(key) && redisUtils.getExpire(key) > 0L) {
return redisUtils.get(key).toString();
}
// 缓存中没有,则调用微信接口获取
Map<String, String> params = new HashMap<>();
params.put("appid", wechatConfig.getAppid());
params.put("secret", wechatConfig.getSecret());
params.put("grant_type", "client_credential");
String response = OkHttpUtil.httpGet(getAccessTokenUrl, null, params);
if (StringUtils.isBlank(response)) {
log.error("调用微信API获取AccessToken失败");
return null;
}
JSONObject jsonObject = JSON.parseObject(response);
if (StringUtils.isBlank(jsonObject.getString(access_token))) {
return null;
}
// 凭证有效时间,单位:秒。目前是7200秒之内的值。
int expiresIn = jsonObject.getIntValue(expires_in);
// 缓存并设置过期时间,设置为提前30秒过期
redisUtils.set(key, jsonObject.getString(access_token), expiresIn - 30, TimeUnit.SECONDS);
return jsonObject.getString(access_token);
}
}
4.2 微信一键登录 + 获取手机号
@Slf4j
@Component
public class WechatAuthApiService {
@Resource
private WechatAppConfig wechatConfig;
@Resource
private WechatAccessTokenService accessTokenService;
private final String code2SessionUrl = "https://api.weixin.qq.com/sns/jscode2session";
private final String session_key = "session_key";
private final String unionid = "unionid";
private final String openid = "openid";
private final String getPhoneNumberUrl

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



