pile_nav_new/lib/service/base.dart

346 lines
10 KiB
Dart
Raw Permalink Normal View History

2024-11-18 14:48:54 +08:00
import 'dart:async';
2024-08-18 22:42:37 +08:00
import 'dart:convert';
2024-11-06 17:23:29 +08:00
import 'dart:developer';
2024-08-18 22:42:37 +08:00
import 'dart:io';
2024-08-29 17:45:39 +08:00
import 'package:cpnav/models/pilePoint/coord_trans.dart';
2024-11-15 17:42:52 +08:00
import 'package:cpnav/pages/setting/setting_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
2024-08-18 22:42:37 +08:00
import 'package:http/http.dart' as http;
2024-11-20 19:42:10 +08:00
import 'package:roslibdart/roslibdart.dart';
2024-08-29 17:45:39 +08:00
2024-11-15 17:42:52 +08:00
import '../main.dart';
2024-08-29 17:45:39 +08:00
import 'user/loginprefs.dart';
2024-08-18 22:42:37 +08:00
LoginPrefs loginPrefs = LoginPrefs();
2024-11-15 17:42:52 +08:00
SettingController settingController = Get.put(SettingController());
GetServices services = settingController.services;
2024-08-18 22:42:37 +08:00
class BaseService {
//创建client实例
final _client = http.Client();
2024-11-15 17:42:52 +08:00
String token = "";
// String baseUrl = "http://192.168.1.154:8001"; //本地
2024-11-06 17:23:29 +08:00
String baseUrl = "http://v5.rdc.pub"; //线上
2024-08-18 22:42:37 +08:00
String loginstatus = "登录失效";
2024-11-15 17:42:52 +08:00
Duration timeout = const Duration(seconds: 10);
2024-08-18 22:42:37 +08:00
//发送GET请求
getClient(String url, [bool useBaseUrl = true]) async {
2024-11-15 17:42:52 +08:00
checkExpire();
if (token == "") {
token = loginPrefs.getToken();
}
2024-08-18 22:42:37 +08:00
try {
2024-11-15 17:42:52 +08:00
Uri fullUrl = Uri.parse(useBaseUrl ? (baseUrl + url) : url);
// log("请求地址: $fullUrl \n token: $token");
http.Response response = await _client.get(fullUrl, headers: {
2024-08-18 22:42:37 +08:00
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.authorizationHeader: token,
2024-11-15 17:42:52 +08:00
}).timeout(timeout);
2024-08-18 22:42:37 +08:00
var res = json.decode(response.body);
if (res['message'].contains(loginstatus)) {
loginPrefs.clearLogin();
return;
}
2024-11-18 14:48:54 +08:00
int code = res['code'];
if (code != 1000) {
// log(res);
2024-11-15 17:42:52 +08:00
String msg = res['message'].toString();
2024-11-18 14:48:54 +08:00
if (code == 404) {
2024-11-15 17:42:52 +08:00
msg = "当前接口不存在:$msg";
}
scaffoldMessengerKey.currentState?.showSnackBar(
SnackBar(
content: Text('发生错误: $msg'),
duration: const Duration(seconds: 5),
backgroundColor: Colors.red,
),
);
2024-11-18 14:48:54 +08:00
return res;
2024-11-15 17:42:52 +08:00
}
2024-11-18 14:48:54 +08:00
return res;
2024-08-18 22:42:37 +08:00
} catch (e) {
2024-11-18 14:48:54 +08:00
log(" catch error: ${e.toString()}");
if (e is TimeoutException) {
return {};
}
2024-11-15 17:42:52 +08:00
scaffoldMessengerKey.currentState?.showSnackBar(
SnackBar(
content: Text('发生错误: ${e.toString()}'),
duration: const Duration(seconds: 5),
backgroundColor: Colors.red,
),
);
2024-11-18 14:48:54 +08:00
2024-08-18 22:42:37 +08:00
return {};
}
}
//发送POST请求
postClient(String url, body) async {
2024-11-15 17:42:52 +08:00
checkExpire();
if (token == "") {
token = loginPrefs.getToken();
}
2024-08-18 22:42:37 +08:00
try {
var data = json.encode(body);
2024-11-15 17:42:52 +08:00
http.Response response = await _client
.post(Uri.parse(baseUrl + url),
headers: {
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.authorizationHeader: token,
},
body: data)
.timeout(timeout);
2024-08-18 22:42:37 +08:00
var res = json.decode(response.body);
if (res['message'].contains(loginstatus)) {
loginPrefs.clearLogin();
return;
}
return res;
} catch (e) {
2024-11-06 17:23:29 +08:00
log(e.toString());
2024-08-18 22:42:37 +08:00
return {};
}
}
2024-11-15 17:42:52 +08:00
checkExpire() {
int old = loginPrefs.getRefreshExpire();
2024-11-18 14:48:54 +08:00
int now = DateTime.now().millisecondsSinceEpoch;
2024-11-15 17:42:52 +08:00
2024-11-18 14:48:54 +08:00
if (old - now <= 2000) {
2024-11-15 17:42:52 +08:00
//2s
refreshToken();
}
}
refreshToken() async {
String refreshToken = loginPrefs.getRefreshToken();
if (refreshToken == "") {
token = "";
return;
}
try {
2024-11-18 14:48:54 +08:00
Uri fullUrl = Uri.parse(
"$baseUrl/admin/base/open/refreshToken?refreshToken=$refreshToken");
http.Response response = await _client.get(fullUrl).timeout(timeout);
var res = json.decode(response.body);
if (res['code'] == 1000 && res["data"] != null) {
token = res["data"]["token"];
loginPrefs.saveExpire(res["data"]["expire"]);
loginPrefs.saveToken(token);
loginPrefs.saveRefreshToken(res["data"]["refreshToken"]);
loginPrefs.saveRefreshExpire(res["data"]["refreshExpire"]);
}
2024-11-15 17:42:52 +08:00
} catch (e) {
log("refreshToken error: $e");
}
}
2024-11-20 19:42:10 +08:00
String rosUrl ="ws://192.168.1.90:9090";
getRos(){
Ros ros = Ros(url: rosUrl);
return ros;
}
2024-08-18 22:42:37 +08:00
}
class GetServices {
BaseService service = BaseService();
// String projCode = 'CJGKJEBYYB';
2024-11-15 17:42:52 +08:00
// int tid = 0;
// String projType = "";
// String projCode = "";
// String org_code = "a";
2024-08-18 22:42:37 +08:00
// int tid = 1000;
2024-11-15 17:42:52 +08:00
2024-08-18 22:42:37 +08:00
// 道路边线
2024-11-15 17:42:52 +08:00
getSideLine(projCode, projType) async {
2024-08-18 22:42:37 +08:00
try {
2024-11-15 17:42:52 +08:00
Map? res = await service.getClient(
2024-08-18 22:42:37 +08:00
'/api/comm/side_line/list?proj_code=$projCode&proj_type=$projType');
2024-11-15 17:42:52 +08:00
if (res != null && res['data'] != null) {
return res['data'];
}
2024-08-18 22:42:37 +08:00
} catch (e) {
2024-11-15 17:42:52 +08:00
return null;
2024-08-18 22:42:37 +08:00
}
2024-11-15 17:42:52 +08:00
return null;
2024-08-18 22:42:37 +08:00
}
// 设备绑定 --- 获取设备
2024-11-15 17:42:52 +08:00
Future getDeviceBind(projType, projCode) async {
2024-08-18 22:42:37 +08:00
Map res = await service.getClient(
'/api/sys/device_bind/list?proj_type=$projType&proj_code=$projCode');
2024-11-15 17:42:52 +08:00
if (res['data'] != null) {
2024-08-18 22:42:37 +08:00
return res['data'];
}
2024-11-15 17:42:52 +08:00
return [];
2024-08-18 22:42:37 +08:00
}
// 项目列表
2024-11-15 17:42:52 +08:00
getproject(org_code, projType) async {
Map res = await service.getClient(
'/api/sys/project/list?org_code=$org_code&proj_type=$projType');
if (res['data'] != null) {
return res['data'];
}
return [];
2024-08-18 22:42:37 +08:00
}
// 液压夯
2024-11-15 17:42:52 +08:00
getRcordData(projType, org_code, projCode, int page, int size, String date,
[String sort = "desc", String order = "tp_id"]) async {
Map res = await service.getClient(
"/api/$projType/record/page?page=$page&size=$size&org_code=$org_code&proj_code=$projCode&date=$date&sort=$sort&order=$order");
return res['data'];
}
2024-08-18 22:42:37 +08:00
//获取水泥搅拌桩点数据
// getRcordData(int page, int size, String date,
// [String sort = "desc", String order = "pile_id"]) async {
// Map res = await service.getClient(
2024-11-15 17:42:52 +08:00
// "/api/$projType/record/page?page=$page&size=$size&org_code=$org_code&proj_code=$projCode&tid=$tid&date=$date&sort=$sort&order=$order");
2024-08-18 22:42:37 +08:00
// return res['data'];
// }
// getRcordList(String date, String? dateEnd) async {
// dateEnd ??= date;
// Map res = await service.getClient(
2024-11-15 17:42:52 +08:00
// "/api/$projType/record/list?org_code=$org_code&proj_code=$projCode&tid=$tid&date=$date&dateEnd=$dateEnd");
2024-08-18 22:42:37 +08:00
// return res['data'];
// }
2024-08-27 18:08:05 +08:00
// getRcordList(String date, [String? dateEnd]) async {
// dateEnd ??= date;
// Map res = await service.getClient(
2024-11-15 17:42:52 +08:00
// "/api/$projType/record/list?org_code=$org_code&proj_code=$projCode&date=$date&dateEnd=$dateEnd");
2024-08-27 18:08:05 +08:00
// if (res['code'] == 1000) {
// return res['data'];
// } else {
// return [];
// }
// }
2024-08-18 22:42:37 +08:00
//获取施工记录的日期
2024-11-15 17:42:52 +08:00
getworkDateData(
String org_code, String projCode, String projType, int tid) async {
2024-08-18 22:42:37 +08:00
Map res = await service.getClient(
2024-11-15 17:42:52 +08:00
"/api/$projType/record/work_date?org_code=$org_code&proj_code=$projCode&tid=$tid");
if (res['data'] != null) {
return res['data'];
2024-08-18 22:42:37 +08:00
}
2024-11-15 17:42:52 +08:00
return [];
2024-08-18 22:42:37 +08:00
}
//施工详细记录
2024-11-15 17:42:52 +08:00
getProcessData(int pileId, String projCode, String projType) async {
2024-08-18 22:42:37 +08:00
Map res = await service.getClient(
2024-11-15 17:42:52 +08:00
"/api/$projType/process/list?tpId=$pileId&proj_code=$projCode");
if (res['data'] != null) {
return res['data'];
}
return [];
2024-08-18 22:42:37 +08:00
}
// 验证码
getsmsCode(String phone) async {
Map res =
await service.postClient("/admin/base/open/smsCode", {"phone": phone});
return res;
}
// 手机号登录
phoneLogin(String phone, String smsCode) async {
Map res = await service.postClient(
"/admin/base/open/phone", {"phone": phone, "smsCode": smsCode});
return res;
}
// 获取用户信息
getPerson() async {
2024-11-15 17:42:52 +08:00
Map? res = await service.getClient("/admin/base/comm/person");
if (res != null && res['data'] != null) {
return res['data'];
}
return res;
2024-08-18 22:42:37 +08:00
}
// 验证码
getCaptcha() async {
Map res =
await service.getClient("/admin/base/open/captcha?height=40&width=150");
2024-11-15 17:42:52 +08:00
if (res['data'] != null) {
return res['data'];
}
return {};
2024-08-18 22:42:37 +08:00
}
getAccountLogin(String captchaId, String password, String username,
String verifyCode) async {
Map res = await service.postClient("/admin/base/open/login", {
"captchaId": captchaId,
"password": password,
"username": username,
"verifyCode": verifyCode
});
return res;
}
2024-11-15 17:42:52 +08:00
getRtuLast(String projType, String projCode) async {
2024-08-18 22:42:37 +08:00
Map res = await service.getClient(
"/api/t2n/rtu/rtu_last?proj_type=$projType&proj_code=$projCode");
2024-11-15 17:42:52 +08:00
if (res['data'] != null) {
return res['data'];
}
return [];
2024-08-18 22:42:37 +08:00
}
2024-11-15 17:42:52 +08:00
getCoordTrans(String projType, String projCode) async {
2024-08-18 22:42:37 +08:00
Map res = await service.getClient(
"/api/comm/coord_trans/list?proj_type=$projType&proj_code=$projCode");
2024-11-15 17:42:52 +08:00
if (res['data'] != null) {
return res['data'];
}
return {};
2024-08-18 22:42:37 +08:00
}
2024-08-27 18:08:05 +08:00
2024-11-15 17:42:52 +08:00
// //获取水泥搅拌桩点数据
// getRcordData(int page, int size, String date,
// [String sort = "desc", String order = "pile_id", int? tid]) async {
// Map res = await service.getClient(
// "/api/$projType/record/page?page=$page&size=$size&org_code=$org_code&proj_code=$projCode&tid=$tid&date=$date&sort=$sort&order=$order");
// if (res['data'] != null) {
// return res['data'];
// }
// return [];
// }
2024-08-27 18:08:05 +08:00
2024-11-15 17:42:52 +08:00
getRcordList(String org_code, String projCode, String projType, String date,
[String? dateEnd, int? tid]) async {
2024-08-27 18:08:05 +08:00
dateEnd ??= date;
2024-11-15 17:42:52 +08:00
Map? res = await service.getClient(
"/api/$projType/record/list?org_code=$org_code&proj_code=$projCode&date=$date&dateEnd=$dateEnd"); //&tid=1000
2024-11-18 14:48:54 +08:00
if (res != null && res.isNotEmpty && res['data'] != null) {
2024-11-15 17:42:52 +08:00
return res['data'];
}
return [];
2024-08-29 17:45:39 +08:00
}
2024-11-15 17:42:52 +08:00
plumAdd(String projType, String projCode, List<PointXY> list) async {
2024-08-29 17:45:39 +08:00
//
Map res = await service.postClient("/api/$projType/xx/add", {"list": list});
return res;
}
2024-11-15 17:42:52 +08:00
getPlumList(String projType, String projCode, String name) async {
2024-08-29 17:45:39 +08:00
Map res = await service.getClient(
"/api/$projType/xx/list?proj_code=$projCode&name=$name"); //&tid=1000
2024-08-27 18:08:05 +08:00
return res['data'];
}
2024-08-18 22:42:37 +08:00
}