<view class="desc"><text class="text">复制的文字</text><image src="图片路径"></image></view><view class="btn"><button bindtap='copyText' data-text="{{copyText}}">复制文案</button><button wx:if="{{saveImgBtn}}" bindtap='saveImg' >保存推广图</button><button open-type="openSetting"bindopensetting='saveImg' wx:if='{{openSettingBtn}}' >授权并保存图片</button></view>
js文件:
(1)这里由于第一次授权点击拒绝之后,再次点击不会出现授权按钮,因此需要使用open-type="openSetting"再次调用授权窗口。 如果未授权或者授权失败,出现 ‘授权并保存图片’按钮。
data: {saveImgBtn: true, //为true时,显示保存按钮openSettingBtn: false, //为true时,显示授权并保存按钮},
/****保存图片 */saveImg: function () {let that = thiswx.getSetting({success(res) { if (!res.authSetting['scope.writePhotosAlbum']) { //未授权 先授权 然后保存wx.authorize({scope: 'scope.writePhotosAlbum',success(re) {that.saveImageToPhotosAlbum();that.setData({saveImgBtn: true,openSettingBtn: false})},fail() {//用户取消授权that.setData({saveImgBtn: false,openSettingBtn: true})}})} else {//授权了, 调用保存到相册方法that.saveImageToPhotosAlbum();that.setData({saveImgBtn: true,openSettingBtn: false})}}})},//保存网络图片到相册方法saveImageToPhotosAlbum: function () {wx.downloadFile({url:' ',success(result) {wx.saveImageToPhotosAlbum({filePath: tempFilePath,success(result) {wx.showToast({title: '已保存至相册' })}})}})},
(2)复制文案
//复制copyText: function (e) {wx.setClipboardData({data: e.currentTarget.dataset.text,success: function (res) {wx.getClipboardData({success: function (res) {wx.showToast({title: '复制成功'})}})}})},