share.js 15 KB
var e = require("../../utils/util.js");

Component({
    properties: {
        show: {
            type: Boolean,
            value: !1,
            observer: function(e, t) {
                this.check(e);
            }
        },
        data: {
            type: Object,
            value: {},
            observer: function(e, t) {}
        },
        goods: {
            type: Boolean,
            value: !1,
            observer: function(e, t) {}
        },
        isVideo: Boolean,
        hideWx: Boolean
    },
    ready: function() {},
    data: {
        imgWidth: 250,
        imgHeight: 420,
        bgImg: "",
        userImg: "",
        qr: "",
        active: "../../packageServicer/resources/images/v.png",
        bg: "./images/bg.png",
        shopBg: "../../packageServicer/resources/images/defaultBg.jpg",
        tempFilePath: "",
        name: "",
        listOfImg: [],
        tags: [],
        flag: !1
    },
    methods: {
        check: function(e) {
            var t = this, i = this.properties.data, s = this.properties.goods;
            e && (s || i.type || this.setData({
                name: i.name
            }), this.getImgData(function(e) {
                e.qr && t.draw(e);
            }));
        },
        noScroll: function() {},
        cancel: function() {
            this.triggerEvent("hide", !1);
        },
        saveImg: function() {
            var e = this;
            wx.saveImageToPhotosAlbum({
                filePath: this.data.tempFilePath,
                success: function(t) {
                    wx.showToast({
                        title: "保存成功"
                    }), e.triggerEvent("callBack");
                },
                fail: function(e) {
                    wx.showToast({
                        title: e
                    });
                }
            });
        },
        save: function() {
            var e = this, t = this;
            wx.getSetting({
                success: function(e) {
                    e.authSetting["scope.writePhotosAlbum"] ? t.saveImg() : wx.authorize({
                        scope: "scope.writePhotosAlbum",
                        success: function() {
                            t.saveImg();
                        },
                        fail: function(e) {
                            wx.showToast({
                                title: "请在小程序设置允许保存图片",
                                icon: "none",
                                mask: !0,
                                success: function() {}
                            });
                        }
                    });
                }
            }), wx.nextTick(function() {
                return e.cancel();
            });
        },
        draw: function(t) {
            var i = this, s = this.properties.data, a = wx.createCanvasContext("img", this), n = this.data.imgWidth, l = this.data.imgHeight;
            (0, e.getViewInfo)(".choose-list", function(t) {
                (0, e.getViewInfo)(".bg", function(s) {
                    (0, e.getViewInfo)(".info", function(e) {
                        console.log(e);
                        var a = (s.height - t.height - e.height) / 2;
                        i.setData({
                            viewTop: a
                        });
                    }, i);
                }, i);
            }, this), a.clearRect(0, 0, n, l);
            var o = this.properties.goods ? "draw_goods" : "draw_servicer_merchant";
            this.properties.isVideo && (o = "draw_video"), this[o](t, s, a, n, l, function(e) {
                a.draw(!0, setTimeout(function() {
                    return i.getCanvasImg();
                }, 500));
            });
        },
        draw_video: function(e, t, i, s, a, n) {
            if (console.log(e, t), i.setTextAlign("left"), i.setTextBaseline("top"), i.setFillStyle("#fff"), 
            i.fillRect(0, 0, s, a), i.save(), i.beginPath(), i.arc(34, 30, 20, 0, 2 * Math.PI), 
            i.clip(), i.drawImage(e.icon || "./images/car.png", 14, 10, 40, 40), i.restore(), 
            i.save(), i.setFillStyle("#333"), i.setFontSize(14), i.fillText(t.name, 60, 20, 176), 
            i.setFontSize(12), t.shareRecommend) {
                var l = t.shareRecommend, o = l.substring(0, 17);
                if (i.fillText(o, 14, 68, 224), l.length > 18) {
                    var r = t.shareRecommend.substring(18, 36);
                    i.fillText(r, 14, 87, 224);
                }
            } else i.fillText("养车不再盲目!根据爱车行驶里程和用车", 14, 68, 224), i.fillText("时长,为您推荐以下项目", 14, 87, 224);
            return i.drawImage(e.photo || "./images/cover.png", s / 2 - 100, 113, 200, 200), 
            i.setFillStyle("#999"), i.setFontSize(10), i.setTextAlign("center"), i.fillText("我正在用这个小程序,推荐你试试~!", s / 2, 316), 
            i.drawImage(e.qr || "./images/cover.png", 26, 352, 60, 60), i.setFillStyle("#333"), 
            i.setFontSize(12), i.setTextAlign("left"), i.fillText("长按二维码", 101, 367), i.fillText("识别小程序后查看更多", 101, 386), 
            n();
        },
        draw_servicer_merchant: function(e, t, i, s, a, n) {
            var l = this.data.bg, o = this.data.active;
            if (t.name = this.data.name, i.save(), t.type) {
                i.beginPath();
                var r = e.photo ? e.photo : this.data.shopBg;
                i.drawImage(r, 0, 0, s, 100);
            } else {
                i.beginPath(), i.drawImage(l, 0, 0, s, 100), i.save(), i.beginPath(), i.arc(55, 50, 27, 0, 2 * Math.PI), 
                i.clip(), i.drawImage(e.photo, 27, 23, 54, 54), i.restore(), i.drawImage(o, 68, 63, 14, 14), 
                i.save(), i.setFontSize(14), i.setTextBaseline("top"), i.setFillStyle("#fff"), i.fillText(t.name, 97, 40);
                var h = i.measureText(t.name).width + 112;
                i.setStrokeStyle("rgba(255,255,255,0.5)"), i.moveTo(h, 42.5), i.lineTo(h, 56.5), 
                i.stroke();
                var c = t.accountGroup ? 50 : 42;
                t.accountGroup && (i.setFontSize(8), i.fillText(t.accountGroup, h + 10, 33)), i.setFontSize(10), 
                i.fillText(t.duty, h + 10, c);
            }
            i.setFillStyle("#fff"), i.fillRect(0, 100, s, a), i.setFontSize(12), i.setFillStyle("#333"), 
            i.setTextBaseline("top"), t.merchantName && t.merchantName.length >= 19 && (t.merchantName = t.merchantName.substring(0, 17) + "..."), 
            i.fillText(t.merchantName, 10, 114), i.setFontSize(10), i.setFillStyle("#999"), 
            i.setTextBaseline("top"), t.type ? (t.address.length >= 23 && (t.address = t.address.substring(0, 21) + "..."), 
            i.fillText(t.address, 10, 140), i.fillText("营业时间:" + t.businessHours, 10, 158), 
            i.fillText("联系电话:" + t.phone, 10, 176)) : (i.fillText("电话:" + t.phone, 10, 140), 
            i.fillText("微信:" + t.wechatNum, 10, 158)), i.setFontSize(12), i.setFillStyle("#333"), 
            t.type ? i.fillText("营业范围", 10, 208) : i.fillText("服务印象", 10, 193);
            var g = 0, f = 0, m = [];
            !t.type && t.userLabelList && t.userLabelList.forEach(function(e) {
                return m.push(e.labelName);
            });
            var u = t.type ? t.mainBusiness : m;
            if (this.setData({
                tags: u
            }), u) {
                i.setFontSize(10), i.setFillStyle("#999");
                for (var d = 0; d < u.length; d++) {
                    var p = u[d];
                    i.setTextBaseline("top");
                    var x = 0 != d ? i.measureText(u[d - 1]).width : null, T = i.measureText(p).width;
                    if (0 != d) {
                        if (g > s - (x + 40) && f == (t.type ? 273 : 257)) break;
                        g + x + 16 < s - 10 && f != (t.type ? 273 : 257) ? g = g + x + 16 : (g = g > s - (T + 40) ? 10 : x + 16 + g, 
                        f = t.type ? 273 : 257);
                    } else g = 10, f = t.type ? 238 : 230;
                    if (g + T + 10 > s) {
                        var v = parseInt((g + T + 10 - s) / 12);
                        p = p.substring(0, p.length - v - 1) + "...";
                    }
                    i.fillText(p, g, f);
                }
            }
            i.drawImage(e.qr, s / 2 - 35, 305, 70, 70), i.setTextBaseline("top"), i.setFontSize(10);
            var w = t.type ? "长按识别小程序码,查看商户" : "长按识别小程序码,保存名片";
            return i.fillText(w, s / 2 - i.measureText(w).width / 2, 390), n("顾问、商铺");
        },
        draw_goods: function(e, t, i, s, a, n) {
            0 == t.marketType ? this.goods_normal(e, t, i, s, a, function(e) {
                return n("商品");
            }) : this.goods_show(e, t, i, s, a, function(e) {
                return n("商品");
            });
        },
        goods_normal: function(e, t, i, s, a, n) {
            i.save(), i.beginPath(), i.drawImage(e.photo, 0, 0, s, 250), i.setFillStyle("#fff"), 
            i.fillRect(0, 250, s, a), i.setTextAlign("left"), i.setFontSize(12), i.setFillStyle("#333"), 
            i.setTextBaseline("top"), t.merchantName && t.merchantName.length >= 18 && (t.merchantName = t.merchantName.substring(0, 16) + "..."), 
            i.fillText(t.shopName, 10, 261), i.setFillStyle("#F6512B");
            var l = t.interval ? 288 : 290;
            i.fillText("¥", 10, l);
            var o = i.measureText("¥").width, r = "";
            t.interval ? (i.setFontSize(16), i.fillText(t.interval, o + 10, 285), r = t.interval) : (i.setFontSize(20), 
            i.fillText(t.flashSalePrice, o + 10, 285), r = t.flashSalePrice);
            var h = i.measureText(r.toString()).width + o + 10;
            return i.setFontSize(8), i.setFillStyle("#999"), i.fillText("秒杀价", h + 6, 285), 
            i.fillText("¥" + t.retailPrice, h + 6, 295), i.restore(), i.setStrokeStyle("#999"), 
            i.moveTo(h + 6, 300), i.lineTo(h + 6 + i.measureText("¥" + t.retailPrice.toString()).width, 300), 
            i.stroke(), i.setFontSize(10), i.setFillStyle("#333"), t.merchantName.length >= 11 && (t.merchantName = t.merchantName.substring(0, 8) + "..."), 
            i.fillText(t.merchantName, 10, 348), i.setFontSize(8), i.setFillStyle("#999"), i.fillText("营业时间:" + t.businessHours, 10, 370), 
            t.address.length >= 10 && (t.address = t.address.substring(0, 11) + "..."), i.fillText("店铺地址:" + t.address, 10, 385), 
            i.fillText("联系电话:" + t.phone, 10, 400), i.drawImage(e.qr, 145, 305, 95, 90), i.fillText("长按识别小程序码", 160, 405), 
            i.fillText("立抢优惠", 178, 415), n("正常模式");
        },
        goods_show: function(e, t, i, s, a, n) {
            if (i.save(), i.beginPath(), i.drawImage(e.photo, 0, 0, s, 250), i.setFillStyle("#fff"), 
            i.fillRect(0, 250, s, a), i.setTextAlign("left"), i.setFontSize(12), i.setFillStyle("#333"), 
            i.setTextBaseline("top"), t.merchantName && t.merchantName.length >= 18 && (t.merchantName = t.merchantName.substring(0, 16) + "..."), 
            i.fillText(t.shopName, 10, 261), t.showPrice) {
                i.setFillStyle("#F34F3E"), i.fillText("¥", 10, 285);
                var l = i.measureText("¥").width;
                i.setFontSize(20), i.fillText(t.showPrice, l + 10, 280);
            }
            var o = t.shopTags;
            if (o && o.length > 0) {
                var r = 0, h = 0;
                i.setFontSize(8), i.setFillStyle("#F17B28");
                for (var c = 0; c < o.length; c++) {
                    var g = o[c];
                    i.setTextBaseline("top");
                    var f = 0 != c ? i.measureText(o[c - 1]).width : null, m = i.measureText(g).width;
                    if (0 != c) {
                        if (r > s - (f + 40) && 308 == h) break;
                        r + f + 16 < s - 10 && 308 != h ? r = r + f + 16 : (r = r > s - (m + 40) ? 10 : f + 16 + r, 
                        h = 308);
                    } else r = 10, h = 308;
                    if (r + m + 10 > s) {
                        var u = parseInt((r + m + 10 - s) / 12);
                        g = g.substring(0, g.length - u - 1) + "...";
                    }
                    i.fillText(g, r, h);
                }
            }
            return i.setFontSize(10), i.setFillStyle("#333"), t.merchantName.length >= 11 && (t.merchantName = t.merchantName.substring(0, 8) + "..."), 
            i.fillText(t.merchantName, 95, 328), i.setFontSize(8), i.setFillStyle("#999"), i.fillText("营业时间:" + t.businessHours, 95, 345), 
            t.address.length >= 10 && (t.address = t.address.substring(0, 11) + "..."), i.fillText("联系电话:" + t.phone, 95, 360), 
            i.fillText("店铺地址:" + t.address, 95, 375), i.drawImage(e.qr, 10, 330, 63, 62), i.fillText("长按识别小程序码 立抢优惠", 76, 400), 
            n("展示模式");
        },
        getCanvasImg: function() {
            var e = this;
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 2 * e.data.imgWidth,
                height: 2 * e.data.imgHeight,
                canvasId: "img",
                fileType: "jpg",
                quality: 1,
                success: function(t) {
                    wx.hideLoading(), e.triggerEvent("init", t.tempFilePath), e.setData({
                        tempFilePath: t.tempFilePath,
                        flag: !0
                    });
                }
            }, this);
        },
        getImgData: function(e) {
            var t = {}, i = this.properties.goods, s = this.properties.data, a = i ? s.goodsPhoto : s.type ? s.shopPhoto : s.photo;
            this.properties.isVideo && (a = s.photo);
            wx.showLoading({
                title: "加载中",
                mask: !0,
                success: function(i) {
                    wx.getImageInfo({
                        src: s.qr,
                        success: function(i) {
                            if (t.qr = i.path, !a) return e(t);
                            wx.getImageInfo({
                                src: a,
                                success: function(i) {
                                    return t.photo = i.path, e(t);
                                },
                                fail: function(e) {
                                    wx.hideLoading(), wx.showToast({
                                        icon: "none",
                                        title: "图片生成失败,请稍后再试"
                                    });
                                }
                            });
                        },
                        fail: function(e) {
                            wx.hideLoading(), wx.showToast({
                                icon: "none",
                                title: "图片生成失败,请稍后再试"
                            });
                        }
                    });
                }
            });
        }
    }
});