Browse Source

[new]增加登录接口流程

zk 1 year ago
parent
commit
0713d9d1bc

+ 2 - 2
android/app/build.gradle

@@ -40,8 +40,8 @@ android {
         // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
         minSdk = rootProject.ext.minSdkVersion
         targetSdk = rootProject.ext.targetSdkVersion
-        versionCode = flutter.versionCode
-        versionName = flutter.versionName
+        versionCode = flutterVersionCode.toInteger()
+        versionName = flutterVersionName
     }
 
     signingConfigs {

+ 2 - 2
assets/string/base/string.xml

@@ -14,8 +14,7 @@
     <string name="home_talk_todo1">想找更多待办?查看</string>
     <string name="home_talk_todo2">全部待办</string>
     <string name="home_popup_tips_txt">当前有未完成录音,\n快来处理</string>
-    <string name="main_no_login_charge_tips">去给小听充电</string>
-    <string name="main_login_charge_tips">小听剩余电量:%d</string>
+    <string name="main_login_charge_tips">小听剩余电量:</string>
     <string name="main_go_charge">去充电</string>
     <string name="main_drawer_template_management">总结模板管理</string>
     <string name="main_drawer_industry_position_change">行业职位变更</string>
@@ -25,4 +24,5 @@
     <string name="main_drawer_complaint_and_report">投诉举报</string>
     <string name="main_drawer_record_number">备案号216545885</string>
     <string name="network_error">网络异常</string>
+    <string name="account">用户</string>
 </resources>

+ 49 - 5
lib/data/repositories/account_repository.dart

@@ -1,35 +1,79 @@
 import 'dart:async';
 
 import 'package:electronic_assistant/data/api/atmob_api.dart';
+import 'package:electronic_assistant/utils/mmkv_util.dart';
 import 'package:flutter/cupertino.dart';
+import 'package:get/get.dart';
 
+import '../../resource/string.gen.dart';
 import '../../utils/http_handler.dart';
 import '../api/request/login_request.dart';
 import '../api/request/verification_code_request.dart';
 import '../api/response/login_response.dart';
 
 class AccountRepository {
+  final ACCOUNT_TOKEN = 'account_token';
+  final ACCOUNT_PHONE = 'account_phone';
+
+  String? token;
+  String? phone;
+  final isLogin = false.obs;
+
   AccountRepository._() {
     debugPrint('AccountRepository init');
+    token = KVUtil.getString(ACCOUNT_TOKEN, null);
+    phone = KVUtil.getString(ACCOUNT_PHONE, null);
+    if (token != null && token!.isNotEmpty) {
+      isLogin.value = true;
+    }
   }
 
-  String? token;
-
   Future<void> getVerificationCode(String phone) {
     return atmobApi
         .getVerificationCode(VerificationCodeRequest(phone))
-        .then(HttpHandler.handle(false));
+        .then(HttpHandler.handle(true));
   }
 
   Future<LoginResponse> login(String phone, String code) {
     return atmobApi
         .login(LoginRequest(phone, code))
-        .then(HttpHandler.handle(true))
+        .then(HttpHandler.handle(false))
         .then((response) {
-      token = response.authToken;
+      onLoginSuccess(phone, response.authToken);
+      refreshUserInfo();
       return response;
     });
   }
+
+  void onLoginSuccess(String phone, String? token) {
+    this.token = token;
+    this.phone = phone;
+    KVUtil.putString(ACCOUNT_TOKEN, token);
+    KVUtil.putString(ACCOUNT_PHONE, phone);
+    isLogin.value = true;
+  }
+
+  void logout() {
+    token = null;
+    phone = null;
+    KVUtil.putString(ACCOUNT_TOKEN, null);
+    KVUtil.putString(ACCOUNT_PHONE, null);
+    isLogin.value = false;
+  }
+
+  void refreshUserInfo() {}
+
+  getUserSubName(String? phone) {
+    String name = StringName.account.tr;
+    if (phone == null) {
+      return name;
+    }
+    //后4位
+    if (phone.length > 4) {
+      phone = phone.substring(phone.length - 4);
+    }
+    return '$name$phone';
+  }
 }
 
 final accountRepository = AccountRepository._();

+ 8 - 0
lib/module/home/controller.dart

@@ -3,7 +3,15 @@ import 'package:electronic_assistant/module/main/controller.dart';
 import 'package:electronic_assistant/resource/string.gen.dart';
 import 'package:get/get.dart';
 
+import '../../data/repositories/account_repository.dart';
+
 class HomePageController extends BaseController {
+  get isLogin => accountRepository.isLogin.value;
+
+  get loginTxt => accountRepository.isLogin.value
+      ? accountRepository.getUserSubName(accountRepository.phone)
+      : StringName.homeGoLogin.tr;
+
   void goTalkRecordPage() {
     Get.find<MainController>().updateIndexByPageName(StringName.mainTabFile);
   }

+ 33 - 21
lib/module/home/view.dart

@@ -149,27 +149,39 @@ class HomePage extends BasePage<HomePageController> {
         });
   }
 
-  GestureDetector buildGoLogin() {
-    return GestureDetector(
-      onTap: () {
-        Get.toNamed(RoutePath.login);
-        // controller.showLoginDrawer();
-      },
-      child: Row(
-        children: [
-          SizedBox(
-              width: 0.1.sw,
-              height: 0.1.sw,
-              child: Assets.images.iconHomeLogged.image()),
-          SizedBox(width: 8.w),
-          Text(StringName.homeGoLogin.tr,
-              style: TextStyle(
-                  fontWeight: FontWeight.bold,
-                  fontSize: 15.sp,
-                  color: ColorName.primaryTextColor))
-        ],
-      ),
-    );
+  Widget buildGoLogin() {
+    return Obx(() {
+      return GestureDetector(
+        onTap: () {
+          if (controller.isLogin) {
+            controller.showLoginDrawer();
+          } else {
+            Get.toNamed(RoutePath.login);
+          }
+        },
+        child: Row(
+          children: [
+            SizedBox(
+                width: 36.w,
+                height: 36.w,
+                child: controller.isLogin
+                    ? Assets.images.iconHomeLogged.image()
+                    : Assets.images.iconHomeNoLogin.image()),
+            SizedBox(width: 8.w),
+            Text(controller.loginTxt,
+                style: TextStyle(
+                    fontWeight: FontWeight.bold,
+                    fontSize: 15.sp,
+                    color: ColorName.primaryTextColor)),
+            SizedBox(width: 6.w),
+            controller.isLogin
+                ? const SizedBox.shrink()
+                : SizedBox(
+                    height: 16.w, child: Assets.images.iconGoLoginArrow.image())
+          ],
+        ),
+      );
+    });
   }
 
   Container buildTopGradient() {

+ 9 - 0
lib/module/login/controller.dart

@@ -32,8 +32,17 @@ class LoginController extends BaseController {
     }
     accountRepository.login(phone.value, code.value).then((data) {
       ToastUtil.showToast("登录成功");
+      Get.back();
     }).catchError((error) {
       ErrorHandler.toastError(error);
     });
   }
+
+  void setPhone(String text) {
+    phone.value = text;
+  }
+
+  void setCode(String text) {
+    code.value = text;
+  }
 }

+ 10 - 4
lib/module/login/view.dart

@@ -11,14 +11,20 @@ class LoginPage extends BasePage<LoginController> {
     return Scaffold(
       body: Column(
         children: <Widget>[
-          const TextField(
-            decoration: InputDecoration(
+          TextField(
+            onChanged: (text) {
+              controller.setPhone(text);
+            },
+            decoration: const InputDecoration(
                 labelText: "手机号",
                 hintText: "您的手机号",
                 prefixIcon: Icon(Icons.person)),
           ),
-          const TextField(
-            decoration: InputDecoration(
+          TextField(
+            onChanged: (text) {
+              controller.setCode(text);
+            },
+            decoration: const InputDecoration(
                 labelText: "验证码",
                 hintText: "您的验证码",
                 prefixIcon: Icon(Icons.lock)),

+ 7 - 0
lib/module/main/controller.dart

@@ -10,6 +10,8 @@ import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:get/get.dart';
 
+import '../../data/repositories/account_repository.dart';
+
 class MainController extends BaseController {
   final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
 
@@ -34,6 +36,11 @@ class MainController extends BaseController {
 
   String get versionName => "v${appInfoUtil.appVersionName}";
 
+  String get loginTxt =>
+      accountRepository.getUserSubName(accountRepository.phone);
+
+  String get electricityTxt => '${StringName.mainLoginChargeTips.tr}';
+
   void changeIndex(int index) {
     _currentIndex.value = index;
   }

+ 18 - 24
lib/module/main/drawer_view.dart

@@ -18,7 +18,7 @@ Widget buildDrawerContent(MainController controller) {
       child: Column(
         children: [
           SizedBox(height: 12.h),
-          buildUserInfoView(),
+          buildUserInfoView(controller),
           SizedBox(height: 12.h),
           Container(
             height: 72.w,
@@ -43,7 +43,7 @@ Widget buildDrawerContent(MainController controller) {
                           height: 18.h,
                           child: Assets.images.iconChargeCenterTxt.image()),
                       SizedBox(height: 4.h),
-                      Text(StringName.mainNoLoginChargeTips.tr,
+                      Text(controller.electricityTxt,
                           style: TextStyle(
                               fontSize: 12.sp, color: ColorName.white))
                     ],
@@ -144,28 +144,22 @@ Widget buildDrawerContent(MainController controller) {
   );
 }
 
-Widget buildUserInfoView() {
-  return GestureDetector(
-    onTap: () {
-      Get.toNamed(RoutePath.login);
-    },
-    child: Row(
-      crossAxisAlignment: CrossAxisAlignment.center,
-      children: [
-        SizedBox(
-            width: 44.w,
-            height: 44.w,
-            child: Assets.images.iconHomeNoLogin.image()),
-        SizedBox(width: 10.w),
-        Text(StringName.homeGoLogin.tr,
-            style: TextStyle(
-                fontSize: 20.sp,
-                color: ColorName.primaryTextColor,
-                fontWeight: FontWeight.bold)),
-        SizedBox(width: 8.w),
-        SizedBox(height: 20.w, child: Assets.images.iconGoLoginArrow.image()),
-      ],
-    ),
+Widget buildUserInfoView(MainController controller) {
+  return Row(
+    crossAxisAlignment: CrossAxisAlignment.center,
+    children: [
+      SizedBox(
+          width: 44.w,
+          height: 44.w,
+          child: Assets.images.iconHomeLogged.image()),
+      SizedBox(width: 10.w),
+      Text(controller.loginTxt,
+          style: TextStyle(
+              fontSize: 20.sp,
+              color: ColorName.primaryTextColor,
+              fontWeight: FontWeight.bold)),
+      SizedBox(width: 8.w),
+    ],
   );
 }
 

+ 1 - 1
lib/utils/http_handler.dart

@@ -7,7 +7,7 @@ class HttpHandler {
     return (BaseResponse<T> response) {
       if (response.code == 0) {
         if (response.data != null || allowEmptyData) {
-          return response.data!;
+          return response.data == null ? Future.value() : response.data!;
         } else {
           throw Exception('data is null');
         }

+ 4 - 4
lib/utils/mmkv_util.dart

@@ -10,7 +10,7 @@ class KVUtil {
     mmkv = MMKV.defaultMMKV();
   }
 
-  static void setString(String key, String value) {
+  static void putString(String key, String? value) {
     mmkv?.encodeString(key, value);
   }
 
@@ -18,7 +18,7 @@ class KVUtil {
     return mmkv?.decodeString(key) ?? defaultValue;
   }
 
-  static void setInt(String key, int value) {
+  static void putInt(String key, int value) {
     mmkv?.encodeInt(key, value);
   }
 
@@ -26,7 +26,7 @@ class KVUtil {
     return mmkv?.decodeInt(key, defaultValue: defaultValue);
   }
 
-  static void setBool(String key, bool value) {
+  static void putBool(String key, bool value) {
     mmkv?.encodeBool(key, value);
   }
 
@@ -34,7 +34,7 @@ class KVUtil {
     return mmkv?.decodeBool(key, defaultValue: defaultValue);
   }
 
-  static void setDouble(String key, double value) {
+  static void putDouble(String key, double value) {
     mmkv?.encodeDouble(key, value);
   }