微信小程序保存canvas繪製的圖片到本地,拒絕圖片授權後繼續授權

 authorizeSaveImg() { //重新授權相冊
		const that = this;
		wx.getSetting({
		  success(res) {
		     // 第一,直接調取保存,系統會自動調取授權
				if (res.authSetting['scope.writePhotosAlbum']) {
					that.canvasToImage();
				} else { // 授權
					wx.authorize({
						scope: 'scope.writePhotosAlbum',
						success() {
							// 授權成功
							that.canvasToImage();
						},
						fail: function() {
							// 授權失敗
							wx.showModal({
								title: '警告',
								content: '您點擊了拒絕授權,將無法正常保存圖片,點擊確定重新獲取授權。',
								success: function(res) {
									if (res.confirm) {
										wx.openSetting({
											success: (res) => {
												console.log('授權成功')
												that.canvasToImage();
											}
										})
									}
								}
							})
						}
					})
				}
				that.$apply()
			}
		})
	},
canvasToImage() { //  canvas畫布轉成圖片
        var that = this;
        wx.canvasToTempFilePath({
            quality: 1,
            fileType: 'jpg',
            canvasId: 'mycanvas',
            success: function(res) {
                wx.hideLoading();
                console.log('圖片地址' + res.tempFilePath);
                that.setData({
                    img_temp: res.tempFilePath
                })
                // wx.previewImage({
                //   current: res.tempFilePath, // 當前顯示圖片的http鏈接
                //   urls: [res.tempFilePath] // 需要預覽的圖片http鏈接列表
                // })
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(res) {
                        console.log(res);
                        wx.showModal({
                            title: '',
                            content: '圖片已保存到相冊',
                            showCancel: false,
                            confirmText: '好的',
                            confirmColor: '#72B9C3',
                            success: function(res) {
                                if (res.confirm) {
                                    console.log('用戶點擊確定');
                                }
                            }
                        })
                    },
                    fail: function(res) {
                       if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny" || res.errMsg ==='saveImageToPhotosAlbum:fail:auth denied') {
							that.authorizeSaveImg();
						} else if (res.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
						    wx.showToast({
								title: '取消保存',
								icon: 'none',
								duration: 2000
							})
						}
                    }
                })

            },
            fail: function(res) {
                console.log(res)
            }
        }, this)
    },
   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章