signup_code.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import 'dart:async';
  2. import 'package:dio/dio.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:link/utils/global.dart';
  6. import 'package:link/utils/storage.dart';
  7. import 'package:link/view/component/toast.dart';
  8. import '../constants.dart';
  9. import '../utils/api.dart';
  10. import '../utils/http_utils.dart';
  11. import '../utils/size_fit.dart';
  12. import 'component/code.dart';
  13. import 'component/link_btn.dart';
  14. import 'component/top_container.dart';
  15. class VerifyCode extends StatefulWidget {
  16. const VerifyCode({Key? key}) : super(key: key);
  17. @override
  18. State<VerifyCode> createState() => _VerifyCodeState();
  19. }
  20. class _VerifyCodeState extends State<VerifyCode> {
  21. bool showCountdown = false;
  22. bool isBind = false;
  23. int seconds = 60;
  24. Timer? timer;
  25. String code = '';
  26. late String content;
  27. late String invite_code;
  28. late String type;
  29. @override
  30. void initState() {
  31. // TODO: implement initState
  32. super.initState();
  33. Map<String, dynamic> data = Get.parameters; //Get.arguments;
  34. content = data['content'];
  35. invite_code = data['code'];
  36. type = data['type'];
  37. if (data['bind'] != null) {
  38. isBind = data['bind'] == '1';
  39. }
  40. beginCountDown();
  41. }
  42. @override
  43. dispose(){
  44. if (timer != null) {
  45. timer!.cancel();
  46. }
  47. super.dispose();
  48. }
  49. beginCountDown() {
  50. setState(() {
  51. seconds = 60;
  52. showCountdown = true;
  53. });
  54. timer = Timer.periodic(const Duration(seconds: 1), (e) {
  55. setState(() {
  56. seconds = seconds - 1;
  57. });
  58. if (seconds == 0) {
  59. showCountdown = false;
  60. timer!.cancel();
  61. }
  62. });
  63. }
  64. Future resend() async {
  65. try {
  66. if (type == 'sms') {
  67. Map<String, dynamic> data2 =
  68. await HttpUtils.post(Api.phoneSendCode, data: {'mobile': content});
  69. print(data2.toString());
  70. } else {
  71. Map<String, dynamic> data2 =
  72. await HttpUtils.post(Api.emailSendCode, data: {'email': content});
  73. print(data2.toString());
  74. }
  75. } on DioError catch (e) {
  76. print(e.toString());
  77. return;
  78. }
  79. beginCountDown();
  80. }
  81. Future verify() async {
  82. Toast().showHud(context: context);
  83. Map<String, dynamic> data2;
  84. if (isBind) {
  85. try {
  86. if (type == 'sms') {
  87. data2 = await HttpUtils.post(Api.phoneBind, data: {
  88. 'mobile': content,
  89. 'verify_code': code
  90. });
  91. print(data2.toString());
  92. } else {
  93. data2 = await HttpUtils.post(Api.emailBind, data: {
  94. 'email': content,
  95. 'verify_code': code
  96. });
  97. print(data2.toString());
  98. }
  99. } on DioError catch (e) {
  100. // print(e.response?.data.toString());
  101. Toast().hideHud();
  102. Toast()
  103. .showInfoText(e.response?.data['error_message'], context: context);
  104. return;
  105. }
  106. Toast().hideHud();
  107. if (isBind) {
  108. Get.close(2);
  109. return;
  110. }
  111. return;
  112. }
  113. try {
  114. if (type == 'sms') {
  115. data2 = await HttpUtils.post(Api.phoneLogin, data: {
  116. 'mobile': content,
  117. 'verify_code': code,
  118. 'invite_code': invite_code
  119. });
  120. print(data2.toString());
  121. } else {
  122. data2 = await HttpUtils.post(Api.emailLogin, data: {
  123. 'email': content,
  124. 'verify_code': code,
  125. 'invite_code': invite_code
  126. });
  127. print(data2.toString());
  128. }
  129. } on DioError catch (e) {
  130. // print(e.response?.data.toString());
  131. Toast().hideHud();
  132. Toast().showInfoText(e.response?.data['error_message'], context: context);
  133. return;
  134. }
  135. Toast().hideHud();
  136. Global().token = data2['token'];
  137. Global().hasLogin = true;
  138. StorageUtil().setJSON('userInfo', data2);
  139. StorageUtil().setBool('hasLogin', true);
  140. StorageUtil().setString('token', data2['token']);
  141. if (data2['need_improve'] == true) {
  142. Get.toNamed('/edit_nick_info');
  143. } else {
  144. Get.offAllNamed('/', parameters: {'u': data2['id']});
  145. }
  146. }
  147. @override
  148. Widget build(BuildContext context) {
  149. SizeFit.initialize(context);
  150. return Material(
  151. color: kBgColor,
  152. child: TopContainer(
  153. child: Stack(children: [
  154. // Positioned(
  155. // left: 0,
  156. // top: 0,
  157. // right: 0,
  158. // child: Container(
  159. // padding: EdgeInsets.only(
  160. // left: 12.px, right: 12.px, top: 14.px, bottom: 14.px),
  161. // alignment: Alignment.topLeft,
  162. // child: Image.asset(
  163. // 'assets/images/navi_back.png',
  164. // width: 20.px,
  165. // height: 20.px,
  166. // ),
  167. // )),
  168. Positioned(
  169. child: SingleChildScrollView(
  170. child: Container(
  171. padding: EdgeInsets.only(top: 47.px),
  172. child: Column(
  173. crossAxisAlignment: CrossAxisAlignment.center,
  174. children: [
  175. Image.asset(
  176. 'assets/images/logo.png',
  177. width: 167.px,
  178. height: 64.px,
  179. ),
  180. SizedBox(
  181. height: 36.px,
  182. ),
  183. Text(
  184. '验证码已发送至 $content',
  185. textAlign: TextAlign.center,
  186. style: TextStyle(
  187. color: const Color(0xFF74747A),
  188. fontSize: 14.px,
  189. height: 1.71),
  190. ),
  191. if (showCountdown)
  192. Text(
  193. seconds.toString() + 's',
  194. style: TextStyle(
  195. color: const Color(0xFF74747A),
  196. fontSize: 14.px,
  197. height: 1.71),
  198. ),
  199. if (!showCountdown)
  200. GestureDetector(
  201. onTap: () {
  202. resend();
  203. },
  204. child: Text(
  205. '重新发送',
  206. style: TextStyle(
  207. color: kBtnColor,
  208. fontSize: 14.px,
  209. height: 1.71),
  210. ),
  211. ),
  212. SizedBox(
  213. height: 12.px,
  214. ),
  215. Code(
  216. type: 2,
  217. changed: (e) {
  218. setState(() {
  219. code = e;
  220. if (e.length == 4) {
  221. verify();
  222. }
  223. });
  224. },
  225. ),
  226. SizedBox(
  227. height: 24.px,
  228. ),
  229. LinkButton(
  230. title: '验证',
  231. disable: false,
  232. isBlack: false,
  233. callback: () {
  234. verify();
  235. //
  236. }),
  237. SizedBox(
  238. height: 52.px,
  239. ),
  240. ])),
  241. ))
  242. ])));
  243. }
  244. }