增加若干基础页面

This commit is contained in:
2023-12-22 23:27:14 +08:00
parent a8f9b5265e
commit a16d808d2d
32 changed files with 801 additions and 51 deletions

85
lib/utils/request.dart Normal file
View File

@@ -0,0 +1,85 @@
import 'package:dio/dio.dart';
import 'package:flutter_jdt_store/utils/utils.dart';
class Request {
// 构造函数
Request() {
init();
}
static final Dio _dio = Dio();
static void init() {
// 基本配置
_dio.options.baseUrl =
"https://www.wanzhuanyongcheng.cn/app"; // 替换为你的 API 地址
_dio.options.connectTimeout = const Duration(seconds: 15); // 连接超时时间,单位是毫秒
_dio.options.receiveTimeout = const Duration(seconds: 15); // 接收超时时间,单位是毫秒
_dio.options.headers["Content-Type"] = "application/json; charset=utf-8";
_dio.options.headers["token"] = StorageUtil().getString("token");
// 添加请求拦截器
_dio.interceptors.add(InterceptorsWrapper(
onRequest: (options, handler) {
// 在请求被发送之前做一些事情
return handler.next(options); // 必须返回 options
},
));
// 添加响应拦截器
_dio.interceptors.add(InterceptorsWrapper(
onResponse: (response, handler) {
// 在响应被处理之前做一些事情
final Map<String, dynamic> data = response.data;
// 状态码不等于200时抛出异常
if (data["code"] != 200) {
ToolFn.tips(data["msg"].toString());
throw DioException(
requestOptions: response.requestOptions,
response: response,
error: data["msg"],
);
}
return handler.next(response); // 必须返回 response
},
onError: (DioException e, handler) {
// 在响应错误被处理之前做一些事情
return handler.next(e); // 必须返回 error
},
));
}
static Future<T> get<T>(String path, {Map<String, dynamic>? params}) async {
try {
final Response<T> response =
await _dio.get<T>(path, queryParameters: params);
return response.data!;
} catch (error) {
throw _handleError(error);
}
}
static Future<T> post<T>(String path, {Map<String, dynamic>? data}) async {
try {
final Response<T> response = await _dio.post<T>(path, data: data);
return response.data!;
} catch (error) {
throw _handleError(error);
}
}
// 处理错误
static Exception _handleError(error) {
if (error is DioException) {
String message = "网络请求失败";
if (error.response != null && error.response!.data != null) {
message = error.response!.data.toString();
}
return Exception(message);
} else {
return Exception("未知错误");
}
}
}

View File

@@ -1,9 +1,45 @@
import 'dart:io';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
class ToolFn {
DateTime? lastPressedAt; //上次点击时间
// 字符串是否为空
static bool isBlank(String? str) {
if (str == null || str.isEmpty) {
static bool isBlank(String str) {
if (str.isEmpty) {
return true;
}
return false;
}
///手机号验证(只验证前三位号段)
static bool checkMobile(String str) {
if (isBlank(str)) {
return true;
}
return !RegExp(r"^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}")
.hasMatch(str);
}
static void tips(String msg) {
BotToast.showText(
text: msg,
contentColor: Colors.black,
textStyle: const TextStyle(fontSize: 17, color: Colors.white));
}
// 监听返回键
void isExit() {
if (lastPressedAt == null ||
DateTime.now().difference(lastPressedAt!) >
const Duration(seconds: 1)) {
//两次点击间隔超过1秒则重新计时
lastPressedAt = DateTime.now();
ToolFn.tips("再按一次退出程序");
return;
}
exit(0);
}
}

View File

@@ -3,4 +3,5 @@ library utils;
export 'logger.dart';
export 'status_bar.dart';
export 'storage.dart';
export 'tool.dart';
export 'tool.dart';
export 'request.dart';