Sfoglia il codice sorgente

feature:增加微信小程序的相册权限

zeng.chen 7 mesi fa
parent
commit
c7ad75b982

+ 4 - 3
pages/components/image-cropper/image-cropper.js

@@ -284,10 +284,11 @@ Component({
          */
         upload() {
             let that = this;
-            wx.chooseImage({
+            wx.chooseMedia({
                 count: 1,
-                sizeType: ['original', 'compressed'],
-                sourceType: ['album', 'camera'],
+                mediaType: ['image'],
+                sourceType: ['album'],
+                // camera: 'back',
                 success(res) {
                     const tempFilePaths = res.tempFilePaths[0];
                     that.pushImg(tempFilePaths);

+ 114 - 1
pages/piano/cropper/cropper.js

@@ -3,6 +3,7 @@
 const { BtCmd } = require('../../../devices/bluetooth/bt_cmd');
 import EventManager from '../../../utils/event_bus'
 import { EnumCmdEvent, CmdEvent } from '../../../devices/cmd_key_event';
+import route_util from '../../../utils/route_util';
 const { BtHelper } = require('../../../devices/bt_helper');
 
 Page({
@@ -111,8 +112,12 @@ Page({
       // app.globalData.imgSrc = obj.url;
       console.log("裁剪图片:", obj);
       _this.readLocalFileAndConvertToBase64(obj.url);
+      // _this.readBinFile("../../pagesA/images/th01.bin");
     });
   },
+  submitTest() {
+    route_util.jump("../../pagesA/index/index")
+  },
   // 读取本地文件并转换为 Base64 字符串
   readLocalFileAndConvertToBase64(filePath) {
     const fs = wx.getFileSystemManager();
@@ -137,7 +142,115 @@ Page({
       }
     });
   },
+  readBinFile(filePath) {
+    const fs = wx.getFileSystemManager();
+
+    fs.readFile({
+      filePath: filePath,
+      encoding: '', // 不指定编码以获取原始二进制数据
+      success: (res) => {
+        console.log("读取 .bin 文件成功:", res.data);
+        // 处理读取到的二进制数据
+        this.processBinData(res.data);
+      },
+      fail: (err) => {
+        console.error('读取 .bin 文件失败:', err);
+      },
+    });
+  },
+  checkAndRequestImagePermission: function () {
+    const self = this;
+
+    // 检查用户是否已经授权访问相册
+    wx.getSetting({
+      success(res) {
+        if (!res.authSetting['scope.writePhotosAlbum']) {
+          // 用户未授权访问相册,请求用户授权
+          wx.authorize({
+            scope: 'scope.writePhotosAlbum',
+            success() {
+              // 用户同意授权
+              console.log('用户已授权访问相册');
+              // 可以在这里执行访问相册的操作
+            },
+            fail() {
+              // 用户拒绝授权
+              console.log('用户拒绝授权访问相册');
+              wx.showModal({
+                title: '提示',
+                content: '您拒绝了访问相册的权限,请在设置中手动开启',
+                showCancel: false,
+                success(res) {
+                  if (res.confirm) {
+                    // 跳转到设置页面
+                    wx.openSetting({
+                      success(settingRes) {
+                        if (settingRes.authSetting['scope.writePhotosAlbum']) {
+                          console.log('用户已在设置中开启访问相册的权限');
+                          // 可以在这里执行访问相册的操作
+                        } else {
+                          console.log('用户仍未授权访问相册');
+                        }
+                      }
+                    });
+                  }
+                }
+              });
+            }
+          });
+        } else {
+          // 用户已授权访问相册
+          console.log('用户已授权访问相册');
+          // 可以在这里执行访问相册的操作
+        }
+      }
+    });
 
+    // 检查用户是否已经授权使用相机
+    wx.getSetting({
+      success(res) {
+        if (!res.authSetting['scope.camera']) {
+          // 用户未授权使用相机,请求用户授权
+          wx.authorize({
+            scope: 'scope.camera',
+            success() {
+              // 用户同意授权
+              console.log('用户已授权使用相机');
+              // 可以在这里执行使用相机的操作
+            },
+            fail() {
+              // 用户拒绝授权
+              console.log('用户拒绝授权使用相机');
+              wx.showModal({
+                title: '提示',
+                content: '您拒绝了使用相机的权限,请在设置中手动开启',
+                showCancel: false,
+                success(res) {
+                  if (res.confirm) {
+                    // 跳转到设置页面
+                    wx.openSetting({
+                      success(settingRes) {
+                        if (settingRes.authSetting['scope.camera']) {
+                          console.log('用户已在设置中开启使用相机的权限');
+                          // 可以在这里执行使用相机的操作
+                        } else {
+                          console.log('用户仍未授权使用相机');
+                        }
+                      }
+                    });
+                  }
+                }
+              });
+            }
+          });
+        } else {
+          // 用户已授权使用相机
+          console.log('用户已授权使用相机');
+          // 可以在这里执行使用相机的操作
+        }
+      }
+    });
+  },
   startImage() {
     BtHelper.getInstance().send(BtCmd.wallPaper(1));
   },
@@ -238,7 +351,7 @@ Page({
   onLoad(options) {
     console.log(options.param)
     // let json = JSON.parse(options.param)
-
+    this.checkAndRequestImagePermission()
     this.cropper = this.selectComponent("#image-cropper");
     this.cropper.imgReset();
     // this.setData({