Browse Source

完成submit

liuyuqi-dellpc 2 years ago
parent
commit
e2a436c275
3 changed files with 61 additions and 36 deletions
  1. 21 21
      lib/dio/track_dao.dart
  2. 21 2
      lib/model/submit_model.dart
  3. 19 13
      lib/pages/submit_page.dart

+ 21 - 21
lib/dio/track_dao.dart

@@ -9,30 +9,34 @@ import 'package:flutter_tracker/model/user_model.dart';
 class TrackDao {
   static Map<String, dynamic> params = <String, dynamic>{};
 
-  static Future<SubmitModel> uploadImg(
+  static Future<SubmitEntity> uploadImg(
       String token, String filePath, String fileName) async {
     params.addAll({"token": token, "filePath": filePath, "fileName": fileName});
-
-    await Dio()
-        .post(Api.uploadImgUrl, queryParameters: params)
-        .then((response) {
-      return SubmitModel.fromJson(response.data);
-    });
-    return null;
+    try {
+      var formData = FormData.fromMap({
+        'token': token,
+        "file": await MultipartFile.fromFile(filePath, filename: fileName),
+      });
+      var response = await Dio().post(Api.uploadImgUrl, data: formData);
+      return SubmitEntity.fromJson(response.data);
+    } catch (e) {
+      print(e);
+      return null;
+    }
   }
 
-  static Future<SubmitModel> uploadTrack(
-      String token, String telphone, String address, bool health) async {
+  static Future<SubmitModel> uploadTrack(String token, String username,
+      String telphone, String address, int health, int submitid) async {
     params.addAll({
       "token": token,
+      "username": username,
       "telphone": telphone,
       "address": address,
-      "health": health
+      "health": health,
+      "submitid": submitid
     });
 
-    await Dio()
-        .post(Api.uploadTrackUrl, queryParameters: params)
-        .then((response) {
+    await Dio().post(Api.uploadSubmitUrl, data: params).then((response) {
       return SubmitModel.fromJson(response.data);
     });
     return null;
@@ -42,20 +46,16 @@ class TrackDao {
   static Future<List<UserModel>> getContactList(String token) async {
     params.addAll({"token": token});
 
-    await Dio()
-        .post(Api.getContactListUrl, queryParameters: params)
-        .then((response) {
+    await Dio().post(Api.getContactListUrl, data: params).then((response) {
       return UserModel.fromJson(response.data);
     });
     return null;
   }
 
-  static Future<UserModel> uploadContact(String token) async{
+  static Future<UserModel> uploadContact(String token) async {
     params.addAll({"token": token});
 
-    await Dio()
-        .post(Api.uploadContactUrl, queryParameters: params)
-        .then((response) {
+    await Dio().post(Api.uploadContactUrl, data: params).then((response) {
       return UserModel.fromJson(response.data);
     });
     return null;

+ 21 - 2
lib/model/submit_model.dart

@@ -1,9 +1,12 @@
 import 'package:flutter_tracker/utils/time_util.dart';
 
+import 'message_model.dart';
+
 /// Description:
 /// Time       : 2021年12月03日 Friday
 /// Author     : liuyuqi.gov@msncn
 class SubmitModel {
+  int id;
   int userid;
   DateTime time;
   String address;
@@ -12,7 +15,8 @@ class SubmitModel {
   String pic;
 
   SubmitModel(
-      {this.userid,
+      {this.id,
+      this.userid,
       this.time,
       this.address,
       this.telphone,
@@ -20,10 +24,25 @@ class SubmitModel {
       this.pic});
 
   SubmitModel.fromJson(Map<String, dynamic> json) {
-    userid = int.parse(json['userid']);
+    id = int.parse(json["submitid"]);
+    // userid = int.parse(json['userid']);
     time = TimeUtil.getDateTime(json['time']);
     address = json['address'];
     telphone = json['telphone'];
     health = json['health'];
   }
 }
+
+class SubmitEntity {
+  SubmitModel submitModel;
+  MessageModel msg;
+
+  SubmitEntity(this.submitModel, this.msg);
+
+  SubmitEntity.fromJson(Map<String, dynamic> json) {
+    msg = MessageModel.fromJson(json);
+    if (msg.success) {
+      submitModel = SubmitModel.fromJson(json["data"]);
+    }
+  }
+}

+ 19 - 13
lib/pages/submit_page.dart

@@ -27,8 +27,8 @@ class _SubmitPageState extends State<SubmitPage> {
   String _userName = "";
   String _tel = "";
   String _addresds = "";
-  bool _heathStatus = false;
-
+  int _heathStatus = 1;
+  int _submitid = 0;
   String _filePath;
 
   Size get _size => MediaQuery.of(context).size;
@@ -97,7 +97,7 @@ class _SubmitPageState extends State<SubmitPage> {
               ),
               RadioListTile(
                 title: Text("阴性"),
-                value: true,
+                value: 1,
                 groupValue: _heathStatus,
                 onChanged: (value) {
                   setState(() {
@@ -107,7 +107,7 @@ class _SubmitPageState extends State<SubmitPage> {
               ),
               RadioListTile(
                 title: Text("阳性"),
-                value: false,
+                value: 2,
                 groupValue: _heathStatus,
                 onChanged: (value) {
                   setState(() {
@@ -199,16 +199,23 @@ class _SubmitPageState extends State<SubmitPage> {
   }
 
   void submitInfo() async {
-    SharedPreferences prefs = await SharedPreferences.getInstance();
-    String token = prefs.getString("token");
-    var uploadTrack =
-        TrackDao.uploadTrack(token, _tel, _addresds, _heathStatus);
+    if (_submitid > 0) {
+      SharedPreferences prefs = await SharedPreferences.getInstance();
+      String token = prefs.getString("token");
+      var uploadTrack = TrackDao.uploadTrack(
+          token, _userName, _tel, _addresds, _heathStatus, _submitid);
+
+      AppUtil.buildToast("提交完成");
+      Navigator.pop(context);
+    } else {
+      AppUtil.buildToast("请先添加核酸检测图片!");
+    }
   }
 
   void chooseImg() async {
     FilePickerResult result = await FilePicker.platform.pickFiles(
       type: FileType.custom,
-      allowedExtensions: ['jpg', 'pdf', 'doc'],
+      allowedExtensions: ['jpg'],
     );
     if (result != null) {
       print('---选择的路径---' + result.files.single.path);
@@ -218,11 +225,10 @@ class _SubmitPageState extends State<SubmitPage> {
 
       SharedPreferences prefs = await SharedPreferences.getInstance();
       var token = prefs.getString("token");
-      SubmitModel fileEntity = await TrackDao.uploadImg(
+      SubmitEntity submiEntity = await TrackDao.uploadImg(
           token, result.files.single.path, result.files.single.name);
-      AppUtil.buildToast("fileEntity.msgModel.msg");
-      prefs.setString("fileName", result.files.single.name);
-      prefs.setInt("fileid", 11);
+      AppUtil.buildToast(submiEntity.msg.msg);
+      _submitid = submiEntity.submitModel.id;
     } else {
       print('用户停止了选择文件');
     }