import 'dart:io'; import 'package:ftrecruiter/api/http_result.dart'; import 'package:ftrecruiter/entity/response/industry_data.dart'; import 'package:path_provider/path_provider.dart'; import 'package:flustars/flustars.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:ftrecruiter/api/api_repository.dart'; import 'package:ftrecruiter/comm/utils/log_utils.dart'; import 'package:ftrecruiter/entity/response/server_time.dart'; import 'package:get/get.dart'; class DemoController extends GetxController with StateMixin { DemoController({required this.apiRepository}); var count = 0.obs; void increase() => ++count; final ApiRepository apiRepository; final registerEmailController = TextEditingController(); //调用接口 Future getUserInfo() async { change(null, status: RxStatus.loading()); //测试Post请求,用户登陆 final result = await apiRepository.userLogin(); if (result.isSuccess) { final token = result.data?.token; if (token != null) { final profile = await apiRepository.getUserProfile(token); if (profile.isSuccess == true) { final nickName = profile.data?.nickName; SmartDialog.compatible.showToast("当前登录的用户为:$nickName"); change(null, status: RxStatus.success()); } } } else { final errorMsg = result.errorMsg; change(null, status: RxStatus.error(errorMsg)); } //测试Post请求,用户登陆 // apiRepository.userLoginEasy(success: (userLogin) { // // SmartDialog.showToast("Token: ${userLogin.token}"); // apiRepository.userProfileUpdate(userLogin.token,success: (userLogin) { // // SmartDialog.showToast("更新用户信息成功"); // // },onError: (code, msg) { // // SmartDialog.showToast("更新用户信息错误: $code $msg"); // // }); // }, onError: (code, msg) { // Log.d("用户登陆-error:$code $msg"); // }); } Future getServerTime2() async { final result = await apiRepository.getServerTime2(); if (result.isSuccess) { SmartDialog.compatible.showToast("获取时间:${result.data?.timestamps}"); } else {} } Future getIndustry2() async { final result = await apiRepository.getIndustryList2(); if (result.isSuccess) { SmartDialog.compatible.showToast("获取行列列表:${result.list?.toString()}"); } else {} } Future getLocalFile(String fileName) async { final directory = await getApplicationDocumentsDirectory(); return File('${directory.path}/$fileName'); } /// 并发执行 Future complicatedFetch() async { //由于是单线程的,对并发的逻辑并不好,不如Kotlin // 等待所有的Future对象都完成 List results = await Future.wait([apiRepository.getServerTime2(), apiRepository.getIndustryList2()]); int? timestamps; List? industries; for (var future in results) { if (future is HttpResult) { final serverTime = future; timestamps = serverTime.data?.timestamps; } else if (future is HttpResult) { final industryList = future; industries = industryList.list; } } SmartDialog.compatible.showToast("并发完成的数据 行业数量:${industries?.length} 当前时间:$timestamps"); } }