circle_app/circle_app/lib/circle_app/userinfo/logic.dart
2024-08-13 09:42:07 +08:00

685 lines
18 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tencent_cloud_chat_uikit/data_services/friendShip/friendship_services.dart';
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import '../../commons/Widgets/base_tip_widget.dart';
import '../../commons/config.dart';
import '../../net/api.dart';
import '../../net/dio_manager.dart';
import '../../utils/SharedPreferencesHelper.dart';
import '../../utils/eventBus.dart';
import '../../utils/qiniu.dart';
import '../../utils/util.dart';
import '../../view/notice.dart';
import '../dialog/BaseDialog.dart';
import 'state.dart';
class UserinfoLogic extends GetxController {
var userId = Get.arguments ?? "";
final UserinfoState state = UserinfoState();
final ImagePicker _picker = ImagePicker();
UserBean? userInfoBean;
String imId = '';
String ageMsg = "";
var isVip = 0;
var onLineCity = "";
var isLikeFoMsg = '';
var quToken = '';
bool isMe = false;
bool isEdit = false;
bool isUrgeStatus = false;
bool isOnline = false;
bool isShowAlbum = true;
bool isLike = false;
bool isBlack = false;
bool isBlackBeen = false;
bool isDestroy = false;
List giftList = [];
List recevigiftList = [];
List openCallOutIdList = [];
Map toUser = {};
int unLockWxNum = 0;
int likeMeCount = 0;
int imageUrgeCount = 0;
final startTime = DateTime.now();
SharedPreferences? sharedPreferences;
@override
void onClose() {
// TODO: implement onClose
super.onClose();
}
@override
void onInit() async {
super.onInit();
// SmartDialog.showLoading();
sharedPreferences = await SharedPreferences.getInstance();
int? sharedUserId =
sharedPreferences!.getInt(SharedPreferencesHelper.USERID);
if (userId.isNotEmpty &&
sharedUserId != null &&
sharedUserId.toString() == userId) {
userId = '';
}
state.imaglist.clear();
if (userId.isEmpty) {
isMe = true;
update();
fetchUserInfo(Api.getUserInfo);
fetchMyAlbum(Api.getMyAlbum);
} else {
fetchUserInfo("${Api.getUserInfoTA + userId}/home");
fetchMyAlbum("${Api.getTaAlbum + userId}/albums");
fetchUrgeStatus("${Api.getUrgeStatus + userId}/urge/album/status");
fetchIsBlack("${Api.setBlock + userId}/block");
}
// SmartDialog.dismiss();
fetchQnToken(Api.getqiniuToken);
loadGiftListData();
}
loadGiftListData() async {
var result = await DioManager.instance.get(url: Api.giftList);
if (result['code'] == 200) {
giftList = result['data'];
loadData();
}
}
void loadData() async {
SharedPreferencesHelper sp = await SharedPreferencesHelper.getInstance();
String myId = sp.getMyUserId();
var result = await DioManager.instance.get(
url: userId.toString().isNotEmpty
? Api.giftHall + userId
: Api.giftHall + myId,
);
if (result['code'] == 200) {
// topTitle = result['topDesc'];
toUser = result['data']['topUser'] ?? {};
// total = result['receiveTotal'];
// "accid" -> "ky_dev_30629"
// accid = result['accid'];
// receiveGiftNum
recevigiftList = result['data']['receiveGiftNum'];
if (recevigiftList.isNotEmpty) {
var receiveList = [];
var noreceiveList = [];
giftList.forEach((element) {
bool isContain = false;
for (var info in recevigiftList) {
if (element['id'] == info['giftId']) {
isContain = true;
}
}
if (isContain) {
receiveList.add(element);
} else {
noreceiveList.add(element);
}
});
receiveList.addAll(noreceiveList.reversed.toList());
giftList = receiveList;
update();
}
update();
}
}
void showBlackDialog(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return BaseDialog(
isDismiss: false,
);
});
}
Future<void> fetchIsBlack(String url) async {
var data = await DioManager.instance.get(url: url);
if (data['code'] == 200) {
isBlack = data["data"]["status"] == 1;
}
}
Future<void> fetchUserInfo(String url) async {
var data = await DioManager.instance.get(url: url);
var bean = BaseResponse<ResponseBean>.fromJson(
data, (data) => ResponseBean.fromJson(data));
if (bean.isSuccess()) {
isMe = userId.isEmpty;
isLike = bean.data.isFollow;
isLikeFoMsg =
"${bean.data.likeMeCount}位圈友感兴趣,其中${bean.data.imageUrgeCount}位已催您更新";
likeMeCount = bean.data.likeMeCount;
imageUrgeCount = bean.data.imageUrgeCount;
userInfoBean = bean.data.user;
unLockWxNum = userInfoBean!.contact!.contains('*') ? 0 : 1;
isVip = userInfoBean?.vip ?? 0;
imId = bean.data.accountId;
print("imId=" + imId);
if (isMe) {
isOnline = true;
} else {
// if(!isLike){
// startCountdown();
// }
isOnline = userInfoBean!.isOnline;
isBlackBeen = bean.data.isBlock;
isDestroy = bean.data.isDestroy;
if (isBlackBeen) {
showOKToast("您已被对方拉黑");
}
}
onLineCity = userInfoBean!.onlineFlag!;
if (userInfoBean?.currentCity != null) {
if (onLineCity.isNotEmpty) {
onLineCity = "$onLineCity·${userInfoBean!.currentCity}";
} else {
onLineCity = userInfoBean!.currentCity! ?? '';
}
} else {
onLineCity = "$onLineCity";
}
ageMsg = getAgeCOntent(userInfoBean!.gender, userInfoBean!.age,
userInfoBean!.role, userInfoBean!.orientation);
} else if (bean.code == 9999) {
showBlackDialog(Get.context!);
} else {
showOKToast(bean.msg);
}
update();
}
Future<bool> checkIsShowTip() async {
var result = await DioManager.instance.get(
url: Api.popup + userId,
);
return result['data'] ?? false;
}
Future<void> fetchMyAlbum(String url) async {
var myAlbumData = await DioManager.instance.get(url: url);
var myAlbumBean = BaseResponse<AlbumResponseBean>.fromJson(
myAlbumData, (myAlbumData) => AlbumResponseBean.fromJson(myAlbumData));
if (myAlbumBean.isSuccess()) {
state.imaglist.clear();
state.imaglist.addAll(myAlbumBean.data.lists);
}
update();
}
Future<void> fetchUrgeStatus(String url) async {
var urgedata = await DioManager.instance.get(url: url);
var urgeBean = BaseResponse<UrgentStatus>.fromJson(
urgedata, (urgedata) => UrgentStatus.fromJson(urgedata));
isUrgeStatus = urgeBean.data?.isUrgent ?? false;
update();
}
Future<void> fetchQnToken(String url) async {
var data = await DioManager.instance.get(url: url, params: {});
var bean = BaseResponse<QnTokenData>.fromJson(
data, (data) => QnTokenData.fromJson(data));
if (bean.isSuccess()) {
quToken = bean.data?.token.toString() ?? '';
}
}
urgeChange() async {
var data = await DioManager.instance
.post(url: "${Api.urgeAlbum + userId}/urge/album");
var bean = BaseResponse<String>.fromJson(data, (data) => data);
if (bean.isSuccess()) {
showOKToast("催更成功");
isUrgeStatus = true;
update();
} else {
showOKToast(bean.msg);
}
}
delAlbumImage(int index) async {
var data = await DioManager.instance
.delete(url: Api.deleteAlbum + state.imaglist[index].id.toString());
var bean = BaseResponse<String>.fromJson(data, (data) => data);
if (bean.code == 200) {
state.imaglist.removeAt(index);
showOKToast('操作成功');
update();
}
}
setLike() async {
if (isBlack || isDestroy) {
showOKToast("喜欢失败,存在拉黑关系或者该账户已注销");
return;
}
var data = await DioManager.instance.post(
url: "${Api.setLike + userId}/follow",
params: {'status': isLike ? "0" : "1"});
var bean = BaseResponse<dynamic>.fromJson(
data,
(jsonData) => jsonData,
);
if (bean.isSuccess()) {
isLike = !isLike;
update();
}
showOKToast(bean.msg);
}
setBlock(String status) async {
List<String> parts = imId.split('_');
if (parts[2] == "10") {
showOKToast("不可拉黑客服号哦~");
return;
}
var data = await DioManager.instance.post(
url: "${Api.setBlock + userId}/block", params: {'status': status});
var bean = BaseResponse<dynamic>.fromJson(
data,
(jsonData) => jsonData,
);
if (bean.isSuccess()) {
final FriendshipServices _friendshipServices =
serviceLocator<FriendshipServices>();
isBlack = status == "1";
try {
if (isBlack) {
Navigator.pop(Get.context!);
var result =
await _friendshipServices.addToBlackList(userIDList: [imId]);
print("拉黑成功" + result.toString());
} else {
var result =
await _friendshipServices.deleteFromBlackList(userIDList: [imId]);
print("取消拉黑成功" + result.toString());
}
// await Future.delayed(Duration(milliseconds: 500));
// Future.delayed(Duration(seconds: 3),() {
EventBusManager.fire(CommentBlackEvent(userId: imId, isBlack: isBlack));
// });
} catch (e) {}
update();
} else if (bean.code == 21201 || bean.code == 21202) {
showVipDialog();
}
showOKToast(bean.msg);
}
void sendGiftData(
String accid,
String giftId,
String userId,
) async {
var result = await DioManager.instance.post(url: Api.sendGift, params: {
'accid': accid,
'giftId': giftId,
'num': 1,
'toUserId': userId
});
if (result['code'] == 200) {
showOKToast('赠送成功');
loadData();
} else if (result['code'] == 31201) {
showOKToast(result['msg']);
showRechargeScreenDialog();
}
}
Future getImageFile() async {
checkPhotosStatus();
try {
final XFile? pickedFile = await _picker.pickImage(
source: ImageSource.gallery,
);
if (null == pickedFile) {
return;
}
SmartDialog.showLoading(msg: '上传中');
uploadImage(quToken, pickedFile, ImgPath.USER_ALBUM_IMAGE,
(result) async {
var data = await DioManager.instance
.post(url: Api.updataAlbum, params: {"type": 1, "url": result});
var myAlbumBean = BaseResponse<AddAlbum>.fromJson(
data, (data) => AddAlbum.fromJson(data));
if (myAlbumBean.code == 200) {
SmartDialog.dismiss();
state.imaglist.insert(
0,
AlbumListItem(
id: myAlbumBean.data.id,
type: myAlbumBean.data.type,
url: result,
urlThumb: result,
isTop: 0));
update();
}
});
} catch (_) {}
}
void sendWhatToWx() async {
var result = await DioManager.instance.get(
url: Api.noticeWxNum + userId,
);
if (result['code'] == 200) {
showOKToast('已提醒对方填写');
} else {
showOKToast(result['msg']);
}
}
void setTopAlbum(bool isTop, int index) async {
var result = await DioManager.instance.post(
url: Api.setTopAlbum,
params: {'picId': state.imaglist[index].id, 'isTop': isTop ? 1 : 0});
if (result['code'] == 200) {
showOKToast('操作成功');
fetchMyAlbum(Api.getMyAlbum);
}
}
}
class UserBean {
int id;
String nickname;
String account_id;
String avatar;
String birthday;
String wx_num;
int chat_need_vip;
int age;
String contact;
int contactType;
String signature;
int vip;
int gender;
int role;
int mark;
int orientation;
int userType;
List<Interest> interests;
List orientations;
double lng;
double lat;
String? city;
String? currentCity;
bool isOnline;
int hide_wx_num;
DateTime? offlineTime;
String avatarThumb;
String onlineFlag;
UserBean({
required this.id,
required this.nickname,
required this.avatar,
required this.account_id,
required this.wx_num,
required this.birthday,
required this.age,
required this.signature,
required this.vip,
required this.userType,
required this.gender,
required this.hide_wx_num,
required this.orientations,
required this.mark,
required this.currentCity,
required this.role,
required this.contactType,
required this.contact,
required this.orientation,
required this.interests,
required this.lng,
required this.lat,
required this.onlineFlag,
this.city,
required this.chat_need_vip,
required this.isOnline,
this.offlineTime,
required this.avatarThumb,
});
factory UserBean.fromJson(Map<String, dynamic> json) {
return UserBean(
id: json['id'],
userType: json['userType'] ?? 0,
currentCity: json['currentCity'] ?? '',
hide_wx_num: json['hide_wx_num'] ?? 0,
orientations: json['orientations'] ?? [],
account_id: json['account_id'] ?? '',
chat_need_vip: json['chatNeedVip'],
wx_num: json['wx_num'] ?? '',
mark: json['mark'] ?? 0,
nickname: json['nickname'],
onlineFlag: json['onlineFlag'] ?? '',
avatar: json['avatar'],
birthday: json['birthday'],
contact: json['contact'] ?? '',
contactType: json['contactType'] ?? 0,
age: json['age'],
signature: json['signature'] ?? '还没有好的签名哦~',
vip: json['vip'],
gender: json['gender'],
role: json['role'],
orientation: json['orientation'],
interests: json['interests'] == null
? []
: List<Interest>.from(
json['interests'].map((x) => Interest.fromJson(x)),
),
lng: json['lng'],
lat: json['lat'],
city: json['city'],
isOnline: json['isOnline'],
offlineTime: json['offlineTime'] != null
? DateTime.parse(json['offlineTime'])
: null,
avatarThumb: json['avatar_thumb'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['age'] = this.age;
data['avatar'] = this.avatar;
data['avatar_thumb'] = this.avatarThumb;
data['birthday'] = this.birthday;
data['city'] = this.city;
data['contact'] = this.contact;
data['contactType'] = this.contactType;
data['currentCity'] = this.currentCity;
data['gender'] = this.gender;
data['id'] = this.id;
if (this.interests != null) {
data['interests'] = this.interests.map((v) => v.toJson()).toList();
}
data['isOnline'] = this.isOnline;
data['lat'] = this.lat;
data['orientation'] = this.orientation;
data['lng'] = this.lng;
data['mark'] = this.mark;
data['nickname'] = this.nickname;
data['offline_time'] = this.offlineTime;
data['onlineFlag'] = this.onlineFlag;
data['role'] = this.role;
data['signature'] = this.signature;
data['userType'] = this.userType;
data['vip'] = this.vip;
return data;
}
}
class Interest {
int id;
String title;
int viewTotal;
Interest({
required this.id,
required this.title,
required this.viewTotal,
});
factory Interest.fromJson(Map<String, dynamic> json) {
return Interest(
id: json['id'],
viewTotal: json['viewTotal'] ?? 0,
title: json['title'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
data['viewTotal'] = this.viewTotal;
return data;
}
}
class ResponseBean {
UserBean user;
int likeMeCount;
int imageUrgeCount;
int unLockWxNum;
bool isFollow;
bool isBlock;
bool isDestroy;
String accountId;
ResponseBean(
{required this.user,
required this.likeMeCount,
required this.imageUrgeCount,
required this.unLockWxNum,
required this.accountId,
required this.isBlock,
required this.isDestroy,
required this.isFollow});
factory ResponseBean.fromJson(Map<String, dynamic> json) {
return ResponseBean(
accountId: json['account_id'],
unLockWxNum: json['unLockWxNum'],
user: UserBean.fromJson(json['user']),
likeMeCount: json['like_me_count'],
imageUrgeCount: json['image_urge_count'],
isBlock: json['is_been_block'],
isDestroy: json['is_destroy'],
isFollow: json['is_follow'],
);
}
}
class AlbumResponseBean {
List<AlbumListItem> lists;
int total;
AlbumResponseBean({
required this.lists,
required this.total,
});
factory AlbumResponseBean.fromJson(Map<String, dynamic> json) {
return AlbumResponseBean(
lists: List<AlbumListItem>.from(
json['lists'].map((x) => AlbumListItem.fromJson(x))),
total: json['total'],
);
}
}
class AlbumListItem {
int id;
int type;
int isTop;
String url;
String urlThumb;
AlbumListItem({
required this.id,
required this.type,
required this.url,
required this.isTop,
required this.urlThumb,
});
factory AlbumListItem.fromJson(Map<String, dynamic> json) {
return AlbumListItem(
id: json['id'],
type: json['type'],
url: json['url'],
urlThumb: json['thumb'],
isTop: json['isTop'] ?? 0);
}
}
class AddAlbum {
int id;
int type;
String url;
AddAlbum({
required this.id,
required this.type,
required this.url,
});
factory AddAlbum.fromJson(Map<String, dynamic> json) {
return AddAlbum(
id: json['id'],
type: json['type'],
url: json['url'],
);
}
}
class UrgentStatus {
bool isUrgent;
UrgentStatus({
required this.isUrgent,
});
factory UrgentStatus.fromJson(Map<String, dynamic> json) {
return UrgentStatus(
isUrgent: json['is_urge'],
);
}
}