356 lines
12 KiB
Dart
356 lines
12 KiB
Dart
import 'dart:io';
|
||
import 'dart:convert';
|
||
|
||
import 'package:circle_app/app/splash/binding.dart';
|
||
import 'package:circle_app/common/values/pushconfig.dart';
|
||
import 'package:circle_app/network/api.dart';
|
||
import 'package:circle_app/network/dio_manager.dart';
|
||
import 'package:circle_app/router/app_pages.dart';
|
||
import 'package:circle_app/router/app_routers.dart';
|
||
import 'package:circle_app/util/PaymentUtils.dart';
|
||
import 'package:circle_app/util/SharedPreferencesHelper.dart';
|
||
import 'package:circle_app/util/device.dart';
|
||
import 'package:circle_app/util/util.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:tencent_cloud_chat_uikit/business_logic/separate_models/tui_chat_separate_view_model.dart';
|
||
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
|
||
|
||
import 'package:tencent_chat_push_for_china/model/appInfo.dart';
|
||
|
||
import 'package:tencent_chat_push_for_china/tencent_chat_push_for_china.dart';
|
||
import 'app/login/login/logic.dart';
|
||
import 'app/splash/view.dart';
|
||
|
||
final CoreServicesImpl coreInstance = TIMUIKitCore.getInstance();
|
||
final TUIChatSeparateViewModel chatSeparateViewModel =
|
||
TUIChatSeparateViewModel();
|
||
|
||
void main() {
|
||
|
||
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
|
||
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
||
runApp(const MyApp());
|
||
}
|
||
|
||
int IM_SDK_APP_ID = 1400799631;
|
||
|
||
String IOS_APP_ID = "";
|
||
|
||
//登录IM
|
||
loginIM(String userId, String sig) async {
|
||
var info = await coreInstance.login(
|
||
userID: userId,
|
||
userSig: sig,
|
||
);
|
||
}
|
||
|
||
//退出IM
|
||
logoutIM() async {
|
||
final result = await coreInstance.logout();
|
||
print(result.desc + '退出IM');
|
||
}
|
||
|
||
initPush() {
|
||
// final TimUiKitPushPlugin cPush = TimUiKitPushPlugin(
|
||
// isUseGoogleFCM: true, // 中国大陆版无此参数
|
||
// );
|
||
// cPush.init(
|
||
// pushClickAction: pushClickAction, // 单击通知后的事件回调,会在STEP6讲解
|
||
// appInfo: PushConfig.appInfo, // 传入STEP1做的appInfo
|
||
// );
|
||
|
||
}
|
||
|
||
//跳转发消息页面
|
||
pushChatPage(String userId, String imId, String userName) async {
|
||
var con = V2TimConversation(
|
||
conversationID: "c2c_$imId", userID: imId, showName: userName, type: 1);
|
||
await createCustomMsg(userId, imId);
|
||
Get.toNamed(AppRoutes.Chat, arguments: con);
|
||
}
|
||
|
||
createCustomMsg(String userId, String imId) async {
|
||
//通过会话ID获取指定会话列表
|
||
V2TimValueCallback<List<V2TimConversation>>
|
||
getConversationListByConversaionIdsRes = await TencentImSDKPlugin
|
||
.v2TIMManager
|
||
.getConversationManager()
|
||
.getConversationListByConversaionIds(
|
||
conversationIDList: ["c2c_$imId"]); //需要获取会话列表数据的会话id列表
|
||
if (getConversationListByConversaionIdsRes.code == 0 &&
|
||
getConversationListByConversaionIdsRes.data!.isEmpty) {
|
||
//获取资料,然后发送自定义卡片消息
|
||
var data = await DioManager.instance.get(
|
||
url: 'msg-service/user/$userId/chat/card',
|
||
);
|
||
if (data['code'] == 200) {
|
||
String desc = '';
|
||
Map<String, dynamic> info = {};
|
||
if (data['data']['both_cities'] != null) {
|
||
List city = data['data']['both_cities'];
|
||
if (city.isNotEmpty) {
|
||
desc = '你们都在$city留下过足迹';
|
||
}
|
||
}
|
||
if (data['data']['both_interests'] != null) {
|
||
List<dynamic> both_interests = data['data']['both_interests'];
|
||
List<String> circleList = [];
|
||
both_interests.forEach((element) {
|
||
circleList.add(element['title']);
|
||
});
|
||
if (circleList.isNotEmpty) {
|
||
info['both_interests'] =
|
||
'你们有${circleList.length}个共同的圈子:${circleList.join('、')}';
|
||
if (desc.isEmpty) {
|
||
desc = '你们有${circleList.length}个共同的圈子:${circleList.join('、')}';
|
||
}
|
||
}
|
||
}
|
||
if (desc.isNotEmpty) {
|
||
info['interests'] = data['data']['interests'] ?? [];
|
||
info['guide_text'] = data['data']['guide_text'] ?? '';
|
||
info['my'] = data['data']['my'] ?? '';
|
||
info['myInterests'] = data['data']['myInterests'] ?? [];
|
||
info['user'] = data['data']['user'];
|
||
info['guide_text'] = data['data']['guide_text'] ?? '请问现在有空吗?';
|
||
await sendCustomMsg(imId, jsonEncode(info), desc);
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//发送文本消息
|
||
sendTextMsg(String userId, {String content = '看看这次缘分匹配到哪位小可爱呢?'}) async {
|
||
// 创建文本消息
|
||
V2TimValueCallback<V2TimMsgCreateInfoResult> createTextMessageRes =
|
||
await TencentImSDKPlugin.v2TIMManager
|
||
.getMessageManager()
|
||
.createTextMessage(
|
||
text: content, // 文本信息
|
||
);
|
||
if (createTextMessageRes.code == 0) {
|
||
// 文本信息创建成功
|
||
String? id = createTextMessageRes.data?.id;
|
||
// 发送文本消息
|
||
// 在sendMessage时,若只填写receiver则发个人用户单聊消息
|
||
// 若只填写groupID则发群组消息
|
||
// 若填写了receiver与groupID则发群内的个人用户,消息在群聊中显示,只有指定receiver能看见
|
||
V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin
|
||
.v2TIMManager
|
||
.getMessageManager()
|
||
.sendMessage(id: id!, receiver: userId, groupID: '');
|
||
if (sendMessageRes.code == 0) {
|
||
// 发送成功
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
// V2TimValueCallback<V2TimMsgCreateInfoResult> createCustomMessageRes =
|
||
// await TencentImSDKPlugin.v2TIMManager
|
||
// .getMessageManager()
|
||
// .createTextAtMessage(text: '看看这次缘分匹配到哪位小可爱呢?',atUserList: [],
|
||
// );
|
||
// if (createCustomMessageRes.code == 0) {
|
||
// String? id = createCustomMessageRes.data?.id;
|
||
// // 发送自定义消息
|
||
// // 在sendMessage时,若只填写receiver则发个人用户单聊消息
|
||
// // 若只填写groupID则发群组消息
|
||
// // 若填写了receiver与groupID则发群内的个人用户,消息在群聊中显示,只有指定receiver能看见
|
||
// V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin
|
||
// .v2TIMManager
|
||
// .getMessageManager()
|
||
// .sendMessage(id: id!, receiver: userId,groupID: '');
|
||
// if (sendMessageRes.code == 0) {
|
||
// // 发送成功
|
||
// return true;
|
||
// }
|
||
// }
|
||
}
|
||
|
||
//发送自定义消息
|
||
sendCustomMsg(String userId, String data, String desc) async {
|
||
// 创建自定义消息
|
||
V2TimValueCallback<V2TimMsgCreateInfoResult> createCustomMessageRes =
|
||
await TencentImSDKPlugin.v2TIMManager
|
||
.getMessageManager()
|
||
.createCustomMessage(
|
||
data: data,
|
||
desc: desc,
|
||
extension: 'cardData',
|
||
);
|
||
if (createCustomMessageRes.code == 0) {
|
||
String? id = createCustomMessageRes.data?.id;
|
||
// 发送自定义消息
|
||
//EventBusManager.fire(SendCoustomMessage(createCustomMessageRes));
|
||
|
||
|
||
// 在sendMessage时,若只填写receiver则发个人用户单聊消息
|
||
// 若只填写groupID则发群组消息
|
||
// 若填写了receiver与groupID则发群内的个人用户,消息在群聊中显示,只有指定receiver能看见
|
||
V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin
|
||
.v2TIMManager
|
||
.getMessageManager()
|
||
.sendMessage(id: id!, receiver: userId, groupID: '');
|
||
if (sendMessageRes.code == 0) {
|
||
// 发送成功
|
||
sendMessageRes.data?.customElem?.data; //自定义data
|
||
sendMessageRes.data?.customElem?.desc; //自定义desc
|
||
sendMessageRes.data?.customElem?.extension; //自定义extension
|
||
}
|
||
}
|
||
}
|
||
|
||
class MyApp extends StatefulWidget {
|
||
const MyApp({super.key});
|
||
|
||
@override
|
||
State<MyApp> createState() => _MyAppState();
|
||
}
|
||
|
||
class _MyAppState extends State<MyApp> {
|
||
// This widget is the root of your application.
|
||
|
||
final List<String> _guideList = ['bg', 'home_back'];
|
||
|
||
@override
|
||
void initState() {
|
||
initIM();
|
||
if (Platform.isAndroid) {
|
||
initWxApi();
|
||
}
|
||
|
||
uploadBuzIDAndToken();
|
||
loadBgImage();
|
||
super.initState();
|
||
// getLoginStyle();
|
||
}
|
||
|
||
getLoginStyle() async {
|
||
if ((await getAuthorization()).isEmpty) {
|
||
pushLoginPage();
|
||
FlutterNativeSplash.remove();
|
||
} else {
|
||
var data =
|
||
await DioManager.instance.put(url: Api.refreshToken, params: {});
|
||
var bean = BaseResponse<LoginData>.fromJson(
|
||
data, (data) => LoginData.fromJson(data));
|
||
if (bean.code == 200) {
|
||
SharedPreferencesHelper.getInstance().then((sharedPreferences) {
|
||
sharedPreferences.setString(SharedPreferencesHelper.AUTHORIZATION,
|
||
bean.data!.Authorization.toString());
|
||
});
|
||
FlutterNativeSplash.remove();
|
||
Get.offNamed(AppRoutes.Home);
|
||
|
||
return;
|
||
} else if (bean.code == 30002) {
|
||
SharedPreferencesHelper.getInstance().then((sharedPreferences) {
|
||
sharedPreferences.setString(SharedPreferencesHelper.AUTHORIZATION,
|
||
bean.data!.Authorization.toString());
|
||
});
|
||
Get.offNamed(AppRoutes.Complete_materialPage);
|
||
FlutterNativeSplash.remove();
|
||
return;
|
||
}
|
||
showToast(bean.msg.toString());
|
||
if (bean.code == 500) {
|
||
await Future.delayed(const Duration(seconds: 5));
|
||
getLoginStyle();
|
||
return;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
initIM() {
|
||
coreInstance.init(
|
||
sdkAppID: IM_SDK_APP_ID,
|
||
// Replace 0 with the SDKAppID of your IM application when integrating
|
||
// language: LanguageEnum.en, // 界面语言配置,若不配置,则跟随系统语言
|
||
loglevel: LogLevelEnum.V2TIM_LOG_DEBUG,
|
||
onTUIKitCallbackListener: (TIMCallback callbackValue) {
|
||
switch (callbackValue.type) {
|
||
case TIMCallbackType.INFO:
|
||
// Shows the recommend text for info callback directly
|
||
showToast(callbackValue.infoRecommendText!);
|
||
break;
|
||
case TIMCallbackType.API_ERROR:
|
||
//Prints the API error to console, and shows the error message.
|
||
print(
|
||
"Error from TUIKit: ${callbackValue.errorMsg}, Code: ${callbackValue.errorCode}");
|
||
if (callbackValue.errorCode == 10004 &&
|
||
callbackValue.errorMsg!.contains("not support @all")) {
|
||
showToast("当前群组不支持@全体成员");
|
||
} else {
|
||
showToast(callbackValue.errorMsg ??
|
||
callbackValue.errorCode.toString());
|
||
}
|
||
break;
|
||
case TIMCallbackType.FLUTTER_ERROR:
|
||
default:
|
||
// prints the stack trace to console or shows the catch error
|
||
if (callbackValue.catchError != null) {
|
||
showToast(callbackValue.catchError.toString());
|
||
} else {
|
||
print(callbackValue.stackTrace);
|
||
// loginIM();
|
||
}
|
||
}
|
||
},
|
||
// [建议配置,详见此部分](https://cloud.tencent.com/document/product/269/70746#callback)
|
||
listener: V2TimSDKListener(onConnectSuccess: () {
|
||
print('IM登录成功');
|
||
// loginIM();
|
||
}));
|
||
}
|
||
|
||
loadBgImage() async {
|
||
await precacheImage(precacheImages(_guideList.first), context);
|
||
await precacheImage(precacheImages(_guideList.last), context);
|
||
await precacheImage(AssetImage(getBaseImage('bg')), context,size: Size(Get.width, Get.height));
|
||
}
|
||
|
||
precacheImages(String image) {
|
||
return AssetImage(getBaseImage(image));
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
|
||
//填入设计稿中设备的屏幕尺寸,单位dp
|
||
// configureDio();
|
||
return ScreenUtilInit(
|
||
designSize: const Size(375, 812),
|
||
minTextAdapt: true,
|
||
splitScreenMode: true,
|
||
builder: (context, child) {
|
||
return GetMaterialApp(
|
||
title: '微乐园',
|
||
initialBinding: SplashBinding(),
|
||
getPages: AppPages.routes,
|
||
home: SplashPage(),
|
||
builder: FlutterSmartDialog.init(),
|
||
debugShowCheckedModeBanner: false,
|
||
);
|
||
});
|
||
}
|
||
|
||
//获取设备Token,获取成功后开启离线推送
|
||
void uploadBuzIDAndToken() async {
|
||
int? businessID = await TimUiKitPushPlugin.getBuzId(PushConfig.appInfo);
|
||
String token = await ChannelPush.getDeviceToken();
|
||
if (token != "") {
|
||
coreInstance.setOfflinePushConfig(
|
||
token: token,
|
||
businessID: businessID
|
||
);
|
||
}
|
||
}
|
||
}
|