login_view_model.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'package:eitc_erm_dental_flutter/entity/login_info.dart';
  2. import 'package:eitc_erm_dental_flutter/exts.dart';
  3. import 'package:eitc_erm_dental_flutter/funcs.dart';
  4. import 'package:eitc_erm_dental_flutter/http/api_service.dart';
  5. import 'package:eitc_erm_dental_flutter/http/http.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:riverpod_annotation/riverpod_annotation.dart';
  8. import '../../../global.dart';
  9. import '../../../sp_util.dart';
  10. part 'login_view_model.g.dart';
  11. @riverpod
  12. class Login extends _$Login {
  13. @override
  14. String build() {
  15. return "";
  16. }
  17. ///登录
  18. ///
  19. /// [mobile] 手机号
  20. Future<bool> login(String mobile) async {
  21. try {
  22. LoginInfo? info = await Http.instance
  23. .request(ApiService(Http.instance.dio).login(mobile));
  24. if (info == null || info.token.isNullOrEmpty) {
  25. logd("登录失败,返回的数据为null或token为空");
  26. return SynchronousFuture(false);
  27. }
  28. await SpUtil.setUserId(info.id!);
  29. await SpUtil.setUserName(mobile);
  30. await SpUtil.setToken(info.token!);
  31. updateToken(info.token!);
  32. } catch (e) {
  33. loge("登录异常", error: e);
  34. return SynchronousFuture(false);
  35. }
  36. return SynchronousFuture(true);
  37. }
  38. ///登出
  39. Future<bool> logout() async {
  40. try {
  41. String tk = token;
  42. Http.instance.request(ApiService(Http.instance.dio).logout(tk));
  43. } catch (e) {
  44. loge("登出异常", error: e);
  45. }
  46. logoutClearData();
  47. return SynchronousFuture(true);
  48. }
  49. }