getAuth.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const downLoadImage = (url) => {
  2. uni.getSetting({
  3. success: (res) => {
  4. // console.log(res.authSetting['scope.writePhotosAlbum'])
  5. let flag = res.authSetting['scope.writePhotosAlbum']
  6. if (flag===false) {
  7. // 未授权之前没该权限 返回的时undefined 拒绝授权是false 同意是true
  8. uni.showModal({
  9. title: '提示',
  10. content: '请先授权保存相册',
  11. success: (res) => {
  12. if (res.confirm) {
  13. uni.openSetting()
  14. } else {
  15. uni.showToast({
  16. icon: 'none',
  17. mask: true,
  18. title: '用户取消授权'
  19. })
  20. }
  21. }
  22. })
  23. } else {
  24. uni.showLoading({
  25. title:'保存中...',
  26. mask:true
  27. })
  28. uni.downloadFile({
  29. url,
  30. success: (res) => {
  31. if (res.statusCode === 200) {
  32. uni.saveImageToPhotosAlbum({
  33. filePath: res.tempFilePath,
  34. success: (res) => {
  35. console.log(res)
  36. uni.showToast({
  37. title: '保存成功',
  38. mask: true
  39. })
  40. },
  41. fail: (err) => {
  42. console.log(err)
  43. uni.showToast({
  44. title: '保存失败',
  45. mask: true,
  46. icon: 'error'
  47. })
  48. }
  49. });
  50. }
  51. },
  52. fail: () => {
  53. uni.showToast({
  54. title: '保存失败',
  55. mask: true,
  56. icon: 'error'
  57. })
  58. }
  59. });
  60. }
  61. }
  62. })
  63. }
  64. //如需动态获取权限,修改res.authSetting['scope.userLocation']与scope: 'scope.userLocation'的值。
  65. /****
  66. userInfo:用户信息
  67. userLocation:地理位置
  68. address:通讯地址
  69. werun:运动步数
  70. record:录音
  71. writePhotosAlbum:保存到相册
  72. camera:摄像头
  73. ****/
  74. const getAuth = (auth) => {
  75. return new Promise((resolve, reject) => {
  76. wx.getSetting({
  77. success(res) {
  78. if (!res.authSetting[`scope.${auth}`]) {
  79. wx.authorize({
  80. scope: `scope.${auth}`,
  81. success() {
  82. resolve()
  83. },
  84. async fail(e) {
  85. wx.hideLoading()
  86. await showText(auth,resolve, reject)
  87. }
  88. })
  89. } else {
  90. resolve()
  91. }
  92. }
  93. })
  94. })
  95. }
  96. const showText = async (auth,resolve, reject)=>{
  97. await uni.showModal({
  98. title: '提示',
  99. content: '请前往设置打开权限',
  100. success(res) {
  101. if (res.confirm) {
  102. uni.openSetting({
  103. async success(res) {
  104. console.log(res,res.authSetting[`scope.${auth}`])
  105. if(!res.authSetting[`scope.${auth}`]){
  106. return await showText(auth,resolve, reject)
  107. }else{
  108. return resolve()
  109. }
  110. },
  111. fail(err) {
  112. console.log(err)
  113. return reject()
  114. }
  115. })
  116. } else if (res.cancel) {
  117. uni.showModal({
  118. title: '提示',
  119. content: '请授权小程序权限,\n「右上角」-「设置」中开启',
  120. showCancel: false,
  121. success(res) {
  122. if (res.confirm) {
  123. return reject()
  124. }
  125. }
  126. })
  127. }
  128. }
  129. })
  130. }
  131. module.exports = {
  132. downLoadImage,
  133. getAuth
  134. }