KKSU
2025-01-16 b174249fbe2d7f41505ba40cfefee52e777eded2
config(xcx): 更新小程序配置并修复AccessToken获取

- 更新小程序appid和secret
- 修复定时任务获取微信AccessToken的逻辑
- 优化日志输出和异常处理
2 files modified
69 ■■■■■ changed files
src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java 65 ●●●●● patch | view | raw | blame | history
src/main/resources/application-prod.yml 4 ●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java
@@ -1,11 +1,17 @@
package cc.mrbird.febs.mall.quartz;
import cc.mrbird.febs.common.properties.XcxProperties;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.common.utils.SpringContextHolder;
import cc.mrbird.febs.mall.entity.MallMemberCoupon;
import cc.mrbird.febs.mall.mapper.MallMemberCouponMapper;
import cc.mrbird.febs.mall.service.MallInvoiceService;
import cc.mrbird.febs.pay.util.WechatConfigure;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -14,6 +20,7 @@
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@Slf4j
@@ -21,7 +28,7 @@
@ConditionalOnProperty(prefix = "system", name = "job", havingValue = "true")
public class WxxcxJob {
//    private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
    private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
    @Autowired
    private RedisUtils redisUtils;
@@ -37,34 +44,34 @@
     * 获取access_token
     * 有效期两小时
     */
//    @Scheduled(cron = "0 0 0/1 * * ? ")
//    public void getAccessToken() throws IOException {
//        log.info("执行access_token刷新");
//        String appId = xcxProperties.getXcxAppid();
//        String appSecret = xcxProperties.getXcxSecret();
//        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
//        String jsonStr = restTemplate.getForObject(url, String.class);
//
//        /**
//         * 返回结果
//         * {"access_token":"ACCESS_TOKEN","expires_in":7200}
//         */
//        if (!jsonStr.contains("access_token")) {
//            System.out.println("获取微信access_token失败");
//        }
//
//        String accessTokenKey = WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY;
//        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
//        String accessToken = jsonObject.getString(accessTokenKey);
//        if (StrUtil.isEmpty(accessToken)) {
//            log.error("获取access token失败: {}" , jsonObject.getString("errmsg"));
//            throw new ApiException("获取access token失败");
//        } else {
//            log.info("wx access_token : {}",accessToken);
//            redisUtils.set(accessTokenKey,accessToken);
//        }
//
//    }
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void getAccessToken() throws IOException {
        log.info("执行access_token刷新");
        String appId = xcxProperties.getXcxAppid();
        String appSecret = xcxProperties.getXcxSecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
        String jsonStr = restTemplate.getForObject(url, String.class);
        /**
         * 返回结果
         * {"access_token":"ACCESS_TOKEN","expires_in":7200}
         */
        if (!jsonStr.contains("access_token")) {
            System.out.println("获取微信access_token失败");
        }
        String accessTokenKey = WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY;
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String accessToken = jsonObject.getString(accessTokenKey);
        if (StrUtil.isEmpty(accessToken)) {
            log.error("获取access token失败: {}" , jsonObject.getString("errmsg"));
            throw new ApiException("获取access token失败");
        } else {
            log.info("wx access_token : {}",accessToken);
            redisUtils.set(accessTokenKey,accessToken);
        }
    }
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void expireCoupon() {
src/main/resources/application-prod.yml
@@ -61,8 +61,8 @@
xcx:
  wechar_login_url: https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code
  xcx_appid: wxad2fdb2fcad10fb2
  xcx_secret: 9bb58d655bff419feec3a6d948ca8b8d
  xcx_appid: wx526879ea8589ba00
  xcx_secret: a4aed487b1b438075776151d0312b36a
  debug: false
  wecharPaynotifyUrl: http://api.blnka.cn/api/xcxPay/wxpayCallback
  wecharRechargePaynotifyUrl: http://api.blnka.cn/api/xcxPay/rechargeCallBack