| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- import 'dart:async';
- import 'package:dio/dio.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:link/utils/global.dart';
- import 'package:link/utils/storage.dart';
- import 'package:link/view/component/toast.dart';
- import '../constants.dart';
- import '../utils/api.dart';
- import '../utils/http_utils.dart';
- import '../utils/size_fit.dart';
- import 'component/code.dart';
- import 'component/link_btn.dart';
- import 'component/top_container.dart';
- class VerifyCode extends StatefulWidget {
- const VerifyCode({Key? key}) : super(key: key);
- @override
- State<VerifyCode> createState() => _VerifyCodeState();
- }
- class _VerifyCodeState extends State<VerifyCode> {
- bool showCountdown = false;
- bool isBind = false;
- int seconds = 60;
- Timer? timer;
- String code = '';
- late String content;
- late String invite_code;
- late String type;
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- Map<String, dynamic> data = Get.parameters; //Get.arguments;
- content = data['content'];
- invite_code = data['code'];
- type = data['type'];
- if (data['bind'] != null) {
- isBind = data['bind'] == '1';
- }
- beginCountDown();
- }
- @override
- dispose(){
- if (timer != null) {
- timer!.cancel();
- }
- super.dispose();
- }
- beginCountDown() {
- setState(() {
- seconds = 60;
- showCountdown = true;
- });
- timer = Timer.periodic(const Duration(seconds: 1), (e) {
- setState(() {
- seconds = seconds - 1;
- });
- if (seconds == 0) {
- showCountdown = false;
- timer!.cancel();
- }
- });
- }
- Future resend() async {
- try {
- if (type == 'sms') {
- Map<String, dynamic> data2 =
- await HttpUtils.post(Api.phoneSendCode, data: {'mobile': content});
- print(data2.toString());
- } else {
- Map<String, dynamic> data2 =
- await HttpUtils.post(Api.emailSendCode, data: {'email': content});
- print(data2.toString());
- }
- } on DioError catch (e) {
- print(e.toString());
- return;
- }
- beginCountDown();
- }
- Future verify() async {
- Toast().showHud(context: context);
- Map<String, dynamic> data2;
- if (isBind) {
- try {
- if (type == 'sms') {
- data2 = await HttpUtils.post(Api.phoneBind, data: {
- 'mobile': content,
- 'verify_code': code
- });
- print(data2.toString());
- } else {
- data2 = await HttpUtils.post(Api.emailBind, data: {
- 'email': content,
- 'verify_code': code
- });
- print(data2.toString());
- }
- } on DioError catch (e) {
- // print(e.response?.data.toString());
- Toast().hideHud();
- Toast()
- .showInfoText(e.response?.data['error_message'], context: context);
- return;
- }
- Toast().hideHud();
- if (isBind) {
- Get.close(2);
- return;
- }
- return;
- }
- try {
- if (type == 'sms') {
- data2 = await HttpUtils.post(Api.phoneLogin, data: {
- 'mobile': content,
- 'verify_code': code,
- 'invite_code': invite_code
- });
- print(data2.toString());
- } else {
- data2 = await HttpUtils.post(Api.emailLogin, data: {
- 'email': content,
- 'verify_code': code,
- 'invite_code': invite_code
- });
- print(data2.toString());
- }
- } on DioError catch (e) {
- // print(e.response?.data.toString());
- Toast().hideHud();
- Toast().showInfoText(e.response?.data['error_message'], context: context);
- return;
- }
- Toast().hideHud();
- Global().token = data2['token'];
- Global().hasLogin = true;
- StorageUtil().setJSON('userInfo', data2);
- StorageUtil().setBool('hasLogin', true);
- StorageUtil().setString('token', data2['token']);
- if (data2['need_improve'] == true) {
- Get.toNamed('/edit_nick_info');
- } else {
- Get.offAllNamed('/', parameters: {'u': data2['id']});
- }
- }
- @override
- Widget build(BuildContext context) {
- SizeFit.initialize(context);
- return Material(
- color: kBgColor,
- child: TopContainer(
- child: Stack(children: [
- // Positioned(
- // left: 0,
- // top: 0,
- // right: 0,
- // child: Container(
- // padding: EdgeInsets.only(
- // left: 12.px, right: 12.px, top: 14.px, bottom: 14.px),
- // alignment: Alignment.topLeft,
- // child: Image.asset(
- // 'assets/images/navi_back.png',
- // width: 20.px,
- // height: 20.px,
- // ),
- // )),
- Positioned(
- child: SingleChildScrollView(
- child: Container(
- padding: EdgeInsets.only(top: 47.px),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Image.asset(
- 'assets/images/logo.png',
- width: 167.px,
- height: 64.px,
- ),
- SizedBox(
- height: 36.px,
- ),
- Text(
- '验证码已发送至 $content',
- textAlign: TextAlign.center,
- style: TextStyle(
- color: const Color(0xFF74747A),
- fontSize: 14.px,
- height: 1.71),
- ),
- if (showCountdown)
- Text(
- seconds.toString() + 's',
- style: TextStyle(
- color: const Color(0xFF74747A),
- fontSize: 14.px,
- height: 1.71),
- ),
- if (!showCountdown)
- GestureDetector(
- onTap: () {
- resend();
- },
- child: Text(
- '重新发送',
- style: TextStyle(
- color: kBtnColor,
- fontSize: 14.px,
- height: 1.71),
- ),
- ),
- SizedBox(
- height: 12.px,
- ),
- Code(
- type: 2,
- changed: (e) {
- setState(() {
- code = e;
- if (e.length == 4) {
- verify();
- }
- });
- },
- ),
- SizedBox(
- height: 24.px,
- ),
- LinkButton(
- title: '验证',
- disable: false,
- isBlack: false,
- callback: () {
- verify();
- //
- }),
- SizedBox(
- height: 52.px,
- ),
-
- ])),
- ))
- ])));
- }
- }
|