美文网首页
看见比较好的代码抄袭

看见比较好的代码抄袭

作者: 糖醋里脊120625 | 来源:发表于2022-06-23 15:04 被阅读0次

hooks

import { reactive, toRefs, unref } from 'vue';
import { Toast } from 'vant';
import API_VERIFICATION from '@/apis/verification';
import { sms } from '@/constants/modules/user';
import { isMobile } from '@/utils/validate';

/**
 * 短信验证码
 */
export function useSmsCode() {
  const state = reactive({
    // 短信验证码
    mobile: '',
    smsCode: '',
    smsTimer: null as TimeoutHandle | null,
    smsText: '获取验证码',
    smsCount: sms.count,
    // 图形验证码
    captchaShow: false,
  });

  function onSmsBtnClicked() {
    if (!unref(state.mobile)) {
      Toast('请输入手机号信息');
      return;
    }

    if (!isMobile(unref(state.mobile))) {
      Toast('手机号格式错误');
      return;
    }

    state.captchaShow = true;
  }

  function onSmsSend({ requestId, code }) {
    API_VERIFICATION.verificationSmsGet({
      mobile: unref(state.mobile),
      key: requestId,
      picCode: code,
    }).then(() => {
      Toast({
        message: '短信已发送,请查收',
        duration: 2000,
      });
      countdown(); // 立即执行一次,避免 setInterval 延迟
      state.smsTimer = setInterval(countdown, 1000);
    });
  }

  function countdown() {
    if (state.smsCount > 0) {
      state.smsCount--;
      state.smsText = `已发送(${state.smsCount}s)`;
    } else {
      state.smsCount = sms.count;
      state.smsText = `重新发送`;
      clearInterval(Number(state.smsTimer));
      state.smsTimer = null;
    }
  }

  return {
    ...toRefs(state),
    onSmsBtnClicked,
    onSmsSend,
    countdown,
  };
}

vue文件

<template>
  <div class="container">
    <div class="main">
      <div class="h2">{{ title }}</div>
      <div class="form">
        <div class="form-item">
          <div class="form-item-country">中国 +86</div>
          <van-field
            v-model="mobile"
            class="form-field"
            :border="false"
            type="tel"
            placeholder="请输入手机号"
            clearable
          />
        </div>
        <div class="form-item">
          <van-field v-model="nick" class="form-field" :border="false" type="tel" placeholder="请输入昵称" clearable>
            <template #right-icon>
              <img class="icon-random" :src="randomIcon" alt="" @click="onNickRandom" />
            </template>
          </van-field>
        </div>
        <div class="form-item">
          <van-field
            v-model="smsCode"
            class="form-field"
            :border="false"
            type="number"
            placeholder="请输入4位验证码"
            clearable
          />
          <template v-if="smsTimer">
            <span class="sms-btn countdown">{{ smsText }}</span>
          </template>
          <template v-else>
            <span class="sms-btn" @click="onSmsBtnClicked">{{ smsText }}</span>
          </template>
        </div>
        <div class="form-item">
          <PwdField
            key="pwd"
            v-model="pwd"
            class="form-field"
            :border="false"
            placeholder="请设置8-25位(数字+字母)密码"
            clearable
          />
        </div>
        <div class="form-item">
          <PwdField
            key="pwd2"
            v-model="pwd2"
            class="form-field"
            :border="false"
            placeholder="请再次输入密码"
            clearable
          />
        </div>
        <van-button
          class="form-submit"
          block
          :disabled="!submitted"
          :loading="submitLoading"
          loading-text="注册中..."
          type="primary"
          @click="onSubmit"
          >确定</van-button
        >
      </div>
    </div>
    <div class="footer">
      <div class="footer-agreement">
        <van-checkbox v-model="agree" icon-size="16px" />
        <span> 阅读并同意</span><a href="javascript:void(0);">《用户协议》</a>和<a href="javascript:void(0);"
          >《隐私政策d》</a
        >
      </div>
    </div>
    <!-- 图形验证码 -->
    <Captcha v-model:show="captchaShow" @success="onSmsSend" />
  </div>
</template>




<script lang="ts" setup>
import { computed, onMounted, ref, unref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Toast } from 'vant';
import Captcha from '@/components/Captcha/index.vue';
import PwdField from '@/components/PwdField/index.vue';
import { nickList } from '@/constants/modules/user';
import { isMobile, isPassWord, isSame } from '@/utils/validate';
import { randomIntegerInRange } from '@/utils/lodash';
import randomIcon from '@/assets/icons/shaizi.svg';

import { useUserStore } from '@/store/modules/user';
import { useSmsCode } from '@/hooks/shared/useSmsCode';

const userStore = useUserStore();

onMounted(() => {
  onNickRandom();
});

const router = useRouter();
const route = useRoute();

const title = ref('免费注册');
const { mobile, smsCode, smsTimer, smsText, captchaShow, onSmsBtnClicked, onSmsSend } = useSmsCode();
const pwd = ref('');
const pwd2 = ref('');

const nick = ref('');
function onNickRandom() {
  nick.value = nickList[randomIntegerInRange(0, nickList.length - 1)];
}

const agree = ref(true);

const submitLoading = ref(false);
const submitted = computed(() => {
  return unref(mobile) && unref(nick) && unref(smsCode) && unref(pwd) && unref(pwd2);
});

function onSubmit() {
  if (!isMobile(unref(mobile))) {
    Toast('手机号格式错误');
    return;
  }

  if (!isPassWord(unref(pwd))) {
    Toast('请设置8-25位(数字+字母)密码');
    return;
  }

  if (!isSame(unref(pwd), unref(pwd2))) {
    Toast('2次输入密码不一致');
    return;
  }

  const params = {
    mobile: unref(mobile),
    code: unref(smsCode),
    nick: unref(nick),
    pwd: unref(pwd),
    autoLogin: true,
  };

  submitLoading.value = true;

  userStore
    .login({ provider: 'register', params })
    .then(() => {
      submitLoading.value = false;
      route.query.redirect ? router.replace({ path: route.query.redirect as string }) : router.replace({ path: '/' });
      Toast.success('注册成功');
    })
    .catch((error) => {
      submitLoading.value = false;
      console.error(error);
    });
}
</script>



<style lang="less" scoped>

</style>

相关文章

  • 看见比较好的代码抄袭

    hooks vue文件

  • 前端最实用、全面的工具类方法

    1、为什么要做这件事我个人最近总是觉得,写前台代码都是大量的抄袭,而且每次抄袭的地方都是来自不同的项目,敲代码的时...

  • PHP相关

    比较好的一些帖子: 【代码审计】PHP代码审计:http://www.jianshu.com/p/eaaebd36...

  • 八尾猫【介绍】

    这本书是原著,不是抄袭,如有人看见一本一样的书,那么是抄袭的,希望你们支持,谢谢,记住我,我是木泽...

  • 冒泡排序

    还是先在本子上理清算法步骤再写代码比较好

  • Python3:if __name__ == '__ma

    一般在风格比较好的代码中会有一行if __name__ == '__main__' :代码,这里说明一下这句代码的...

  • AFNetworking实现文件断点下载

    本文为记录学习轨迹,内容抄袭自行走少年郎这位大神 因为是抄袭,所以直接上代码了,想要看细节的,可点上方链接,去查看...

  • 2018-05-24

    算法导论,分治算法,最大子数组问题。python ,代码抄袭,Dacixie的博客--https://blog.c...

  • C# timer定时在屏幕上输出信息的源码

    把写代码过程中比较好的代码记录起来,下边代码是关于C# timer定时在屏幕上输出信息的的代码。 using Sy...

  • C#通过IHttpModule接口修改http输出的代码

    把做工程过程中比较好的代码做个收藏,下边代码是关于C#通过IHttpModule接口修改http输出的代码,应该能...

网友评论

      本文标题:看见比较好的代码抄袭

      本文链接:https://www.haomeiwen.com/subject/ywxmvrtx.html