AWSC的语言配置
需要预设定语言配置,多语言切换需要配置upLang来联动使用才有效果
关于reset后宽度初始化和自定义样式不一致问题
可以直接在配置中指定固定宽度
width: "500",
验证失败后重置滑块状态
window.nc.reset();
源码
<template><div class="verify-box"><div id="nc"></div></div></template><script setup lane="ts">import { onMounted, watchEffect } from "vue";import { useGlobal } from "@/hooks/useGlobal";import { useMessage } from "naive-ui";import * as LoginAction from "@/api/login";const { appConfig } = useGlobal();const message = useMessage();const emit = defineEmits(["verifySuccess"]);var myLang = {id: {// //加载状态提示。// LOADING: "加载中...",// //等待滑动状态提示。// SLIDE: "请向右滑动验证",// //验证通过状态提示。// SUCCESS: "验证通过",// //验证失败触发拦截状态提示。// ERROR: "非常抱歉,网络出错了...",// //验证失败触发拦截状态提示。// FAIL: "验证失败,请点击重试",//加载状态提示。LOADING: "Memuatkan...",//等待滑动状态提示。SLIDE: "Silakan slide kanan untuk",//验证通过状态提示。SUCCESS: "passed",//验证失败触发拦截状态提示。ERROR: "Maaf, ada kesalahan jaringan...",//验证失败触发拦截状态提示。FAIL: "Verifikasi gagal, silakan klik Coba lagi",},en: {//加载状态提示。LOADING: "Loading...",//等待滑动状态提示。SLIDE: "Please swipe right to verify",//验证通过状态提示。SUCCESS: "Verification passed",//验证失败触发拦截状态提示。ERROR: "I am very sorry, there is a network error...",//验证失败触发拦截状态提示。FAIL: "Verification failed, please click retry",},};onMounted(() => {watchEffect(() => {if (appConfig.showLang) {fetchConfig();}});});function fetchConfig() {LoginAction.fetchAfsConfig().then((res) => {console.log(res.afsConfig, "fetchConfig");removeVertify();onInit(res.afsConfig);}).catch((err) => console.error(err));}function removeVertify() {let dom = document.getElementById("nc");dom.innerHTML = "";}function onInit(afsConfig) {AWSC.use("nc", function (state, module) {// 初始化window.nc = module.init({// 应用类型标识。它和使用场景标识(scene字段)一起决定了滑动验证的业务场景与后端对应使用的策略模型。您可以在阿里云验证码控制台的配置管理页签找到对应的appkey字段值,请务必正确填写。appkey: afsConfig.appKey,//使用场景标识。它和应用类型标识(appkey字段)一起决定了滑动验证的业务场景与后端对应使用的策略模型。您可以在阿里云验证码控制台的配置管理页签找到对应的scene值,请务必正确填写。scene: afsConfig.scene,// 声明滑动验证需要渲染的目标ID。renderTo: "nc",language: appConfig.showLang,width: "500",upLang: myLang,// test: module.TEST_PASS, // 测试滑动通过状态// test: module.TEST_BLOCK, // 测试滑动失败状态//前端滑动验证通过时会触发该回调参数。您可以在该回调参数中将会话ID(sessionId)、签名串(sig)、请求唯一标识(token)字段记录下来,随业务请求一同发送至您的服务端调用验签。success: function (data) {emit("verifySuccess", data);},// 滑动验证失败时触发该回调参数。fail: function (failCode) {window.console && console.log(failCode);window.nc.reset();message.warning("Verification failed. Please try again later.");},// 验证码加载出现异常时触发该回调参数。error: function (errorCode) {window.nc.reset();window.console && console.log(errorCode);},});});}</script><style lang="scss" scoped>#nc {width: 100% !important;display: contents;.nc_scale {width: 100% !important;}}:deep(.nc-container #nc_1_wrapper) {width: 100%;height: 36px;line-height: 36px;#nc_1_n1t,#nc_1__bg,#nc_1_n1z,#nc_1__scale_text,.nc-lang-cnt {height: 36px;box-sizing: border-box;line-height: 36px;}}.verify-box {width: 100%;height: 34px;margin-bottom: 2.25rem;}</style>
