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/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'; import 'push_util.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); } ///微信登录 static void loginWX() async { fluwx .sendWeChatAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test") .then((value) {}); } ///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 { 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 updateUserInfo(BuildContext context) async { int? uid = await getUid(); if (uid == null) { return; } Map? result = await UserApiUtil.getUserInfo(context, uid); var code = result!["code"]; if (code == 0) { UserInfo user = UserInfo.fromJson(result["data"]); //保存用户信息 UserUtil.setUserInfo(user); } else if (code == 80001 || code == 80002) { await logout(); } } static Future 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 { await _logout(); await PushUtil.removeAlias(); eventBus.fire(LoginEventBus(false)); } static _logout() async { SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.remove("user_info"); } }