141 lines
4.6 KiB
Dart
Executable File
141 lines
4.6 KiB
Dart
Executable File
|
|
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:get/get.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:star_lock/login/login/entity/LoginData.dart';
|
|
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
|
import 'package:star_lock/main/lockDetail/passwordKey/passwordKeyList/passwordKeyListEntity.dart';
|
|
import 'package:star_lock/mine/minePersonInfo/minePersonInfoPage/minePersonInfo_entity.dart';
|
|
import 'package:star_lock/mine/minePersonInfo/minePersonInfoPage/minePersonInfo_state.dart';
|
|
import 'package:star_lock/widget/permission/permission_dialog.dart';
|
|
import '../../../../network/api_repository.dart';
|
|
import '../../../../tools/baseGetXController.dart';
|
|
import '../../../tools/eventBusEventManage.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'minePersonGetUploadFileInfo_entity.dart';
|
|
|
|
class MinePersonInfoLogic extends BaseGetXController {
|
|
MinePersonInfoState state = MinePersonInfoState();
|
|
|
|
//用户信息
|
|
Future<void> getUserInfoRequest() async {
|
|
final MinePersonInfoEntity entity = await ApiRepository.to.getUserInfo();
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.mineInfoData.value = entity.data!;
|
|
state.headUrl.value = state.mineInfoData.value.headUrl!;
|
|
}
|
|
}
|
|
|
|
//上传头像 先获取upToken 再调用updateUserInfo
|
|
Future<void> getUpTokenRequest(String filename, int size) async {
|
|
final MinePersonGetUploadFileInfoEntity entity = await ApiRepository.to.getUpToken(
|
|
userId: state.mineInfoData.value.uid!.toString(),
|
|
filename: filename,
|
|
size: size);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
uploadFile(entity);
|
|
}
|
|
}
|
|
|
|
// 上传头像
|
|
Future<void> uploadFile(MinePersonGetUploadFileInfoEntity minePersonGetUploadFileInfoEntity) async {
|
|
final File bytes = File(state.image!.path);
|
|
final Uint8List enc = await bytes.readAsBytes();
|
|
|
|
final FormData form = FormData(minePersonGetUploadFileInfoEntity.data!.formData!);
|
|
form.files.add(
|
|
MapEntry(
|
|
minePersonGetUploadFileInfoEntity.data!.fileField!,
|
|
MultipartFile(enc, filename: minePersonGetUploadFileInfoEntity.data!.formData!['key'])
|
|
)
|
|
);
|
|
|
|
final LoginEntity entity = await ApiRepository.to.uploadFile(
|
|
url: minePersonGetUploadFileInfoEntity.data!.uploadUrl!, body: form);
|
|
if (entity.errorCode! == -1) {
|
|
// 没有返回 返回null成功
|
|
updateUserInfoRequest(minePersonGetUploadFileInfoEntity.data!.fileUrl!);
|
|
}
|
|
}
|
|
|
|
//更新个人信息-头像
|
|
Future<void> updateUserInfoRequest(String headUrl) async {
|
|
final PasswordKeyListEntity entity = await ApiRepository.to.updateUserHeadUrlInfo(headUrl: headUrl);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.headUrl.value = headUrl;
|
|
state.mineInfoData.value.headUrl = state.headUrl.value;
|
|
final LoginData? loginData = await Storage.getLoginData();
|
|
loginData!.headUrl = headUrl;
|
|
await Storage.saveLoginData(loginData);
|
|
eventBus.fire(MineInfoChangeRefreshUI());
|
|
showToast('上传成功'.tr);
|
|
}
|
|
}
|
|
|
|
//上传头像选择
|
|
Future<void> chooseCallback(int index) async {
|
|
if (index == 0) {
|
|
//拍照选项
|
|
_checkCameraPermission();
|
|
} else if (index == 1) {
|
|
_checkPhotoPermission();
|
|
}
|
|
}
|
|
|
|
///拍摄照片
|
|
Future<void> selectCamera() async {
|
|
final XFile? photo = await state.imagePicker.pickImage(
|
|
source: ImageSource.camera,
|
|
maxHeight: 250,
|
|
maxWidth: 250,
|
|
);
|
|
if (photo != null) {
|
|
state.image = photo;
|
|
final File bytes = File(state.image!.path);
|
|
final Uint8List enc = await bytes.readAsBytes();
|
|
getUpTokenRequest(state.image!.name, enc.length);
|
|
}
|
|
}
|
|
|
|
///从相册选取
|
|
Future<void> selectImage() async {
|
|
final XFile? image = await state.imagePicker.pickImage(
|
|
source: ImageSource.gallery,
|
|
maxHeight: 250,
|
|
maxWidth: 250,
|
|
);
|
|
if (image != null) {
|
|
state.image = image;
|
|
final File bytes = File(state.image!.path);
|
|
final Uint8List enc = await bytes.readAsBytes();
|
|
getUpTokenRequest(state.image!.name, enc.length);
|
|
}
|
|
}
|
|
|
|
//权限判断 访问相机
|
|
Future<void> _checkCameraPermission() async {
|
|
final bool status = await PermissionDialog.request(Permission.camera);
|
|
if (status) {
|
|
selectCamera();
|
|
}
|
|
}
|
|
|
|
//权限判断 访问相册
|
|
Future<void> _checkPhotoPermission() async {
|
|
final bool status = await PermissionDialog.requestPhotos();
|
|
if (status) {
|
|
selectImage();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
getUserInfoRequest();
|
|
}
|
|
}
|