admin
2021-11-25 70a344485bd0c9b68ac91f72ed23ec5bfa998b09
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
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 'package:locations/model/user/user_info.dart';
import 'package:locations/utils/string_util.dart';
import 'package:shared_preferences/shared_preferences.dart';
 
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 {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? result = await prefs.getString("user_info");
    if (StringUtil.isNullOrEmpty(result)) {
      return null;
    } else {
      return UserInfo.fromJson(jsonDecode(result!));
    }
  }
 
  static Future<int?> getUid() async {
    UserInfo? user = await getUserInfo();
    if (user != null) {
      return user.id;
    }
    return null;
  }
 
  static Future setUserInfo(UserInfo user) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString("user_info", jsonEncode(user));
  }
 
  //退出登录
  static logout() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove("user_info");
  }
}