123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- // 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,
- });
- },
- });
|