绿满眶商城微信小程序-uniapp
zxl
4 天以前 c9928dd4f6d25e2339ea1400f59ec58674a927a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<template>
 
  <div class="index">
    <u-modal v-model="show" :show-title="false" :show-confirm-button="false" mask-close-able>
      <view class="slot-content">
        <image @click="downLoad()" class="img" :src="imgUrl" />
        <div class="canvas-hide">
          <!-- #ifdef MP-WEIXIN -->
          <canvas id="canvas" type="2d" style="width: 600px; height: 960px" />
          <!-- #endif -->
          <!-- #ifndef MP-WEIXIN -->
          <canvas canvas-id="canvas" id="canvas" style="width: 600px; height: 960px" />
          <!-- #endif -->
        </div>
      </view>
    </u-modal>
 
  </div>
</template>
<script>
import '@/components/uview-components/uview-ui'
// 引入绘制插件
import DrawPoster from "@/js_sdk/u-draw-poster";
 
export default {
  data: () => ({
    imgUrl: "", //绘制出来的图片路径
    show: false, //是否展示模态框
    dp: {}, //绘制的dp对象,用于存储绘制等一些方法。
    logo: require("@/pages/passport/static/logo-title.png"), //本地logo地址
  }),
 
  props: {
    /**
     * 父级传参的数据
     */
    res: {
      type: null,
      default: "",
    },
  },
  onUnload() {},
 
  methods: {
    /**
     * 解决微信小程序中图片模糊问题
     */
    // #ifdef MP-WEIXIN
    st2: (size) => size * 2,
    // #endif
 
    // #ifndef MP-WEIXIN
    st2: (size) => size,
    // #endif
 
    /**
     * 保存图片
     */
    downLoad() {
      uni.saveImageToPhotosAlbum({
        filePath: this.imgUrl,
        success: function () {
          uni.showToast({
            title: "保存成功!",
            icon: "none",
          });
        },
        fail: function () {
          uni.showToast({
            title: "保存失败,请稍后重试!",
            icon: "none",
          });
        },
      });
    },
 
    /**
     * 创建canvas
     */
    async init() {
      this.show = true;
      this.dp = await DrawPoster.build({
        selector: "canvas",
        componentThis: this,
        loading: true,
        debugging: true,
      });
      let dp = this.dp;
      // #ifdef MP-WEIXIN
      // 用于微信小程序中画布错乱问题
      dp.canvas.width = this.st2(600);
      dp.canvas.height = this.st2(960);
      // #endif
      this.draw(dp);
    },
 
    async draw(dp) {
      const { width, height, background, title } = this.res.container;
      const { code, img, price } = this.res.bottom;
 
      // /** 绘制背景 */
      await dp.draw((ctx) => {
        ctx.fillStyle = background;
        ctx.fillRoundRect(
          this.st2(0),
          this.st2(0),
          this.st2(width),
          this.st2(height),
          this.st2(12)
        );
        ctx.clip();
      });
      /** 绘制图片 */
      dp.draw(async (ctx) => {
        await Promise.all([
          // 绘制Logo
          ctx.drawImage(
            this.logo,
            this.st2(175),
            this.st2(0),
            this.st2(256),
            this.st2(144)
          ),
          // 中间图片
          ctx.drawImage(
            img,
            this.st2(100),
            this.st2(150),
            this.st2(400),
            this.st2(400)
          ),
          // 二维码
          ctx.drawImage(
            code,
            this.st2(39),
            this.st2(750),
            this.st2(150),
            this.st2(150)
          ),
        ]);
      });
 
      /** 绘制中间文字*/
      await dp.draw((ctx) => {
        ctx.fillStyle = "#333";
        ctx.font = `bold ${this.st2(24)}px PingFang SC`;
        ctx.textAlign = "center";
        ctx.fillWarpText({
          text: title,
          maxWidth: this.st2(500),
          x: this.st2(300),
          y: this.st2(600),
          layer: 1,
        });
 
        ctx.fillStyle = "#ff3c2a";
        ctx.font = `${this.st2(38)}px PingFang SC`;
        ctx.textAlign = "center";
        ctx.fillText(price, this.st2(300), this.st2(680));
      });
 
      // /** 绘制底部文字 */
      await dp.draw((ctx) => {
        ctx.fillStyle = "#666";
        ctx.font = `${this.st2(24)}px PingFang SC`;
        ctx.fillText("长按图片,识别二维码", this.st2(200), this.st2(866));
        ctx.fillStyle = "#666";
        ctx.font = `${this.st2(24)}px PingFang SC`;
        ctx.fillText("查看商品详情", this.st2(200), this.st2(900));
      });
 
      this.imgUrl = await dp.createImagePath();
 
      // console.log(posterImgUrl)
    },
  },
 
  async mounted() {
    this.init();
  },
};
</script>
 
<style lang="scss" scoped>
page,
.index {
  height: 100%;
}
.canvas-hide {
  /* 1 */
  position: fixed;
  right: 100vw;
  bottom: 100vh;
  /* 2 */
  z-index: -9999;
  /* 3 */
  opacity: 0;
}
.index {
  position: relative;
  text-align: center;
  background: rgba($color: grey, $alpha: 0.2);
}
 
image {
  display: block;
}
.img {
  width: 600rpx;
  height: 960rpx;
}
</style>