207 lines
6.6 KiB
Dart
207 lines
6.6 KiB
Dart
import 'dart:convert';
|
||
import 'dart:io';
|
||
import 'package:qhd_prevention/services/StorageService.dart';
|
||
import 'package:qhd_prevention/tools/encrypt.dart';
|
||
import 'package:qhd_prevention/tools/tools.dart' hide C1C2C3;
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'package:fluttertoast/fluttertoast.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import '../http/ApiService.dart';
|
||
import 'package:qhd_prevention/services/SessionService.dart';
|
||
import 'package:qhd_prevention/services/heartbeat_service.dart';
|
||
|
||
class AuthService {
|
||
static const _keyUser = 'USER';
|
||
static const _keyUserInfoData = 'user_info';
|
||
static const _keyRemember = 'remember';
|
||
static const _keyIsLoggedIn = 'isLoggedIn';
|
||
|
||
/// 登录
|
||
static Future<Map<String, dynamic>> login(
|
||
Map formData,
|
||
String username,
|
||
String password,
|
||
) async {
|
||
final data = {'phone': username, 'password': password, ...formData};
|
||
final res = await AuthApi.userlogin(data);
|
||
if (!res['success']) {
|
||
Fluttertoast.showToast(msg: res['errMessage'] ?? '');
|
||
return {};
|
||
}
|
||
final resData = res['data'];
|
||
bool isInfoComplete = resData['isInfoComplete'] ?? false;
|
||
List firmList = resData['corpInfoCOList'] ?? [];
|
||
|
||
if (firmList.length == 1 && isInfoComplete) {
|
||
// 入职一个企业直接进行底座登录传入企业
|
||
Map data = {
|
||
'id': firmList.first['id'] ?? '',
|
||
'corpinfoId': firmList.first['corpinfoId'] ?? '',
|
||
};
|
||
return AuthService.gbsLogin(username, password, data);
|
||
} else if (firmList.length > 1) {
|
||
// 如果入职多个
|
||
if (StorageService.instance.getString('key.saveJoinFirmInfo') != null) {
|
||
// 有缓存的登录过的企业
|
||
Map jsonData = json.decode(
|
||
StorageService.instance.getString('key.saveJoinFirmInfo') ?? '{}',
|
||
);
|
||
return AuthService.gbsLogin(username, password, jsonData);
|
||
} else {
|
||
// 多个企业 也没有缓存过登录的企业
|
||
return {
|
||
'isChooseFirm': false,
|
||
'isInfoComplete': isInfoComplete,
|
||
'firmList': firmList,
|
||
'userName': username,
|
||
'password': password,
|
||
};
|
||
}
|
||
} else {
|
||
return {
|
||
'isChooseFirm': false,
|
||
'isInfoComplete': isInfoComplete,
|
||
'firmList': [],
|
||
'userName': username,
|
||
'password': password,
|
||
};
|
||
}
|
||
}
|
||
|
||
static Future<Map<String, dynamic>> gbsLogin(
|
||
String username,
|
||
String password,
|
||
Map params,
|
||
) async {
|
||
final encrypted = Encrypt.encrypt(password);
|
||
if (encrypted == null) {
|
||
Fluttertoast.showToast(msg: '加密失败');
|
||
return {};
|
||
}
|
||
Map formData = {
|
||
'captcha': "9999",
|
||
'clientId': ApiService.clientId,
|
||
'identifier': '',
|
||
'username': username,
|
||
'captchaModeEnum': 'GRAPH',
|
||
'grantTypeEnum': 'ACCOUNT_MOBILE_PASSWORD',
|
||
'smsTwiceVerification': 'FALSE',
|
||
'userTypeEnum': 'PLATFORM',
|
||
...params,
|
||
};
|
||
final _data = Map<String, dynamic>.from(formData);
|
||
|
||
_data['password'] = encrypted;
|
||
// printLongString(jsonEncode(_data));
|
||
final result = await AuthApi.loginCheck(_data);
|
||
final success = result['success'] as bool;
|
||
if (!success) {
|
||
Fluttertoast.showToast(msg: result['errMessage'] ?? '');
|
||
return result;
|
||
// return false;
|
||
}
|
||
|
||
printLongString('token:${jsonEncode(result['data']['token'])}');
|
||
final data = result['data'] as Map<String, dynamic>;
|
||
final token = data['token'] ?? '';
|
||
SessionService.instance.setToken(token);
|
||
final prefs = await SharedPreferences.getInstance();
|
||
await prefs.setString(_keyUser, json.encode(data));
|
||
await prefs.setStringList(_keyRemember, [username, password]);
|
||
await prefs.setBool(_keyIsLoggedIn, true);
|
||
|
||
/// 保存登录的企业
|
||
StorageService.instance.setString(
|
||
'key.saveJoinFirmInfo',
|
||
json.encode(params),
|
||
);
|
||
|
||
await AuthApi.getUserData().then((result) async {
|
||
SessionService.instance.updateFromApiResponse(result);
|
||
await SessionService.instance.saveToPrefs();
|
||
SessionService.instance.setToken(token);
|
||
});
|
||
return result;
|
||
}
|
||
|
||
// 检查并请求相机权限
|
||
static Future<bool> checkCameraPermission() async {
|
||
final status = await Permission.camera.status;
|
||
if (status.isDenied) {
|
||
final result = await Permission.camera.request();
|
||
return result.isGranted;
|
||
}
|
||
return status.isGranted;
|
||
}
|
||
|
||
// 检查并请求照片库权限(iOS)和存储权限(Android)
|
||
static Future<bool> checkPhotoLibraryPermission() async {
|
||
if (Platform.isIOS) {
|
||
final status = await Permission.photos.status;
|
||
if (status.isDenied) {
|
||
final result = await Permission.photos.request();
|
||
return result.isGranted;
|
||
}
|
||
return status.isGranted;
|
||
} else {
|
||
final status = await Permission.storage.status;
|
||
if (status.isDenied) {
|
||
final result = await Permission.storage.request();
|
||
return result.isGranted;
|
||
}
|
||
return status.isGranted;
|
||
}
|
||
}
|
||
|
||
/// 验证是否已登录(通过重新登录)
|
||
static Future<Map<String, dynamic>> isLoggedIn() async {
|
||
return {};
|
||
// final prefs = await SharedPreferences.getInstance();
|
||
// final isLocalLoggedIn = prefs.getBool(_keyIsLoggedIn) ?? false;
|
||
// if (!isLocalLoggedIn) return {};
|
||
//
|
||
// final remember = prefs.getStringList(_keyRemember);
|
||
// if (remember == null || remember.length < 2) return {};
|
||
//
|
||
// final username = remember[0];
|
||
// final password = remember[1];
|
||
//
|
||
// // 用存储的账号密码重新登录
|
||
// final data = await login(username, password);
|
||
//
|
||
// if (data.isEmpty||data['result']!= 'success') {
|
||
// await logout();
|
||
// return data;
|
||
// }
|
||
// return data;
|
||
}
|
||
|
||
/// 获取已保存的登录信息
|
||
static Future<Map<String, dynamic>?> getUser() async {
|
||
final prefs = await SharedPreferences.getInstance();
|
||
final jsonStr = prefs.getString(_keyUser);
|
||
if (jsonStr == null) return null;
|
||
return json.decode(jsonStr);
|
||
}
|
||
|
||
/// 获取已保存的用户信息
|
||
static Future<Map<String, dynamic>?> getUserInfoData() async {
|
||
final prefs = await SharedPreferences.getInstance();
|
||
final jsonStr = prefs.getString(_keyUserInfoData);
|
||
if (jsonStr == null) return null;
|
||
return json.decode(jsonStr);
|
||
}
|
||
|
||
/// 登出
|
||
static Future<void> logout() async {
|
||
final prefs = await SharedPreferences.getInstance();
|
||
await prefs.remove(_keyUser);
|
||
await prefs.remove(_keyRemember);
|
||
await prefs.setBool(_keyIsLoggedIn, false);
|
||
// 停止心跳服务
|
||
HeartbeatService().stop();
|
||
// 清空Session
|
||
await SessionService.instance.clear();
|
||
}
|
||
}
|