123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<dynamic> {
- DemoController({required this.apiRepository});
- var count = 0.obs;
- void increase() => ++count;
- final ApiRepository apiRepository;
- final registerEmailController = TextEditingController();
- //调用接口
- Future<void> 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<void> getServerTime2() async {
- final result = await apiRepository.getServerTime2();
- if (result.isSuccess) {
- SmartDialog.compatible.showToast("获取时间:${result.data?.timestamps}");
- } else {}
- }
- Future<void> getIndustry2() async {
- final result = await apiRepository.getIndustryList2();
- if (result.isSuccess) {
- SmartDialog.compatible.showToast("获取行列列表:${result.list?.toString()}");
- } else {}
- }
- Future<File> getLocalFile(String fileName) async {
- final directory = await getApplicationDocumentsDirectory();
- return File('${directory.path}/$fileName');
- }
- /// 并发执行
- Future<void> complicatedFetch() async {
- //由于是单线程的,对并发的逻辑并不好,不如Kotlin
- // 等待所有的Future对象都完成
- List<dynamic> results = await Future.wait([apiRepository.getServerTime2(), apiRepository.getIndustryList2()]);
- int? timestamps;
- List<IndustryData?>? industries;
- for (var future in results) {
- if (future is HttpResult<ServerTime?>) {
- final serverTime = future;
- timestamps = serverTime.data?.timestamps;
- } else if (future is HttpResult<IndustryData?>) {
- final industryList = future;
- industries = industryList.list;
- }
- }
- SmartDialog.compatible.showToast("并发完成的数据 行业数量:${industries?.length} 当前时间:$timestamps");
- }
- }
|