需求:
1:扫码返回的结果必须是DECA开头,否则提示非法二维码
2:扫码返回的结果必须是16位
3:支付宝,微信等其他非项目公司提供的二维码扫描均无效
正则:
const reg = /^DECA[A-Za-z0-9]{12}$/;
主要代码:
wx.scanCode({
success: (res) => {
const result = res.result;
const reg = /^DECA[A-Za-z0-9]{12}$/;
// const reg = /^DECA/;
const a = reg.test(result);
if (!a) {
wx.showToast({
icon: 'none',
title: '非法二维码',
})
}
wx.navigateTo({
url: '../bind/bind?title=' + res.result
})
}
})
好了,现在开始写代码
//index.js
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
result: ''
},
onLoad: function () {
},
getScancode: function () {
var _this = this;
// 允许从相机和相册扫码
wx.scanCode({
success: (res) => {
const result = res.result;
const reg = /^DECA[A-Za-z0-9]{12}$/;
const a = reg.test(result);
if (!a) {
wx.showToast({
icon: 'none',
title: '非法二维码',
})
}
wx.navigateTo({
url: '../bind/bind?title=' + res.result
})
}
})
}
})
demo
小程序扫码成功后带着参数跳转到指定页面
https://www.jianshu.com/p/413c5831ddd6
原文作者:祈澈姑娘 技术博客:https://www.jianshu.com/u/05f416aefbe1
90后前端妹子,爱编程,爱运营,文艺与代码齐飞,魅力与智慧共存的程序媛一枚。
坚持总结工作中遇到的技术问题,坚持记录工作中所所思所见,对于博客上面有不会的问题,可以加入qq技术交流群聊:649040560。
网友评论