Taro3 验证码百度小程序接入注意事项
2021.12.15 10:39:51
- 移动组件代码
执行 npm install @yidun/swan-ne-captcha
(如已执行,可忽略)
将 node_modules/@yidun/swan-ne-captcha
拷贝到 Taro 开发目录中,比如 components
下
- 在页面配置文件中引入组件
// index.json
export default {
usingComponents: {
"ne-captcha": "../../components/swan-ne-captcha/dist/index"
}
}
- 使用验证码组件
// index.js
import { Component } from 'react'
import { View, Text, Button } from '@tarojs/components'
import './index.scss'
import { getCurrentInstance } from '@tarojs/taro'
export default class Index extends Component {
saveCaptchaRef = (node) => this.captchaIns = node
componentWillMount () { }
componentDidMount () { }
componentWillUnmount () { }
componentDidShow () { }
componentDidHide () { }
handleClick () {
const { page } = getCurrentInstance()
const ins = page.selectComponent(this.captchaIns.uid)
ins.popup() // 常规验证码使用 ins.popup(), 智能无感知验证码调用 ins.verify()
}
handleVerify (ev) {
// ev.detail 包含 validate 信息
}
render () {
return (
<View className='index'>
<Button onClick={() => this.handleClick()}>验证码</Button>
<View>
<ne-captcha
captchaId="业务id"
width="640rpx"
ref={this.saveCaptchaRef}
onVerify={this.handleVerify}>
</ne-captcha>
</View>
</View>
)
}
}