taro小程序进入腾讯验证码

接入原因

昨天突然晚上有人刷我们公司的登录发送短信接口,紧急将小程序的验证码校验更新上去了

接下来就是我们的接入方法,其实很简单,不过有时候可能大家着急就没有仔细看文档,腾讯验证码文档微信小程序地址注意这里分三种接入的前端框架,我这里就着急使用了小程序原生的接入代码,虽然看到了Taro框架的小程序接入示例

Taro 框架小程序插件接入示例

  1. 在 app.config.ts 引入小程序插件。 版本有更新现在是2.1.1
bash 复制代码
 {
  "plugins": {
    "captcha": {
      "version": "2.1.1",
      "provider": "wx1fe8d9a3cb067a75"
    }
  }
}
  1. 在需要加载验证码的页面配置插件,如page/index/index.config.ts。
bash 复制代码
 {
  "usingComponents": {
    "t-captcha": "plugin://captcha/t-captcha"
  }
}
  1. 在页面调用验证码,如 page/index/index.tsx。
bash 复制代码
import Taro from '@tarojs/taro'

export default function Index() {
	constructor(props) {
		this.bindverify = this.bindverify.bind(this)
	}
	
	
 	const { page } = Taro.getCurrentInstance();
    const captcha = page.selectComponent && page?.selectComponent('#captcha');
    try {
      captcha?.show();
    } catch (error) {
      // 进行业务逻辑,若出现错误需重置验证码,执行以下方法
      captcha?.refresh();
    }
	
	// 验证码回调
	bindverify = (event) => {
		// 获取randstr 和 ticket
	    console.log(event.detail)
	    // 验证码验证完成
	    if(event.detail.ret === 0) {
	      this.setState({
	        txJiYan: event.detail
	      },()=>{
	      	// 发送短信
	        this.sendCode()
	      })
	    } else {
	      // 验证失败
	      // 请不要在验证失败中调用refresh,验证码内部会进行相应处理
	    }
	}
	render() {
		return (
			<t-captcha
	          id='captcha'
	          appId={CaptchaAppId}
	          themeColor={themeColor}
	          onVerify={this.bindverify}
	        />
		)
	}
}

注意一定要按照对应的小程序架构插件去对接,原因是不同架构的编译模式不一样,Taro会将app-id中的都以小写去编译,插件是拿不到值的,所以这里要用appId ,而返回的方法 都要是on开头的,所以引用的时候这里要留意


区别一览

小程序原型

bash 复制代码
<t-captcha
     id="captcha"
     app-id="小程序插件验证码CaptchaAppId"
     bindverify="handlerVerify"
     bindready="handlerReady"
     bindclose="handlerClose"
     binderror="handlerError" />

uni-app框架

bash 复制代码
<t-captcha
     id="captcha"
     app-id="小程序插件验证码CaptchaAppId"
     @verify="handlerVerify"
     @ready="handlerReady"
     @close="handlerClose"
     @error="handlerError" />

Taro小程序

bash 复制代码
<t-captcha id='captcha' appId='小程序插件验证码CaptchaAppId' onVerify={handlerVerify} />
相关推荐
小小王app小程序开发12 小时前
淘宝扭蛋机小程序核心玩法拆解与技术运营分析
大数据·小程序
说私域15 小时前
AI智能名片商城小程序数据清洗的持续运营策略与实践研究
大数据·人工智能·小程序·流量运营·私域运营
东东51615 小时前
xxx食堂移动预约点餐系统 (springboot+微信小程序)
spring boot·微信小程序·小程序·毕业设计·个人开发·毕设
CHU72903516 小时前
一番赏盲盒抽卡机小程序:解锁惊喜体验与社交乐趣的多元功能设计
前端·小程序·php
2501_9159184118 小时前
HTTPS 代理失效,启用双向认证(mTLS)的 iOS 应用网络怎么抓包调试
android·网络·ios·小程序·https·uni-app·iphone
数字游民952719 小时前
半年时间大概上了70个web站和小程序,累计提示词超过20w
人工智能·ai·小程序·vibecoding·数字游民9527
说私域19 小时前
微商企业未来迭代的核心方向与多元探索——以链动2+1模式AI智能名片商城小程序为核心支撑
大数据·人工智能·小程序·流量运营·私域运营
276695829219 小时前
美团 小程序 mtgsig
python·小程序·node·js·mtgsig1.2·美团小程序·大众点评小程序
2501_9151063220 小时前
混合应用(Hybrid)安全加固,不依赖源码对成品 IPA 混淆
android·安全·小程序·https·uni-app·iphone·webview
00后程序员张21 小时前
无需越狱,来对 iOS 设备进行调试、管理与分析
android·ios·小程序·https·uni-app·iphone·webview