新版改版:

模块化各个功能
改为okhttp网络请求
改为sqlite作为存储
预设QQ机器人API
This commit is contained in:
2024-06-15 15:53:48 +08:00
parent 6482c919f0
commit b178010f8f
103 changed files with 3519 additions and 3460 deletions

View File

@@ -0,0 +1,17 @@
package com.yutou.databases;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.Data;
@Data
public class AbsDatabasesBean {
String tableName;
public JSONObject toJson() {
JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(this, JSONWriter.Feature.WriteMapNullValue));
json.remove("tableName");
return json;
}
}

View File

@@ -0,0 +1,302 @@
package com.yutou.databases;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.yutou.utils.Log;
import lombok.Data;
import lombok.Getter;
import java.io.File;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public abstract class SQLiteManager {
protected Connection conn;
private String url = "jdbc:sqlite:";
private File sql;
@Getter
public BuildSql build;
public void init() {
AbsDatabasesBean data = getDataBean();
List<BuildSqlItem> items = new ArrayList<>();
Field[] fields = data.getClass().getDeclaredFields();
for (Field field : fields) {
String name = field.getAnnotation(JSONField.class).name();
if (name.equals("tableName")) continue;
String type = BuildSqlItem.TYPE_STRING;
if (field.getType() == int.class) {
type = BuildSqlItem.TYPE_INT;
} else if (field.getType() == String.class) {
type = BuildSqlItem.TYPE_STRING;
} else if (field.getType() == Date.class) {
type = BuildSqlItem.TYPE_TIME;
}
items.add(BuildSqlItem.create().setName(name).setType(type).setNull(true).setKey(false));
}
BuildSql sql = BuildSql.create()
.setFileName("bilibili_login.db")
.setTable(BuildSqlTable.create().setName(data.getTableName()).setItem(items));
build(sql);
}
public void startBatch() {
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
Log.e(e);
}
}
public void closeBatch() {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
Log.e(e);
}
}
public void close() {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void commit() {
try {
conn.commit();
} catch (SQLException e) {
Log.e(e);
}
}
public <T extends AbsDatabasesBean> void add(T t) {
try {
Statement statement = conn.createStatement();
StringBuilder sb = new StringBuilder();
StringBuilder value = new StringBuilder();
sb.append("INSERT INTO `").append(t.getTableName()).append("` ");
sb.append("(");
value.append("(");
for (String key : t.toJson().keySet()) {
sb.append("`").append(key).append("`,");
value.append("'").append(t.toJson().get(key)).append("',");
}
sb.deleteCharAt(sb.length() - 1);
value.deleteCharAt(value.length() - 1);
value.append(")");
sb.append(") VALUES ");
sb.append(value);
statement.executeUpdate(sb.toString());
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public <T extends AbsDatabasesBean> List<T> get(String table, Class<T> tClass) {
List<T> list = new ArrayList<>();
try {
Statement statement = conn.createStatement();
String sql = "SELECT * FROM `" + table + "`";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
JSONObject json = new JSONObject();
for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
String name = resultSet.getMetaData().getColumnName(i + 1);
json.put(name, resultSet.getObject(name));
}
list.add(json.to(tClass));
}
resultSet.close();
statement.closeOnCompletion();
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
private void createSql(BuildSql build) {
try {
sql.mkdirs();
sql.delete();
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
} catch (Exception e) {
Log.e(e);
}
startBatch();
for (BuildSqlTable table : build.getTable()) {
Log.i("创建表:" + table.getName());
createSqlOfTable(table);
}
closeBatch();
}
private void createSqlOfTable(BuildSqlTable table) {
String tableName = table.getName();
try {
Statement statement = conn.createStatement();
List<BuildSqlItem> items = table.getItem();
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE `")
.append(tableName)
.append("` (");
for (BuildSqlItem item : items) {
StringBuilder builder = new StringBuilder();
String type = switch (item.getType()) {
case "int" -> " INTEGER ";
case "TIME" -> " NUMERIC ";
default -> " TEXT ";
};
builder.append("`")
.append(item.getName())
.append("`")
.append(type)
.append(item.isNull() ? "" : " NOT NULL ")
.append(item.isKey() ? " PRIMARY KEY AUTOINCREMENT " : "")
.append(",");
sql.append(builder.toString());
}
sql.append(");");
System.out.println("创建表 > " + sql.toString());
statement.execute(sql.toString().replace(",);", ");"));
statement.closeOnCompletion();
} catch (Exception e) {
Log.e(e);
}
}
protected void build(BuildSql buildSql) {
try {
this.build = buildSql;
Class.forName("org.sqlite.JDBC");
sql = new File("databases" + File.separator + buildSql.getFileName());
if (!sql.exists()) {
createSql(buildSql);
} else {
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
}
} catch (Exception e) {
Log.e(e);
}
}
public boolean setDB(String fileName) {
try {
Class.forName("org.sqlite.JDBC");
sql = new File("db" + File.separator + fileName);
if (sql.exists()) {
if (conn != null && !conn.isClosed()) {
conn.close();
}
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
return true;
}
} catch (Exception e) {
Log.e(e);
}
return false;
}
protected abstract AbsDatabasesBean getDataBean();
public static void main(String[] args) {
}
@Data
public static class BuildSql {
String fileName;
List<BuildSqlTable> table;
public static BuildSql create() {
return new BuildSql();
}
public BuildSql setFileName(String fileName) {
this.fileName = fileName;
return this;
}
public BuildSql setTable(List<BuildSqlTable> table) {
this.table = table;
return this;
}
public BuildSql setTable(BuildSqlTable... table) {
this.table = List.of(table);
return this;
}
}
@Data
public static class BuildSqlTable {
String name;
List<BuildSqlItem> item;
public static BuildSqlTable create() {
return new BuildSqlTable();
}
public BuildSqlTable setName(String name) {
this.name = name;
return this;
}
public BuildSqlTable setItem(List<BuildSqlItem> item) {
this.item = item;
return this;
}
public BuildSqlTable setItem(BuildSqlItem... item) {
this.item = List.of(item);
return this;
}
}
@Data
public static class BuildSqlItem {
public static final String TYPE_INT = "init";
public static final String TYPE_STRING = "String";
public static final String TYPE_TIME = "TIME";
String name;
String type;
boolean isNull;
boolean isKey;
public static BuildSqlItem create() {
return new BuildSqlItem();
}
public BuildSqlItem setName(String name) {
this.name = name;
return this;
}
public BuildSqlItem setType(String type) {
this.type = type;
return this;
}
public BuildSqlItem setNull(boolean aNull) {
isNull = aNull;
return this;
}
public BuildSqlItem setKey(boolean key) {
isKey = key;
return this;
}
}
}

View File

@@ -0,0 +1,7 @@
package com.yutou.inter;
public interface IHttpApiCheckCallback<T> {
void onSuccess(T api);
void onError(int code, String error);
}

View File

@@ -0,0 +1,6 @@
package com.yutou.okhttp;
import java.io.Serializable;
public class BaseBean implements Serializable {
}

View File

@@ -0,0 +1,32 @@
package com.yutou.okhttp;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Request;
import java.util.HashMap;
public class GetRequestParams implements IRequestParam {
/**
* 构建Request
*
* @param request
* @return
*/
@Override
public Request getRequest(HashMap<String, String> headerMap, HashMap<String, String> map, Request request) {
Headers.Builder headerBuild = request.headers().newBuilder();
if (!headerMap.isEmpty()) {
for (String key : headerMap.keySet()) {
headerBuild.add(key, headerMap.get(key));
}
}
//添加公共参数
HttpUrl.Builder builder = request.url().newBuilder();
for (String key : map.keySet()) {
builder.addQueryParameter(key, String.valueOf(map.get(key)));
}
return request.newBuilder().url(builder.build()).headers(headerBuild.build()).build();
}
}

View File

@@ -0,0 +1,14 @@
package com.yutou.okhttp;
import lombok.Data;
@Data
public class HttpBody<T> {
private String msg;
private String status;
private int code;
private int retcode;
private T data;
private String src;
}

View File

@@ -0,0 +1,35 @@
package com.yutou.okhttp;
import okhttp3.Headers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public abstract class HttpCallback<T> implements Callback<HttpBody<T>> {
public abstract void onResponse(Headers headers,int code, String status, T response, String rawResponse);
public abstract void onFailure(Throwable throwable);
@Override
public void onResponse(Call<HttpBody<T>> call, Response<HttpBody<T>> response) {
if (response.body() != null) {
onResponse(
response.headers(),
response.body().getRetcode(),
response.body().getStatus(),
response.body().getData(),
response.body().getSrc()
);
} else {
onFailure(new NullPointerException("response body is null"));
}
call.cancel();
}
@Override
public void onFailure(Call<HttpBody<T>> call, Throwable throwable) {
onFailure(throwable);
call.cancel();
}
}

View File

@@ -0,0 +1,209 @@
package com.yutou.okhttp;
import com.yutou.utils.Log;
import okhttp3.*;
import okhttp3.internal.http.HttpHeaders;
import okio.Buffer;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class HttpLoggingInterceptor implements Interceptor {
private static final String TAG = "HttpLogging";
private static final Charset UTF8 = Charset.forName("UTF-8");
private volatile Level printLevel = Level.NONE;
private java.util.logging.Level colorLevel;
private Logger logger;
private static boolean prLog;
public static void setLog(boolean log) {
prLog = log;
}
public enum Level {
NONE, //不打印log
BASIC, //只打印 请求首行 和 响应首行
HEADERS, //打印请求和响应的所有 Header
BODY //所有数据全部打印
}
public HttpLoggingInterceptor(String tag) {
logger = Logger.getLogger(tag);
colorLevel = java.util.logging.Level.INFO;
}
public void setPrintLevel(Level level) {
if (printLevel == null)
throw new NullPointerException("printLevel == null. Use Level.NONE instead.");
printLevel = level;
}
public void setColorLevel(java.util.logging.Level level) {
colorLevel = level;
}
private void log(String message) {
//logger.log(colorLevel, message);
if (prLog) {
Log.i(TAG, message);
}
//Log.e(TAG,message);
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (request.body() != null && request.body().contentLength() == 0) {
request = chain.call().request();
}
if (printLevel == Level.NONE) {
return chain.proceed(request);
}
//请求日志拦截
logForRequest(request, chain.connection());
//执行请求,计算请求时间
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
log("<-- HTTP FAILED: " + e);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
//响应日志拦截
return logForResponse(response, tookMs);
}
private void logForRequest(Request request, Connection connection) throws IOException {
boolean logBody = (printLevel == Level.BODY);
boolean logHeaders = (printLevel == Level.BODY || printLevel == Level.HEADERS);
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
try {
String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
log(requestStartMessage);
if (logHeaders) {
if (hasRequestBody) {
// Request body headers are only present when installed as a network interceptor. Force
// them to be included (when available) so there values are known.
if (requestBody.contentType() != null) {
log("\tContent-Type: " + requestBody.contentType());
}
if (requestBody.contentLength() != -1) {
log("\tContent-Length: " + requestBody.contentLength());
}
}
Headers headers = request.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
String name = headers.name(i);
// Skip headers from the request body as they are explicitly logged above.
if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
log("\t" + name + ": " + headers.value(i));
}
}
log(" ");
if (logBody && hasRequestBody) {
if (isPlaintext(requestBody.contentType())) {
bodyToString(request);
} else {
log("\tbody: maybe [binary body], omitted!");
}
}
}
} catch (Exception e) {
logger.log(java.util.logging.Level.WARNING, e.getMessage(), e);
} finally {
log("--> END " + request.method());
}
}
private Response logForResponse(Response response, long tookMs) {
Response.Builder builder = response.newBuilder();
Response clone = builder.build();
ResponseBody responseBody = clone.body();
boolean logBody = (printLevel == Level.BODY);
boolean logHeaders = (printLevel == Level.BODY || printLevel == Level.HEADERS);
try {
log("<-- " + clone.code() + ' ' + clone.message() + ' ' + clone.request().url() + " (" + tookMs + "ms");
if (logHeaders) {
Headers headers = clone.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
log("\t" + headers.name(i) + ": " + headers.value(i));
}
log(" ");
if (logBody && HttpHeaders.hasBody(clone)) {
if (responseBody == null) return response;
if (isPlaintext(responseBody.contentType())) {
byte[] bytes = responseBody.byteStream().readAllBytes();
MediaType contentType = responseBody.contentType();
String body = new String(bytes, getCharset(contentType));
log("\tbody:" + body);
responseBody = ResponseBody.create(responseBody.contentType(), bytes);
return response.newBuilder().body(responseBody).build();
} else {
log("\tbody: maybe [binary body], omitted!");
}
}
}
} catch (Exception e) {
logger.log(java.util.logging.Level.WARNING, e.getMessage(), e);
} finally {
log("<-- END HTTP");
}
return response;
}
private static Charset getCharset(MediaType contentType) {
Charset charset = contentType != null ? contentType.charset(UTF8) : UTF8;
if (charset == null) charset = UTF8;
return charset;
}
/**
* Returns true if the body in question probably contains human readable text. Uses a small sample
* of code points to detect unicode control characters commonly used in binary file signatures.
*/
private static boolean isPlaintext(MediaType mediaType) {
if (mediaType == null) return false;
if (mediaType.type() != null && mediaType.type().equals("text")) {
return true;
}
String subtype = mediaType.subtype();
if (subtype != null) {
subtype = subtype.toLowerCase();
if (subtype.contains("x-www-form-urlencoded") || subtype.contains("json") || subtype.contains("xml") || subtype.contains("html")) //
return true;
}
return false;
}
private void bodyToString(Request request) {
try {
Request copy = request.newBuilder().build();
RequestBody body = copy.body();
if (body == null) return;
Buffer buffer = new Buffer();
body.writeTo(buffer);
Charset charset = getCharset(body.contentType());
log("\tbody:" + buffer.readString(charset));
} catch (Exception e) {
logger.log(java.util.logging.Level.WARNING, e.getMessage(), e);
}
}
}

View File

@@ -0,0 +1,9 @@
package com.yutou.okhttp;
import okhttp3.Request;
import java.util.HashMap;
public interface IRequestParam {
Request getRequest(HashMap<String,String> header, HashMap<String,String> map, Request request);
}

View File

@@ -0,0 +1,43 @@
package com.yutou.okhttp;
import okhttp3.Request;
import java.util.HashMap;
public class ParamsContext {
private IRequestParam iRequestParam;
private Request request;
private HashMap<String, String> map;
private HashMap<String, String> headerMap;
public ParamsContext(HashMap<String, String> map, Request request) {
if (map == null) {
map = new HashMap<>();
}
this.map = map;
this.request = request;
this.headerMap = new HashMap<>();
}
public ParamsContext(HashMap<String, String> headerMap, HashMap<String, String> map, Request request) {
if (map == null) {
map = new HashMap<>();
}
this.map = map;
this.request = request;
this.headerMap = headerMap;
}
public Request getInRequest() {
switch (request.method()) {
case "GET":
iRequestParam = new GetRequestParams();
break;
case "POST":
iRequestParam = new PostRequestParams();
break;
}
headerMap.remove("tableName");
return iRequestParam.getRequest(headerMap, map, request);
}
}

View File

@@ -0,0 +1,73 @@
package com.yutou.okhttp;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class PostRequestParams implements IRequestParam {
@Override
public Request getRequest(HashMap<String, String> headerMap, HashMap<String, String> map, Request request) {
Headers.Builder headerBuilder = request.headers().newBuilder();
if (!headerMap.isEmpty()) {
for (String key : headerMap.keySet()) {
headerBuilder.add(key, headerMap.get(key));
}
}
if (request.body() instanceof FormBody) {
FormBody.Builder bodyBuilder = new FormBody.Builder();
FormBody formBody = (FormBody) request.body();
for (int i = 0; i < formBody.size(); i++) {
bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
}
for (String key : map.keySet()) {
bodyBuilder.addEncoded(key, String.valueOf(map.get(key)));
}
formBody = bodyBuilder.build();
request = request.newBuilder().headers(headerBuilder.build()).post(formBody).build();
} else if (request.body() != null) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), toUrlParams(map));
request = request.newBuilder()
.headers(headerBuilder.build())
.post(request.body())
.post(requestBody).build();
}
return request;
}
public static String toUrlParams(JSONObject json) {
StringBuilder string = new StringBuilder();
Set<String> keys = json.keySet();
for (String key : keys) {
try {
string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
try {
string.append("&").append(URLEncoder.encode(key, "UTF-8")).append("=");
// string += "&" + key + "=";
} catch (Exception e1) {
string.append("&").append(key).append("=");
}
}
}
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString();
}
public static String toUrlParams(Map<String, String> map) {
if (map.isEmpty()) {
return "";
}
StringBuilder builder = new StringBuilder();
for (String key : map.keySet()) {
builder.append(key).append("=").append(map.get(key)).append("&");
}
return builder.substring(0, builder.length() - 1);
}
}

View File

@@ -0,0 +1,107 @@
package com.yutou.okhttp.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.yutou.okhttp.HttpLoggingInterceptor;
import com.yutou.okhttp.ParamsContext;
import com.yutou.okhttp.converter.JsonCallAdapter;
import com.yutou.okhttp.converter.JsonConverterFactory;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
public class BaseApi {
private String URL;
private HashMap<String, String> params;
private HashMap<String, String> headers;
public BaseApi(String URL) {
this.URL = URL;
}
public BaseApi setURL(String URL) {
this.URL = URL;
return this;
}
public BaseApi setHeaders(HashMap<String, String> headers) {
this.headers = headers;
return this;
}
public BaseApi setParams(HashMap<String, String> params) {
this.params = params;
return this;
}
/**
* 创建一个接口方法
*
* @param okHttpClient okhttp客户端
* @param converterFactory 处理工厂类
* @param callAdapterFactory 请求适配器工厂
* @param baseUrl 基础地质
* @param service 接口
* @param <T> 接口泛型
* @return 接口
*/
public <T> T create(OkHttpClient okHttpClient, Converter.Factory converterFactory, CallAdapter.Factory callAdapterFactory, String baseUrl, Class<T> service) {
Retrofit.Builder builder = new Retrofit.Builder()
//基础url
.baseUrl(baseUrl)
//客户端OKHttp
.client(okHttpClient);
//添加转换工厂
if (null != converterFactory) {
builder.addConverterFactory(converterFactory);
}
//添加请求工厂
if (null != callAdapterFactory) {
builder.addCallAdapterFactory(callAdapterFactory);
}
//创建retrofit对象
Retrofit retrofit = builder.build();
//返回创建的api
return retrofit.create(service);
}
public <T> T createApi(Class<T> apiClass) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.create();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("http");
loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient()
.newBuilder()
.addInterceptor(initQuery())
.addInterceptor(loggingInterceptor);
return create(builder.build(),
JsonConverterFactory.create(gson),
JsonCallAdapter.create(),
URL,
apiClass);
}
public Interceptor initQuery() {
Interceptor addQueryParameterInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//配置公共参数
request = new ParamsContext(headers,params,request).getInRequest();
return chain.proceed(request);
}
};
return addQueryParameterInterceptor;
}
}

View File

@@ -0,0 +1,19 @@
package com.yutou.okhttp.converter;
import org.jetbrains.annotations.Nullable;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
public class JsonCallAdapter extends CallAdapter.Factory{
public static JsonCallAdapter create(){
return new JsonCallAdapter();
}
@Nullable
@Override
public CallAdapter<?, ?> get(Type type, Annotation[] annotations, Retrofit retrofit) {
return null;
}
}

View File

@@ -0,0 +1,40 @@
package com.yutou.okhttp.converter;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.Nullable;
import retrofit2.Converter;
import retrofit2.Retrofit;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
public class JsonConverterFactory extends Converter.Factory {
Gson gson;
public static JsonConverterFactory create(Gson gson) {
return new JsonConverterFactory(gson);
}
private JsonConverterFactory(Gson gson) {
this.gson = gson;
}
@Nullable
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
// return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new JsonRequestBodyConverter<>(gson,adapter);
}
@Nullable
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
// return super.responseBodyConverter(type, annotations, retrofit);
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new JsonResponseBodyConverter<>(gson,adapter,type);
}
}

View File

@@ -0,0 +1,36 @@
package com.yutou.okhttp.converter;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonWriter;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import org.jetbrains.annotations.Nullable;
import retrofit2.Converter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class JsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
Gson gson;
TypeAdapter<T> adapter;
public JsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson=gson;
this.adapter=adapter;
}
@Nullable
@Override
public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), StandardCharsets.UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter, value);
jsonWriter.close();
byte[] bytes = buffer.readByteArray();
return RequestBody.create(MediaType.parse("application/json; charset=UTF-8"),bytes );
}
}

View File

@@ -0,0 +1,45 @@
package com.yutou.okhttp.converter;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.yutou.okhttp.HttpBody;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.Nullable;
import retrofit2.Converter;
import java.io.IOException;
import java.lang.reflect.Type;
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
Gson gson;
TypeAdapter<?> adapter;
Type type;
public JsonResponseBodyConverter(Gson gson, TypeAdapter<?> adapter, Type type) {
this.gson = gson;
this.adapter = adapter;
this.type = type;
}
@Nullable
@Override
public T convert(ResponseBody responseBody) throws IOException {
String string = new String(responseBody.bytes());
responseBody.close();
HttpBody<T> body;
try {
body = JSONObject.parseObject(string, type, JSONReader.Feature.FieldBased);
body.setSrc(string);
return (T) body;
} catch (Exception e) {
e.printStackTrace();
body = new HttpBody();
body.setSrc(string);
}
return (T) body;
}
}

View File

@@ -0,0 +1,335 @@
package com.yutou.utils;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.xml.bind.DatatypeConverter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import java.io.*;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
public class AppTools {
public static boolean saveFile(File file, String data){
try {
FileWriter writer=new FileWriter(file);
writer.write(data);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
Log.e(e);
return false;
}
}
public static String readFile(File file){
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
String tmp;
StringBuilder str= new StringBuilder();
while ((tmp=reader.readLine())!=null){
str.append(tmp);
}
reader.close();
return str.toString();
} catch (Exception ignored) {
}
return null;
}
/**
* 获取项目路径
*
* @param request
* @return
*/
public static String getPath(HttpServletRequest request) {
return request.getServletContext().getRealPath("/") + "/";
}
public static String getToDayTime() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
} public static String getToDayTime(Date date) {
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
public static Date getToDayStartTime(){
return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00",new ParsePosition(0));
}
public static Date getToDayNowTime(){
return new Date();
}
public static String getToDayNowTimeToString(){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
public static String getToDayTimeToString(Date date){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
public static Map<String,String> getUrlParams(String url){
Map<String,String> map=new HashMap<>();
if(url.contains("?")){
String param=url.split("\\?")[1];
String[] params=param.split("&");
for (String par : params) {
map.put(par.split("=")[0],par.split("=")[1]);
}
}
return map;
}
/**
* N以内的不重复随机数
*
* @param min 最小值
* @param max 最大值
* @param n
* @return
*/
public static int[] randomCommon(int min, int max, int n) {
int len = max - min + 1;
if (max < min || n > len) {
return new int[0];
}
// 初始化给定范围的待选数组
int[] source = new int[len];
for (int i = min; i < min + len; i++) {
source[i - min] = i;
}
int[] result = new int[n];
Random rd = new Random();
int index = 0;
for (int i = 0; i < result.length; i++) {
// 待选数组0到(len-2)随机一个下标
index = Math.abs(rd.nextInt() % len--);
// 将随机到的数放入结果集
result[i] = source[index];
// 将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
source[index] = source[len];
}
return result;
}
/**
* 设置Cookie
*
* @param response
* @param key
* @param value
* @param time
*/
public static void setCookie(HttpServletResponse response, String key, String value, int time) {
Cookie cookie = new Cookie(key, value);
if (time == -1) {
time= 31536000;
}
cookie.setMaxAge(time);
cookie.setPath("/");
response.addCookie(cookie);
}
/**
* 获取Cookie
*
* @param request
* @param key
* @return
*/
public static Cookie getCookie(HttpServletRequest request, String key) {
Cookie[] cookies = request.getCookies();
try {
for (Cookie cookie : cookies) {
if (key != null && cookie.getName().equals(key)) {
return cookie;
}
}
} catch (Exception ignored) {
}
return null;
}
/**
* 删除Cookie
*
* @param request
* @param response
* @param key
* @return
*/
public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) {
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals(key)) {
Log.i("删除key=" + key);
cookie.setMaxAge(0);
cookie.setPath("/");
cookie.setValue(null);
response.addCookie(cookie);
}
}
return "ok";
}
public static String alphabetsInUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String alphabetsInLowerCase = "abcdefghijklmnopqrstuvwxyz";
public static String numbers = "0123456789";
public static String randomString(int length) {
return randomString(length,new String[]{alphabetsInLowerCase,alphabetsInUpperCase,numbers});
}
public static String randomString(int length,String[] strs){
String allCharacters="";
for (String str : strs) {
allCharacters+=str;
}
// create a super set of all characters
// initialize a string to hold result
StringBuffer randomString = new StringBuffer();
// loop for 10 times
for (int i = 0; i < length; i++) {
// generate a random number between 0 and length of all characters
int randomIndex = (int)(Math.random() * allCharacters.length());
// retrieve character at index and add it to result
randomString.append(allCharacters.charAt(randomIndex));
}
return randomString.toString();
}
public static String getLoginToken(HttpServletRequest request){
String token = request.getParameter("token");
if(StringUtils.isEmpty(token)){
Cookie cookie=AppTools.getCookie(request,"login");
if(cookie!=null){
token=cookie.getValue();
}
}
return token;
}
public static List<File> scanFilePath(String path){
Log.i("scan > "+path);
List<File> files=new ArrayList<>();
File file=new File(path);
for (File listFile : Objects.requireNonNull(file.listFiles())) {
if(listFile.isDirectory()){
files.addAll(scanFilePath(listFile.getAbsolutePath()));
}else{
files.add(listFile);
Log.i("add file is > "+listFile.getAbsolutePath());
}
}
return files;
}
/**
* 构造给前端的文件
*
* @param file 文件路径
* @return 前端获取的文件
*/
public static ResponseEntity<FileSystemResource> getFile(File file) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
try {
headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
}
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
}
public static String getMD5(String data) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(data.getBytes());
return DatatypeConverter.printHexBinary(digest.digest());
} catch (Exception e) {
Log.e(e);
}
return "";
}
public static boolean copyFileToName(String srcFileName,String destFileName,String fileName,boolean overlay) {
File srcFile = new File(srcFileName);
// 判断源文件是否存在
if (!srcFile.exists()) {
System.err.println("源文件不存在:"+srcFile.getAbsolutePath()+" > "+destFileName);
return false;
} else if (!srcFile.isFile()) {
System.err.println("源文件是目录:"+srcFile.getAbsolutePath());
return false;
}
// 判断目标文件是否存在
File destFile = new File(destFileName);
// 如果目标文件所在目录不存在,则创建目录
if (!destFile.exists()) {
// 目标文件所在目录不存在
if (!destFile.mkdirs()) {
// 复制文件失败:创建目标文件所在目录失败
System.err.println("创建文件夹失败:"+destFile.getAbsolutePath());
return false;
}
}else{
if(srcFileName.equals("Activity.smali")){
Log.i("文件夹已存在:"+destFileName);
}
}
// 复制文件
int byteread = 0; // 读取的字节数
InputStream in = null;
OutputStream out = null;
try {
if(fileName==null) {
fileName=srcFile.getName();
}
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile + File.separator +fileName );
byte[] buffer = new byte[1024];
while ((byteread = in.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
}
out.close();
in.close();
return true;
} catch (FileNotFoundException e) {
Log.e(e);
return false;
} catch (IOException e) {
Log.e(e);
return false;
}
}
public static boolean isRuntimeSystemOfWindow(){
return System.getProperty ("os.name").contains("Windows");
}
public static Process exec(String exec)throws Exception{
if(AppTools.isRuntimeSystemOfWindow()) {
return Runtime.getRuntime().exec(new String[]{
"cmd",
"/c",
exec
}
);
}else{
return Runtime.getRuntime().exec(new String[]{
"sh",
"-c",
exec
}
);
}
}
}

View File

@@ -0,0 +1,34 @@
package com.yutou.utils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
public class Base64Tools {
public static String encode(String str) {
return Base64.getEncoder().encodeToString(str.getBytes());
}
public static String decode(String str) {
return new String(Base64.getDecoder().decode(str));
}
public static String encode(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
public static String encode(File file) {
try {
byte[] fileContent = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
return encode(fileContent);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,96 @@
package com.yutou.utils;
import com.alibaba.fastjson2.JSONObject;
import java.io.*;
/**
* 配置和参数
*/
public class ConfigTools {
public static final String CONFIG="config.json";
public static final String DATA="data.json";
public static final String SQLITE="sqlite.json";
public static final String NAPCAT_URL = "napcat.url";
public static final String QQ = "qq_bot";
static {
try {
File file=new File(CONFIG);
if(!file.exists()){
file.createNewFile();
}
file=new File(DATA);
if(!file.exists()){
file.createNewFile();
}
file=null;
}catch (Exception e){
Log.e(e);
}
}
public static Object load(String type, String key) {
return load(type, key, Object.class, null);
}
public static <T> T load(String type, String key, Class<T> t) {
return load(type, key, t, null);
}
public static <T> T load(String type, String key, Class<T> t, T def) {
File file = new File(type);
//com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
String src = readFile(file);
if (src != null) {
try {
JSONObject json = JSONObject.parseObject(src);
return json.getObject(key, t);
} catch (Exception e) {
}
}
return def;
}
public static boolean save(String type,String key,Object data){
File file=new File(type);
String src=readFile(file);
if(src==null){
src="{}";
}
JSONObject json=JSONObject.parseObject(src);
json.put(key,data);
saveFile(file,json.toJSONString());
return false;
}
public static boolean saveFile(File file,String data){
try {
FileWriter writer=new FileWriter(file);
writer.write(data);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
Log.e(e);
return false;
}
}
public static String readFile(File file){
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
String tmp;
StringBuilder str= new StringBuilder();
while ((tmp=reader.readLine())!=null){
str.append(tmp);
}
reader.close();
return str.toString();
} catch (Exception e) {
Log.i("error = "+file.getAbsolutePath());
Log.e(e);
}
return null;
}
}

View File

@@ -0,0 +1,64 @@
package com.yutou.utils;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Log {
private static Logger logger;
public static void i(String tag, Object log) {
i('[' + tag + ']' + log);
}
public static void i(Object log) {
if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logcat"))) {
return;
}
System.out.printf("[%s]%s%n",
AppTools.getToDayNowTimeToString(),
log
);
}
public static void e(Exception e) {
if (!ConfigTools.load(ConfigTools.CONFIG, "logcat", Boolean.class)){
return;
}
i(e.getMessage());
e.printStackTrace();
}
public static void i(String timer, int roomId, Object log) {
String logFlag = RedisTools.get("live-log", 1);
if (logFlag != null && logFlag.startsWith("true")) {
if (logFlag.contains("|")) {
if (log.toString().contains(logFlag.split("\\|")[1])) {
getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString());
}
} else {
getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString());
}
}
}
public static Logger getLogger(String fileName) {
if (logger == null) {
try {
if (!new File("logs").exists()) {
new File("logs").mkdirs();
}
logger = Logger.getLogger("Live-Log");
FileHandler handler = new FileHandler("logs" + File.separator + fileName + ".log");
logger.addHandler(handler);
} catch (IOException e) {
e.printStackTrace();
}
}
return logger;
}
}

View File

@@ -0,0 +1,188 @@
package com.yutou.utils;
import com.alibaba.fastjson2.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class RedisTools {
public static final int QQBOT_USER = 3;
private static boolean isNotInstallRedis = false;
private static String host;
private static int port;
public static int TOKEN_TIMEOUT_DEFAULT = 360;
private RedisTools() {
}
// 写成静态代码块形式,只加载一次,节省资源
static {
//Properties properties = PropertyUtil.loadProperties("jedis.properties");
//host = properties.getProperty("redis.host");
//port = Integer.valueOf(properties.getProperty("redis.port"));
host = "127.0.0.1";
port = 6379;
}
public static boolean set(int dbIndex, String key, String value) {
try {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
jedis.select(dbIndex);
String ret = jedis.set(key, value);
jedis.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static boolean set(Object key, String value) {
return set(0, key.toString(), value);
}
public static boolean set(String key, String value, int timeout) {
try {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
if (timeout == -1) {
jedis.set(key, value);
} else {
jedis.setex(key, timeout, value);
}
jedis.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static String get(String key, int dbIndex) {
String value = "-999";
if (isNotInstallRedis) {
return value;
}
try (Jedis jedis = getRedis()) {
jedis.select(dbIndex);
value = jedis.get(key);
jedis.close();
} catch (Exception e) {
// TODO: handle exception
// e.printStackTrace();
}
return value;
}
public static String get(Object key) {
return get(key.toString(), 0);
}
public static boolean remove(Object key) {
return remove(key.toString(),0);
}
public static void removeLoginState(String uid) {
Jedis jedis = getRedis();
Set<String> keys = jedis.keys("*");
for (String string : keys) {
if (string.equals(uid)) {
jedis.del(string);
}
}
}
public static String ping() {
Jedis jedis = getRedis();
String tmp = jedis.ping();
jedis.close();
return tmp;
}
public static boolean exists(Object key, String value) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
boolean flag = value.equals(jedis.get(key.toString()));
jedis.close();
return flag;
}
public static Jedis getRedis() {
return new Jedis(host, port);
}
public static boolean remove(String key, int index) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
jedis.select(index);
Long i = jedis.del(key);
jedis.close();
if (i > 0) {
return true;
} else {
return false;
}
}
private static class PropertyUtil {
// 加载property文件到io流里面
public static Properties loadProperties(String propertyFile) {
Properties properties = new Properties();
try {
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
if (is == null) {
is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
}
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
}
private static Jedis getPoolRedis() {
if (isNotInstallRedis) {
return null;
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(0);
poolConfig.setMaxWaitMillis(1000);
JedisPool pool = new JedisPool(poolConfig, host);
return pool.getResource();
}
public static void pullMsg(String channel, String msg) {
Jedis jedis = getPoolRedis();
jedis.publish(channel, msg);
jedis.close();
}
private static boolean init = false;
public static void main(String[] args) {
RedisTools.pullMsg("msg", "abc");
}
}