地址接口的视线
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
import 'package:ef/ef.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_city_picker/model/address.dart';
|
||||
import 'package:flutterflow_ui/flutterflow_ui.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
||||
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
||||
import 'package:vbvs_app/controller/user_info_controller.dart';
|
||||
import 'package:vbvs_app/model/user_data.dart';
|
||||
|
||||
import 'package:lpinyin/lpinyin.dart';
|
||||
part 'address_controller.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@@ -14,10 +19,11 @@ class AddressModel {
|
||||
String? city; //市
|
||||
String? county; //区
|
||||
String? street; //街道
|
||||
String? detail; //详细信息
|
||||
String? address; //详细信息
|
||||
String? name; //名字
|
||||
String? phone; //手机号
|
||||
bool? ischecked = false; //是否默认
|
||||
String? tel; //手机号
|
||||
@JsonKey(name: 'default')
|
||||
int? default_ = 0; //是否默认
|
||||
|
||||
int currentType = 0;
|
||||
String? all_address;
|
||||
@@ -52,105 +58,161 @@ class AddressController extends GetControllerEx<AddressModel> {
|
||||
address["street"] = model.addressList![3].name; // 第四个元素为街道
|
||||
}
|
||||
}
|
||||
address['detail'] = model.detail;
|
||||
address['address'] = model.address;
|
||||
address['name'] = model.name;
|
||||
address['phone'] = model.phone;
|
||||
address['isChecked'] = model.ischecked;
|
||||
address['tel'] = model.tel;
|
||||
address['default'] = model.default_;
|
||||
|
||||
// try {
|
||||
// final data = await ef.client.rpc("get_now_datetime");
|
||||
// final response = await ef.client.from("app_user_address").update({
|
||||
// 'province': address["province"],
|
||||
// 'city': address["city"],
|
||||
// 'county': address["county"],
|
||||
// 'street': address["street"],
|
||||
// 'detail': address["detail"],
|
||||
// 'name': address["name"],
|
||||
// 'phone': address["phone"],
|
||||
// 'ischecked': address['isChecked'] ? 1 : 0,
|
||||
// 'update_time':
|
||||
// DateFormat("yyyy-MM-dd HH:mm:ss").parse("$data").toString(),
|
||||
// }).eq("id", address['id']);
|
||||
// } catch (e) {
|
||||
// print('Error fetching repairs: $e');
|
||||
// return e.toString();
|
||||
// }
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.add_address;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
await requestWithLog(
|
||||
logTitle: "更新地址",
|
||||
method: MyHttpMethod.put,
|
||||
queryUrl: queryUrl,
|
||||
data: {
|
||||
"id": address['_id'],
|
||||
"default": address['default'],
|
||||
"name": address['name'],
|
||||
"tel": address['tel'],
|
||||
// "area": address['all_address'],
|
||||
"address": address['address'],
|
||||
"province": address['province'],
|
||||
"city": address['city'],
|
||||
"county": address['county'],
|
||||
"street": address['street'],
|
||||
},
|
||||
onSuccess: (res) {
|
||||
if (res.code == 1) {
|
||||
print("更新地址成功");
|
||||
}
|
||||
},
|
||||
);
|
||||
return '';
|
||||
}
|
||||
|
||||
addAddress(AddressModel model) async {
|
||||
// try {
|
||||
// final UserInfoController userInfoController =
|
||||
// Get.find<UserInfoController>();
|
||||
// UserModel user = userInfoController.model.user!;
|
||||
Future<void> addAddress(AddressModel model, BuildContext context) async {
|
||||
try {
|
||||
final UserInfoController userInfoController =
|
||||
Get.find<UserInfoController>();
|
||||
UserModel user = userInfoController.model.user!;
|
||||
|
||||
// // 设置省市区街道名称
|
||||
// if (model.addressList != null && model.addressList!.isNotEmpty) {
|
||||
// if (model.addressList!.length > 0)
|
||||
// model.province = model.addressList![0].name; // 第一个元素为省
|
||||
// if (model.addressList!.length > 1)
|
||||
// model.city = model.addressList![1].name; // 第二个元素为市
|
||||
// if (model.addressList!.length > 2)
|
||||
// model.county = model.addressList![2].name; // 第三个元素为区
|
||||
// if (model.addressList!.length > 3)
|
||||
// model.street = model.addressList![3].name; // 第四个元素为街道
|
||||
// }
|
||||
// 设置省市区街道名称
|
||||
if (model.addressList != null && model.addressList!.isNotEmpty) {
|
||||
if (model.addressList!.length > 0)
|
||||
model.province = model.addressList![0].name;
|
||||
if (model.addressList!.length > 1)
|
||||
model.city = model.addressList![1].name;
|
||||
if (model.addressList!.length > 2)
|
||||
model.county = model.addressList![2].name;
|
||||
if (model.addressList!.length > 3)
|
||||
model.street = model.addressList![3].name;
|
||||
}
|
||||
|
||||
// // 查询数据库是否已有该用户的地址
|
||||
// final existingAddresses = await ef.client
|
||||
// .from('app_user_address')
|
||||
// .select()
|
||||
// .eq('user_id', user.uid!);
|
||||
// 查询现有地址判断是否设置为默认
|
||||
String listUrl =
|
||||
"${ServiceConstant.service_address}${ServiceConstant.server_service}${ServiceConstant.address_list}";
|
||||
final listRes = await requestWithLog(
|
||||
logTitle: "查询地址列表",
|
||||
method: MyHttpMethod.get,
|
||||
queryUrl: listUrl,
|
||||
);
|
||||
if (listRes.data.length <= 0) {
|
||||
model.default_ = 1;
|
||||
}
|
||||
// 添加地址
|
||||
String addUrl =
|
||||
"${ServiceConstant.service_address}${ServiceConstant.server_service}${ServiceConstant.add_address}";
|
||||
final addRes = await requestWithLog(
|
||||
logTitle: '添加地址',
|
||||
method: MyHttpMethod.post,
|
||||
queryUrl: addUrl,
|
||||
data: model.toJson(),
|
||||
);
|
||||
|
||||
// // 如果没有地址,将新增地址默认选中
|
||||
// if (existingAddresses.isEmpty) {
|
||||
// model.ischecked = true;
|
||||
// } else if (model.ischecked == true) {
|
||||
// // 如果新地址被选中,将其他地址的 `ischecked` 字段设为 `0`
|
||||
// await ef.client
|
||||
// .from('app_user_address')
|
||||
// .update({'ischecked': 0}).eq('user_id', user.uid!);
|
||||
// }
|
||||
|
||||
// // 添加新地址
|
||||
// final response = await ef.client.from('app_user_address').insert({
|
||||
// 'province': model.province,
|
||||
// 'city': model.city,
|
||||
// 'county': model.county,
|
||||
// 'street': model.street,
|
||||
// 'detail': model.detail,
|
||||
// 'name': model.name,
|
||||
// 'phone': model.phone,
|
||||
// 'ischecked': model.ischecked! ? 1 : 0,
|
||||
// 'user_id': user.uid,
|
||||
// });
|
||||
// } catch (e) {
|
||||
// print(e);
|
||||
// }
|
||||
if (addRes.code == 1) {
|
||||
TopSlideNotification.show(context, text: "添加成功".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<AddressNode>> getData({int? level, int? pid}) async {
|
||||
// 构建查询
|
||||
return [];
|
||||
// var query = ef.from("app_area_city").select();
|
||||
List<dynamic>? _cityCache;
|
||||
|
||||
// // 如果 pid 不为 null,添加 pid 的条件
|
||||
// if (pid != null) {
|
||||
// query = query.eq("pid", pid);
|
||||
// }
|
||||
// if (level != null) {
|
||||
// query = query.eq("deep", level);
|
||||
// }
|
||||
// List arr = await query;
|
||||
Future<void> _loadCityDataOnce() async {
|
||||
if (_cityCache != null) return;
|
||||
|
||||
// List<AddressNode> addressNodes = arr.map((item) {
|
||||
// return AddressNode.fromJson({
|
||||
// "name": item["ext_name"], // ext_name 对应 name
|
||||
// "code": item["id"], // id 对应 code
|
||||
// "letter": item["pinyin_prefix_upper"], // pinyin_prefix_upper 对应 letter
|
||||
// });
|
||||
// }).toList();
|
||||
String country = "CN";
|
||||
String queryUrl =
|
||||
"${ServiceConstant.service_address}${ServiceConstant.server_service}${ServiceConstant.city_data}?country=$country";
|
||||
|
||||
// return addressNodes;
|
||||
await requestWithLog(
|
||||
logTitle: "查询城市数据",
|
||||
method: MyHttpMethod.get,
|
||||
queryUrl: queryUrl,
|
||||
onSuccess: (res) {
|
||||
if (res.data != null && res.data is List) {
|
||||
// 提取所有 data 并展开成一个统一列表
|
||||
_cityCache = res.data
|
||||
.where((e) => e["data"] != null && e["data"] is List)
|
||||
.expand((e) => e["data"] as List)
|
||||
.toList();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 构建查询
|
||||
Future<List<AddressNode>> getData({int? level, String? parentCode}) async {
|
||||
await _loadCityDataOnce(); // 确保只加载一次
|
||||
if (_cityCache == null) return [];
|
||||
|
||||
// 获取省级数据
|
||||
if (parentCode == null || parentCode.isEmpty) {
|
||||
return _cityCache!.map<AddressNode>((item) {
|
||||
return AddressNode.fromJson({
|
||||
"name": item["name"],
|
||||
"code": item["code"],
|
||||
"letter": _getLetter(item["name"]),
|
||||
});
|
||||
}).toList();
|
||||
}
|
||||
|
||||
// 递归查找下级
|
||||
final parent = _findNodeByCode(_cityCache!, parentCode);
|
||||
if (parent == null || parent["children"] == null) return [];
|
||||
|
||||
List children = parent["children"];
|
||||
return children.map<AddressNode>((item) {
|
||||
return AddressNode.fromJson({
|
||||
"name": item["name"],
|
||||
"code": item["code"],
|
||||
"letter": _getLetter(item["name"]),
|
||||
});
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Map<String, dynamic>? _findNodeByCode(List<dynamic> list, String code) {
|
||||
for (var item in list) {
|
||||
if (item is Map<String, dynamic>) {
|
||||
if (item["code"] == code) return item;
|
||||
if (item["children"] != null && item["children"] is List) {
|
||||
var result = _findNodeByCode(item["children"], code);
|
||||
if (result != null) return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 工具:中文首字母转大写英文(你也可以用更专业的拼音库)
|
||||
String _getLetter(String name) {
|
||||
if (name.isEmpty) return "#";
|
||||
String pinyin = PinyinHelper.getPinyinE(name,
|
||||
separator: '', defPinyin: '', format: PinyinFormat.WITHOUT_TONE);
|
||||
if (pinyin.isEmpty) return "#";
|
||||
return pinyin[0].toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ AddressModel _$AddressModelFromJson(Map<String, dynamic> json) => AddressModel()
|
||||
..city = json['city'] as String?
|
||||
..county = json['county'] as String?
|
||||
..street = json['street'] as String?
|
||||
..detail = json['detail'] as String?
|
||||
..address = json['address'] as String?
|
||||
..name = json['name'] as String?
|
||||
..phone = json['phone'] as String?
|
||||
..ischecked = json['ischecked'] as bool?
|
||||
..tel = json['tel'] as String?
|
||||
..default_ = (json['default'] as num?)?.toInt()
|
||||
..currentType = (json['currentType'] as num).toInt()
|
||||
..all_address = json['all_address'] as String?;
|
||||
|
||||
@@ -24,10 +24,10 @@ Map<String, dynamic> _$AddressModelToJson(AddressModel instance) =>
|
||||
'city': instance.city,
|
||||
'county': instance.county,
|
||||
'street': instance.street,
|
||||
'detail': instance.detail,
|
||||
'address': instance.address,
|
||||
'name': instance.name,
|
||||
'phone': instance.phone,
|
||||
'ischecked': instance.ischecked,
|
||||
'tel': instance.tel,
|
||||
'default': instance.default_,
|
||||
'currentType': instance.currentType,
|
||||
'all_address': instance.all_address,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:ef/ef.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||||
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
||||
|
||||
part 'address_list_controller.g.dart';
|
||||
|
||||
@@ -23,42 +24,48 @@ class AddressListController extends GetControllerEx<AddressListModel> {
|
||||
attr = GetModel(AddressListModel()).obs;
|
||||
}
|
||||
|
||||
// getAddressList() async {
|
||||
// await ApiService.request.get("/api/address/info/list").then((d) {
|
||||
// model.addressList = d.data["data"] ?? [];
|
||||
// updateAll();
|
||||
// }).catchError((e) {
|
||||
// print("$e");
|
||||
// });
|
||||
// }
|
||||
getAddressList() async {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.address_list;
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||||
|
||||
// //更新默认
|
||||
// Future<void> updateDefault(address) async {
|
||||
// var id = address['id'];
|
||||
// var uid = address['userId'];
|
||||
// try {
|
||||
// await ef.client
|
||||
// .from("app_user_address")
|
||||
// .update({
|
||||
// 'ischecked': 0,
|
||||
// })
|
||||
// .eq("user_id", address['userId'])
|
||||
// .eq("ischecked", 1);
|
||||
// await ef.client.from("app_user_address").update({
|
||||
// 'ischecked': 1,
|
||||
// }).eq("id", address['id']);
|
||||
// } catch (e) {
|
||||
// print('Error fetching repairs: $e');
|
||||
// }
|
||||
// }
|
||||
requestWithLog(
|
||||
logTitle: '查询地址列表',
|
||||
method: MyHttpMethod.get,
|
||||
queryUrl: queryUrl,
|
||||
onSuccess: (res) {
|
||||
// 安全检查 res.data 是否为 List
|
||||
if (res.data != null && res.data is List && res.data.isNotEmpty) {
|
||||
model.addressList = res.data;
|
||||
} else {
|
||||
model.addressList = []; // 设为空数组,防止空指针问题
|
||||
}
|
||||
updateAll();
|
||||
},
|
||||
onFailure: (err) {
|
||||
model.addressList = []; // 请求失败时也清空列表以防旧数据残留
|
||||
updateAll();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// // 删除地址
|
||||
// Future<void> deleteAddress(String id) async {
|
||||
// try {
|
||||
// await ef.client.from("app_user_address").delete().eq("id", id);
|
||||
// print("Address with ID $id has been successfully deleted.");
|
||||
// } catch (e) {
|
||||
// print("Error deleting address with ID $id: $e");
|
||||
// }
|
||||
// }
|
||||
Future<void> deleteAddress(String id) async {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.add_address;
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi?id=$id";
|
||||
requestWithLog(
|
||||
logTitle: '删除地址',
|
||||
method: MyHttpMethod.delete,
|
||||
queryUrl: queryUrl,
|
||||
onSuccess: (res) {
|
||||
getAddressList();
|
||||
},
|
||||
onFailure: (err) {
|
||||
getAddressList();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:vbvs_app/common/color/repair_status.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/common/util/myDialog/my_dialog_controller.dart';
|
||||
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
||||
import 'package:vbvs_app/controller/mh_controller/device_list_controller.dart';
|
||||
import 'package:vbvs_app/controller/user_info_controller.dart';
|
||||
import 'package:vbvs_app/model/user_data.dart';
|
||||
|
||||
@@ -216,16 +217,14 @@ class ApplyRepairController extends GetControllerEx<ApplyRepairModel> {
|
||||
return message;
|
||||
}
|
||||
|
||||
// Future<void> getDeviceList() async {
|
||||
// final UserInfoController userInfoController =
|
||||
// Get.find<UserInfoController>();
|
||||
// // UserModel loginUser = userInfoController.model.user!;
|
||||
// DeviceListController deviceListController = Get.find();
|
||||
// await deviceListController.getDeviceList();
|
||||
// var aa = deviceListController.model.deviceListWyf;
|
||||
// ApplyRepairController applyRepairController = Get.find();
|
||||
// applyRepairController.model.device_list = aa;
|
||||
// }
|
||||
Future<void> getDeviceList() async {
|
||||
// UserModel loginUser = userInfoController.model.user!;
|
||||
DeviceListController deviceListController = Get.find();
|
||||
await deviceListController.getDeviceList();
|
||||
var aa = deviceListController.model.deviceList;
|
||||
ApplyRepairController applyRepairController = Get.find();
|
||||
applyRepairController.model.device_list = aa;
|
||||
}
|
||||
|
||||
// String getPublicUrl(String path) {
|
||||
// try {
|
||||
|
||||
@@ -18,7 +18,7 @@ part 'device_list_controller.g.dart';
|
||||
class DeviceListModel {
|
||||
//设备列表
|
||||
List<dynamic> deviceList = [];
|
||||
List<dynamic> deviceListWyf = [];
|
||||
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
String? keyword;
|
||||
|
||||
@@ -7,12 +7,9 @@ part of 'device_list_controller.dart';
|
||||
// **************************************************************************
|
||||
|
||||
DeviceListModel _$DeviceListModelFromJson(Map<String, dynamic> json) =>
|
||||
DeviceListModel()
|
||||
..deviceList = json['deviceList'] as List<dynamic>
|
||||
..deviceListWyf = json['deviceListWyf'] as List<dynamic>;
|
||||
DeviceListModel()..deviceList = json['deviceList'] as List<dynamic>;
|
||||
|
||||
Map<String, dynamic> _$DeviceListModelToJson(DeviceListModel instance) =>
|
||||
<String, dynamic>{
|
||||
'deviceList': instance.deviceList,
|
||||
'deviceListWyf': instance.deviceListWyf,
|
||||
};
|
||||
|
||||
@@ -19,8 +19,8 @@ class RepairModel {
|
||||
// String? issue; //问题描述
|
||||
String? fileUrl; //文件地址
|
||||
String? mac; //状态
|
||||
String? desc;//问题描述
|
||||
List? img;//上传图片
|
||||
String? desc; //问题描述
|
||||
List? img; //上传图片
|
||||
RepairModel();
|
||||
factory RepairModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
|
||||
@@ -18,7 +18,6 @@ UserInfoModel _$UserInfoModelFromJson(Map<String, dynamic> json) =>
|
||||
..deviceId = json['deviceId'] as String?
|
||||
..deviceModel = json['deviceModel'] as String?
|
||||
..appVersion = json['appVersion'] as String?
|
||||
..img_bucket = json['img_bucket'] as String?
|
||||
..login = (json['login'] as num?)?.toInt()
|
||||
..deviceBindNum = (json['deviceBindNum'] as num?)?.toInt()
|
||||
..loginPhone = (json['loginPhone'] as num?)?.toInt();
|
||||
@@ -33,7 +32,6 @@ Map<String, dynamic> _$UserInfoModelToJson(UserInfoModel instance) =>
|
||||
'deviceId': instance.deviceId,
|
||||
'deviceModel': instance.deviceModel,
|
||||
'appVersion': instance.appVersion,
|
||||
'img_bucket': instance.img_bucket,
|
||||
'login': instance.login,
|
||||
'deviceBindNum': instance.deviceBindNum,
|
||||
'loginPhone': instance.loginPhone,
|
||||
|
||||
Reference in New Issue
Block a user