uniapp使用网易易盾

43 篇文章 0 订阅
6 篇文章 0 订阅

插件地址:https://ext.dcloud.net.cn/plugin?id=1617

创建组件yidunCaptcha.vue
<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
			<ne-captcha
				v-if="captchaId"
				:captchaId="captchaId"
				:width="width"
				:lang="lang"
				:customStyles="customStyles"
				@verify="handleBindVerify($event.detail)"
				@error="handleError($event.detail)"
				id="neCaptcha" />
		<!-- #endif -->
		<!-- #ifdef H5 -->
			<view id="captcha"></view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		props: {
			isSense: false,
			captchaId: String,
			width: {
				type: [String, Number],
				default: 320
			},
			lang: {
				type: String,
				default: 'zh-CN'
			},
			customStyles: {
				type: Object,
				default: () => ({})
			},
			timeout: {
				type: Number,
				default: 12000
			}, // 客户端初始化超时时长
			dimAmount: Number, // 仅限客户端
			isToutchOutsideDisappear: Boolean, // 仅限客户端
			isHideCloseBtn: Boolean, // 仅限客户端
			useDefaultFallback: {
				type: Boolean,
				defaultL: true
			}, // 降级仅限客户端,h5端若要使用降级,请接入降级方案,参考官网
			failedMaxRetryCount: {
				type: Number,
				default: 3
			} // 降级配置,仅限客户端
		},
		data() {
			return {
				captchaIns: null,
				options: null
			}
		},
		created () {
			this.initNeCaptcha()
		},
		destroyed () {
			this.captchaIns = null
		},
		methods: {
			initNeCaptcha () {
				// #ifdef H5
				// h5其他配置项请参考文档
				const options = {
					captchaId: this.captchaId,
					width: this.width,
					lang: this.lang,
					element: '#captcha',
					protocol: 'https',
					enableClose: this.isHideCloseBtn,
					customStyles: this.customStyles,
					onVerify: (err, data) => {
						!err && this.handleVerify(data.validate)
					}
				}
				if (this.isSense) {
					options.mode = 'bind'
				} else {
					options.mode = 'popup'
				}
				
				initNECaptcha({ ...options }, ins => {
					this.captchaIns = ins
				}, err => {
					this.handleError(err)
				})
				// #endif
				
				// #ifdef MP-WEIXIN
					this.captchaIns = this.selectComponent('#neCaptcha')
				// #endif
				
				// #ifdef APP-PLUS
				let appCustom = {}
				if (this.customStyles && this.customStyles.icon) {
					const { slider } = this.customStyles.icon
					appCustom = {
						'control_bar_start_url': slider,
						'control_bar_moving_url': slider,
						'control_bar_error_url': slider
					}
				}
				this.captchaIns = uni.requireNativePlugin('YD-Captcha')
				this.captchaIns.init({
					...appCustom,
					'captcha_id': this.captchaId,
					'is_no_sense_mode': this.isSense,
					'language_type': this.lang,
					'dimAmount': this.dimAmount,
					'is_touch_outside_disappear': this.isToutchOutsideDisappear,
					'timeout': this.timeout,
					'is_hide_close_button': this.isHideCloseBtn,
					'use_default_fallback': this.useDefaultFallback,
					'failed_max_retry_count': this.failedMaxRetryCount
				}, (data) => {
					console.log(data);
					if (data.validate) {
						this.handleVerify(data.validate)
					} else {
						this.handleError(data)
					}
				})
				// #endif
				
			},
			verify () {
				// #ifdef H5
					if(this.isSense) {
						this.captchaIns.verify()
					} else {
						this.captchaIns.popUp()
					}
				// #endif
				// #ifdef MP-WEIXIN
					// 传统验证码
					if (this.isSense) {
						this.captchaIns.verify()
					} else {
						this.captchaIns.popup()
					}
				// #endif

				// #ifdef APP-PLUS
				this.captchaIns.showCaptcha((data)=>{
					console.log(data);
				})
				// #endif
			},
			reset () {
				// #ifdef H5
					this.captchaIns.refresh()
				// #endif
				
				// #ifdef MP-WEIXIN
					this.captchaIns.reset()
				// #endif
				
				// 原生实现reset可以先销毁(destroyCaptha)再进行初始化
				// #ifdef APP-PLUS
				this.destroyCap()
				setTimeout(() => {
					this.initNeCaptcha()
				}, 0)
				// #endif
			},
			destroyCap () {
				// 小程序端销毁直接清空captchaId即可
				// #ifdef H5
					this.captchaIns.destroy()
				// #endif
				
				// #ifdef APP-PLUS
					this.captchaIns.destroyCaptcha()
				// #endif
			},
			handleError ({ code, msg, message } = {}) {
				const err = new Error( msg ? `Error ${code}"${msg}` : message)
				this.$emit('captchaError')
			},
			handleVerify (validate) {
				this.$emit('verify', validate)
			},
			handleBindVerify ([err, validate] = []) {
				!err && this.handleVerify(validate)
			}
		}
	}
</script>

父组件

html
<yidun-captcha captcha-id="你的网易易盾id" ref="captcha" :isSense="false"
@verify="handleCaptcha"></yidun-captcha>
js
import YidunCaptcha from '../../components/yidunCaptcha.vue';
export default {
		components: {
			YidunCaptcha
		},
		methods: {
			//直接投票
			tryToVerify() {
				this.$refs.captcha.verify();
				// setTimeout(() => {
				// 	this.tryReset()
				// }, 30000)
			},
			handleCaptcha(validate) {
				console.log(`code: ${validate}`);
				console.log(validate, "网易易盾");
				this.vote_now(validate);//发送请求
			},
			tryReset() {
				this.$refs.captcha.reset();
			},
},

在根目录下创建template.h5.html文件
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<title>
			<%= htmlWebpackPlugin.options.title %>
		</title>
		<!-- 正式发布的时候使用,开发期间不启用。↓ -->
        <!-- <script src="/h5/touch-emulator.js"></script>
		<script>
            TouchEmulator();
			if (document.documentElement.clientWidth > 1024) {
				window.location.href = '/h5/pcguide.html#'+location.pathname+location.search;
			}
		</script>
        <style>
            ::-webkit-scrollbar{
                display: none;
            }
        </style>
        <script>
            var _hmt = _hmt || [];
            (function() {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?";// 百度统计key
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();
        </script> -->
        <!-- 正式发布的时候使用,开发期间不启用。↑ -->
		<script>
			document.addEventListener('DOMContentLoaded', function() {
				document.documentElement.style.fontSize = document.documentElement.clientWidth / 20 + 'px'
			})
		</script>
		<script src="https://cstaticdun.126.net/load.min.js?t=201903281201"></script>
		<link rel="stylesheet" href="<%= BASE_URL %>static/index.css" />
	</head>
	<body>
		<!-- 该文件为 H5 平台的模板 HTML,并非应用入口。 -->
		<!-- 请勿在此文件编写页面代码或直接运行此文件。 -->
		<!-- 详见文档:https://uniapp.dcloud.io/collocation/manifest?id=h5-template -->
		<noscript>
			<strong>Please enable JavaScript to continue.</strong>
		</noscript>
		<div id="app"></div>
		<!-- built files will be auto injected -->
		<script>
			/*BAIDU_STAT*/
		</script>
	</body>
</html>

引入template.h5.html

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值