change_phoneno.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:eitc_erm_app/utils/Component.dart';
  4. import 'package:eitc_erm_app/utils/Constants.dart';
  5. import 'package:eitc_erm_app/utils/Utils.dart';
  6. import 'package:eitc_erm_app/utils/logger.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:http/http.dart' as http;
  10. import 'package:http/http.dart';
  11. import 'bean/normal_response.dart';
  12. import 'login.dart';
  13. class ChangePhoneNo extends StatefulWidget {
  14. @override
  15. ChangePhoneNoState createState() => ChangePhoneNoState();
  16. }
  17. class ChangePhoneNoState extends State<ChangePhoneNo> {
  18. final _formKey = GlobalKey<FormState>();
  19. String _phoneNumber = '';
  20. String _newPassword = '';
  21. bool _isLoading = false;
  22. bool isButtonEnable = true;
  23. int count = 60;
  24. Timer? timer;
  25. String buttonText = '发送验证码';
  26. TextEditingController mController = TextEditingController();
  27. TextEditingController newPhoneController = TextEditingController();
  28. void _buttonClickListen() {
  29. setState(() {
  30. if (isButtonEnable) {
  31. sendCaptchaCode();
  32. //当按钮可点击时
  33. isButtonEnable = false; //按钮状态标记
  34. _initTimer();
  35. return null; //返回null按钮禁止点击
  36. } else {
  37. //当按钮不可点击时
  38. // debuglogd('false');
  39. return null; //返回null按钮禁止点击
  40. }
  41. });
  42. }
  43. void _initTimer() {
  44. timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
  45. count--;
  46. setState(() {
  47. if (count == 0) {
  48. timer.cancel(); //倒计时结束取消定时器
  49. isButtonEnable = true; //按钮可点击
  50. count = 60; //重置时间
  51. buttonText = '发送验证码'; //重置按钮文本
  52. } else {
  53. buttonText = '重新发送($count)'; //更新文本内容
  54. }
  55. });
  56. });
  57. }
  58. @override
  59. void dispose() {
  60. timer?.cancel();
  61. super.dispose();
  62. }
  63. Future<void> _changePhoneNo({required String phoneNumber}) async {
  64. if (!Utils.isChinaPhoneLegal(phoneNumber)) {
  65. Component.toast("请输入正确手机号码!", 0);
  66. return null;
  67. }
  68. bool bo = await _checkCaptchaCode(phoneNumber);
  69. if (!bo) {
  70. Component.toast("验证码错误!", 0);
  71. return null;
  72. }
  73. var params = {
  74. 'phoneNumber': phoneNumber,
  75. 'captchaCode': mController.text,
  76. };
  77. logd("$phoneNumber ${mController.text}");
  78. final response =
  79. await http.post(Uri.parse('${Global.BaseUrl}user/updatePhoneNumber'),
  80. headers: jsonHeaders(withToken: true),
  81. body: encodeBody(params));
  82. setState(() {
  83. _isLoading = true;
  84. });
  85. try {
  86. final json = decodeBodyToJson(response.bodyBytes);
  87. logd("修改手机号结果$json");
  88. NormalResponse mNormalResponse = new NormalResponse.fromJson(json);
  89. if (mNormalResponse.code == Global.responseSuccessCode) {
  90. Component.toast("手机号修改成功!", 2);
  91. Navigator.of(context).pop();
  92. Navigator.pushReplacement(
  93. context,
  94. MaterialPageRoute(builder: (context) => Login()),
  95. );
  96. } else {
  97. Component.toast(mNormalResponse.msg.toString(), 0);
  98. }
  99. } catch (e) {
  100. Component.toast("异常: $e", 0);
  101. } finally {
  102. setState(() {
  103. _isLoading = false;
  104. });
  105. }
  106. }
  107. ///检查验证码
  108. Future<bool> _checkCaptchaCode(String phoneNumber) async {
  109. Map<String, dynamic> params = {
  110. "phoneNumber": phoneNumber,
  111. "captchaCode": mController.text
  112. };
  113. Uri uri = Uri.parse("${Global.BaseUrl}api/checkCaptchaCode");
  114. logd("检查验证码,uri=$uri");
  115. Response response =
  116. await http.post(uri, body: encodeBody(params), headers: jsonHeaders());
  117. if (response.statusCode == 200) {
  118. final json = decodeBodyToJson(response.bodyBytes);
  119. logd("检查验证码结果$json");
  120. NormalResponse mNormalResponse = NormalResponse.fromJson(json);
  121. return mNormalResponse.code == 200;
  122. } else {
  123. logd("检查验证码异常${response.body}");
  124. }
  125. return false;
  126. }
  127. @override
  128. Widget build(BuildContext context) {
  129. return Scaffold(
  130. appBar: AppBar(
  131. title: const Text('修改手机号',
  132. style: TextStyle(
  133. color: Colors.white,
  134. )),
  135. centerTitle: true,
  136. elevation: 0.5,
  137. backgroundColor: Global.StatusBarColor,
  138. leading: new IconButton(
  139. tooltip: '返回上一页',
  140. icon: const Icon(
  141. Icons.arrow_back_ios,
  142. color: Colors.white,
  143. ),
  144. onPressed: () {
  145. Navigator.of(context).pop();
  146. //_nextPage(-1);
  147. },
  148. ),
  149. ),
  150. body: Form(
  151. key: _formKey,
  152. child: Padding(
  153. padding: const EdgeInsets.all(20.0),
  154. child: Column(
  155. children: <Widget>[
  156. SizedBox(height: 30.0),
  157. TextFormField(
  158. enabled: false,
  159. decoration: InputDecoration(
  160. labelText: '手机号',
  161. ),
  162. initialValue: Global.loginPhoneNo,
  163. // obscureText: true,
  164. onSaved: (value) => _newPassword = value!,
  165. ),
  166. TextFormField(
  167. controller: newPhoneController,
  168. decoration: InputDecoration(labelText: '新登录手机号'),
  169. onSaved: (value) => _phoneNumber = value!,
  170. ),
  171. Row(
  172. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  173. crossAxisAlignment: CrossAxisAlignment.baseline,
  174. textBaseline: TextBaseline.alphabetic,
  175. children: <Widget>[
  176. Expanded(
  177. child: Padding(
  178. padding: EdgeInsets.only(left: 0, right: 15, top: 15),
  179. child: TextFormField(
  180. maxLines: 1,
  181. onSaved: (value) {},
  182. controller: mController,
  183. textAlign: TextAlign.left,
  184. inputFormatters: [
  185. FilteringTextInputFormatter.digitsOnly,
  186. LengthLimitingTextInputFormatter(6)
  187. ],
  188. decoration: const InputDecoration(
  189. hintText: ('请输入验证码'),
  190. contentPadding: EdgeInsets.only(top: -5, bottom: 0),
  191. alignLabelWithHint: true,
  192. // border:
  193. // OutlineInputBorder(borderSide: BorderSide.none),
  194. ),
  195. ),
  196. ),
  197. ),
  198. Container(
  199. padding: const EdgeInsets.all(0),
  200. width: 120,
  201. child: ElevatedButton(
  202. style: ButtonStyle(
  203. backgroundColor:
  204. WidgetStateProperty.resolveWith<Color>((states) {
  205. return Colors.blue; // Regular color
  206. }),
  207. foregroundColor: WidgetStateProperty.all(Colors.white),
  208. ),
  209. // backgroundColor : isButtonEnable
  210. // ? Color(0xff44c5fe)
  211. // : Colors.grey.withOpacity(0.1), //按钮的颜色
  212. // splashColor: isButtonEnable
  213. // ? Colors.white.withOpacity(0.1)
  214. // : Colors.transparent,
  215. // shape: StadiumBorder(side: BorderSide.none),
  216. onPressed: () {
  217. setState(() {
  218. _buttonClickListen();
  219. });
  220. },
  221. child: Text(
  222. buttonText,
  223. style: const TextStyle(
  224. fontSize: 12,
  225. ),
  226. ),
  227. ),
  228. ),
  229. ],
  230. ),
  231. const Text(
  232. '更换手机号后,将同步修改就诊人为本人的手机号码。',
  233. style: TextStyle(fontSize: 15, color: Colors.grey),
  234. ),
  235. Container(
  236. width: double.infinity,
  237. height: 45,
  238. margin: EdgeInsets.only(top: 40, left: 10, right: 10),
  239. child: ElevatedButton(
  240. style: ButtonStyle(
  241. backgroundColor:
  242. MaterialStateProperty.resolveWith<Color>((states) {
  243. return Colors.blue; // Regular color
  244. }),
  245. ),
  246. onPressed: _isLoading
  247. ? null
  248. : () {
  249. if (_formKey.currentState!.validate()) {
  250. _formKey.currentState?.save();
  251. _changePhoneNo(
  252. phoneNumber: _phoneNumber,
  253. );
  254. }
  255. },
  256. child: _isLoading
  257. ? const CircularProgressIndicator(
  258. valueColor:
  259. AlwaysStoppedAnimation<Color>(Colors.white),
  260. )
  261. : const Text(
  262. '确定',
  263. style: TextStyle(color: Colors.white, fontSize: 15),
  264. ),
  265. ),
  266. ),
  267. ],
  268. ),
  269. ),
  270. ),
  271. );
  272. }
  273. Future<String?> sendCaptchaCode() async {
  274. if (!Utils.isChinaPhoneLegal(newPhoneController.text)) {
  275. Component.toast("请输入正确手机号码!", 0);
  276. return null;
  277. }
  278. var params = {
  279. 'phoneNumber': newPhoneController.text,
  280. };
  281. var response = await http.post(
  282. Uri.parse('${Global.BaseUrl}sendCaptchaCode'),
  283. body: encodeBody(params),
  284. headers: jsonHeaders());
  285. if (response.statusCode == 200) {
  286. final json = decodeBodyToJson(response.bodyBytes);
  287. logd("发送验证码结果=$json");
  288. NormalResponse mNormalResponse = new NormalResponse.fromJson(json);
  289. if (mNormalResponse.code == Global.responseSuccessCode) {
  290. Component.toast("短信发送成功,请查收!", 2);
  291. } else {
  292. Component.toast(mNormalResponse.msg.toString(), 0);
  293. }
  294. return response.toString();
  295. } else {
  296. Component.toast("出错了,请稍后再试!", 0);
  297. return null;
  298. }
  299. }
  300. }