Browse Source

我的页面,登录页面功能完善

liuyuqi-dellpc 6 months ago
parent
commit
cdb838802c

+ 3 - 3
android/app/src/main/AndroidManifest.xml

@@ -2,23 +2,23 @@
     package="me.yoqi.flutter.tracker">
 
     <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.BLUETOOTH" />
-    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-feature android:name="android.hardware.location.gps" />
     <uses-feature android:name="android.hardware.location.network" />
     <uses-feature
         android:name="android.hardware.bluetooth_le"
         android:required="true" />
 
-    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 
     <application

+ 2 - 2
lib/dio/login_dao.dart

@@ -41,10 +41,10 @@ class LoginDao {
     }
   }
 
-  static Future<UserModel> getUserInfo(String token) async {
+  static Future<UserEntity> getUserInfo(String token) async {
     params.addAll({"token": token});
     Response response = await Dio().post(Api.getUserInfoUrl, data: params);
-    return UserModel.fromJson(response.data);
+    return UserEntity.fromJson(response.data);
   }
 
   //通过设备id查找用户

+ 1 - 0
lib/main.dart

@@ -44,3 +44,4 @@ class MyApp extends StatelessWidget {
     );
   }
 }
+

+ 3 - 0
lib/model/login_entity.dart

@@ -1,4 +1,5 @@
 import 'package:flutter_tracker/model/message_model.dart';
+import 'package:flutter_tracker/model/user_model.dart';
 
 /// Description:
 /// Time       : 2021年12月03日 Friday
@@ -6,11 +7,13 @@ import 'package:flutter_tracker/model/message_model.dart';
 class LoginEntity {
   String token;
   MessageModel msgModel;
+  UserModel userModel;
 
   LoginEntity.fromJson(Map<String, dynamic> json) {
     msgModel = MessageModel.fromJson(json);
     if (msgModel.success) {
       token = json["data"]["token"];
+      userModel = UserModel.fromJson(json["data"]);
     }
   }
 }

+ 7 - 3
lib/model/user_model.dart

@@ -12,7 +12,7 @@ class UserModel {
   String nickname;
   int sex; // 性别:1男2女
   String avatar; // 头像
-  int type; //类型:2商家,1顾客
+  int healthStatus; //类型:2商家,1顾客
 
   UserModel(
       {this.id,
@@ -23,10 +23,14 @@ class UserModel {
       this.pwd,
       this.sex,
       this.avatar,
-      this.type});
+      this.healthStatus});
 
   UserModel.fromJson(Map<String, dynamic> json) {
+    username = json["username"];
+    pwd = json["password"];
     avatar = json["avatar"];
+    updateTime = json["modifytime"];
+    healthStatus = int.parse(json["health_status"]);
   }
 }
 
@@ -39,7 +43,7 @@ class UserEntity {
   UserEntity.fromJson(Map<String, dynamic> json) {
     msg = MessageModel.fromJson(json);
     if (msg.success) {
-      user = UserModel.fromJson(json);
+      user = UserModel.fromJson(json["data"]);
     }
   }
 }

+ 3 - 0
lib/pages/login_page.dart

@@ -114,9 +114,12 @@ class _LoginPageState extends State<LoginPage> {
       _formKey.currentState.save();
       LoginEntity loginEntity = await LoginDao.login(_username, _password);
       if (loginEntity.msgModel.success) {
+        prefs.setString("token", loginEntity.token);
         prefs.setString('username', _username);
         prefs.setString('password', _password);
         prefs.setBool('isLogin', true);
+        prefs.setInt("health_status", loginEntity.userModel.healthStatus);
+        prefs.setString("modifytime", loginEntity.userModel.updateTime);
         Navigator.of(context).pushNamed(Routes.indexPage);
       }
       AppUtil.buildToast(loginEntity.msgModel.msg);

+ 15 - 5
lib/pages/mine_page.dart

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
 import 'package:flutter_tracker/dio/login_dao.dart';
 import 'package:flutter_tracker/model/config.dart';
 import 'package:flutter_tracker/model/message_model.dart';
+import 'package:flutter_tracker/model/user_model.dart';
 import 'package:flutter_tracker/routes/routes.dart';
 import 'package:flutter_tracker/utils/app_util.dart';
 import 'package:shared_preferences/shared_preferences.dart';
@@ -19,7 +20,8 @@ class MinePage extends StatefulWidget {
 class _MinePageState extends State<MinePage> {
   Size get _size => MediaQuery.of(context).size;
   String _name = "张三";
-  int type = 1;
+  int _health_status = 1;
+  String _updateTime = "2021-11-25 10:10:10";
 
   @override
   Widget build(BuildContext context) {
@@ -57,7 +59,7 @@ class _MinePageState extends State<MinePage> {
                     const SizedBox(
                       height: 10,
                     ),
-                    const Text("2021-11-25 10:10:10"),
+                    Text(_updateTime),
                     const SizedBox(
                       height: 10,
                     )
@@ -212,7 +214,7 @@ class _MinePageState extends State<MinePage> {
   }
 
   Widget buildImgStatus() {
-    switch (type) {
+    switch (_health_status) {
       case 1:
         return Image.asset(
           "assets/images/green.jpg",
@@ -240,12 +242,20 @@ class _MinePageState extends State<MinePage> {
   @override
   void initState() {
     super.initState();
+    getUserInfo();
   }
 
-  void getUserInfo(String token) async {
+  void getUserInfo() async {
     var sharedPreferences = await SharedPreferences.getInstance();
     String token = sharedPreferences.getString("token");
-    await LoginDao.getUserInfo(token);
+    UserEntity userEntity = await LoginDao.getUserInfo(token);
+    if (userEntity.msg.success) {
+      setState(() {
+        _name = userEntity.user.username;
+        _health_status = userEntity.user.healthStatus;
+        _updateTime = userEntity.user.updateTime;
+      });
+    }
   }
 
   void logout() async {

+ 3 - 2
lib/pages/track_page.dart

@@ -197,12 +197,13 @@ class _TrackPageState extends State<TrackPage> {
     }
   }
 
-  void getPermissions() {
+  void getPermissions() async {
     Nearby().askLocationAndExternalStoragePermission();
   }
 
   Future<void> getCurrentUser() async {
-    // UserModel user=
+    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
+    UserModel user = sharedPreferences.get("user");
   }
 
   void addContactsToList() async {