// pages/poster/poster.js const net = require("../../utils/net.js"); const util = require("../../utils/util.js"); const { api_share_qrCode } = require("../../utils/api.js"); import Poster from "../../utils/poster.js"; Page({ /** * 页面的初始数据 */ data: { template: null, postImgSrc: null, isShowQRPoster: true, isShowCanvas: false, isShowPostMask: false, posterData: null, shareQRImgSrc: null, shareQRImgLoad: false, poster: null, isShowPoster: true, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options && options.data) { const posterData = JSON.parse(options.data); wx.showLoading({ title: "生成海报中...", }); this._posterData = posterData; this.getBarcode(); } else { util.showToast({ title: "程序员小哥哥努力开发中..." }); } }, getBarcode: function () { const { shareObj, id } = this._posterData; const userInfo = util.getStorageSync("USERINFO"); let shareToken = id; if (userInfo && userInfo.userCode && userInfo.userCode.length) { shareToken += userInfo.userCode; } shareToken = shareToken.length > 32 ? shareToken.substring(0, 31) : shareToken; // 做容错判断,超过32位 let params = {}; params.url = api_share_qrCode; const path = shareObj && shareObj.path ? shareObj.path : "pages/newDetail/newDetail"; params.data = { scene: shareToken, page: path, width: 150, }; net .req(params, true) .then((data) => { if (data.qrImg && data.qrImg.length) { this.setData({ posterData: this._posterData, postImgSrc: data.qrImg, isShowQRPoster: true, }); } else { wx.hideLoading(); util.showToast({ title: "程序员小哥哥努力开发中..." }); } }) .catch(() => { wx.hideLoading(); util.showToast({ title: "程序员小哥哥努力开发中..." }); }); }, posterCallack: function (arr) { if (!arr) return; arr.forEach((v, i, a) => { let { id, dataset } = v; if (id.indexOf("img") !== -1) { wx.getImageInfo({ src: dataset.what, success: (res) => { v.dataset.uri = res.path; }, }); } }); if (Array.isArray(arr) && arr.length) { this.setData( { isShowCanvas: true, isShowQRPoster: false, }, () => { let palette = new Poster().palette(arr); this.setData({ template: palette, }); } ); } else { util.showToast({ title: "程序员小哥哥努力开发中..." }); } }, onImgOK: function (e) { const canvasSrc = e.detail.path; wx.hideLoading(); this.setData( { isShowCanvas: false, }, () => { this.setData({ shareQRImgSrc: canvasSrc, isShowPostMask: true, }); this.saveImgAction(); } ); }, saveImgAction: function () { const _this = this; wx.saveImageToPhotosAlbum({ filePath: this.data.shareQRImgSrc, success(res) { // wx.showToast({ // title: "海报保存成功", // icon: "none", // duration: 1000, // }); _this.setData({ isShowPostMask: false, }); }, fail() { wx.showModal({ title: "授权失败", content: "是否打开小程序设置界面,完成对该小程序授权状态", success: function (res) { if (res.confirm) { wx.openSetting({ success(res) { }, }); } }, }); }, }); }, postMaskHide: function () { this.setData({ isShowPostMask: false, }); }, shareQRImgLoad() { this.setData({ shareQRImgLoad: true, }); }, handleFinishAction() { wx.navigateBack(); }, imgLoad() { this.setData({ isShowPoster: false, }); }, });