• 使用springboot每日推送早安问候语到用户微信


    本文主要实现给不同的用户推动不同的问候模板

    准备工作

    申请微信公众平台的测试号

    申请微信测试公众号
    创建成功后,可以看到appid和appsecret,这个后面认证时需要
    在这里插入图片描述

    申请模板

    可自行修改

    今天是:{{now.DATA}} 不管那一天,每一天都是想你的一天 
    当前城市:{{city.DATA}} 
    今天的天气:{{text.DATA}}
    最低气温:{{low.DATA}} 度
    最高气温:{{high.DATA}} 度 
    今天是我们想恋的第:{{scq_day.DATA}} 天 
    距你的生日还有:{{bir_day.DATA}} 天 
    {{daily_english_cn.DATA}} 
    {{daily_english_en.DATA}}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    新建成功后,记得保存模板Id后续有用
    在这里插入图片描述

    扫描关注该测试公众号

    扫码关注后,记录微信号,后续推送通知用

    在这里插入图片描述

    申请百度天气

    申请百度天气

    完成认证后,创建一个应用,保存ak
    在这里插入图片描述

    申请天行数据的接口

    申请天行数据

    • 申请彩虹屁
    • 申请每日一句

    记得保存申请接口的key

    在这里插入图片描述

    开发工作

    技术栈

    • springboot 2.7.5
    • swagger 3.0
    • mysql 8.x
    • spring data jpa
    • openfeign

    依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>com.zhaogroupId>
        <artifactId>daily-weatherartifactId>
        <version>1.0.0version>
        <name>daily-weathername>
        <description>微信用户推送消息description>
    
        <properties>
            <java.version>1.8java.version>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <spring-boot.version>2.7.5spring-boot.version>
            <swagger.version>3.0.0swagger.version>
            <knife4j.version>3.0.3knife4j.version>
            <fastjson.version>2.0.15fastjson.version>
            <openfeign.version>3.1.3openfeign.version>
            <hutools.version>5.7.5hutools.version>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>cn.hutoolgroupId>
                <artifactId>hutool-coreartifactId>
                <version>${hutools.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.fastjson2groupId>
                <artifactId>fastjson2artifactId>
                <version>${fastjson.version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-openfeignartifactId>
                <version>${openfeign.version}version>
            dependency>
            
            <dependency>
                <groupId>com.github.binarywanggroupId>
                <artifactId>weixin-java-mpartifactId>
                <version>3.3.0version>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-boot-starterartifactId>
                <version>${swagger.version}version>
            dependency>
            
            <dependency>
                <groupId>com.github.xiaoymingroupId>
                <artifactId>knife4j-spring-boot-starterartifactId>
                <version>${knife4j.version}version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-jpaartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <scope>runtimescope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintagegroupId>
                        <artifactId>junit-vintage-engineartifactId>
                    exclusion>
                exclusions>
            dependency>
        dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>${spring-boot.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.1version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                        <encoding>UTF-8encoding>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.3.7.RELEASEversion>
                    <configuration>
                        <mainClass>com.zhao.wechat.DailyWeatherApplicationmainClass>
                    configuration>
                    <executions>
                        <execution>
                            <id>repackageid>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    配置文件

    server:
      port: 43310
    # 微信配置
    tencent:
      wechat:
        appId: 微信appid
        appSecret: 微信appSecret
      # 模板消息集合
      templatelist:
        - type: 1
          templateId: 微信模板id
        - type: 2
          templateId: 微信模板id
        - type: 3
          templateId: 微信模板id
    # 百度天气配置
    baidu:
      server: https://api.map.baidu.com
      ak: 百度数据的ak
    # 天行数据
    tianxin:
      server: http://api.tianapi.com
      key: 天行数据的key
    spring:
      mvc:
        pathmatch:
          matching-strategy: ant_path_matcher
      datasource:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://ip:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: 564929
        hikari:
          minimum-idle: 5
          # 空闲连接存活最大时间,默认600000(10分钟)
          idle-timeout: 180000
          # 连接池最大连接数,默认是10
          maximum-pool-size: 10
          # 此属性控制从池返回的连接的默认自动提交行为,默认值:true
          auto-commit: true
          # 连接池名称
          pool-name: MyHikariCP
          # 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
          max-lifetime: 1800000
          # 数据库连接超时时间,默认30秒,即30000
          connection-timeout: 30000
          connection-test-query: SELECT 1
      jpa:
        show-sql: true # 默认false,在日志里显示执行的sql语句
        database: mysql
        database-platform: org.hibernate.dialect.MySQL5Dialect
        hibernate:
          ddl-auto: update #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会 新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建
          naming:
            #指定jpa的自动表生成策略,驼峰自动映射为下划线格式7
            implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
            #physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    # 日志
    #logging:
    #  config: classpath:logback-spring-dev.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    统一数据封装

    package com.zhao.wechat.advice;
    
    
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.zhao.wechat.common.Result;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.core.MethodParameter;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
    
    /**
     * 对前端响应结果统一封装
     */
    @Slf4j
    @RestControllerAdvice(basePackages = {"com.zhao.wechat.rest"})
    public class ApiResponseAdvice implements ResponseBodyAdvice<Object> {
    
    
    	/**
    	 * 打印统一请求响应规范
    	 */
    	ApiResponseAdvice(){
    		log.info("启动请求统一响应规范... ...");
    	}
    
    
    	/**
    	 * 判断是否需要对返回值进行封装
    	 * @param returnType the return type
    	 * @param converterType the selected converter type
    	 * @return
    	 */
    	@Override
    	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    		// 如果返回的结果是Result.class类型就不用封装
    		if (returnType.getParameterType().equals(Result.class)){
    			return false;
    		}
    		return true;
    	}
    
    	/**
    	 * 对返回前端的值统一封装
    	 * @param body the body to be written
    	 * @param returnType the return type of the controller method
    	 * @param selectedContentType the content type selected through content negotiation
    	 * @param selectedConverterType the converter type selected to write to the response
    	 * @param request the current request
    	 * @param response the current response
    	 * @return
    	 */
    	@Override
    	public Object beforeBodyWrite(Object body,
    								  MethodParameter returnType,
    								  MediaType selectedContentType,
    								  Class<? extends HttpMessageConverter<?>> selectedConverterType,
    								  ServerHttpRequest request,
    								  ServerHttpResponse response) {
    	 if (returnType.getParameterType().equals(String.class)){
    		 // 如果是String类需要特殊处理
    		 ObjectMapper objectMapper = new ObjectMapper();
    		 try {
    			 // 设置响应数据格式为json
    			 response.getHeaders().add("content-type","application/json;charset=UTF-8");
    			 return objectMapper.writeValueAsString(Result.success(body));
    		 } catch (JsonProcessingException e) {
    			 throw new RuntimeException(e);
    		 }
    	 }
    	 return Result.success(body);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    工程结构

    在这里插入图片描述

    用户controller

    package com.zhao.wechat.rest;
    
    import com.zhao.wechat.domain.UserInfo;
    import com.zhao.wechat.service.UserInfoService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.web.PageableDefault;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * 用户信息管理视图层
     */
    @RestController
    @RequestMapping("/userInfo")
    @Api(tags = "用户信息管理")
    public class UserInfoRest {
    
    	@Autowired
    	private UserInfoService userInfoService;
    
    	/**
    	 * 分页获取所有的用户
    	 * @return
    	 */
    	@ApiOperation(value = "分页获取所有的用户")
    	@PostMapping("/queryPage")
    	public Page<UserInfo> queryUserPage(@PageableDefault Pageable pageable){
    		Page<UserInfo> userInfos = userInfoService.queryUserPage(pageable.getPageNumber(), pageable.getPageSize());
    		return userInfos;
    	}
    
    	/**
    	 * 添加或者修改用户
    	 */
    	@ApiOperation(value = "添加或者修改用户")
    	@PostMapping("/saveOrUpdate")
        public	void saveOrUpdate(@RequestBody UserInfo userInfo){
    		userInfoService.saveOrUpdate(userInfo);
    	}
    
    	/**
    	 * 删除用户
    	 */
    	@ApiOperation(value = "删除用户")
    	@DeleteMapping("/delete")
    	public void deleteUserById(@RequestParam("id") Long id){
    		userInfoService.deleteUserById(id);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    调用第三方接口

    package com.zhao.wechat.remote;
    
    import com.zhao.wechat.remote.param.TianXinParam;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.cloud.openfeign.SpringQueryMap;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.GetMapping;
    
    /**
     * 第三方接口天行数据
     */
    @FeignClient(value = "TianDataRemoteClient",url = "${tianxin.server}")
    public interface TianDataRemoteClient {
    
    	/**
    	 * 获取彩虹屁
    	 */
    	@GetMapping(value = "/caihongpi/index",
    			consumes = {MediaType.APPLICATION_JSON_VALUE},
    			produces = {MediaType.APPLICATION_JSON_VALUE}
    	)
    	String queryRainbow(@SpringQueryMap TianXinParam tianXinParam);
    
    	/**
    	 * 获取优美的句子
    	 */
    	@GetMapping(value = "/ensentence/index",
    			consumes = {MediaType.APPLICATION_JSON_VALUE},
    			produces = {MediaType.APPLICATION_JSON_VALUE}
    	)
    	String queryEnsentence(@SpringQueryMap TianXinParam tianXinParam);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    推送微信数据

    package com.zhao.wechat.service;
    
    
    import cn.hutool.core.date.DatePattern;
    import cn.hutool.core.date.DateUnit;
    import cn.hutool.core.date.DateUtil;
    import cn.hutool.core.util.StrUtil;
    import com.alibaba.fastjson2.JSON;
    import com.zhao.wechat.config.WechatConfig;
    
    import com.zhao.wechat.config.WechatTemplate;
    import com.zhao.wechat.config.WechatTemplateList;
    import com.zhao.wechat.domain.*;
    import com.zhao.wechat.remote.BaiduWeatherRemoteClient;
    import com.zhao.wechat.remote.TianDataRemoteClient;
    import com.zhao.wechat.remote.param.BaiduWeatherParam;
    import com.zhao.wechat.remote.param.TianXinParam;
    import lombok.extern.slf4j.Slf4j;
    import me.chanjar.weixin.common.error.WxErrorException;
    import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
    import me.chanjar.weixin.mp.api.WxMpService;
    import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
    import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
    import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.util.CollectionUtils;
    
    
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.util.Objects;
    
    @Slf4j
    @Service
    public class PushDailyWechatImpl implements PushDailyWechat{
    	@Autowired
    	private UserInfoService userInfoService;
    	@Autowired
    	private BaiduWeatherRemoteClient baiduWeatherRemoteClient;
    	@Autowired
    	private TianDataRemoteClient tianDataRemoteClient;
    	/** 微信配置类 **/
    	@Autowired
    	private WechatConfig wechatConfig;
    	/** 模板列表**/
    	@Autowired
    	private WechatTemplateList wechatTemplateList;
    
    	/** ak **/
    	@Value("${baidu.ak}")
    	private String ak;
    
    	/** key **/
    	@Value("${tianxin.key}")
    	private String key;
    
    
    	/**
    	 * 给不同的用户推送消息
    	 */
    	@Override
    	public void pushWechat() throws WxErrorException {
    		// 获取用户列表
    		List<UserInfo> userInfoList = userInfoService.listUserInfo();
    		if (!CollectionUtils.isEmpty(userInfoList)){
    			// 根据用户的type类型和模板type进行匹配
    			for (UserInfo userInfo : userInfoList) {
    				for (WechatTemplate template : wechatTemplateList.getTemplatelist()) {
    					if (userInfo.getType().equals(template.getType())){
    						this.wechatData(userInfo.getWechatId(), template.getTemplateId(),userInfo);
    					}
    				}
    			}
    		}
    	}
    
    	/**
    	 * 封装微信数据
    	 * @param wechatId
    	 * @param templateId
    	 */
    	private void wechatData(String wechatId,String templateId,UserInfo userInfo) throws WxErrorException {
    		// 创建配置信息
    		WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
    		wxStorage.setAppId(wechatConfig.getAppId());
    		wxStorage.setSecret(wechatConfig.getAppSecret());
    		WxMpService wxMpService = new WxMpServiceImpl();
    		wxMpService.setWxMpConfigStorage(wxStorage);
    
    		// 创建模板信息
    		WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
    				.toUser(wechatId)
    				.templateId(templateId)
    				.build();
    		// 获取天气预报信息
    		BaiduWeatherParam baiduWeatherParam = BaiduWeatherParam.builder()
    				.district_id(userInfo.getDistrictId())
    				.data_type("all")
    				.ak(this.ak)
    				.build();
    		String queryWeather = baiduWeatherRemoteClient.queryWeather(baiduWeatherParam);
    		log.info("查询的百度天气信息为:{}",queryWeather);
    		BaiduNowWeather baiduNowWeather = ApiResponse.parseBaiduNowData(queryWeather, BaiduNowWeather.class);
    		List<BaiduForecastsWeather> baiduForecastsWeatherList = ApiResponse.parseBaiduForecastsData(queryWeather, BaiduForecastsWeather.class);
    		log.info("baiduNowWeather:{},baiduForecastsWeather:{}",baiduNowWeather,baiduForecastsWeatherList);
    		// 获取彩虹屁
    		TianXinParam tianXinParam = TianXinParam.builder().key(this.key).build();
    		String queryRainbow = tianDataRemoteClient.queryRainbow(tianXinParam);
    		List<TianRainbow> rainbowList = ApiResponse.parseTianData(queryRainbow, TianRainbow.class);
    
    		// 获取每日一句
    		String queryEnsentence = tianDataRemoteClient.queryEnsentence(tianXinParam);
    		List<TianEnsentence> tianEnsentenceList = ApiResponse.parseTianData(queryEnsentence, TianEnsentence.class);
    
    		// 封装模板数据
    		templateMessage.addData(new WxMpTemplateData("now", this.pareDateNow(baiduForecastsWeatherList.get(0)),"#FFB6C1"));
    		templateMessage.addData(new WxMpTemplateData("city",userInfo.getCity(),"#B95EA6"));
    		templateMessage.addData(new WxMpTemplateData("text",baiduNowWeather.getText(),"#173177"));
    		templateMessage.addData(new WxMpTemplateData("high",baiduForecastsWeatherList.get(0).getHigh(),"#87cefa"));
    		templateMessage.addData(new WxMpTemplateData("low",baiduForecastsWeatherList.get(0).getLow(),"#FF6347"));
    		templateMessage.addData(new WxMpTemplateData("scq_day",this.calScqDate(userInfo),"#FF1493"));
    		templateMessage.addData(new WxMpTemplateData("bir_day",this.calBirData(userInfo),"#FF00FF" ));
    		templateMessage.addData(new WxMpTemplateData("daily_english_cn",rainbowList.get(0).getContent(),"#800080"));
    		templateMessage.addData(new WxMpTemplateData("daily_english_en",tianEnsentenceList.get(0).getEn(),"#FFA500"));
    
    		log.info("发送的消息为:{}", JSON.toJSONString(templateMessage));
    		wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
    	}
    
    	/**
    	 * 计算想认识/想恋日期
    	 * @return
    	 */
    	private String calScqDate(UserInfo userInfo){
    		// 获取第一次想认识的时间
    		if (Objects.nonNull(userInfo)){
    			Date scqTime = userInfo.getScqTime();
    			// 计算时间差
    			long between = DateUtil.between(scqTime, DateUtil.date(), DateUnit.DAY);
    			return String.valueOf(between);
    		}
    		return "";
    	}
    
    	/**
    	 * 计算生日
    	 * @param userInfo
    	 * @return
    	 */
    	private String calBirData(UserInfo userInfo){
    		// 获取用户的出生日期
    		if (Objects.nonNull(userInfo)){
    			Date birTime = userInfo.getBirTime();
    			// 今日日期
    			Calendar today = Calendar.getInstance();
    			// 出生日期
    			Calendar birthDay = Calendar.getInstance();
    			// 设置生日
    			birthDay.setTime(birTime);
    			// 修改为本年
    			int bir;
    			birthDay.set(Calendar.YEAR,today.get(Calendar.YEAR));
    			if (birthDay.get(Calendar.DAY_OF_YEAR) < today.get(Calendar.DAY_OF_YEAR)){
    				// 生日已经过了,计算明年的
    				bir = today.getActualMaximum(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
    				bir += birthDay.get(Calendar.DAY_OF_YEAR);
    			} else {
    				// 生日还没过
    				bir = birthDay.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
    			}
    			return String.valueOf(bir);
    		}
    		return "";
    	}
    
    
    	/**
    	 * 拼接今日时间
    	 * @return
    	 */
    	private String pareDateNow(BaiduForecastsWeather baiduForecastsWeather){
    		// 获取当前日期
    		String now =  DateUtil.format(DateUtil.date(), DatePattern.CHINESE_DATE_PATTERN);
    		// 获取星期几
    		String week = baiduForecastsWeather.getWeek();
    		return now+" "+week;
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193

    定时任务

    package com.zhao.wechat.job;
    
    import com.zhao.wechat.service.PushDailyWechat;
    import me.chanjar.weixin.common.error.WxErrorException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    /**
     * 推送消息到用户定时任务
     */
    @Component
    public class PushWechatJob {
    
    	@Autowired
        private PushDailyWechat pushDailyWechat;
    
    	/**
    	 * 每天早上7点推送到微信
    	 * @throws WxErrorException
    	 */
    	@Scheduled(cron = "0 30 7 1/1 * ? ")
    	void  doJob() throws WxErrorException {
    		pushDailyWechat.pushWechat();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    效果

    在这里插入图片描述

    推送到微信

    在这里插入图片描述

  • 相关阅读:
    加窗函数后频谱幅值发生了变化的修正技巧
    OCR之CRNN模型简单理解
    5款实用的Redis可视化工具
    【无标题】
    React封装自定义表单校验方法
    drf-分页,coreapi自动生成接口文档
    FlinkCDC菜鸟教程/演示 Postgres 基于Flink CDC 导入 Elasticsearch
    FL Studio21.2宿主软件中文免费版下载
    【vue3-ts】-小兔鲜儿项目2022新版-系列开篇
    1Panel:一个现代化、开源的 Linux 服务器运维管理面板
  • 原文地址:https://blog.csdn.net/weixin_45206218/article/details/127872136