share.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const app = require('../../utils/util.js');
  2. Page({
  3. data: {
  4. codeImg: '../../images/ewm.png'
  5. },
  6. save() {
  7. let that= this;
  8. wx.getSetting({
  9. success: (res) => {
  10. console.log(res)
  11. if (!res.authSetting['scope.writePhotosAlbum']) {
  12. wx.authorize({
  13. scope: 'scope.writePhotosAlbum',
  14. success() {
  15. console.log(res, "我点击同意啦")
  16. wx.showLoading({
  17. title: '保存中...'
  18. })
  19. let imgSrc = that.data.codeImg;
  20. wx.downloadFile({
  21. url: imgSrc,
  22. success: function (res) {
  23. console.log(res, '我下载完成啦');
  24. //图片保存到本地
  25. wx.saveImageToPhotosAlbum({
  26. filePath: res.tempFilePath,
  27. success: function (data) {
  28. console.log(data, '我保存成功啦')
  29. wx.hideLoading();
  30. wx.showToast({
  31. title: '保存成功',
  32. icon: 'success',
  33. duration: 2000
  34. })
  35. },
  36. fail: function (err) {
  37. console.log(err, '我保存失败啦')
  38. wx.hideLoading();
  39. if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  40. console.log("当初用户拒绝,再次发起授权")
  41. wx.openSetting({
  42. success(settingdata) {
  43. console.log(settingdata)
  44. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  45. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  46. } else {
  47. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  48. }
  49. }
  50. })
  51. }
  52. },
  53. complete(res) {
  54. console.log(res);
  55. }
  56. })
  57. },
  58. complete(res) {
  59. wx.hideLoading()
  60. }
  61. })
  62. }
  63. })
  64. } else {
  65. wx.showLoading({
  66. title: '保存中...'
  67. })
  68. console.log(this.data.classInfo.codeImg)
  69. let imgSrc = this.data.classInfo.codeImg
  70. wx.downloadFile({
  71. url: imgSrc,
  72. success: function (res) {
  73. console.log(res, '我下载完成啦');
  74. wx.hideLoading();
  75. //图片保存到本地
  76. wx.saveImageToPhotosAlbum({
  77. filePath: res.tempFilePath,
  78. success: function (data) {
  79. console.log(data, '我保存成功啦')
  80. wx.hideLoading();
  81. wx.showToast({
  82. title: '保存成功',
  83. icon: 'success',
  84. duration: 2000
  85. })
  86. },
  87. fail: function (err) {
  88. console.log(err, '我保存失败啦')
  89. wx.hideLoading();
  90. if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  91. console.log("当初用户拒绝,再次发起授权")
  92. wx.openSetting({
  93. success(settingdata) {
  94. console.log(settingdata)
  95. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  96. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  97. } else {
  98. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  99. }
  100. }
  101. })
  102. }
  103. },
  104. complete(res) {
  105. console.log(res);
  106. }
  107. })
  108. }
  109. })
  110. }
  111. }
  112. })
  113. },
  114. /**
  115. * 生命周期函数--监听页面加载
  116. */
  117. onLoad: function (options) {
  118. },
  119. /**
  120. * 生命周期函数--监听页面初次渲染完成
  121. */
  122. onReady: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面显示
  126. */
  127. onShow: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload: function () {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh: function () {
  143. },
  144. /**
  145. * 页面上拉触底事件的处理函数
  146. */
  147. onReachBottom: function () {
  148. },
  149. /**
  150. * 用户点击右上角分享
  151. */
  152. onShareAppMessage: function () {
  153. }
  154. })