1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| Page({ data: { cameraStatus: false, src: "", }, onLoad() { const _this = this; wx.getSetting({ success: (res) => { if (res.authSetting["scope.camera"]) { _this.setData({ cameraStatus: true, }); } else { wx.authorize({ scope: "scope.camera", success() { _this.setData({ cameraStatus: true, }); }, fail() { _this.openSetting().then((res) => { _this.setData({ cameraStatus: true, }); }); }, }); } }, fail: (res) => { console.log("获取用户授权信息失败"); }, }); },
openSetting() { const _this = this; let promise = new Promise((resolve, reject) => { wx.showModal({ title: "授权", content: "请先授权获取摄像头权限", success(res) { if (res.confirm) { wx.openSetting({ success(res) { if (res.authSetting["scope.camera"]) { resolve(true); } else { _this.openSetting().then((res) => { resolve(true); }); } }, fail(res) { console.log(res); }, }); } else if (res.cancel) { _this.openSetting().then((res) => { resolve(true); }); } }, }); }); return promise; }, takePhoto() { const ctx = wx.createCameraContext(); ctx.takePhoto({ quality: "high", success: (res) => { this.setData({ src: res.tempImagePath, cameraStatus: false, }); }, }); }, rePhoto() { this.setData({ cameraStatus: true, src: "", }); }, });
|