先上一波效果图!
一、微信公众号测试平台
地址: http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
需要的数据:
二、代码实现
纪念日工具类:
package Monster.weixin.tuisong.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* @ClassName JiNianRi
* @Description TODO
* @Author Monster
* @Date 2022/8/26 17:32
*/
public class JiNianRi {
/**
* 恋爱
*/
static String lianAi = "2022-02-15";
/**
* 领证
*/
static String linZheng = "2023-02-15";
/**
* 结婚
*/
static String jieHun = "2022-07-08";
/**
* 生日
*/
static String shengRi = "2023-04-19";
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* 过去多少天
* @param date
* @return
*/
public static int before(String date) {
int day = 0;
try {
long time = System.currentTimeMillis() - simpleDateFormat.parse(date).getTime();
day = (int) (time / 86400000L);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
/**
* 还剩多少天
* @param date
* @return
*/
public static int after(String date) {
int day = 0;
try {
long time = simpleDateFormat.parse(date).getTime() - System.currentTimeMillis();
day = (int) (time / 86400000L);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
public static int getJieHun() {
return before(jieHun);
}
public static int getLinZheng() {
return before(linZheng);
}
public static int getLianAi() {
return before(lianAi);
}
public static int getShengRi(){
return after(shengRi);
}
public static void main(String[] args) {
System.out.println(getJieHun());
}
}
请求http的帮助类:
package Monster.weixin.tuisong.util;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/**
* 请求http的帮助类
*/
public class HttpUtil {
static final int retry = 3;
static PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
static ConnectionKeepAliveStrategy myStrategy;
public HttpUtil() {
}
public static String doPost(String url, String data) {
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setKeepAliveStrategy(myStrategy).setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).build();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(20000).setConnectionRequestTimeout(10000).build();
httpPost.setConfig(requestConfig);
String context = "";
if (data != null && data.length() > 0) {
StringEntity body = new StringEntity(data, "utf-8");
httpPost.setEntity(body);
}
httpPost.addHeader("Content-Type", "application/json");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
context = EntityUtils.toString(entity, "UTF-8");
} catch (Exception var16) {
var16.getStackTrace();
} finally {
try {
response.close();
httpPost.abort();
} catch (Exception var15) {
var15.getStackTrace();
}
}
return context;
}
public static String getUrl(String url) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
String var7;
try {
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Connection", "close");
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(18000).setConnectTimeout(5000).setConnectionRequestTimeout(18000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
Object entity;
if (response1.getStatusLine().getStatusCode() != 200) {
if (response1.getStatusLine().getStatusCode() != 404) {
return null;
}
entity = "";
return (String)entity;
}
entity = response1.getEntity();
String result = EntityUtils.toString((HttpEntity)entity);
EntityUtils.consume((HttpEntity)entity);
var7 = result;
} finally {
response1.close();
}
} finally {
httpclient.close();
}
return var7;
}
static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception var2) {
}
}
}
static void close(InputStream inputStream) {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception var2) {
var2.printStackTrace();
}
}
}
static {
connectionManager.setMaxTotal(1000);
connectionManager.setDefaultMaxPerRoute(1000);
myStrategy = new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
BasicHeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Keep-Alive"));
String param;
String value;
do {
if (!it.hasNext()) {
return 60000L;
}
HeaderElement he = it.nextElement();
param = he.getName();
value = he.getValue();
} while(value == null || !param.equalsIgnoreCase("timeout"));
return Long.parseLong(value) * 1000L;
}
};
}
}
百度地图API:https://lbsyun.baidu.com/apiconsole/key#/home
package Monster.weixin.tuisong.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @ClassName Tianqi
* @Description TODO
* @Author Monster
* @Date 2022/8/26 16:45
*/
public class Tianqi {
private static String ak = "你的百度地图API密钥ak"; // 百度地图API密钥ak
private static String district_id = "440400"; //珠海行政区划代码
// private static String district_id = "440100"; //广州行政区划代码
public static JSONObject getNanjiTianqi() {
String result = null;
JSONObject today = new JSONObject();
try {
result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getString("message").equals("success")) {
JSONArray arr = jsonObject.getJSONObject("result").getJSONArray("forecasts");
String location = jsonObject.getJSONObject("result").getString("location");
String now = jsonObject.getJSONObject("result").getString("now");
String s = (location + now).replace("}{", ",");
String ss = arr.getJSONObject(0).toString();
String msg = (s + ss).replace("}{", ",");
today = JSONObject.parseObject(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
return today;
}
public static void main(String[] args) {
System.out.println(getNanjiTianqi());
}
}
彩虹屁API:https://www.tianapi.com/apiview/181
package Monster.weixin.tuisong.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @ClassName CaiHongPi
* @Description TODO
* @Author Monster
* @Date 2022/8/26 17:26
*/
public class CaiHongPi {
private static String key = "你的彩虹屁key";
private static String url = "http://api.tianapi.com/caihongpi/index?key=";
private static List<String> jinJuList = new ArrayList<>();
private static String name = "老婆";
public static String getCaiHongPi() {
//默认彩虹屁
String str = "阳光落在屋里,爱你藏在心里";
try {
JSONObject jsonObject = JSONObject.parseObject(HttpUtil.getUrl(url+key).replace("XXX", name));
if (jsonObject.getIntValue("code") == 200) {
str = jsonObject.getJSONArray("newslist").getJSONObject(0).getString("content");
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
/**
* 载入金句库
*/
static {
InputStream inputStream = CaiHongPi.class.getClassLoader().getResourceAsStream("jinju.txt");
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String str = "";
String temp = "";
while ((temp = br.readLine()) != null) {
if (!StringUtils.isEmpty(temp)) {
str = str + "\r\n" + temp;
} else {
jinJuList.add(str);
str = "";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getJinJu() {
Random random = new Random();
return jinJuList.get(random.nextInt(jinJuList.size()));
}
public static void main(String[] args) {
System.out.println(getJinJu());
}
}
消息推送:
package Monster.weixin.tuisong.util;
import com.alibaba.fastjson.JSONObject;
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;
/**
*@ClassName Pusher
*@Description TODO
*@Author Monster
*@Date 2022/8/26 16:03
*/
public class Pusher {
/**
* 测试号的appId和secret
*/
private static String appId = "测试号的appId";
private static String secret = "测试号的secret";
//模版id
private static String templateId = "模版id";
public static void push(String openId){
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId(appId);
wxStorage.setSecret(secret);
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)
.templateId(templateId)
//.url("https://30paotui.com/")//点击模版消息要访问的网址
.build();
//3,如果是正式版发送模版消息,这里需要配置你的信息
// templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
//填写变量信息,比如天气之类的
JSONObject todayWeather = Tianqi.getNanjiTianqi();
templateMessage.addData(new WxMpTemplateData("riqi",todayWeather.getString("date") + " "+ todayWeather.getString("week"),"#00BFFF"));
templateMessage.addData(new WxMpTemplateData("chengshi",todayWeather.getString("province") + todayWeather.getString("city"),"#8E2323"));
// templateMessage.addData(new WxMpTemplateData("tianqi",todayWeather.getString("text_day"),"#3232CD"));
templateMessage.addData(new WxMpTemplateData("fengli",todayWeather.getString("wind_class"),"#DEB887"));
templateMessage.addData(new WxMpTemplateData("fengxiang",todayWeather.getString("wind_dir"),"#FF7F50"));
templateMessage.addData(new WxMpTemplateData("low",todayWeather.getString("low") + "","#173177"));
templateMessage.addData(new WxMpTemplateData("high",todayWeather.getString("high")+ "","#FF6347" ));
templateMessage.addData(new WxMpTemplateData("shishi",todayWeather.getString("temp"),"#7093DB"));
templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#FF1493"));
templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#FFA500"));
templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPi.getCaiHongPi(),"#FF69B4"));
templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));
//templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
// templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
String beizhu = "";
if(JiNianRi.getJieHun() % 365 == 0){
beizhu = "今天是结婚纪念日哦!";
}
if(JiNianRi.getLianAi() % 365 == 0){
beizhu = "今天是恋爱纪念日哦!";
}
/*if(JiNianRi.getLinZhen() % 365 == 0){
beizhu = "今天是领证纪念日哦!";
}*/
templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));
String tianqi = todayWeather.getString("text_day");
if(tianqi.indexOf('晴') != -1){
templateMessage.addData(new WxMpTemplateData("tianqi","今天" + todayWeather.getString("text_day") + ",注意防暑防晒,不要贪'凉'哦~_~","#3232CD"));
}
if(tianqi.indexOf('雨') != -1){
templateMessage.addData(new WxMpTemplateData("tianqi","今天" + todayWeather.getString("text_day") + ",记得带伞哦~_~","#3232CD"));
}
if(tianqi.indexOf('云') != -1){
templateMessage.addData(new WxMpTemplateData("tianqi","今天" + todayWeather.getString("text_day") + ",一缕思念化清风,丝丝凉意到身边~_~","#3232CD"));
}
try {
System.out.println(templateMessage.toJson());
System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
} catch (Exception e) {
System.out.println("推送失败:" + e.getMessage());
e.printStackTrace();
}
}
}
定时任务:
package Monster.weixin.tuisong.job;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import Monster.weixin.tuisong.util.Pusher;
/**
*@ClassName JobWorker
*@Description TODO
*@Author Monster
*@Date 2022/8/26 16:00
*/
@Component
public class JobWorker {
//要推送的用户微信号
private static String openId = "oFjg591hpCAE1o9Oc2TMsMNZo";
private static String openId2 = "oFjgj5qf8LS9ba5LZ6Tu484XI";
@Scheduled(cron = "0/30 * * * * ?") // 每30秒执行一次
// @Scheduled(cron = "0 30 7 * * ?") //每天7:30执行一次
public void goodMorning(){
Pusher.push(openId);
// Pusher.push(openId2);
}
}
测试:
package Monster.weixin.tuisong.controller;
/**
*@ClassName PushController
*@Description TODO
*@Author Monster
*@Date 2022/8/26 15:48
*/
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import Monster.weixin.tuisong.util.Pusher;
@RestController
public class PushController {
//要推送的用户openid
private static String mxp = "oFjg591hpCAE1o9Oc2TMsMZo";
private static String zyd = "odbd-66ygdSTCldsJ6s0kxXA";
/**
* 微信测试账号推送
*
*/
@GetMapping("/push")
public void push() {
Pusher.push(mxp);
}
/**
* 微信测试账号推送
* */
@GetMapping("/push/zyd")
public void pushZyd() {
Pusher.push(zyd);
}
/**
* 微信测试账号推送
* */
@GetMapping("/push/{id}")
public void pushId(@PathVariable("id") String id) {
Pusher.push(id);
}
}
配置文件:
spring.application.name=weixin.tuisong
server.port=9999
金句:可以加些自己喜欢的,内容太长只做展示。
Positive thinking initiates more happiness!
积极的思考带来更多的快乐。
Failure is the fog through which we glimpse triumph.
透过失败的迷雾,才能瞥见胜利的光辉。
Trust is earned.
信任是要靠行动争取的。
Tears will never help.
眼泪永远没办法帮上忙。
The only way to achieve the impossible is to believe it is possible.
实现“不可能”唯一的方法,就是相信它是可能的。
We all carry something with us.
我们都在负重前行。
Don't you love New Year's Day? You get to start all over!
你不喜欢过新年吗?你能一切从头开始!
Where did the time go?
时间都到哪去了?
Everybody deserves to be loved.
每个人都值得被爱。
模板:
{{riqi.DATA}}
{{chengshi.DATA}}
天气:{{tianqi.DATA}}
风力:{{fengli.DATA}}
风向:{{fengxiang.DATA}}
最低温度:{{low.DATA}} ℃
最高温度:{{high.DATA}} ℃
当前温度:{{shishi.DATA}} ℃
今天是我们恋爱的第 {{lianai.DATA}} 天
距离你的生日还有 {{shengri.DATA}} 天
{{caihongpi.DATA}}
{{jinju.DATA}}
部署到服务器:
华为云:新人注册会有一个月的免费体验时间!
https://console.huaweicloud.com/ecm/?agencyId=0c396fbe4b0e4f3e9963caa85af706fa®ion=cn-south-1&locale=zh-cn#/ecs/manager/vmListxx
步骤:
1.将项目打包jar包
2.将项目上传到服务器
3.使用后台启动方式启动项目
# 后台启动并记录日志信息到logs.log文件
nouhp java -jar jar包 &> logs.log &
记得点赞和关注哦!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)