controller.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'package:electronic_assistant/base/base_controller.dart';
  2. import 'package:electronic_assistant/data/repositories/account_repository.dart';
  3. import 'package:electronic_assistant/popup/talk_popup.dart';
  4. import 'package:electronic_assistant/utils/error_handler.dart';
  5. import 'package:electronic_assistant/utils/expand.dart';
  6. import 'package:electronic_assistant/utils/toast_util.dart';
  7. import 'package:electronic_assistant/widget/alert_dialog.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_screenutil/flutter_screenutil.dart';
  10. import 'package:get/get.dart';
  11. class LoginController extends BaseController {
  12. final phone = "".obs;
  13. final code = "".obs;
  14. final isAgree = false.obs;
  15. @override
  16. void onInit() {
  17. // TODO: implement onInit
  18. super.onInit();
  19. }
  20. void getUserCode() {
  21. if (phone.value.isEmpty) {
  22. ToastUtil.showToast("请输入手机号");
  23. return;
  24. }
  25. accountRepository.getVerificationCode(phone.value).then((data) {
  26. ToastUtil.showToast("验证码已发送");
  27. }).catchError((error) {
  28. ErrorHandler.toastError(error);
  29. });
  30. }
  31. void login() {
  32. FocusScope.of(Get.context!).requestFocus(FocusNode());
  33. if (phone.value.isEmpty) {
  34. ToastUtil.showToast("请输入手机号");
  35. return;
  36. }
  37. if (code.value.isEmpty) {
  38. ToastUtil.showToast("请输入验证码");
  39. return;
  40. }
  41. if (!isAgree.value) {
  42. EAAlertDialog.show(
  43. title: "隐私政策及权限说明",
  44. contentWidget: Wrap(
  45. children: [
  46. Text(
  47. "进入下一步前,请先阅读并同意小听的",
  48. textAlign: TextAlign.center,
  49. style: TextStyle(
  50. color: "#5F5F61".toColor(),
  51. fontSize: 14,
  52. decoration: TextDecoration.none,
  53. ),
  54. ),
  55. GestureDetector(
  56. onTap: () {},
  57. child: Text(
  58. "《隐私政策》",
  59. textAlign: TextAlign.center,
  60. style: TextStyle(
  61. color: "#5E8BFF".toColor(),
  62. fontSize: 14,
  63. decoration: TextDecoration.none,
  64. ),
  65. ),
  66. ),
  67. Text(
  68. "和",
  69. textAlign: TextAlign.center,
  70. style: TextStyle(
  71. color: "#5F5F61".toColor(),
  72. fontSize: 14,
  73. decoration: TextDecoration.none,
  74. ),
  75. ),
  76. GestureDetector(
  77. onTap: () {},
  78. child: Text(
  79. "《用户试用协议》",
  80. textAlign: TextAlign.center,
  81. style: TextStyle(
  82. color: "#5E8BFF".toColor(),
  83. fontSize: 14,
  84. decoration: TextDecoration.none,
  85. ),
  86. ),
  87. ),
  88. Text(
  89. "。",
  90. textAlign: TextAlign.center,
  91. style: TextStyle(
  92. color: "#5F5F61".toColor(),
  93. fontSize: 14,
  94. decoration: TextDecoration.none,
  95. ),
  96. ),
  97. ],
  98. ),
  99. cancelText: "不同意",
  100. confirmText: "同意并继续",
  101. cancelOnTap: () {
  102. EAAlertDialog.dismiss();
  103. },
  104. confirmOnTap: () {
  105. EAAlertDialog.dismiss();
  106. isAgree.value = true;
  107. login();
  108. },
  109. );
  110. return;
  111. }
  112. if (phone.value.isEmpty || code.value.isEmpty) {
  113. return;
  114. }
  115. accountRepository.login(phone.value, code.value).then((data) {
  116. ToastUtil.showToast("登录成功");
  117. Get.back();
  118. }).catchError((error) {
  119. ErrorHandler.toastError(error);
  120. });
  121. }
  122. void setPhone(String text) {
  123. phone.value = text;
  124. }
  125. void setCode(String text) {
  126. code.value = text;
  127. }
  128. }