Files
tuiche/lib/pages/mh_page/user/controller/mht_login_controller.dart
2026-01-26 15:01:57 +08:00

435 lines
15 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get_storage/get_storage.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/CommonVariables.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/time/countdown_controller.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/model/api_response.dart';
import 'package:vbvs_app/model/user_data.dart';
import 'package:vbvs_app/pages/mh_page/user/controller/mht_register_controller.dart';
part 'mht_login_controller.g.dart';
@JsonSerializable()
class LoginModel {
//版本id
int? loginStyle =
1; //1.密码登录 2.短信登录 3.邮箱登录(登录类型: 1:手机号/邮箱+验证码 2:微信一键登录 3:手机号+密码)
// String? account = '17649984946'; //账户
// String? account = '13953240733'; //账户
// String? password = '123wyf..'; //密码
// String? account = '15255134931'; //账户
// String? password = 'mht123,.'; //密码
// String? account = '18607922869'; //账户
// String? password = '373068'; //密码
String? account = ''; //账户
String? password = ''; //密码
String? phone; //手机号/邮箱
String? code; //验证码
String? register_code;
bool? showPd = true;
int? forceLogin = 0;
bool? isIos; //是否为ios设备
bool? isWeChatNotInstalled; //是否安装微信
LoginModel();
static LoginModel fromJson(Map<String, dynamic> json) =>
_$LoginModelFromJson(json);
Map<String, dynamic> toJson() => _$LoginModelToJson(this);
}
class MHTLoginController extends GetControllerEx<LoginModel> {
// 初始化实例
final Fluwx fluwx = Fluwx();
// 微信监听返回值
FluwxCancelable? fluwxCancelable;
MHTLoginController() {
attr = GetModel(LoginModel()).obs;
}
MHTRegisterController registerController = Get.find();
//登录
Future<String> login(BuildContext context) async {
String message = '';
String account = '';
String password = '';
if (model.loginStyle == null) {
message = '请选择登录方式'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
if (model.loginStyle == 1) {
//账号登录
if (model.account == null || model.account!.isEmpty) {
message = '账户不能为空'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
if (model.password == null || model.password!.isEmpty) {
message = '密码不能为空'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
account = model.account!;
password = model.password!;
}
if (model.loginStyle == 2) {
//账号登录
if (model.phone == null || model.phone!.isEmpty) {
message = '请输入手机号'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
if (!MyUtils.isValidPhoneNumber(model.phone!)) {
message = '请输入正确的手机号'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
if (model.code == null || model.code!.isEmpty) {
message = '验证码不能为空'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
account = model.phone!;
password = model.code!;
}
if (model.loginStyle == 3) {
//账号登录
if (model.phone == null || model.phone!.isEmpty) {
message = '请输入邮箱'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
if (!MyUtils.isValidEmail(model.phone!)) {
message = '请输入正确的邮箱'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
if (model.code == null || model.code!.isEmpty) {
message = '验证码不能为空'.tr;
TopSlideNotification.show(context,
text: message, textColor: stringToColor("#FF7159"));
return message;
}
account = model.phone!;
password = model.code!;
}
if (registerController.model.register_agree == null ||
registerController.model.register_agree != true) {
message = "需要同意协议".tr;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
return message;
}
int type = 1;
if (model.loginStyle == 1) {
type = 3;
} else if (model.loginStyle == 2) {
type = 1;
}
var data = {
"type": type,
"userName": account,
"password": password,
'khCode': 'mht',
};
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.login;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
await requestWithLog(
logTitle: "用户登录".tr,
method: MyHttpMethod.post,
queryUrl: queryUrl,
data: data,
onSuccess: (res) {
res.rawResponse;
CountdownController countdownController = Get.find();
countdownController.countdown.value = 0;
UserInfoController userInfoController = Get.find();
userInfoController.model.login = 1;
userInfoController.model.user = UserModel.fromJson(res.data);
String token = res.rawResponse.headers['token']!.first;
EasyDartModule.dio.token = token;
final box = GetStorage();
box.write('token', token); // 存储 token
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
//todo 更新消息通知
updateAppSystemNotify(true);
},
onFailure: (res) {
message = res.msg!;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
},
);
model.forceLogin = 0;
return message;
}
Future<String> getCode(BuildContext context) async {
String message = "";
// if (registerController.model.register_agree == null ||
// registerController.model.register_agree != true) {
// message = "需要同意协议".tr;
// TopSlideNotification.show(context,
// text: message, textColor: themeController.currentColor.sc9);
// return message;
// }
// model.loginStyle;// 2 手机号 3 邮箱
// if (model.phone == null || model.phone!.isEmpty) {
// message = "请输入手机号".tr;
// TopSlideNotification.show(context,
// text: message, textColor: themeController.currentColor.sc9);
// return message;
// }
// if (!MyUtils.isValidPhoneNumber(model.phone!)) {
// message = '请输入正确的手机号'.tr;
// TopSlideNotification.show(context,
// text: message, textColor: themeController.currentColor.sc9);
// return message;
// }
// 2 手机号 3 邮箱
if (model.loginStyle == 2) {
// 短信登录:验证手机号
if (model.phone == null || model.phone!.isEmpty) {
message = "请输入手机号".tr;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
return message;
}
if (!MyUtils.isValidPhoneNumber(model.phone!)) {
message = '请输入正确的手机号'.tr;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
return message;
}
} else if (model.loginStyle == 3) {
// 邮箱登录:验证邮箱
if (model.phone == null || model.phone!.isEmpty) {
message = "请输入邮箱".tr;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
return message;
}
if (!MyUtils.isValidEmail(model.phone!)) {
message = '请输入正确的邮箱地址'.tr;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
return message;
}
} else {
// 其他登录类型(如密码登录)
// 根据你的实际需求处理
message = "请选择正确的登录方式".tr;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
return message;
}
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.send_code;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
var data = {
"userName": model.phone,
"type": 1,
};
data['code'] = "mht";
await requestWithLog(
data: data,
logTitle: "发送验证码".tr,
method: MyHttpMethod.post,
queryUrl: queryUrl,
onSuccess: (res) {
TopSlideNotification.show(context, text: "发送验证码成功".tr);
},
onFailure: (res) {
message = res.msg!;
TopSlideNotification.show(context,
text: message, textColor: themeController.currentColor.sc9);
},
);
return message;
}
//微信登录
Future<void> wxLoginSendAuth(BuildContext context) async {
/*
1、目前移动应用上微信登录只提供原生的登录方式需要用户安装微信客户端才能配合使用。
2、对于Android应用建议总是显示微信登录按钮当用户手机没有安装微信客户端时请引导用户下载安装微信客户端。
3、对于iOS应用考虑到iOS应用商店审核指南中的相关规定建议开发者接入微信登录时先检测用户手机是否已安装微信客户端
使用sdk中isWXAppInstalled函数 ),对未安装的用户隐藏微信登录按钮,只提供其他登录方式(比如手机号注册登录、游客登录等)
*/
if (isAndroid) {
bool isWeChatInstalled = await fluwx.isWeChatInstalled;
debugPrint('is wechat installed: $isWeChatInstalled');
if (!isWeChatInstalled) {
TopSlideNotification.show(context,
text: "请先安装微信APP再使用微信登录".tr,
textColor: themeController.currentColor.sc9);
return;
}
}
fluwx
.authBy(
which: NormalAuth(
scope: 'snsapi_userinfo',
state: 'wechat_sdk_zhmht_wxlogin',
))
.then((data) {
//返回true表示成功或者false表示失败这边没有意义从login_controller页面构造函数监听中去处理
debugPrint('msg:$data');
});
}
//退出登录
Future<void> logout() async {
// await repository.logout();
}
Future<int> loginByWechatCode(String code) async {
try {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.login;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
String? language = "";
var data = {
"type": 2,
"password": code,
"khCode": "mht",
};
ApiResponse apiResponse = await requestWithLog(
logTitle: "微信登录".tr,
method: MyHttpMethod.post,
queryUrl: queryUrl,
data: data);
ef.log("微信登录授权开始");
if (apiResponse.code == HttpStatusCodes.ok) {
ef.log("微信登录授权成功:${apiResponse}");
UserInfoController userInfoController = Get.find();
userInfoController.model.user = UserModel.fromJson(apiResponse.data);
if (userInfoController.model.user!.phone == null ||
userInfoController.model.user!.phone!.isEmpty) {
await Get.toNamed("/auth_bind_tel", arguments: {'code': 'mht'});
}
if (userInfoController.model.user!.phone == null ||
userInfoController.model.user!.phone!.isEmpty) {
userInfoController.model.login = 0;
}
userInfoController.model.login = 1;
String token = apiResponse.rawResponse.headers['token']!.first;
EasyDartModule.dio.token = token;
final box = GetStorage();
box.write('token', token); // 存储 token
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
} else {
EasyDartModule.logger.error("[微信登录]:失败-》${apiResponse}");
}
return apiResponse.code!;
} catch (e) {
ef.log("微信登录:$e");
}
return 1;
}
//注销账号
Future<int> deletedAccount() async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.delete_account;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
int code = 0;
await requestWithLog(
logTitle: "注销账号",
method: MyHttpMethod.delete,
queryUrl: queryUrl,
onSuccess: (res) {
code = res.code!;
},
onFailure: (res) {
code = res.code!;
},
);
return code;
}
Future<void> openWeChatCustomerService(BuildContext context) async {
bool isWeChatInstalled = await fluwx.isWeChatInstalled;
if (!isWeChatInstalled) {
TopSlideNotification.show(context,
text: "请先安装微信APP再联系客服".tr,
textColor: themeController.currentColor.sc9);
return;
}
TopSlideNotification.show(
context,
text: "正在打开微信客服...".tr,
);
await fluwx.open(
target: CustomerServiceChat(
corpId: CommonVariables.wxCorpId, url: CommonVariables.wxKfUrl));
}
}
Future<void> updateAppSystemNotify(bool status) async {
final box = GetStorage();
var data = {
"pid": box.read("rid"),
"status": status,
};
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.app_system_push_message;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
await requestWithLog(
logTitle: "激活消息通知",
method: MyHttpMethod.put,
queryUrl: queryUrl,
data: data,
onSuccess: (res) {
print(res);
},
onFailure: (res) {
print(res);
});
}