217 lines
6.4 KiB
Dart
217 lines
6.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mysql_client/mysql_client.dart';
|
|
import 'dart:async';
|
|
import 'dart:math';
|
|
import 'package:mailer/mailer.dart';
|
|
import 'package:mailer/smtp_server.dart';
|
|
import 'package:validators/validators.dart' as validator;
|
|
|
|
void main() {
|
|
runApp(MaterialApp(
|
|
home: ForgetPasswordPage(),
|
|
));
|
|
}
|
|
|
|
class ForgetPasswordPage extends StatefulWidget {
|
|
@override
|
|
_ForgetPasswordPageState createState() => _ForgetPasswordPageState();
|
|
}
|
|
|
|
class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
|
|
late FocusNode _emailFocusNode;
|
|
final TextEditingController _emailController = TextEditingController();
|
|
|
|
bool _isButtonEnabled = true;
|
|
String _generatedCode = ''; // 用來存儲生成的臨時密碼
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchData();
|
|
_emailFocusNode = FocusNode();
|
|
}
|
|
|
|
void _fetchData() async {
|
|
final conn = await MySQLConnection.createConnection(
|
|
host: 'comprehensive-guardian.systems',
|
|
port: 33061,
|
|
userName: 'root',
|
|
password: 'Topic@2024',
|
|
databaseName: 'care',
|
|
);
|
|
await conn.connect();
|
|
}
|
|
|
|
String _generateVerificationCode() {
|
|
final random = Random();
|
|
const availableChars = '0123456789';
|
|
return List.generate(
|
|
6, (index) => availableChars[random.nextInt(availableChars.length)])
|
|
.join();
|
|
}
|
|
|
|
Future<void> _sendEmail(String recipientEmail, String code) async {
|
|
final smtpServer = SmtpServer(
|
|
'smtp.mail.me.com',
|
|
port: 587,
|
|
username: 'ltesr125124015@icloud.com',
|
|
password: 'vwtp-bruz-xiav-rjee',
|
|
ssl: false,
|
|
allowInsecure: false,
|
|
);
|
|
|
|
final message = Message()
|
|
..from = Address('user_manager@comprehensive-guardian.systems', '全方位照護守護者')
|
|
..recipients.add(recipientEmail)
|
|
..subject = '您的臨時密碼'
|
|
..text = '您的臨時密碼是: $code';
|
|
|
|
try {
|
|
await send(message, smtpServer);
|
|
print('臨時密碼發送成功');
|
|
} catch (e) {
|
|
print('臨時密碼發送失敗: $e');
|
|
}
|
|
}
|
|
|
|
void _sendVerificationCode() async {// 發送並更新資料庫中的密碼
|
|
final email = _emailController.text;
|
|
if (email.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('請輸入電子信箱地址')),
|
|
);
|
|
return;
|
|
} else if (!validator.isEmail(email)) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('請輸入正確的電子信箱')),
|
|
);
|
|
_emailFocusNode.requestFocus();
|
|
return;
|
|
}
|
|
|
|
//_generatedCode = _generateVerificationCode();
|
|
//_sendEmail(email, _generatedCode);
|
|
|
|
final conn = await MySQLConnection.createConnection(
|
|
host: 'comprehensive-guardian.systems',
|
|
port: 33061,
|
|
userName: 'root',
|
|
password: 'Topic@2024',
|
|
databaseName: 'care',
|
|
);
|
|
await conn.connect();
|
|
try {
|
|
var result = await conn.execute(
|
|
'SELECT * FROM HomeLogin WHERE homeEmail = :email',
|
|
{'email': email},
|
|
);
|
|
|
|
if (result.rows.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('失敗')),
|
|
);
|
|
} else {
|
|
|
|
_generatedCode = _generateVerificationCode();
|
|
await conn.execute(
|
|
'UPDATE HomeLogin SET homePassword = :tempPassword WHERE homeEmail = :email',
|
|
{'tempPassword': _generatedCode, 'email': email},
|
|
);
|
|
|
|
// 發送臨時密碼到用戶電子信箱
|
|
_sendEmail(email, _generatedCode);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('臨時密碼已發送到您的電子信箱')),
|
|
);
|
|
|
|
_emailController.clear();
|
|
}
|
|
} catch (e) {
|
|
print('資料庫錯誤: $e');
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('註冊失敗:系統錯誤')),
|
|
);
|
|
} finally {
|
|
await conn.close();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
_emailFocusNode.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 32.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Container(
|
|
margin: EdgeInsets.only(bottom: 40),
|
|
height: 100,
|
|
child: Icon(
|
|
Icons.account_circle,
|
|
size: 100,
|
|
color: Color(0xFF4FC3F7),
|
|
),
|
|
),
|
|
Text(
|
|
'全方位照護守護者',
|
|
style: TextStyle(
|
|
fontSize: 32,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black,
|
|
),
|
|
),
|
|
SizedBox(height: 20),
|
|
TextField(
|
|
controller: _emailController,
|
|
focusNode: _emailFocusNode,
|
|
decoration: InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.email_outlined),
|
|
labelText: '電子信箱',
|
|
),
|
|
keyboardType: TextInputType.emailAddress,
|
|
),
|
|
SizedBox(height: 20),
|
|
|
|
ElevatedButton(
|
|
onPressed: _sendVerificationCode,
|
|
child: Text('送出'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Color(0xFF4FC3F7),
|
|
padding:
|
|
EdgeInsets.symmetric(horizontal: 50, vertical: 15),
|
|
textStyle: TextStyle(fontSize: 18),
|
|
),
|
|
),
|
|
SizedBox(height: 10),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text('取消'),
|
|
style: TextButton.styleFrom(
|
|
backgroundColor: Colors.transparent,
|
|
textStyle: TextStyle(fontSize: 18),
|
|
shadowColor: Colors.transparent,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |