You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.6 KiB
107 lines
2.6 KiB
|
|
import 'dart:convert';
|
|
|
|
import 'package:dating_touchme_app/model/home/test_data.dart';
|
|
import 'package:dating_touchme_app/network/user_api.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class TestController extends GetxController {
|
|
final int type;
|
|
TestController({required this.type});
|
|
|
|
late UserApi _userApi;
|
|
|
|
final testData = TestData().obs;
|
|
|
|
final subData = <Map<String, int>>[
|
|
|
|
].obs;
|
|
|
|
final index = 0.obs;
|
|
final sIndex = 0.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_userApi = Get.find<UserApi>();
|
|
getTestInfo();
|
|
}
|
|
|
|
final resultData = {}.obs;
|
|
final resultTitle = "".obs;
|
|
|
|
reTest(){
|
|
resultData.value = {};
|
|
resultTitle.value = "";
|
|
subData.value = [];
|
|
index.value = 0;
|
|
sIndex.value = 0;
|
|
}
|
|
|
|
submit() async {
|
|
try {
|
|
final response = await _userApi.userSubmitPersonalityTest({
|
|
"id": type,
|
|
"personalityOptionList": subData
|
|
});
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
final data = response.data.data;
|
|
if(type == 1){
|
|
print(data);
|
|
resultTitle.value = data["type"];
|
|
resultData.value = jsonDecode(data["report"])["report"];
|
|
print(resultTitle.value);
|
|
print(resultData.value);
|
|
} else {
|
|
print(data);
|
|
resultTitle.value = data["type"];
|
|
resultData.value = jsonDecode(data["report"])["report"];
|
|
print(resultTitle.value);
|
|
print(resultData.value);
|
|
}
|
|
} else {
|
|
|
|
// 响应失败,抛出异常
|
|
throw Exception(response.data.message ?? '获取数据失败');
|
|
}
|
|
} catch(e){
|
|
print('钱包数据获取失败: $e');
|
|
SmartDialog.showToast('提交答案数据获取失败');
|
|
rethrow;
|
|
|
|
}
|
|
}
|
|
|
|
select(int qId, int oId){
|
|
if(subData.length < testData.value.questionList!.length){
|
|
subData.add({
|
|
"personalityQuestionId": qId,
|
|
"personalityOptionId": oId
|
|
});
|
|
sIndex.value++;
|
|
if(index.value < testData.value.questionList!.length - 1){
|
|
index.value++;
|
|
}
|
|
}
|
|
}
|
|
|
|
getTestInfo() async {
|
|
try {
|
|
final response = await _userApi.userGetPersonalityTestDetail(type: type);
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
testData.value = response.data.data ?? TestData();
|
|
} else {
|
|
|
|
// 响应失败,抛出异常
|
|
throw Exception(response.data.message ?? '获取数据失败');
|
|
}
|
|
} catch(e){
|
|
print('钱包数据获取失败: $e');
|
|
SmartDialog.showToast('测试数据获取失败');
|
|
rethrow;
|
|
|
|
}
|
|
}
|
|
|
|
}
|