import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../constants/api.dart'; import '../model/login_info_model.dart'; import '../model/network/api_client.dart'; import '../model/user_info_model.dart'; import '../utils/hive_store.dart'; import '../utils/toast/cloud_toast.dart'; class UserProvider extends ChangeNotifier { bool _isLogin = false; bool _kg = false; bool get kg => _kg; bool get isLogin => _isLogin; late UserInfoModel _userInfo; UserInfoModel get userInfo => _userInfo; late String _contentCon; String get contentCon => _contentCon; late String _contentRef; String get contentRef => _contentRef; Future init() async { if (HiveStore.appBox?.containsKey('token') ?? false) { final token = HiveStore.appBox?.get('token') as String; _isLogin = true; apiClient.setToken(token); await updateUserInfo(); return true; } else { _isLogin = false; return false; } } Future setKg(bool lockage) async { _kg = lockage; notifyListeners(); } Future setToken(String token, {User? user}) async { apiClient.setToken(token); await HiveStore.appBox!.put('token', token); _isLogin = true; //每次打开app更新用户信息 await updateUserInfo(); await updateConSms(); await updateRefSms(); } Future logout() async { apiClient.clearToken(); _isLogin = false; await HiveStore.appBox?.delete('token'); } Future updateUserInfo() async { var base = await apiClient.request(API.app.info); if (base.code == 0) { _userInfo = UserInfoModel.fromJson(base.data); } else { CloudToast.show(base.msg); _userInfo = UserInfoModel.fail; } notifyListeners(); } Future updateConSms() async { final prefs = await SharedPreferences.getInstance(); var base = await apiClient.request(API.app.find, data: {'status': 1}); if (base.code == 0) { await prefs.remove('refSms'); await prefs.setString('conSms', base.data['content']); print(prefs.getString('action')); // _contentCon = base.data['content']; } else { CloudToast.show(base.msg); } notifyListeners(); } Future updateRefSms() async { final prefs = await SharedPreferences.getInstance(); final service=FlutterBackgroundService(); var base = await apiClient.request(API.app.find, data: {'status': 2}); if (base.code == 0) { // await prefs.remove('refSms'); print(base.data['content']); await prefs.setString('refSms', base.data['content']); print(prefs.getString('refSms')); service.startService(); // _contentRef = base.data['content']; } else { CloudToast.show(base.msg); } notifyListeners(); } }