demo_controller.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:io';
  2. import 'package:ftrecruiter/api/http_result.dart';
  3. import 'package:ftrecruiter/entity/response/industry_data.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:flustars/flustars.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  8. import 'package:ftrecruiter/api/api_repository.dart';
  9. import 'package:ftrecruiter/comm/utils/log_utils.dart';
  10. import 'package:ftrecruiter/entity/response/server_time.dart';
  11. import 'package:get/get.dart';
  12. class DemoController extends GetxController with StateMixin<dynamic> {
  13. DemoController({required this.apiRepository});
  14. var count = 0.obs;
  15. void increase() => ++count;
  16. final ApiRepository apiRepository;
  17. final registerEmailController = TextEditingController();
  18. //调用接口
  19. Future<void> getUserInfo() async {
  20. change(null, status: RxStatus.loading());
  21. //测试Post请求,用户登陆
  22. final result = await apiRepository.userLogin();
  23. if (result.isSuccess) {
  24. final token = result.data?.token;
  25. if (token != null) {
  26. final profile = await apiRepository.getUserProfile(token);
  27. if (profile.isSuccess == true) {
  28. final nickName = profile.data?.nickName;
  29. SmartDialog.compatible.showToast("当前登录的用户为:$nickName");
  30. change(null, status: RxStatus.success());
  31. }
  32. }
  33. } else {
  34. final errorMsg = result.errorMsg;
  35. change(null, status: RxStatus.error(errorMsg));
  36. }
  37. //测试Post请求,用户登陆
  38. // apiRepository.userLoginEasy(success: (userLogin) {
  39. //
  40. // SmartDialog.showToast("Token: ${userLogin.token}");
  41. // apiRepository.userProfileUpdate(userLogin.token,success: (userLogin) {
  42. //
  43. // SmartDialog.showToast("更新用户信息成功");
  44. //
  45. // },onError: (code, msg) {
  46. //
  47. // SmartDialog.showToast("更新用户信息错误: $code $msg");
  48. //
  49. // });
  50. // }, onError: (code, msg) {
  51. // Log.d("用户登陆-error:$code $msg");
  52. // });
  53. }
  54. Future<void> getServerTime2() async {
  55. final result = await apiRepository.getServerTime2();
  56. if (result.isSuccess) {
  57. SmartDialog.compatible.showToast("获取时间:${result.data?.timestamps}");
  58. } else {}
  59. }
  60. Future<void> getIndustry2() async {
  61. final result = await apiRepository.getIndustryList2();
  62. if (result.isSuccess) {
  63. SmartDialog.compatible.showToast("获取行列列表:${result.list?.toString()}");
  64. } else {}
  65. }
  66. Future<File> getLocalFile(String fileName) async {
  67. final directory = await getApplicationDocumentsDirectory();
  68. return File('${directory.path}/$fileName');
  69. }
  70. /// 并发执行
  71. Future<void> complicatedFetch() async {
  72. //由于是单线程的,对并发的逻辑并不好,不如Kotlin
  73. // 等待所有的Future对象都完成
  74. List<dynamic> results = await Future.wait([apiRepository.getServerTime2(), apiRepository.getIndustryList2()]);
  75. int? timestamps;
  76. List<IndustryData?>? industries;
  77. for (var future in results) {
  78. if (future is HttpResult<ServerTime?>) {
  79. final serverTime = future;
  80. timestamps = serverTime.data?.timestamps;
  81. } else if (future is HttpResult<IndustryData?>) {
  82. final industryList = future;
  83. industries = industryList.list;
  84. }
  85. }
  86. SmartDialog.compatible.showToast("并发完成的数据 行业数量:${industries?.length} 当前时间:$timestamps");
  87. }
  88. }