【微信小程序开发】用户一键登录 + 获取手机号 + 微信支付V3

一、技术点摘要

  1. 基于Springboot搭建微信小程序后端框架
  2. 获取并缓存微信api调用凭证AccessToken
  3. 封装微信一键登录、获取手机号API
  4. 封装微信支付客户端
  5. 封装函数式接口,统一捕获微信支付异常,减少重复代码

二、微信支付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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qiye丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值