65 lines
1.8 KiB
Dart
65 lines
1.8 KiB
Dart
import 'dart:io';
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
|
import 'state.dart';
|
|
import '../../utils/utils.dart';
|
|
import '../../api/index.dart';
|
|
|
|
class LoginLogic extends GetxController {
|
|
final LoginState state = LoginState();
|
|
|
|
void setPhone(String value) => state.phone = value;
|
|
|
|
void setSmsCode(String value) => state.code = value;
|
|
|
|
// 验证码倒计时
|
|
void startCountdownTimer() {
|
|
if (state.countTime > 0) {
|
|
state.countTime--;
|
|
update();
|
|
Future.delayed(const Duration(seconds: 1), startCountdownTimer);
|
|
} else {
|
|
state.countTime = 0;
|
|
update();
|
|
}
|
|
}
|
|
|
|
// 获取验证码
|
|
Future<void> getCode(context) async {
|
|
if (ToolFn.isBlank(state.phone) || ToolFn.checkMobile(state.phone)) {
|
|
return ToolFn.tips("请输入正确的手机号");
|
|
}
|
|
if (state.countTime == 0) {
|
|
final res =
|
|
await Request.post<Map<String, dynamic>>(LoginApi.getCode, data: {
|
|
"phone": state.phone,
|
|
});
|
|
TDToast.showText(res["msg"].toString(), context: context);
|
|
state.countTime = 60;
|
|
startCountdownTimer();
|
|
}
|
|
}
|
|
|
|
// 登录
|
|
Future<void> loginFn(context) async {
|
|
if (ToolFn.isBlank(state.phone) || ToolFn.checkMobile(state.phone)) {
|
|
return ToolFn.tips("请输入正确的手机号");
|
|
}
|
|
if (ToolFn.isBlank(state.code)) {
|
|
return ToolFn.tips("请输入正确的验证码");
|
|
}
|
|
FocusScope.of(context).unfocus();
|
|
final res =
|
|
await Request.post<Map<String, dynamic>>(LoginApi.phoneLogin, data: {
|
|
"Phone": state.phone,
|
|
"Code": state.code,
|
|
});
|
|
// 保存token
|
|
StorageUtil().putString("token", res["data"]["token"]);
|
|
// 保存用户信息
|
|
StorageUtil().putJSON("userInfo", res["data"]["data"]);
|
|
}
|
|
}
|