poster.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // pages/poster/poster.js
  2. const net = require("../../utils/net.js");
  3. const util = require("../../utils/util.js");
  4. const { api_share_qrCode } = require("../../utils/api.js");
  5. import Poster from "../../utils/poster.js";
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. template: null,
  12. postImgSrc: null,
  13. isShowQRPoster: true,
  14. isShowCanvas: false,
  15. isShowPostMask: false,
  16. posterData: null,
  17. shareQRImgSrc: null,
  18. shareQRImgLoad: false,
  19. poster: null,
  20. isShowPoster: true,
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. if (options && options.data) {
  27. const posterData = JSON.parse(options.data);
  28. wx.showLoading({
  29. title: "生成海报中...",
  30. });
  31. this._posterData = posterData;
  32. this.getBarcode();
  33. } else {
  34. util.showToast({ title: "程序员小哥哥努力开发中..." });
  35. }
  36. },
  37. getBarcode: function () {
  38. const { shareObj, id } = this._posterData;
  39. const userInfo = util.getStorageSync("USERINFO");
  40. let shareToken = id;
  41. if (userInfo && userInfo.userCode && userInfo.userCode.length) {
  42. shareToken += userInfo.userCode;
  43. }
  44. shareToken =
  45. shareToken.length > 32 ? shareToken.substring(0, 31) : shareToken; // 做容错判断,超过32位
  46. let params = {};
  47. params.url = api_share_qrCode;
  48. const path =
  49. shareObj && shareObj.path ? shareObj.path : "pages/newDetail/newDetail";
  50. params.data = {
  51. scene: shareToken,
  52. page: path,
  53. width: 150,
  54. };
  55. net
  56. .req(params, true)
  57. .then((data) => {
  58. if (data.qrImg && data.qrImg.length) {
  59. this.setData({
  60. posterData: this._posterData,
  61. postImgSrc: data.qrImg,
  62. isShowQRPoster: true,
  63. });
  64. } else {
  65. wx.hideLoading();
  66. util.showToast({ title: "程序员小哥哥努力开发中..." });
  67. }
  68. })
  69. .catch(() => {
  70. wx.hideLoading();
  71. util.showToast({ title: "程序员小哥哥努力开发中..." });
  72. });
  73. },
  74. posterCallack: function (arr) {
  75. if (!arr) return;
  76. arr.forEach((v, i, a) => {
  77. let { id, dataset } = v;
  78. if (id.indexOf("img") !== -1) {
  79. wx.getImageInfo({
  80. src: dataset.what,
  81. success: (res) => {
  82. v.dataset.uri = res.path;
  83. },
  84. });
  85. }
  86. });
  87. if (Array.isArray(arr) && arr.length) {
  88. this.setData(
  89. {
  90. isShowCanvas: true,
  91. isShowQRPoster: false,
  92. },
  93. () => {
  94. let palette = new Poster().palette(arr);
  95. this.setData({
  96. template: palette,
  97. });
  98. }
  99. );
  100. } else {
  101. util.showToast({ title: "程序员小哥哥努力开发中..." });
  102. }
  103. },
  104. onImgOK: function (e) {
  105. const canvasSrc = e.detail.path;
  106. wx.hideLoading();
  107. this.setData(
  108. {
  109. isShowCanvas: false,
  110. },
  111. () => {
  112. this.setData({
  113. shareQRImgSrc: canvasSrc,
  114. isShowPostMask: true,
  115. });
  116. this.saveImgAction();
  117. }
  118. );
  119. },
  120. saveImgAction: function () {
  121. const _this = this;
  122. wx.saveImageToPhotosAlbum({
  123. filePath: this.data.shareQRImgSrc,
  124. success(res) {
  125. // wx.showToast({
  126. // title: "海报保存成功",
  127. // icon: "none",
  128. // duration: 1000,
  129. // });
  130. _this.setData({
  131. isShowPostMask: false,
  132. });
  133. },
  134. fail() {
  135. wx.showModal({
  136. title: "授权失败",
  137. content: "是否打开小程序设置界面,完成对该小程序授权状态",
  138. success: function (res) {
  139. if (res.confirm) {
  140. wx.openSetting({
  141. success(res) { },
  142. });
  143. }
  144. },
  145. });
  146. },
  147. });
  148. },
  149. postMaskHide: function () {
  150. this.setData({
  151. isShowPostMask: false,
  152. });
  153. },
  154. shareQRImgLoad() {
  155. this.setData({
  156. shareQRImgLoad: true,
  157. });
  158. },
  159. handleFinishAction() {
  160. wx.navigateBack();
  161. },
  162. imgLoad() {
  163. this.setData({
  164. isShowPoster: false,
  165. });
  166. },
  167. });