钉钉扫条形码学习2目录
调用钉钉扫一扫
调用biz.util.scan扫条形码或二维码。
安装包:
npm install dingtalk-jsapi --save
引用:
import * as dd from "dingtalk-jsapi";
函数调用:
dd.biz.util.scan({
type: String , // type 为 all、qrCode、barCode,默认是all。
onSuccess: function(data) {
//onSuccess将在扫码成功之后回调
/* data结构
{ 'text': String}
*/
},
onFail : function(err) {
}
})
安装一下工具吧会更方便一些:
npm i dingtalk-design-cli@latest -g
ding init
2021/6/25
在使用上面的函数调用的时候发现了问题,不能够调用无法使能,最后经过排查发现上面的是使用的H5的开发调用函数,所以在这里换成小程序里的调用接口:
dd.scan({
type: 'qr',
success: (res) => {
dd.alert({
title: res.code });
},
});
在调用成功后success返回值:
如果错误,错误码为:
推荐一个钉钉提供的API地址
然后就可以开始,这个是.axml文件
<view class="form-row-label">姓名</view>
<view class="form-row-content">
<input class="input" onInput="bindKeyInput" placeholder="请填入姓名" />
</view>
<view class="form-row-label">工号</view>
<view class="form-row-content">
<input class="input" onInput="bindKeyInput" placeholder="请填入工号" />
</view>
<view class="form-row-label">号码1</view>
<view class="form-row-content">
<input type='text' value='{
{scanCodeMsg1}}'></input>
</view>
<form onSubmit="scanCode">
<button type="primary" onTap="scan1">扫码1</button>
<view class="form-row-label">号码2</view>
<view class="form-row-content">
<input type='text' value='{
{scanCodeMsg2}}'></input>
</view>
<form onSubmit="scanCode">
<button type="primary" onTap="scan2">扫码2</button>
<view class="form-row-label">号码3</view>
<view class="form-row-content">
<input type='text' value='{
{scanCodeMsg3}}'></input>
</view>
<form onSubmit="scanCode">
<button type="primary" onTap="scan3">扫码3</button>
在.js文件中有:
//import * as dd from "dingtalk-jsapi";
Page({
data: {
scanMsg1: "",
scanMsg2: "",
scanMsg3: "",
focus: false,
inputValue: ''
},
bindButtonTap() {
// blur 事件和这个冲突
setTimeout(() => {
this.onFocus();
}, 100);
},
onFocus() {
this.setData({
focus: true,
});
},
onBlur() {
this.setData({
focus: false,
});
},
bindKeyInput(e) {
this.setData({
inputValue: e.detail.value,
});
},
bindHideKeyboard(e) {
if (e.detail.value === '123') {
// 收起键盘
dd.hideKeyboard();
}
},
handleSearch(e) {
console.log('search', e.detail.value);
this.setData({
search: e.detail.value,
});
},
doneSearch() {
console.log('doneSearch', this.data.search);
dd.hideKeyboard();
},
clearSearch() {
console.log('clear search', this.data.search);
this.setData({
search: '',
});
},
scan1() {
var that = this;
dd.scan({
type: 'qr',
success: (res) => {
dd.alert({
title: res.code });
that.setData({
scanCodeMsg1: res.code});
}
});
},