xiaoyong931011
2022-08-10 24e986fa22f7e1b94c0b2197fb1b62ae51023cef
20220810
3 files modified
64 ■■■■ changed files
src/main/java/cc/mrbird/febs/FebsShiroApplication.java 13 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java 24 ●●●●● patch | view | raw | blame | history
src/test/java/cc/mrbird/febs/ProfitTest.java 27 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/FebsShiroApplication.java
@@ -4,9 +4,14 @@
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
/**
 * @author MrBird
@@ -24,4 +29,12 @@
                .run(args);
    }
    @Bean
    RestTemplate getRestTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        //解决中文乱码问题
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
}
src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java
@@ -6,13 +6,16 @@
import cc.mrbird.febs.common.utils.SpringContextHolder;
import cc.mrbird.febs.pay.util.WechatConfigure;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -25,26 +28,29 @@
    @Autowired
    private RedisUtils redisUtils;
    @Resource
    RestTemplate restTemplate;
    /**
     * 获取access_token
     * 有效期两小时
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void getAccessToken() throws IOException {
        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);
        Map<String,String> params = new HashMap<>();
        params.put("grant_type","client_credential");
        params.put("appid",appId);
        params.put("secret",appSecret);
        //发送请求
        String result = HttpCurlUtil.sendGetHttp(WechatConfigure.WX_ACCESS_TOKEN , params);
        /**
         * 返回结果
         * {"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(result);
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String accessToken = jsonObject.getString(accessTokenKey);
        if (StrUtil.isEmpty(accessToken)) {
            log.error("获取access token失败: {}" , jsonObject.getString("errmsg"));
src/test/java/cc/mrbird/febs/ProfitTest.java
@@ -7,10 +7,14 @@
import cc.mrbird.febs.mall.service.IMallAchieveService;
import cc.mrbird.febs.mall.service.IMemberProfitService;
import cc.mrbird.febs.rabbit.consumer.AgentConsumer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
@@ -92,4 +96,27 @@
    public void changeAmount(Map<String, Integer> amount) {
        amount.put("amount", 2);
    }
    @Resource
    RestTemplate restTemplate;
    @Test
    public void getWeChatAccessToken() {
        String appId = "wx5cc58f796224af61";
        String appSecret = "71403646f666f9b9dca308d4f357765c";
        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失败");
        }
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        System.out.println(jsonObject.get("access_token").toString());
    }
}