117 lines
3.5 KiB
Dart
117 lines
3.5 KiB
Dart
/*
|
|
* @Author: wangyang 1147192855@qq.com
|
|
* @Date: 2022-07-14 18:16:06
|
|
* @LastEditors: wangyang 1147192855@qq.com
|
|
* @LastEditTime: 2022-07-28 15:33:02
|
|
* @FilePath: \marking_app\lib\provider\user_provider.dart
|
|
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
|
*/
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:marking_app/common/mixin/common.dart';
|
|
import 'package:marking_app/common/model/common/base_structure_result_report.dart';
|
|
import 'package:marking_app/common/model/user/user_info.dart';
|
|
import 'package:marking_app/common/model/user/user_info_report.dart';
|
|
import 'package:marking_app/utils/index.dart';
|
|
import 'package:marking_app/utils/request/rest_client_report.dart';
|
|
|
|
const Map<String, dynamic> emptyUser = {
|
|
'id': '',
|
|
'userName': '',
|
|
'loginName': '',
|
|
'subjectIds': [],
|
|
'positionNames': [],
|
|
'schoolId': 0,
|
|
'schoolName': '',
|
|
'avatar': '',
|
|
};
|
|
final userProvider = StateNotifierProvider<UserProviderHandle, UserInfo>((ref) => UserProviderHandle(UserInfo.fromJson(emptyUser)));
|
|
|
|
class UserProviderHandle extends StateNotifier<UserInfo> {
|
|
UserProviderHandle(UserInfo user) : super(user);
|
|
|
|
// 初始化用户数据
|
|
void initUserInfo() async {
|
|
String? userJsonStr = await FastData.getInstance().getUser();
|
|
if (userJsonStr != null) {
|
|
debugPrint('本地用户信息$userJsonStr');
|
|
state = UserInfo.fromJson(jsonDecode(userJsonStr));
|
|
}
|
|
}
|
|
|
|
Future<void> clean() async {
|
|
state = UserInfo.fromJson(emptyUser);
|
|
FastData.getInstance().cleanUser();
|
|
}
|
|
}
|
|
|
|
/// ---------------- 用户TOKEN -------------------------------
|
|
|
|
final userTokenProvider = StateNotifierProvider<UserTokenHandle, String>((ref) => UserTokenHandle(ref, ''));
|
|
|
|
class UserTokenHandle extends StateNotifier<String> {
|
|
final Ref ref;
|
|
UserTokenHandle(this.ref, String token) : super(token);
|
|
|
|
// 初始化用户数据
|
|
Future<bool> initToken() async {
|
|
String? theToken = await FastData.getInstance().getToken();
|
|
|
|
if (theToken != null && theToken != '') {
|
|
state = theToken;
|
|
return true;
|
|
}
|
|
|
|
clean();
|
|
ref.read(userProvider.notifier).clean();
|
|
return false;
|
|
}
|
|
|
|
void clean() {
|
|
// 缓存清空
|
|
FastData fastData = FastData.getInstance();
|
|
fastData
|
|
..cleanToken()
|
|
..cleanUser()
|
|
..cleanInputKeyboardGuidePage()
|
|
..cleanUserReport();
|
|
|
|
state = '';
|
|
}
|
|
}
|
|
|
|
/// 报告用户信息
|
|
final userReportProvider = StateNotifierProvider<UserReportHandle, UserInfoReport>((ref) => UserReportHandle(ref, UserInfoReport(positions: [], positionIndex: 0)));
|
|
|
|
class UserReportHandle extends StateNotifier<UserInfoReport> with CommonMixin {
|
|
final Ref ref;
|
|
UserReportHandle(this.ref, UserInfoReport infoReport) : super(infoReport);
|
|
|
|
// 初始化用户数据
|
|
Future<bool> initUserReport() async {
|
|
try {
|
|
RestClientReport client = await getClientReport();
|
|
BaseStructureResultReport<UserInfoReport> res = await client.getReportSysUserinfo();
|
|
if (res.success && res.data != null) {
|
|
state = res.data!..normal = true;
|
|
} else {
|
|
throw new Error();
|
|
}
|
|
return res.success;
|
|
} catch (e) {}
|
|
return false;
|
|
}
|
|
|
|
void setPositionIndex(int thePositionIndex) {
|
|
state.positionIndex = thePositionIndex;
|
|
}
|
|
|
|
void clean() {
|
|
// 缓存清空
|
|
FastData fastData = FastData.getInstance();
|
|
fastData.cleanUserReport();
|
|
}
|
|
}
|