小程序
小游戏
企业微信
微信支付
扫描小程序码分享
问题:
1、本地通过mkcert配置https微信中可正常访问,手机和电脑在同一局域网,手机已安装ca证书,手机真机调试时还是提示“无法打开该网页”
2、我部署到服务器上(真实域名),测试微信小程序调用H5页面进行扫码,提示摄像头权限问题,怎么解决
1 个回答
本回答由AI生成,可能已过期、失效或不适用于当前情形,请谨慎参考
加粗
标红
插入代码
插入链接
插入图片
上传视频
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
* 微信小程序扫码核销
*/
async miniProgramScan() {
try {
// 检查 token
const role = breakfast.storage.getRole();
const token = breakfast.storage.getToken(role);
if (!token) {
breakfast.utils.showError('请先登录');
return;
}
// 1. 先检查摄像头权限
const hasAuth = await this.checkCameraAuth();
if (!hasAuth) {
return; // 权限检查失败,已弹窗提示
}
uni.showLoading({ title: '准备扫码...' });
// 2. 调起小程序扫码
const scanRes = await uni.scanCode({
onlyFromCamera: true,
scanType: ['qrCode']
});
uni.hideLoading();
if (!scanRes || !scanRes.result) {
return;
}
// 3. 执行核销
await this.executeWriteOff(scanRes.result);
} catch (err) {
uni.hideLoading();
// 用户取消扫码不提示错误
if (err.errMsg && err.errMsg.includes('cancel')) {
return;
}
console.error('小程序扫码失败:', err);
breakfast.utils.showError(err.errMsg || '扫码失败,请重试');
}
},
/**
* 检查并请求摄像头权限(微信小程序专用)
* 注意:此方法必须在用户手势(如点击按钮)中调用
*/
checkCameraAuth() {
return new Promise((resolve) => {
// 获取当前授权状态
uni.getSetting({
success: (res) => {
const cameraAuth = res.authSetting['scope.camera'];
console.log('当前摄像头权限状态:', cameraAuth);
if (cameraAuth === true) {
// 已有权限
resolve(true);
} else if (cameraAuth === false) {
// 已被拒绝,引导用户去设置页开启
uni.showModal({
title: '摄像头权限未开启',
content: '核销优惠券需要开启摄像头权限,请在设置中开启',
confirmText: '去设置',
cancelText: '取消',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.camera']) {
resolve(true);
} else {
resolve(false);
}
},
fail: () => resolve(false)
});
} else {
resolve(false);
}
}
});
} else {
// 未询问过,主动请求权限(真机上会弹出授权窗口)
uni.authorize({
scope: 'scope.camera',
success: () => {
console.log('摄像头授权成功');
resolve(true);
},
fail: (err) => {
console.error('摄像头授权失败:', err);
if (err.errMsg && err.errMsg.includes('auth deny')) {
// 用户拒绝授权,引导去设置
uni.showModal({
title: '需要摄像头权限',
content: '核销优惠券需要使用摄像头扫描二维码',
confirmText: '去设置',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting();
}
resolve(false);
}
});
} else {
resolve(false);
}
}
});
}
},
fail: () => resolve(false)
});
});
},