index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const QR = require('../../miniprogram_npm/qrcode-base64/index.js');
  2. import {
  3. ApiModel
  4. } from "../../utils/api";
  5. const _Http = new ApiModel();
  6. import {
  7. base64src
  8. } from './base64src';
  9. Component({
  10. /**
  11. * 组件的属性列表
  12. */
  13. properties: {
  14. title: {
  15. type: String
  16. },
  17. codeName: {
  18. type: String
  19. }
  20. },
  21. lifetimes: {
  22. attached: function () {
  23. _Http.basic({
  24. "accesstoken": wx.getStorageSync('userData').token,
  25. "classname": "customer.tagents.tagents",
  26. "method": "query_enterpriseAgentsMain",
  27. "content": {}
  28. }).then(res => {
  29. if (res.msg != '成功') return wx.showToast({
  30. title: res.msg,
  31. icon: "none"
  32. });
  33. let codeText = '';
  34. if (this.data.title == '商户二维码') {
  35. codeText = 'https://www.buwanjia.com/qrcode?type=shop&fbrand=' + res.data[0].fbrand + '&tagentsid=' + res.data[0].tagentsid;
  36. } else {
  37. codeText = 'https://www.buwanjia.com/qrcode?type=partner&fbrand=' + res.data[0].fbrand + '&tagentsid=' + res.data[0].tagentsid;
  38. };
  39. this.qrCode(codeText)
  40. });
  41. },
  42. detached: function () {
  43. // 在组件实例被从页面节点树移除时执行
  44. },
  45. },
  46. /**
  47. * 组件的初始数据
  48. */
  49. data: {
  50. qrcodeURL: "",
  51. },
  52. /**
  53. * 组件的方法列表
  54. */
  55. methods: {
  56. qrCode(codeText) {
  57. var imgData = QR.drawImg(codeText, {
  58. typeNumber: 4,
  59. errorCorrectLevel: 'M',
  60. size: 500
  61. })
  62. base64src(imgData, res => {
  63. this.setData({
  64. qrcodeURL: res
  65. });
  66. });
  67. },
  68. /* 预览大图 */
  69. preViewImage() {
  70. const url = [this.data.qrcodeURL];
  71. wx.previewImage({
  72. urls: url,
  73. })
  74. },
  75. /* 下载二维码 */
  76. download() {
  77. const that = this;
  78. wx.authorize({
  79. /* 这个就是保存相册的 */
  80. scope: 'scope.writePhotosAlbum',
  81. success() {
  82. /* 保存图片方法 */
  83. that.img();
  84. },
  85. complete(res) {
  86. /* 这里判断一下如果没有授权重新打开设置选项 */
  87. wx.getSetting({
  88. success(res) {
  89. if (!res.authSetting['scope.writePhotosAlbum']) {
  90. /* 打开设置的方法 */
  91. that.opensit();
  92. }
  93. }
  94. });
  95. }
  96. });
  97. },
  98. /* 授权提示 ,这里就是重复提示用户去授权*/
  99. opensit() {
  100. wx.showModal({
  101. content: '由于您还没有允许保存图片到您相册里,这无法进行分享操作点击确定去允许授权',
  102. success: function (res) {
  103. if (res.confirm) {
  104. /* 这个就是打开设置的API*/
  105. wx.openSetting({
  106. success(res) {
  107. console.log(res.authSetting);
  108. }
  109. });
  110. } else if (res.cancel) {
  111. wx.showModal({
  112. cancelText: '依然取消',
  113. confirmText: '重新授权',
  114. content: '很遗憾你点击了取消,这将无法进行分享操作,请慎重考虑',
  115. success: function (res) {
  116. if (res.confirm) {
  117. wx.openSetting({
  118. success(res) {
  119. console.log(res.authSetting);
  120. }
  121. });
  122. } else if (res.cancel) {
  123. console.log('用户不授权');
  124. }
  125. }
  126. });
  127. }
  128. }
  129. });
  130. },
  131. /* 特别注意要先获取图片信息在进行保存,不让保存不了 */
  132. img() {
  133. const that = this;
  134. /* 保存图片到相册 */
  135. wx.saveImageToPhotosAlbum({
  136. filePath: that.data.qrcodeURL,
  137. success: function () {
  138. console.log('save success');
  139. wx.showModal({
  140. title: '保存成功',
  141. content: '图片已成功保存到相册,快去分享到您的圈子吧',
  142. showCancel: false
  143. });
  144. },
  145. complete(res) {
  146. console.log(res);
  147. }
  148. });
  149. }
  150. }
  151. })