From 17b203adcc3c3d051d9e40adf18ba2630f9bfbce Mon Sep 17 00:00:00 2001 From: xuthus Date: Fri, 10 Nov 2023 22:57:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E7=94=A8go-cqhttp?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 43 ++++++ gradle/wrapper/gradle-wrapper.properties | 5 +- pom.xml | 37 +++++ .../wx_post/controller/ApiController.java | 128 +++++++++++++++++- 4 files changed, 209 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index bc3f8e9..c34ea3d 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,8 @@ dependencies { implementation 'com.github.binarywang:wx-java-mp-spring-boot-starter:4.1.0' implementation 'io.github.biezhi:oh-my-email:0.0.4' testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.4' + // json 依赖库 + implementation 'org.json:json:20210307' } group = 'cn.xuthus83' @@ -38,3 +40,44 @@ publishing { tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } + + +//用于添加Java插件,以及一些内置任务,打包jar就要用到这里的插件 +apply plugin: 'java' +//用于把项目构建成idea项目 +apply plugin: 'idea' +//定义项目的一些信息 +group = "com.jiliang" +version = "1.1" +description = "hello, this demo for gradle." + +repositories { + mavenCentral() +} + +java { + archivesBaseName = "gradle-project" + sourceCompatibility = JavaVersion.VERSION_1_8 +} + +jar { + String someString = '' + //通过configurations.runtime拿到所有的运行时的依赖jar包,然后.each遍历他,通过it.name获取到每个的jar包的name,赋值 + //遍历项目的所有依赖的jar包赋值给变量 + configurations.runtimeClasspath.each {someString = someString + " lib\\"+it.name}someString + manifest { + attributes 'Main-Class': 'com.each.dubboMainEnd' + attributes 'Class-Path': someString + } +} +//建立函数copyJar 用于把依赖的jar复制到对应的目录下 +task copyJar(type:Copy){ + from configurations.runtime + into ('build/libs/lib') + +} +//函数release即是我们打包的时候的执行的函数,dependsOn[build,copyJar]这里会让执行relese函数的时候先执行build(系统自带的函数) 和copyJar +task release(type: Copy,dependsOn: [build,copyJar]) { + // from 'conf' + // into ('build/libs/eachend/conf') +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ae04661..51187a8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Sat Sep 17 16:58:38 CST 2022 +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/pom.xml b/pom.xml index f986f77..66739c9 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,11 @@ oh-my-email 0.0.4 + + org.json + json + 20210307 + @@ -60,6 +65,38 @@ + + com.spotify + dockerfile-maven-plugin + 1.4.9 + + + tag-latest + deploy + + build + tag + push + + + latest + + + + tag-version + deploy + + build + tag + push + + + ${project.version} + + + + + diff --git a/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java b/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java index 3c076c3..0e623a2 100644 --- a/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java +++ b/src/main/java/cn/xuthus83/wx_post/controller/ApiController.java @@ -4,20 +4,29 @@ import cn.xuthus83.wx_post.service.ApiService; import io.github.biezhi.ome.SendMailException; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.URL; +import java.net.URLConnection; + @RestController -@RequestMapping("/wx") +@RequestMapping() @Slf4j public class ApiController { @Autowired private ApiService apiService; - @RequestMapping("sms/{title}/{content}") + @RequestMapping("/wx/sms/{title}/{content}") public String wxpostsms(@PathVariable String title, @PathVariable String content) throws WxErrorException, SendMailException { try { apiService.emailByQQ(title, content); @@ -27,4 +36,119 @@ public class ApiController { apiService.wxTemplate(title, content); return title + "/" + content; } + + @RequestMapping("/qq/sms") + public String qqpostsms(@RequestBody(required = false) String content) { + JSONObject jsonObject = new JSONObject(content); + JSONObject data = jsonObject.getJSONObject("data"); + JSONObject params = new JSONObject(); + if (!data.isNull("item_name")) { + String item_name = data.getString("item_name"); + String event = data.getString("event"); + params.put("message", item_name + ": " + event); + params.put("user_id", "913651466"); + + sendPost("http://192.168.123.123:8080/send_msg", params.toString()); + } + return "请求成功"; + } + + /** + * 向指定URL发送GET方法的请求 + * + * @param url 发送请求的URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @return URL 所代表远程资源的响应结果 + */ + public static String sendGet(String url, String param) { + String result = ""; + BufferedReader in = null; + try { + String urlNameString = url + "?" + param; + URL realUrl = new URL(urlNameString); + // 打开和URL之间的连接 + URLConnection connection = realUrl.openConnection(); + // 设置通用的请求属性 + connection.setRequestProperty("accept", "*/*"); + connection.setRequestProperty("connection", "Keep-Alive"); + connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + // 建立实际的连接 + connection.connect(); + // 定义 BufferedReader输入流来读取URL的响应 + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + while ((line = in.readLine()) != null) { + result += line; + } + } catch (Exception e) { + System.out.println("发送GET请求出现异常!" + e); + e.printStackTrace(); + } + // 使用finally块来关闭输入流 + finally { + try { + if (in != null) { + in.close(); + } + } catch (Exception e2) { + e2.printStackTrace(); + } + } + return result; + } + + /** + * 向指定 URL 发送POST方法的请求 + * + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 JSONObject.toJSONString(param) 的形式。 + * @return 所代表远程资源的响应结果 + */ + public static String sendPost(String url, String param) { + PrintWriter out = null; + BufferedReader in = null; + String result = ""; + try { + URL realUrl = new URL(url); + // 打开和URL之间的连接 + URLConnection conn = realUrl.openConnection(); + // 设置通用的请求属性 + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + // 获取URLConnection对象对应的输出流 + out = new PrintWriter(conn.getOutputStream()); + // 发送请求参数 + out.print(param); + // flush输出流的缓冲 + out.flush(); + // 定义BufferedReader输入流来读取URL的响应 + in = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String line; + while ((line = in.readLine()) != null) { + result += line; + } + } catch (Exception e) { + System.out.println("发送 POST 请求出现异常!" + e); + e.printStackTrace(); + } + //使用finally块来关闭输出流、输入流 + finally { + try { + if (out != null) { + out.close(); + } + if (in != null) { + in.close(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + return result; + } }