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");
|
}
|
}
|