Skip to content

Commit

Permalink
新增 wx.pro.saveImageToPhotosAlbum,内部封装了四个API,用户只需调用该方法,我们已经完美地处理了用户拒绝授…
Browse files Browse the repository at this point in the history
…权的场景
  • Loading branch information
youngjuning committed Apr 26, 2018
1 parent 774e896 commit a44f237
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wx-promise-pro",
"version": "2.0.4",
"version": "2.1.0",
"description": "强大的、优雅的小程序 Promise 库",
"main": "wxPromise.js",
"scripts": {
Expand Down
54 changes: 54 additions & 0 deletions wxPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,60 @@ const wxPromise = () => {
})
})
})
},

// 保存图片到系统相册。需要用户授权 scope.writePhotosAlbum
wx.pro.saveImageToPhotosAlbum = (tempFilePath) => {
return new Promise((resolve, reject) =>{
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.writePhotosAlbum']) {
// 没有授权,向用户发起授权请求
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
// 用户同意授权,调用 api 保存图片
wx.saveImageToPhotosAlbum({
filePath: tempFilePath,
success(res) {
resolve(res)
},
fail(err) {
reject(err)
}
})
},
fail: err => {
// 用户拒绝授权,提醒用户打开设置页面
wx.hideLoading()
wx.pro.showModal({
title: '温馨提示',
content: '请授权系统使用保存图片接口',
confirmText: '知道了',
showCancel: false
}).then(res => {
wx.openSetting()
})
}
})
} else {
// 已经授权,直接调用 api 保存图片
wx.saveImageToPhotosAlbum({
filePath: tempFilePath,
success(res) {
resolve(res)
},
fail(err) {
reject(err)
}
})
}
},
fail: (err) => {
reject(err)
}
})
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion wxPromise.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a44f237

Please sign in to comment.