pile_nav_new/lib/service/base.dart
2024-11-20 19:42:10 +08:00

346 lines
10 KiB
Dart

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