<template>
  <view class="draw-style1" :style="{ zoom: zoom }">
    <u-top-tips ref="uTips"></u-top-tips>
    <canvas
      canvas-id="firstCanvas"
      class="sty-box"
      @error="canvasIdErrorCallback"
    ></canvas>
  </view>
</template>

<script>
export default {
  props: {
    autoHeight: {
      type: String,
      default: "0",
    },
    images: {
      type: Array,
      default: [],
    },
    bgType: {
      type: Number,
      default: 0,
    },
    bgColor: {
      type: String,
      default: "",
    },
    info: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      zoom: 1,
      config: {
        images: [],
        bgType: 1,
        bgColor: "",
        info: {},
        loca_images: [],
      },
      ctx: null,
    };
  },
  watch: {
    images: {
      handler: function (val, oldval) {
        this.config.images = val;
        this.drawCtx();
      },
      deep: true,
    },
    bgType: {
      handler: function (val, oldval) {
        this.config.bgType = val;
        this.drawCtx();
      },
      deep: true,
    },
    bgColor: {
      handler: function (val, oldval) {
        this.config.bgColor = val;
        this.drawCtx();
      },
      deep: true,
    },
    info: {
      handler: function (val, oldval) {
        this.config.info = val;
        this.drawCtx();
      },
      deep: true,
    },
  },
  mounted() {
    console.log("in draw");

    this.zoom = (parseFloat(this.autoHeight) / 1344.0).toFixed(2);
    this.config.images = this.images;
    this.config.bgType = this.bgType;
    this.config.bgColor = this.bgColor;
    this.drawCtx();
  },
  onReady: function (e) {},
  methods: {
    colorRgb(color) {
      var sColor = color.toLowerCase();
      //十六进制颜色值的正则表达式
      var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
      // 如果是16进制颜色
      if (sColor && reg.test(sColor)) {
        if (sColor.length === 4) {
          var sColorNew = "#";
          for (var i = 1; i < 4; i += 1) {
            sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
          }
          sColor = sColorNew;
        }
        //处理六位的颜色值
        var sColorChange = [];
        for (var i = 1; i < 7; i += 2) {
          sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
        }
        return sColorChange.join(",");
      }
      return sColor;
    },
    saveImage() {
      uni.showLoading({
        title: "正在生成海报",
      });
      let that = this;
      uni.canvasToTempFilePath(
        {
          canvasId: "firstCanvas",
          success: (res) => {
            uni.hideLoading();
            uni.showLoading({
              title: "正在保存海报",
            });
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function () {
                uni.showToast({
                  title: "保存成功",
                });
              },
              fail: function (e) {
                that.$refs.uTips.show({
                  title: "保存失败，请稍后重试",
                  type: "error",
                  duration: "2300",
                });
              },
              complete: function () {
                uni.hideLoading();
              },
            });
          },
          fail: function (e) {
            console.log(e);
            that.$refs.uTips.show({
              title: "生成失败，请稍后重试",
              type: "error",
              duration: "2300",
            });
          },
        },
        this
      );
    },
    drawCtx() {
      let ctx = uni.createCanvasContext("firstCanvas", this);
      let that = this;
      if (!this.info.avatar_loca) {
        uni.getImageInfo({
          src: this.info.avatar,
          success: function (image) {
            that.drawBg(ctx);
            that.info.avatar_loca = image.path;
            that.drawAvatar(ctx);
            that.drawTitle(ctx);
            that.drawGoodPicture(ctx);
          },
        });
      } else {
        this.drawBg(ctx);
        this.drawAvatar(ctx);
        this.drawTitle(ctx);
        this.drawGoodPicture(ctx);
      }
    },
    drawGoodPicture(ctx) {
      this.roundRect(ctx, 25, 138, 700, 1182, 15, "#fff");
      if (this.config.images.length == 1) {
        this.drawOnePic(ctx);
      } else if (this.config.images.length == 2) {
        this.drawTowPic(ctx);
      } else if (this.config.images.length == 3) {
        this.drawThreePic(ctx);
      } else if (this.config.images.length == 4) {
        this.drawFourPic(ctx);
      } else if (this.config.images.length == 5) {
        this.drawFivePic(ctx);
      }
    },
    drawAvatar(ctx) {
      ctx.save();
      ctx.beginPath();
      ctx.arc(74, 58, 50, 0, Math.PI * 2);
      ctx.setFillStyle("#ffffff");
      ctx.fill();
      ctx.clip();
      ctx.drawImage(this.info.avatar_loca, 24, 8, 100, 100);
      ctx.restore();
    },
    drawTitle(ctx) {
      let text = this.info.nickname + "向您推荐一个好物";
      this.roundRect(ctx, 144, 34, text.length * 20, 55, 55, "#f5f5f5");
      ctx.fillStyle = "#333";
      ctx.font = "20px 'microsoft yahei'";
      ctx.fillText(text, 165, 70);
    },
    drawBg(ctx) {
      if (this.config.bgType == 1) {
        ctx.fillStyle = this.config.bgColor;
        ctx.fillRect(0, 0, 750, 1344);
      } else {
        let g = ctx.createLinearGradient(0, 0, 0, 1344);
        g.addColorStop(0, this.config.bgColor);
        console.log(`rgba(${this.colorRgb(this.config.bgColor)},0.3)`);
        g.addColorStop(1, `rgba(${this.colorRgb(this.config.bgColor)},0.3)`);
        ctx.fillStyle = g;
        ctx.fillRect(0, 0, 750, 1344);
      }
    },
    drawQrCode(ctx) {
      uni.getImageInfo({
        src: this.info.qrcode_url,
        success: function (image) {
          ctx.drawImage(image.path, 306, 1097, 138, 138);
          ctx.draw();
        },
      });
    },
    drawOnePic(ctx) {
      let that = this;
      if (!this.config.loca_images[0]) {
        uni.getImageInfo({
          src: this.config.images[0],
          success: function (image) {
            that.config.loca_images[0] = image.path;
            ctx.drawImage(image.path, 50, 407, 650, 650);
            that.otherDraw(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.loca_images[0], 50, 407, 650, 650);
        that.otherDraw(ctx);
      }
    },
    drawTowPic(ctx) {
      if (this.config.loca_images.length < 2) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            console.log(image);
            that.config.loca_images.push(image);
            that.drawTowPic(ctx);
          },
        });
      } else {
        ctx.fillStyle = "#000";
        ctx.fillRect(50, 407, 650, 325);
        var imgRect = this.coverImg(
          650,
          325,
          this.config.loca_images[0].width,
          this.config.loca_images[0].height
        );
        ctx.drawImage(
          this.config.loca_images[0].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          50,
          407,
          650,
          325
        );

        ctx.fillStyle = "#000";
        ctx.fillRect(50, 732, 650, 325);
        var imgRect = this.coverImg(
          650,
          325,
          this.config.loca_images[1].width,
          this.config.loca_images[1].height
        );
        ctx.drawImage(
          this.config.loca_images[1].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          50,
          732,
          650,
          325
        );
        this.otherDraw(ctx);
      }
    },
    drawThreePic(ctx) {
      if (this.config.loca_images.length < 3) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            that.config.loca_images.push(image);
            that.drawThreePic(ctx);
          },
        });
      } else {
        ctx.fillStyle = "#000";
        ctx.fillRect(50, 407, 650, 325);
        var imgRect = this.coverImg(
          650,
          325,
          this.config.loca_images[0].width,
          this.config.loca_images[0].height
        );
        ctx.drawImage(
          this.config.loca_images[0].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          50,
          407,
          650,
          325
        );
        ctx.drawImage(this.config.loca_images[1].path, 50, 732, 325, 325);
        ctx.drawImage(this.config.loca_images[2].path, 375, 732, 325, 325);
        this.otherDraw(ctx);
      }
    },
    drawFourPic(ctx) {
      if (this.config.loca_images.length < 4) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            that.config.loca_images.push(image);
            that.drawFourPic(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.loca_images[0].path, 50, 407, 325, 325);
        ctx.drawImage(this.config.loca_images[1].path, 375, 407, 325, 325);
        ctx.drawImage(this.config.loca_images[2].path, 50, 732, 325, 325);
        ctx.drawImage(this.config.loca_images[3].path, 375, 732, 325, 325);
        this.otherDraw(ctx);
      }
    },
    drawFivePic(ctx) {
      if (this.config.loca_images.length < 5) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            console.log(image);
            that.config.loca_images.push(image);
            that.drawFivePic(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.loca_images[0].path, 50, 407, 325, 325);
        ctx.drawImage(this.config.loca_images[1].path, 50, 732, 325, 325);

        ctx.fillStyle = "#000";
        ctx.fillRect(375, 407, 325, 216);
        var imgRect = this.coverImg(
          325,
          216,
          this.config.loca_images[2].width,
          this.config.loca_images[2].height
        );
        ctx.drawImage(
          this.config.loca_images[2].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          375,
          407,
          325,
          216
        );
        ctx.fillStyle = "#000";
        ctx.fillRect(375, 623, 325, 217);
        imgRect = this.coverImg(
          325,
          217,
          this.config.loca_images[3].width,
          this.config.loca_images[3].height
        );
        ctx.drawImage(
          this.config.loca_images[3].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          375,
          623,
          325,
          217
        );
        ctx.fillStyle = "#000";
        ctx.fillRect(375, 840, 325, 217);
        imgRect = this.coverImg(
          325,
          217,
          this.config.loca_images[4].width,
          this.config.loca_images[4].height
        );
        ctx.drawImage(
          this.config.loca_images[4].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          375,
          840,
          325,
          217
        );
        this.otherDraw(ctx);
      }
    },
    coverImg(box_w, box_h, source_w, source_h) {
      var sx = 0,
        sy = 0,
        sWidth = source_w,
        sHeight = source_h;
      if (source_w > source_h || (source_w == source_h && box_w < box_h)) {
        sWidth = (box_w * sHeight) / box_h;
        sx = (source_w - sWidth) / 2;
      } else if (
        source_w < source_h ||
        (source_w == source_h && box_w > box_h)
      ) {
        sHeight = (box_h * sWidth) / box_w;
        sy = (source_h - sHeight) / 2;
      }
      return {
        sx,
        sy,
        sWidth,
        sHeight,
      };
    },

    otherDraw(ctx) {
      this.showGoodName(ctx);
      this.showGoodPrice(ctx);
      this.showTips(ctx);
      this.drawQrCode(ctx);
    },
    showGoodPrice(ctx) {
      ctx.fillStyle = "#fc4a3b";
      ctx.font = "26px 'microsoft yahei'";
      let x = 55;
      let y = 308;
      ctx.fillText("￥", x, y);
      ctx.fillStyle = "#fc4a3b";
      ctx.font = "40px 'microsoft yahei'";
      ctx.fillText(this.info.min_price, 85, y);
    },
    showTips(ctx) {
      ctx.fillStyle = "gray";
      ctx.font = "24px 'microsoft yahei'";
      let x = 262;
      let y = 1271;
      ctx.fillText("长按识别小程序码进入", x, y);
    },
    showGoodName(ctx) {
      ctx.fillStyle = "#000000";
      ctx.font = "30px 'microsoft yahei'";
      let maxWidth = 616;
      let n = this.info.goods_name;
      let lines = 2;

      let x = 55;
      let y = 200;
      for (let i = 0; i < lines; i++) {
        let lineText = "";
        let currentWidth = 0;
        for (let x = 0; x < n.length; x++) {
          currentWidth += ctx.measureText(n[x]).width;
          if (currentWidth > maxWidth) {
            break;
          } else {
            lineText += n[x];
          }
        }
        ctx.fillText(lineText, x, y + i * 55);
        n = n.substring(lineText.length);
        if (n == "") {
          break;
        }
      }
      if (n != "") {
        ctx.fillText("...", x + maxWidth, y + 55);
      }
    },
    roundRect(ctx, x, y, w, h, r, c = "#fff") {
      if (w < 2 * r) {
        r = w / 2;
      }
      if (h < 2 * r) {
        r = h / 2;
      }

      ctx.beginPath();
      ctx.fillStyle = c;

      ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
      ctx.moveTo(x + r, y);
      ctx.lineTo(x + w - r, y);
      ctx.lineTo(x + w, y + r);

      ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
      ctx.lineTo(x + w, y + h - r);
      ctx.lineTo(x + w - r, y + h);

      ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
      ctx.lineTo(x + r, y + h);
      ctx.lineTo(x, y + h - r);

      ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
      ctx.lineTo(x, y + r);
      ctx.lineTo(x + r, y);

      ctx.fill();
      ctx.closePath();
    },
    canvasIdErrorCallback: function (e) {
      console.error(e.detail.errMsg);
    },
  },
};
</script>

<style>
.draw-style1 {
  width: 750px;
  height: 1344px;
  background: #fff;
}
.draw-style1 .sty-box {
  width: 100%;
  height: 100%;
}
.draw-style1 .sty-box .header {
  height: 90px;
  display: flex;
  align-items: center;
  margin-bottom: 40px;
}
.draw-style1 .sty-box .header .image {
  width: 90px;
  height: 90px;
  border-radius: 90px;
  margin-right: 20px;
}
.draw-style1 .sty-box .header .title {
  font-size: 20px;
  color: #000;
  line-height: 55px;
  height: 55px;
  border-radius: 55px;
  padding: 0 24px;
  background: #fff;
}
.draw-style1 .sty-box .content-img,
.draw-style1 .sty-box .content-img-one {
  width: 100%;
  height: 702px;
}
.draw-style1 .sty-box .info-box {
  padding: 28px 30px;
  display: flex;
  align-items: center;
  background: #fff;
}
.draw-style1 .sty-box .info-box .qrcode {
  width: 215px;
  height: 215px;
}
.draw-style1 .sty-box .info-box .left-info {
  width: 1px;
  flex: 1;
}
.draw-style1 .sty-box .info-box .left-info .good-name {
  font-size: 28px;
  color: #000;
  line-height: 1.5;
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}
.draw-style1 .sty-box .info-box .left-info .good-price {
  color: #fc4a3b;
  font-size: 40px;
  margin-bottom: 75px;
}
.draw-style1 .sty-box .info-box .left-info .tips {
  font-size: 24px;
  color: gray;
}
</style>
