微信小程序授权过程 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 小程序相关 >

微信小程序授权过程

发表时间:2020-10-9

发布人:葵宇科技

浏览次数:31

首先询问用户是否同意授权,同意则直接授权,否则弹框提示用户没有授权将影响后续操作。引导用户去设置页开启授权wx.openSetting

wx.getSetting({
    success(res) {
        //没有权限,发起授权
        if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success() {
                    //用户允许授权,保存图片到相册
                    that.chooseImg()
                },
                fail(err) {
                    //用户点击拒绝授权,跳转到设置页,引导用户授权
                    that.showSettingToast()
                }
            });
        } else {
            //用户已授权,保存到相册
            that.chooseImg()
        }
    }
});

// 打开权限设置页提示框
showSettingToast(e) {
    wx.showModal({
        content: '检测到您没打开此小程序的此权限,是否去设置打开?',
        confirmText: "确认",
        cancelText: "取消",
        success: function(res) {
            //点击“确认”时打开设置页面
            if (res.confirm) {
                wx.openSetting({
                    success: (res) => {}
                })
            } else {
                console.log('用户点击取消')
            }
        }
    });
},

保存图片到本地,保存图片也需要先查询用户是否同意保存到本地

// 保存图片到本地
savePhoto() {
    let that = this
    uni.getImageInfo({
        src: that.currentItemUrl,
        success: function(res) {
            console.log(res);
            //保存图片
            uni.saveImageToPhotosAlbum({
                filePath: res.path,
                success(result) {
                    uni.showToast({
                        title: result
                    })
                }
            })
        }
    })
},

图片转为base64

imgBase64(tempPath) {
    // 转base64
    wx.getFileSystemManager().readFile({
        filePath: tempPath, //选择图片chooseImage返回的相对路径
        encoding: "base64", //这个是很重要的
        success: res => { //成功的回调
            //返回base64格式
            let photo = res.data
            this.storeUpdatedImg(photo)

        }
    })
},

相关案例查看更多