diff --git a/AuthInterceptor.java b/AuthInterceptor.java new file mode 100644 index 0000000..844664a --- /dev/null +++ b/AuthInterceptor.java @@ -0,0 +1,26 @@ +package com.example.myapplication.Tool; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; +import java.io.IOException; + +public class AuthInterceptor implements Interceptor { + private String authToken; + + public AuthInterceptor(String token) { + this.authToken = token; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request originalRequest = chain.request(); + + // 添加 Authorization header + Request newRequest = originalRequest.newBuilder() + .header("Authorization", authToken) + .build(); + + return chain.proceed(newRequest); + } +} \ No newline at end of file diff --git a/BackgroundToast.java b/BackgroundToast.java new file mode 100644 index 0000000..83f21db --- /dev/null +++ b/BackgroundToast.java @@ -0,0 +1,26 @@ +package com.example.myapplication.Tool; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.widget.Toast; + +public class BackgroundToast { + private static Handler handler; + private static Toast currentToast; + + public static void show(final Context context, final String message) { + if (handler == null) { + handler = new Handler(Looper.getMainLooper()); + } + + handler.post(() -> { + if (currentToast != null) { + currentToast.cancel(); + } + + currentToast = Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_SHORT); + currentToast.show(); + }); + } +} \ No newline at end of file diff --git a/CommonImageSourceFetcher.java b/CommonImageSourceFetcher.java new file mode 100644 index 0000000..8a85295 --- /dev/null +++ b/CommonImageSourceFetcher.java @@ -0,0 +1,115 @@ +package com.example.myapplication.Tool; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.util.Log; + +import com.example.myapplication.DataBase.DatabaseHelper; +import com.example.myapplication.api.CommonService; +import com.example.myapplication.model.ApiResponse; +import com.example.myapplication.model.ImageSourceItem; + +import java.io.IOException; +import java.util.List; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + + +public class CommonImageSourceFetcher { + private String token; + private final CommonService commonService; + private final DatabaseHelper dbHelper; + private final Context context; + public CommonImageSourceFetcher(String token,Context context) { + + this.token=token; + this.dbHelper = new DatabaseHelper(context); + this.context = context; + Retrofit retrofit =RetrofitClient.getClient(token); + + + this.commonService = retrofit.create(CommonService.class); + } + + /** + * 同步获取图像来源列表 + */ + public List fetchImageSourceListSync() throws IOException { + if (isNetworkAvailable()) { + Call>> call = commonService.getImageSourceList(); + + Response>> response = call.execute(); + + if (response.isSuccessful() && response.body() != null && response.body().isSuccess()) { + List result = response.body().getData(); + // 保存到数据库 + dbHelper.saveImageSources(result); + return result; + } else { + throw new IOException("请求失败,状态码: " + response.code()); + } + } else { + // 无网络时从数据库获取 + return dbHelper.getImageSources(); + } + } + + /** + * 异步获取图像来源列表 + */ + public void fetchImageSourceListAsync(ImageSourceCallback callback) { + List cachedSources = dbHelper.getImageSources(); + if (!cachedSources.isEmpty()) { + callback.onSuccess(cachedSources); + } + + // 检查网络连接 + if (isNetworkAvailable()) { + Call>> call = commonService.getImageSourceList(); + + call.enqueue(new Callback>>() { + @Override + public void onResponse(Call>> call, + Response>> response) { + if (response.isSuccessful() && response.body() != null && response.body().isSuccess()) { + List result = response.body().getData(); + + Log.e("data信息:", result != null ? result.toString() : "Result is null"); + // 保存到数据库 + dbHelper.saveImageSources(result); + callback.onSuccess(result); + } else if (cachedSources.isEmpty()) { + callback.onFailure(new IOException("请求失败,状态码: " + response.code())); + } + } + + @Override + public void onFailure(Call>> call, Throwable t) { + if (cachedSources.isEmpty()) { + callback.onFailure(t); + } + } + }); + } else if (cachedSources.isEmpty()) { + callback.onFailure(new IOException("无网络连接且无缓存数据")); + } + } + private boolean isNetworkAvailable() { + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connectivityManager != null) { + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + return activeNetworkInfo != null && activeNetworkInfo.isConnected(); + } + return false; + } + + public interface ImageSourceCallback { + void onSuccess(List imageSources); + void onFailure(Throwable t); + } +} diff --git a/DocxHelper.java b/DocxHelper.java new file mode 100644 index 0000000..536322a --- /dev/null +++ b/DocxHelper.java @@ -0,0 +1,75 @@ +package com.example.myapplication.Tool; + + + +import android.content.Context; +import android.util.Log; +import android.widget.Toast; + +import com.chaquo.python.PyObject; +import com.chaquo.python.Python; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class DocxHelper { + private static final String TAG = "DocxHelper"; + public static File convertJsonToDocx(Context context, String jsonAssetName, String outputFileName) { + // 1. 从assets读取JSON数据 + String jsonData = readJsonFromAssets(context, jsonAssetName); + + if (jsonData == null) { + Toast.makeText(context, "Failed to read JSON from assets",Toast.LENGTH_SHORT).show(); + return null; + } + + // 2. 准备输出文件路径 + File outputFile = new File(context.getExternalFilesDir(null), outputFileName); + String outputPath = outputFile.getAbsolutePath(); + + // 3. 调用Python处理 + try { + Python py = Python.getInstance(); + PyObject module = py.getModule("test"); + + // 调用Python函数 + module.callAttr("json_to_docx", jsonData, outputPath); + + Toast.makeText(context, "DOCX file generated at:"+outputPath, Toast.LENGTH_SHORT).show(); + + } catch (Exception e) { + Log.e(TAG, "Python processing failed", e); + } + finally { + return outputFile; + } + + } + private static String readJsonFromAssets(Context context, String filename) { + try { + // 1. 打开assets中的文件输入流 + InputStream is = context.getAssets().open(filename); + + + // 2. 创建缓冲读取器 + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + // 3. 使用StringBuilder高效拼接字符串 + StringBuilder sb = new StringBuilder(); + String line; + // 4. 逐行读取文件内容 + while ((line = reader.readLine()) != null) { + sb.append(line); + } + // 5. 关闭资源 + reader.close(); + // 6. 返回完整的JSON字符串 + return sb.toString(); + } catch (IOException e) { + Toast.makeText(context, "Error reading JSON file"+e.getMessage(), Toast.LENGTH_SHORT).show(); + return null; + } + } +} \ No newline at end of file diff --git a/DynamicDataFetcher.java b/DynamicDataFetcher.java new file mode 100644 index 0000000..da7babb --- /dev/null +++ b/DynamicDataFetcher.java @@ -0,0 +1,136 @@ +package com.example.myapplication.Tool; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.util.Log; + +import com.example.myapplication.DataBase.DatabaseHelper; +import com.example.myapplication.api.CommonService; +import com.example.myapplication.model.ApiResponse; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class DynamicDataFetcher { + private final String token; + private final CommonService commonService; + private final DatabaseHelper dbHelper; + private final Context context; + private final String dataType; // 新增:数据类型标识 + + /** + * @param token 用户令牌 + * @param context 上下文 + * @param dataType 数据类型标识(如"work_types"、"image_sources"等) + */ + public DynamicDataFetcher(String token, Context context, String dataType) { + this.token = token; + this.context = context; + this.dataType = dataType; + this.dbHelper = new DatabaseHelper(context); + Retrofit retrofit = RetrofitClient.getClient(token); + this.commonService = retrofit.create(CommonService.class); + } + + /** + * 同步获取动态数据列表 + */ + public List> fetchDynamicDataSync() throws IOException { + if (isNetworkAvailable()) { + Call>>> call = commonService.getDynamicDataList(); + + Response>>> response = call.execute(); + + if (response.isSuccessful() && response.body() != null && response.body().isSuccess()) { + List> result = response.body().getData(); + // 保存到数据库(带数据类型标识) + dbHelper.saveDynamicData(dataType, result); + return result; + } else { + throw new IOException("请求失败,状态码: " + response.code()); + } + } else { + // 无网络时从数据库获取(带数据类型标识) + return dbHelper.getDynamicData(dataType); + } + } + + /** + * 异步获取动态数据列表 + */ + public void fetchDynamicDataAsync(DynamicDataCallback callback) { + List> cachedData = dbHelper.getDynamicData(dataType); + if (!cachedData.isEmpty()) { + callback.onSuccess(cachedData); + } + + if (isNetworkAvailable()) { + Call>>> call = commonService.getDynamicDataList(); + + call.enqueue(new Callback>>>() { + @Override + public void onResponse(Call>>> call, + Response>>> response) { + if (response.isSuccessful() && response.body() != null && response.body().isSuccess()) { + Log.e("图片源列表:",response.body().toString()); + List> result = response.body().getData(); + Log.e("result",result.toString()); + // 保存到数据库(带数据类型标识) + dbHelper.saveDynamicData(dataType, result); + callback.onSuccess(result); + } else if (cachedData.isEmpty()) { + callback.onFailure(new IOException("请求失败,状态码: " + response.code())); + } + } + + @Override + public void onFailure(Call>>> call, Throwable t) { + if (cachedData.isEmpty()) { + callback.onFailure(t); + } + } + }); + } else if (cachedData.isEmpty()) { + callback.onFailure(new IOException("无网络连接且无缓存数据")); + } + } + + /** + * 检查网络是否可用 + */ + private boolean isNetworkAvailable() { + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connectivityManager != null) { + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + return activeNetworkInfo != null && activeNetworkInfo.isConnected(); + } + return false; + } + + /** + * 获取该数据类型的最后同步时间 + */ + public long getLastSyncTime() { + return dbHelper.getDynamicDataLastSync(dataType); + } + + /** + * 清除当前数据类型的缓存 + */ + public void clearCache() { + dbHelper.clearDynamicData(dataType); + } + + public interface DynamicDataCallback { + void onSuccess(List> dataList); + void onFailure(Throwable t); + } +} \ No newline at end of file