項目中使用的是wepy框架開發的小程序,,,
使用場景是,用戶點擊下載圖片的時候,要調起授權button(小程序拉起授權的功能都集成在了button組件,所以這里需要用到button組件里的一個open-type屬性,屬性值為'openSetting'和一個綁定事件bindopensetting聯合使用)來進行授權,
具體代碼如下:
<button id="download" class="bottomButton" @tap="downloadButton"> <view style="width:32rpx;height:32rpx;margin-top:16rpx;"> <image src="../assets/icons/download.png" /> </view> <view style="margin-top:-8rpx;"> 下載 </view> </button> <button id="share" class="bottomButton" open-type="share"> <view style="width:32rpx;height:32rpx;margin-top:20rpx;"> <image src="../assets/icons/share.png" /> </view> <view style="margin-top:-8rpx;"> 分享 </view> </button> </view> <button type='primary' class='openSetting' open-type="openSetting" bindopensetting='handleSetting' hidden='{{openSettingBtnHidden}}'>去授權</button>
js方法:
downloadButton() { wepy.showLoading({ title: '下載中' }) wepy.saveImageToPhotosAlbum({ filePath: this.preview }).then((res) => { wepy.hideLoading() wepy.showToast({ title: '已保存到相冊' }) }).catch(error => { wepy.hideLoading() console.log(error) if (error.errMsg === 'saveImageToPhotosAlbum:fail cancel' || error.errMsg === 'saveImageToPhotosAlbum:fail auth deny') { this.openSettingBtnHidden = false wepy.showToast({ title: '授權失敗,請點擊授權', icon: 'none' }) this.$apply() } }) },
handleSetting(e) { if (!e.detail.authSetting['scope.writePhotosAlbum']) { wepy.showModal({ title: '警告', content: '若不打開授權,則無法將圖片保存在相冊中!', showCancel: false }) this.openSettingBtnHidden = true } else { wepy.showModal({ title: '提示', content: '您已授權,趕緊將圖片保存在相冊中吧!', showCancel: false }) this.openSettingBtnHidden = true } this.$apply() },