美文网首页
单例模型

单例模型

作者: xieyinghao | 来源:发表于2023-06-08 14:08 被阅读0次

    单例模型

    import 'dart:convert';
    import '/App/AppManager/AppRequest.dart';
    import '/static/Logger.dart';
    import '/FBTool/FBSave/FBSave.dart';
    import '/User/UserModel.dart';
    
    class UserManager {
    
      bool startLogout = false;
    
      // 工厂模式
      factory UserManager(value) => _getInstance(value);
      static UserManager get instance => _getInstance(null);
      static UserManager _instance;
      UserManager._internal(String v) {
        Logger.log("初始化用户管理类:$this");
        readUserString(v);
      }
      static UserManager _getInstance(String v) {
        if (_instance == null) {
          _instance = new UserManager._internal(v);
        }
        return _instance;
      }
    
      void readUserString(String v) async {
        if (v == null || v.isEmpty){
          v = await FBSave.getString(FBSave.KUserLoginInfoKey);
        }
    
        Logger.log("通过json初始化用户:$v");
        if (v != null && v.isNotEmpty && v.length > 2) {
          Map<String, dynamic> json = JsonDecoder().convert(v);
          _userInfo = UserModel.fromJson(json);
        }
      }
    
    
      get user => _getUser();
      void setUser(UserModel userModel){
        _userInfo = userModel;
        startLogout = false;
        reSave();
      }
    
      void reSave(){
        FBSave.saveObj(FBSave.KUserLoginInfoKey, user);
        Logger.log("pOfficeName--22: ${user.pOfficeName} - ${user.name}");
      }
    
      void clearUserInfo(){
        AppRequest.clearUserIcon();
        user.token = "";
        reSave();
      }
    
      bool userIsLogin(){
        String token = user.token;
        if (token == null || token.isEmpty || token.length == 0){
          return false;
        }
        return true;
      }
    
      UserModel _userInfo;
      UserModel _getUser() {
        if (_userInfo == null){
          _userInfo = UserModel();
        }
        return _userInfo;
      }
    
    }
    

    模型:

    class UserModel {
    
      int loginType;
      String officeId;
      String officeIdname;
      String mobile;
      String name; //姓名 
      String username; //UM号
      String token;
      String exipreTime;
      String thridToken;
      String emNo;
      String sex;
      String emTypeName;
      int emTypeId;  // 员工员工类型:1正编 2金服 3集团 4外包 5游客  登录接口给你了
      String buDesc; //公司名
      String pnbrDesc; //岗位
      String businessUnit; //公司编码
      String adAccount; //ad域账号
    
      String pOfficeName;//常驻职场名称
      String pOfficeId;  //常驻职场ID
    
      UserModel({
        this.officeId,
        this.mobile,
        this.name,
        this.username,
        this.token,
        this.officeIdname,
        this.exipreTime,
        this.loginType,
        this.thridToken,
        this.emNo,
        this.sex,
        this.emTypeName,
        this.emTypeId,
        this.buDesc,
        this.pnbrDesc, 
        this.businessUnit,
        this.pOfficeName,
        this.pOfficeId,
        this.adAccount
      });
    
        factory UserModel.fromJson(Map<String, dynamic> json){
          return UserModel(
            officeId: json["officeId"],
            mobile: json["mobile"],
            name: json["name"],
            username: json["username"],
            token: json["token"],
            officeIdname: json["officeIdname"],
            exipreTime: json["exipreTime"],
            thridToken: json["thridToken"],
            loginType: json["loginType"],
            emNo: json["emNo"],
            sex: json["sex"],
            businessUnit: json["businessUnit"],
            emTypeName: json["emTypeName"],
            emTypeId: json["emTypeId"],
            buDesc: json["buDesc"],
            pnbrDesc: json["pnbrDesc"],
            pOfficeId: json["pOfficeId"],
            pOfficeName: json["pOfficeName"],
              adAccount: json["adAccount"]
          );
        }
    
        Map<String, dynamic> toJson() =>
            {
              'officeId': officeId,
              'mobile': mobile,
              'name': name,
              'username': username,
              'token': token,
              'officeIdname': officeIdname,
              'exipreTime': exipreTime,
              'thridToken': thridToken,
              'loginType': loginType,
              'emNo': emNo,
              'sex': sex,
              'emTypeName': emTypeName,
              'emTypeId': emTypeId,
              'buDesc': buDesc,
              'pnbrDesc': pnbrDesc,
              'businessUnit': businessUnit,
              'pOfficeName': pOfficeName,
              'pOfficeId': pOfficeId,
              'adAccount': adAccount
            };
    
        bool isMyWorkPlace(){
          if (this.officeId == pOfficeId){
            return true;
          }
          return false;
        }
    }
    
    import 'dart:async';
    import 'dart:convert';
    import 'package:shared_preferences/shared_preferences.dart';
    
    typedef ValueCallBack<T> = Function(T value);
    
    class FBSave{
    
      static const String KUserLoginInfoKey = "KUserLoginInfoKey";
      static const String KOffLineConfigJson = "KOffLineConfigJson";
      static const String KAppInfoVersionKey = "KAppInfoVersionKey";
      static const String KAdsKey = "KAdsKey";
      static const String KGuideUrlsKey = "KGuideUrlsKey";
      static const String KUserFistTimeUseKey = "KUserFistTimeUseKey";
    
      //实践
      static saveString(String key, String value) async {
        SharedPreferences share = await SharedPreferences.getInstance();
        await share.setString(key, value);
      }
    
      static Future<String> getString(String key) async {
        SharedPreferences share = await SharedPreferences.getInstance();
        return share.getString(key);
      }
    
      static void saveObj(String key, Object obj) async {
         assert(key != null && obj != null);
         SharedPreferences share = await SharedPreferences.getInstance();
         String value = JsonEncoder().convert(obj);
         await share.setString(key, value);
      }
    
      static void remove(String key) async {
        SharedPreferences share = await SharedPreferences.getInstance();
        await share.remove(key);
      }
    
      static Future<bool> containKey(String key) async {
        SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
        return sharedPreferences.containsKey(key);
      }
    
    }
    

    使用:
    1.判断是否登陆状态

    if (!UserManager.instance.userIsLogin()) {
          Logger.log("没有登录:${this.context}");
          Navigator.of(this.context).pushNamedAndRemoveUntil("/loginpage", (Route route) => false);
          return;
        }
        Logger.log("用户已经登录过:${UserManager.instance.user.username}");
    

    2.登陆成功赋值

    UserManager.instance.setUser(loginUserModel);
    

    3.退出登陆

     UserManager.instance.clearUserInfo();
    

    相关文章

      网友评论

          本文标题:单例模型

          本文链接:https://www.haomeiwen.com/subject/bcsgedtx.html