uni.scanCode不支持传参数(utoken)和(ApiUrl)嘛,要不然,就只要简单扫一扫功能了。
<template>
<view>
<view class="page-body">
<view class="page-section-title">扫码结果</view>
<view class="uni-list">
<view class="uni-cell">
<view class="uni-input">{{result}}</view>
</view>
</view>
<view class="btn-area">
<button type="primary" @tap="scanCode">扫一扫</button>
</view>
</view>
</view>
</template>
<script>
import {
mapState,
mapMutations
} from 'vuex'
export default {
computed: mapState(['utoken']),
data() {
return {
title: 'scanCode',
result: '',
}
},
onUnload:function(){
this.result = '';
},
methods: {
scanCode: function () {
uni.scanCode({
onlyFromCamera: true,
success: function (res) {
//getScan();
//this.getScan();
uni.request({
crossDomian:true,
method:"POST",
header : {
"Content-type":"application/x-www-form-urlencoded",
"Authorization":"Bearer "+this.utoken,
"Accept":"application/vnd.baid.v1+json"
},
url: ApiUrl+'/v1scan',
data:{
'scankey':res.result,
'term':plus.os,
'dev':plus.device,
'clientinfo':JSON.stringify(plus.push.getClientInfo()),
},
success: (res) => {
uni.hideLoading();
uni.showModal({
title: '提示',
content: JSON.stringify(res.data)
});
}
})
},
complete:function(res){
uni.showLoading({
'title': '识别中...'
});
setTimeout(function () {
uni.hideLoading();
}, 2000);
},
fail: function (res) {
setTimeout(function () {
uni.hideLoading();
}, 2000);
}
})
},
getScan:function(){
uni.request({
crossDomian:true,
method:"POST",
header : {
"Content-type":"application/x-www-form-urlencoded",
"Authorization":"Bearer "+this.utoken,
"Accept":"application/vnd.baid.v1+json"
},
url: ApiUrl+'/v1scan',
data:{
'scankey':this.result,
//'term':plus.os,
//'dev':plus.device,
//'clientinfo':JSON.stringify(plus.push.getClientInfo()),
},
success: (res) => {
uni.showModal({
title: '提示',
content: JSON.stringify(res.data)
});
}
})
}
}
}
</script>
<style>
</style>
如果是单用uni.request(里面的是ok的;
8***@qq.com
用箭头函数或者定个变量保存this指向
第一种:scan:function(){
uni.scanCode({
success: res => {
console.log('条码类型:' + res.scanType);
console.log('条码内容:' + res.result);
this.orderNo = res.result;
}
});
},
第二种:scan:function(){
let _this = this
uni.scanCode({
success: function (res) {
console.log('条码类型:' + res.scanType);
console.log('条码内容:' + res.result);
_this.orderNo = res.result;
}
});
},
2019-02-22 20:33
2***@qq.com
回复 8***@qq.com:谢谢大佬了,我之前用了第二种方法,看来文档还是要多看看
2019-03-25 16:01
菜逼开发者
回复 2***@qq.com: 现在两种方法都不行了
2020-12-17 15:03