admin
2022-03-31 02f1c9fd2c594323f772f8e8f0f2187a285c1749
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import 'dart:async';
import 'dart:convert';
import 'dart:ui';
 
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx;
import '../../api/user_api.dart';
import '../../utils/share_preference.dart';
import '../api/http.dart';
import '../model/user/user_info.dart';
import '../utils/event_bus_util.dart';
import '../utils/string_util.dart';
import 'package:shared_preferences/shared_preferences.dart';
 
Timer? locationTimer;
 
class UserUtil {
  static const _loginMessageChannel =
      BasicMessageChannel('ThirdLogin', StandardMessageCodec());
 
  //是否同意了用户协议
  static Future<bool> isAgreeProtocol() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool? agree = prefs.getBool("agree_protocol");
    if (agree == null) {
      return false;
    }
    return agree;
  }
 
  //设置已同意用户协议
  static Future<bool> setAgreeProtocol() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setBool("agree_protocol", true);
  }
 
  ///微信登录
  static void loginWX() async {
    fluwx
        .sendWeChatAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test")
        .then((value) {});
  }
 
  ///QQ登录
  static Future<Map> loginQQ() async {
    Map value = await _loginMessageChannel.send({"method": "loginQQ"}) as Map;
    return value;
  }
 
  //是否已经登录
  static Future<bool> isLogin() async {
    UserInfo? user = await getUserInfo();
    return user != null;
  }
 
  //用户信息
  static Future<UserInfo?> getUserInfo() async {
 
    // return null;
 
    MySharedPreferences prefs =  MySharedPreferences.getInstance();
    String? result =await prefs.getString("user_info");
    if (StringUtil.isNullOrEmpty(result)) {
      return null;
    } else {
      return UserInfo.fromJson(jsonDecode(result!));
    }
  }
 
  static Future<UserInfo?> updateUserInfo(BuildContext context) async {
    String? uid = await getUid();
    if (uid == null) {
      return null;
    }
 
    Map<String, dynamic>? result = await UserApiUtil.getUserInfo(context, uid);
    var code = result!["IsPost"];
    if (code == "true") {
      UserInfo user = UserInfo.fromJson(result["Data"]);
      //保存用户信息
      UserUtil.setUserInfo(user);
 
      return user;
    } else if (code == 80001 || code == 80002) {
      await logout();
    }
    return null;
  }
 
  static Future<String?> getUid() async {
    UserInfo? user = await getUserInfo();
    if (user != null) {
      return user.id;
    }
    return null;
  }
 
  static Future setUserInfo(UserInfo user) async {
    MySharedPreferences prefs =  MySharedPreferences.getInstance();
    await prefs.setString("user_info", jsonEncode(user));
  }
 
  //退出登录
  static Future logout() async {
    await _logout();
    eventBus.fire(LoginEventBus(false));
  }
 
  static Future _logout() async {
    MySharedPreferences prefs =  MySharedPreferences.getInstance();
    await prefs.remove("user_info");
  }
}