最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年06月27日 14:59:25   作者:element  
這篇文章主要介紹了vue + typescript + 極驗 登錄驗證的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

本項目為前后端分離,所以后端部署部分,請自行參考文檔操作

后臺接口:

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

頁面可能報錯: Uncaught SyntaxError: Unexpected token <


將報錯對象添加到與public同級的static目錄下(沒有則新建),修改引入路徑即可。

源碼:

<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('用戶名不能為空'));
 // } else if (!isValidUsername(value)) {
 //  callback(new Error('請輸入正確的用戶名'));
 } 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: '正在初始化,請稍后',
 //   fullscreen: true,
 //   lock: true,
 //  });
 // }

 /** ==================== 驗證 START ========================= */
 /**
  * 頁面掛載后,后臺獲取初始化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對象
  captchaObj
   .onReady(() => { // 驗證碼就位
   })
   .onSuccess(() => {
    const rst = captchaObj.getValidate();
    if (!rst) {
     this.$message({ type: 'warning', message: '請完成驗證'});
    }

    // 調(diào)用后臺check this.captchaObj
    this.verify_check(rst);
   })
   .onError((err: Error) => {
    console.log(err);
   });
 }
 /**
  * 后臺驗證初始化結(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) {
    // 驗證通過,發(fā)送登錄請求
    this.handleLogin(result.data.token);
   } else {
    this.$message({ type: 'error', message: '驗證失敗'});
    return false;
   }
  });
 }
 /** ==================== 驗證 END ========================= */
 /**
  * 點(diǎn)擊登錄按鈕,彈出驗證框
  */
 private login_btn_click() {
  (this.$refs.refform as ElForm).validate((valid) => {
   if (valid) {
    this.captchaEntity.verify(); // 顯示驗證碼
   }
  });
 }
 /**
  * 驗證成功,發(fā)送登錄請求
  */
 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="用戶名"/>
     <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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3獲取元素DOM的兩種方法

    Vue3獲取元素DOM的兩種方法

    Vue3 DOM是Vue.js框架的一部分,用于處理與瀏覽器DOM相關(guān)的操作,它提供了一組API和工具,使開發(fā)者能夠輕松地操作和管理DOM元素,本文給大家介紹了Vue3獲取元素DOM的兩種方法:ref模板引用和傳統(tǒng)方法,并有詳細(xì)的代碼示例,需要的朋友可以參考下
    2024-04-04
  • 使用mockjs如何生成隨機(jī)數(shù)據(jù)

    使用mockjs如何生成隨機(jī)數(shù)據(jù)

    Mockjs是一個用于生成隨機(jī)數(shù)據(jù)和攔截Ajax請求的庫,可以與Vue和Axios結(jié)合使用,提高前端開發(fā)效率,通過在項目中引入Mock.js文件,可以模擬后端API,攔截Ajax請求并返回自定義響應(yīng),這種方法適用于在后端尚未開發(fā)完成時的前端開發(fā)測試
    2024-10-10
  • Vue制作Todo List網(wǎng)頁

    Vue制作Todo List網(wǎng)頁

    這篇文章主要為大家詳細(xì)介紹了Vue制作Todo List網(wǎng)頁的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 解決vue $http的get和post請求跨域問題

    解決vue $http的get和post請求跨域問題

    這篇文章主要介紹了解決vue $http的get和post請求跨域問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 使用Vue編寫一個日期選擇器

    使用Vue編寫一個日期選擇器

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue編寫一個簡單的日期選擇器,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • Vue引入ElementUI并使用的詳細(xì)過程

    Vue引入ElementUI并使用的詳細(xì)過程

    Element UI是一個基于Vue 2.0的桌面端組件庫,旨在構(gòu)建簡潔、快速的用戶界面,這篇文章主要介紹了Vue如何引入ElementUI并使用,需要的朋友可以參考下
    2024-06-06
  • 淺談vue+webpack項目調(diào)試方法步驟

    淺談vue+webpack項目調(diào)試方法步驟

    本篇文章主要介紹了淺談vue+webpack項目調(diào)試方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 解決Vue3.0刷新頁面警告[Vue Router warn]:No match found for location with path /xxx

    解決Vue3.0刷新頁面警告[Vue Router warn]:No match 

    這篇文章主要介紹了解決Vue3.0刷新頁面警告[Vue Router warn]:No match found for location with path /xxx問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • ElementUI表單驗證validate和validateField的使用及區(qū)別

    ElementUI表單驗證validate和validateField的使用及區(qū)別

    Element-UI作為前端框架,最常使用到的就是表單驗證,下面這篇文章主要給大家介紹了關(guān)于ElementUI表單驗證validate和validateField的使用及區(qū)別,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Vue-Router模式和鉤子的用法

    Vue-Router模式和鉤子的用法

    本篇文章主要介紹了Vue-Router模式和鉤子的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02

最新評論

内黄县| 墨脱县| 汶上县| 清苑县| 霸州市| 缙云县| 凤冈县| 正宁县| 秀山| 苍梧县| 五莲县| 双流县| 铜川市| 乌什县| 历史| 甘谷县| 澄城县| 延川县| 巫山县| 宁国市| 密山市| 河北省| 衡阳县| 垣曲县| 九台市| 剑河县| 夏津县| 成武县| 洛浦县| 洪洞县| 蛟河市| 广宁县| 油尖旺区| 双牌县| 盈江县| 那坡县| 东明县| 信宜市| 鄢陵县| 永嘉县| 广德县|