share.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const app = require('../../utils/util.js');
  2. const tool = require('../../utils/tool.js');
  3. Page({
  4. data: {
  5. codeImg: '../../images/ewm.png',
  6. bj: '../../images/ewmbj.png'
  7. },
  8. save() {
  9. console.log('我调取save了')
  10. wx.saveImageToPhotosAlbum({
  11. filePath: this.data.imagePath,
  12. success(res) {
  13. wx.showToast({
  14. title: '图片保存成功!',
  15. icon: 'none'
  16. })
  17. },
  18. fail: (res) => {
  19. console.log(res)
  20. if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny" || res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  21. this.doAuth()
  22. }
  23. }
  24. })
  25. },
  26. // 获取授权
  27. doAuth() {
  28. wx.showModal({
  29. title: '获取授权',
  30. content: '您是否同意重新授权保存图片',
  31. cancelText: '不同意',
  32. confirmText: '同意',
  33. confirmColor: '#21c0ae',
  34. success: function (res) {
  35. if (res.confirm) { // 点击确认
  36. wx.openSetting({
  37. success(settingdata) {
  38. if (settingdata.authSetting["scope.writePhotosAlbum"]) {
  39. console.log("获取权限成功,再次点击图片保存到相册")
  40. } else {
  41. console.log("获取权限失败")
  42. }
  43. },
  44. fail: function (res) {
  45. console.log(res)
  46. }
  47. })
  48. }
  49. }
  50. })
  51. },
  52. getEwm() {
  53. wx.request({
  54. url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/share',
  55. method: "get",
  56. success: (res) => {
  57. if (res.data.code == 0) {
  58. console.log(res.data.qrcode, '我是获取的二维码')
  59. this.draw(res.data.qrcode);
  60. }
  61. }
  62. })
  63. },
  64. draw(qrcode) {
  65. wx.downloadFile({
  66. url: qrcode,
  67. success: (res) => {
  68. const ctx = wx.createCanvasContext('myCanvasId');
  69. ctx.drawImage(this.data.bj, 0, 0, 302, 256); //里面的参数无非就是图片放置的位置即图片的横纵坐标,图片的宽高
  70. ctx.drawImage(res.tempFilePath, 88, 114, 114, 114);
  71. ctx.draw();
  72. setTimeout(() => {
  73. wx.canvasToTempFilePath({
  74. canvasId: 'myCanvasId',
  75. success: (res) => {
  76. var tempFilePath = res.tempFilePath;
  77. this.setData({
  78. imagePath: tempFilePath
  79. })
  80. },
  81. fail: (res) => {
  82. console.log(res);
  83. }
  84. });
  85. }, 200);
  86. }
  87. })
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function (options) {
  93. tool.openidStatus().then(result => {
  94. this.setData({
  95. openid: result[0],
  96. sessionkey: result[1]
  97. })
  98. this.getEwm();
  99. })
  100. }
  101. })