溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2020-10-14 13:13:15 來(lái)源:腳本之家 閱讀:218 作者:element 欄目:web開(kāi)發(fā)

此功能基于vue(v2.6.8) + typescript(v3.3.3333), 引入極驗(yàn)(geetest v3+)(官方api),使用其product: 'bind'模式, 頁(yè)面掛載后初始化ininGeetest,點(diǎn)擊登錄按鈕后先做表單驗(yàn)證,通過(guò)后彈出滑塊框,拖動(dòng)驗(yàn)證成功,執(zhí)行登錄方法。

本項(xiàng)目為前后端分離,所以后端部署部分,請(qǐng)自行參考文檔操作

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

后臺(tái)接口:

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

開(kāi)始:/public/js目錄添加 jquery-1.12.3.min.js文件 和 gt.js(下載)在/public/index.html中引入以上添加的兩個(gè)文件login.vue使用注意事項(xiàng):要注意在gt.js中,initGeetest已被掛載到window對(duì)象

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

頁(yè)面可能報(bào)錯(cuò): Uncaught SyntaxError: Unexpected token <

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法

將報(bào)錯(cuò)對(duì)象添加到與public同級(jí)的static目錄下(沒(méi)有則新建),修改引入路徑即可。

源碼:

<script lang="ts">
import { isValidUsername } from '@/utils/validate';
import { Component, Vue, Watch } from 'vue-property-decorator';
import { Route } from 'vue-router';
import { ElForm } from 'element-ui/types/form';
import { Loading } from 'element-ui';

import { Action } from 'vuex-class';
import AuthServices from '@/services/user/auth.ts';
import ThirdpartyServices from '@/services/thirdparty/index.ts';

const validateUsername = (rule: any, value: string, callback: any) => {
 if (! value) {
  callback(new Error('用戶(hù)名不能為空'));
 // } else if (!isValidUsername(value)) {
 //  callback(new Error('請(qǐng)輸入正確的用戶(hù)名'));
 } else {
  callback();
 }
};
const validatePass = (rule: any, value: string, callback: any) => {
  if (! value) {
  callback(new Error('密碼不能為空'));
 // } else if (value.length < 5) {
 //  callback(new Error('密碼不能小于5位'));
 } else {
  callback();
 }
};

@Component({
 name: 'login',
})
export default class Login extends Vue {
 @Action('auth/login') private login_action!: CCS.LoginAction;

 private loginForm = { username: '', password: '' };
 private loginRules = {
  username: [{trigger: 'blur', validator: validateUsername }],
  password: [{trigger: 'blur', validator: validatePass }],
 };
 private loading = false;
 private redirect: string | undefined = undefined;
 private captchaEntity: any;
 // private loadingInstance: any;

 @Watch('$route', { immediate: true }) private OnRouteChange(route: Route) {
  this.redirect = route.query && route.query.redirect as string;
 }

 // private created() {
 //  this.loadingInstance = Loading.service({
 //   customClass: 'login_loading',
 //   text: '正在初始化,請(qǐng)稍后',
 //   fullscreen: true,
 //   lock: true,
 //  });
 // }

 /** ==================== 驗(yàn)證 START ========================= */
 /**
  * 頁(yè)面掛載后,后臺(tái)獲取初始化initGeetest所需參數(shù)值
  */
 private async mounted() {
  ThirdpartyServices.geetest_init().then((result) => {
   // this.loadingInstance.close();
   if (result.status) {
    this.initGeetest(result.data);
   } else {
    this.$message({ type: 'error', message: result.message });
   }
  });
 }
  /**
  * initGeetest 初始化
  */
 private initGeetest(param: CCS.GeettestInitType) {
  if ( ! (window as any) || ! (window as any).initGeetest ) {
   return false;
  }
  (window as any).initGeetest({
   gt: param.gt,
   challenge: param.challenge,
   offline: ! param.success,
   new_captcha: param.newcaptcha,
   timeout: '5000',
   product: 'bind',
   width: '300px',
   https: true,

  }, this.captchaObj_callback);
 }
 /**
  * 初始化后的回調(diào)函數(shù)
  */
 private async captchaObj_callback(captchaObj: any) {
  this.captchaEntity = captchaObj; // promise對(duì)象
  captchaObj
   .onReady(() => { // 驗(yàn)證碼就位
   })
   .onSuccess(() => {
    const rst = captchaObj.getValidate();
    if (!rst) {
     this.$message({ type: 'warning', message: '請(qǐng)完成驗(yàn)證'});
    }

    // 調(diào)用后臺(tái)check this.captchaObj
    this.verify_check(rst);
   })
   .onError((err: Error) => {
    console.log(err);
   });
 }
 /**
  * 后臺(tái)驗(yàn)證初始化結(jié)果
  */
 private async verify_check(validateResult: any) {
  ThirdpartyServices.geetest_checked(validateResult.geetest_challenge, validateResult.geetest_validate, validateResult.geetest_seccode ).then((result) => {
   if (result.status && result.data.result) {
    // 驗(yàn)證通過(guò),發(fā)送登錄請(qǐng)求
    this.handleLogin(result.data.token);
   } else {
    this.$message({ type: 'error', message: '驗(yàn)證失敗'});
    return false;
   }
  });
 }
 /** ==================== 驗(yàn)證 END ========================= */
 /**
  * 點(diǎn)擊登錄按鈕,彈出驗(yàn)證框
  */
 private login_btn_click() {
  (this.$refs.refform as ElForm).validate((valid) => {
   if (valid) {
    this.captchaEntity.verify(); // 顯示驗(yàn)證碼
   }
  });
 }
 /**
  * 驗(yàn)證成功,發(fā)送登錄請(qǐng)求
  */
 private async handleLogin(token: string) {
  this.loading = true;
  const { status, message} = await this.login_action({username: this.loginForm.username.trim(), password: this.loginForm.password, token});

  this.loading = false;
  if (status) {
   this.$message({type: 'success', message: '登錄成功'});
   this.$router.push({ path: this.redirect || '/' });
  } else {
   this.$message({type: 'error', message});
  }
 }
}
</script>

<template>
 <div class="login-container">
  <div class="login_form_wraper">
   <div class="logo_show">
    <img :src="require('@/assets/images/logo_w328.png')">
   </div>
   <img class="form_bg" :src="require('@/assets/images/login_form.png')">
   <el-form ref="refform" class="login-form" auto-complete="on" label-position="left"
     :model="loginForm" :rules="loginRules">
    <el-form-item prop="username">
     <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="用戶(hù)名"/>
     <i class="iconfont icon-zhanghaodenglu icon_prefix"></i>
    </el-form-item>
    <el-form-item prop="password">
     <el-input v-model="loginForm.password" name="password" type="password" auto-complete="on" placeholder="密碼"
      @keyup.enter.native="handleLogin" />
     <i class="iconfont icon-mima icon_prefix"></i>
    </el-form-item>

    <el-form-item class="login_btn">
     <el-button v-if="!loading" @click.native.prevent="login_btn_click">登錄</el-button>
     <el-button :loading="loading" v-else @click.native.prevent="handleLogin">登錄中</el-button>
    </el-form-item>
   </el-form>

  </div>
 </div>
</template>

<style lang="stylus" scoped>
@import '~@/assets/styles/var.styl';
@import '~@/assets/styles/pages/login.styl';

.login-container
 pass

</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI