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 isAgreeProtocol() async { SharedPreferences prefs = await SharedPreferences.getInstance(); bool? agree = prefs.getBool("agree_protocol"); if (agree == null) { return false; } return agree; } //设置已同意用户协议 static Future setAgreeProtocol() async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.setBool("agree_protocol", true); } ///QQ登录 static Future loginQQ() async { Map value = await _loginMessageChannel.send({"method": "loginQQ"}) as Map; return value; } //是否已经登录 static Future isLogin() async { UserInfo? user = await getUserInfo(); return user != null; } //用户信息 static Future getUserInfo() async { // return UserInfo(id: "123456",nickName: "测试昵称",portrait: "http://img.mm.yeshitv.com/imgs/default_portrait.png"); // 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 updateUserInfo(BuildContext context) async { String? uid = await getUid(); if (uid == null) { return null; } Map? result = await UserApiUtil.getUserInfo(context, uid); var code = result!["code"]; if (code == 0) { UserInfo user = UserInfo.fromJson(result["data"]); //保存用户信息 UserUtil.setUserInfo(user); return user; } else if (code == 80001 || code == 80002) { await logout(); } return null; } static Future 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"); } }