index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 shopData = {
  34. fbrand: res.data[0].fbrand,
  35. tagentsid: res.data[0].tagentsid,
  36. },
  37. codeText = '';
  38. const data = JSON.stringify(shopData);
  39. if (this.data.title == '商户二维码') {
  40. codeText = 'https://www.cnd3b.com/qr?type=shop';
  41. } else {
  42. codeText = 'https://www.cnd3b.com/qr?type=partner&fbrand=' + res.data[0].fbrand + '&tagentsid=' + res.data[0].tagentsid;
  43. };
  44. this.qrCode(codeText)
  45. }); */
  46. },
  47. detached: function () {
  48. // 在组件实例被从页面节点树移除时执行
  49. },
  50. },
  51. /**
  52. * 组件的初始数据
  53. */
  54. data: {
  55. qrcodeURL: "",
  56. },
  57. /**
  58. * 组件的方法列表
  59. */
  60. methods: {
  61. qrCode(codeText) {
  62. var imgData = QR.drawImg(codeText, {
  63. typeNumber: 4,
  64. errorCorrectLevel: 'M',
  65. size: 500
  66. })
  67. base64src(imgData, res => {
  68. this.setData({
  69. qrcodeURL: res
  70. });
  71. });
  72. },
  73. /* 预览大图 */
  74. preViewImage() {
  75. const url = [this.data.qrcodeURL];
  76. wx.previewImage({
  77. urls: url,
  78. })
  79. },
  80. /* 下载二维码 */
  81. download() {
  82. const that = this;
  83. wx.authorize({
  84. /* 这个就是保存相册的 */
  85. scope: 'scope.writePhotosAlbum',
  86. success() {
  87. /* 保存图片方法 */
  88. that.img();
  89. },
  90. complete(res) {
  91. /* 这里判断一下如果没有授权重新打开设置选项 */
  92. wx.getSetting({
  93. success(res) {
  94. if (!res.authSetting['scope.writePhotosAlbum']) {
  95. /* 打开设置的方法 */
  96. that.opensit();
  97. }
  98. }
  99. });
  100. }
  101. });
  102. },
  103. /* 授权提示 ,这里就是重复提示用户去授权*/
  104. opensit() {
  105. wx.showModal({
  106. content: '由于您还没有允许保存图片到您相册里,这无法进行分享操作点击确定去允许授权',
  107. success: function (res) {
  108. if (res.confirm) {
  109. /* 这个就是打开设置的API*/
  110. wx.openSetting({
  111. success(res) {
  112. console.log(res.authSetting);
  113. }
  114. });
  115. } else if (res.cancel) {
  116. wx.showModal({
  117. cancelText: '依然取消',
  118. confirmText: '重新授权',
  119. content: '很遗憾你点击了取消,这将无法进行分享操作,请慎重考虑',
  120. success: function (res) {
  121. if (res.confirm) {
  122. wx.openSetting({
  123. success(res) {
  124. console.log(res.authSetting);
  125. }
  126. });
  127. } else if (res.cancel) {
  128. console.log('用户不授权');
  129. }
  130. }
  131. });
  132. }
  133. }
  134. });
  135. },
  136. /* 特别注意要先获取图片信息在进行保存,不让保存不了 */
  137. img() {
  138. const that = this;
  139. /* 保存图片到相册 */
  140. wx.saveImageToPhotosAlbum({
  141. filePath: that.data.qrcodeURL,
  142. success: function () {
  143. console.log('save success');
  144. wx.showModal({
  145. title: '保存成功',
  146. content: '图片已成功保存到相册,快去分享到您的圈子吧',
  147. showCancel: false
  148. });
  149. },
  150. complete(res) {
  151. console.log(res);
  152. }
  153. });
  154. }
  155. }
  156. })