官方地址:https://uniapp.dcloud.io/api/system/barcode.html
<template>
<view class="scanCodeBox">
<image class='imgCode' src="../static/code.png" mode="" @click="scanCode"></image>
<view class="desc">
扫一扫
</view>
</view>
</template>
<script>
export default {
methods: {
// 点击扫一扫按钮
scanCode() {
uni.scanCode({
success: (res) => {
console.log('扫码成功', res)
},
fail: (err) => {
console.log('扫码失败', err)
},
complete: () => {
console.log('扫码结束')
}
})
},
}
}
</script>
var that = this
// 只允许从相机扫码
wx.scanCode({
onlyFromCamera: true,
scanType: ['qrCode'],
success(res) {
console.log(res)
if (res.path) {//合法本小程序路径
var str = res.path
var index = str.indexOf("=");//截取=符号后面的字符串
var scene = str.substr(index + 1, str.length); //获得=符号后面所有字符
var type = parseInt(scene[0])
// console.log(type)
//console.log(scene[2])
if (type == 3) {
that.setData({
oil_station_id: scene[2]
})
//执行获得参数后的操作
}
}
}
})
//decodeURIComponent解析生成二维码的链接。
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
if (options.scene) {
//获取二维码的携带的链接信息
let qrUrl = decodeURIComponent(options.scene)
console.log(qrUrl)
this.setData({
//获取链接中的参数信息
name: utils.getQueryString(qrUrl, 'name'),
id: utils.getQueryString(qrUrl, 'id'),
})
}
},
onLoad (option) {
console.log(option)
if (option.scene) {
let obj = decodeURIComponent(option.scene)
... // 这里就是你拿着参数obj进行操作
}
}