index.js 5.5 KB

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