Sanakey
2021-01-31 53cc6f817625897935fb10c3845ad7be400f3036
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// component/authorize/authorize.js
import utils from '../../utils/util.js'
const app = getApp();
let imgDownloadObj = {};
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    qrCode:{
      type: String,
      value: '',
    },
    goodsItemImg:{
      type: String,
      value: '',
    },
    goodsItemName:{
      type: String,
      value: '',
    },
    goodsItemPrice:{
      type: Number,
      value: 0,
    },
    goodsItemSign:{
      type: String,
      value: '¥',
    },
    posterDes:{
      type: String,
      value: '我看上了这款商品',
    },
    posterDes2:{
      type: String,
      value: '帮我看看咋样啊~~',
    }
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    maskHidden:true
    // isAuthorize: true,
  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached() {
      imgDownloadObj = {
        isComplete:false
      };
     },
    // moved() { },
    detached() {
      imgDownloadObj = {
        isComplete:false
      };
     },
    // ready() {
      
    // }
  },
 
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    // show() {
    //   utils.isAuthorize(this);
    // },
    // hide() {
    // },
    // onUnload(){
    //   imgDownloadObj = {
    //     isComplete:false
    //   };
    // },
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    createCanvas(e){
      
      wx.showToast({
        title: '生成中...',
        icon: 'loading',
        mask:true,
        duration: 10000
      });
    
      if (imgDownloadObj.isComplete) {
        // wx.hideToast()
        // let posterCreate =  this.selectComponent('#posterCreate');
        // posterCreate.formSubmit(e);
        this.sendCommand(e);
      } else {
        // 下载图片到本地
        this.downloadFile();
        // utils.watchObj(imgDownloadObj,(prop,val)=>{
        //   console.log(JSON.stringify(imgDownloadObj)+prop+'值变化!!!','新值为'+val)
        utils.watchObj(imgDownloadObj,'isComplete',(val)=>{
          console.log(JSON.stringify(imgDownloadObj)+'isComplete值变化!!!','新值为'+val)
          if (imgDownloadObj.isComplete) {
            this.sendCommand(e);
          }
          // wx.hideToast()
          // let posterCreate =  this.selectComponent('#posterCreate');
          // posterCreate.formSubmit(e);
          
        })
        
      }
    
    },
 
    sendCommand(e){
      wx.hideToast()
      let posterCreate =  this.selectComponent('#posterCreate');
      posterCreate.formSubmit(e);
    },
    compressImg(img){
      // let img = this.data.goodsItemImg;
      let lastIndex = img.lastIndexOf('.');
      let ext = img.substring(lastIndex);
      let noext = img.slice(0,lastIndex);
      return noext.split('_')[0]+'_360x360'+ext
    },
    downloadFile(){
      console.log(app.globalData.cardUserData)
      console.log('头像',app.globalData.cardUserData.Avatar)
      console.log('二维码',this.data.qrCode)
      console.log('商品',this.data.goodsItemImg,this.compressImg(this.data.goodsItemImg))
      let avatarUrl = wx.pro.downloadFile({
        url: app.globalData.cardUserData.Avatar
      }).then((res)=>{
        if (res.statusCode === 200) {
          // imgDownloadSum++;
          console.log('......头像',res.tempFilePath)
          this.setData({
            avatarUrl: res.tempFilePath
          })
        }
      })
      let goodsImg = wx.pro.downloadFile({
        url: this.compressImg(this.data.goodsItemImg)
      }).then((res)=>{
        if (res.statusCode === 200) {
          // imgDownloadSum++;
          console.log('......商品',res.tempFilePath)
          this.setData({
            goodsImg: res.tempFilePath
          })
        }
      })
      let qrCodeImg = wx.pro.downloadFile({
        url: this.data.qrCode
      }).then((res)=>{
        if (res.statusCode === 200) {
          console.log('......二维码',res.tempFilePath)
          // imgDownloadSum++;
          this.setData({
            qrCodeImg: res.tempFilePath
          })
        }
      })
 
      Promise.all([avatarUrl,goodsImg,qrCodeImg]).then((res)=>{
 
        imgDownloadObj.isComplete = true;
 
        // imgDownloadObj.isComplete = true;
      }).catch((err)=>{
        // this.getPosterQrCode();
        console.log('请检查名片头像、公司logo等图片是否已经配置',err)
        // \n 点击确定重试,点击取消关闭弹窗。
        wx.hideToast();
        wx.showModal({
          title:'提示',
          content:`${err.errMsg}`,
          showCancel:false,
          success:(res)=>{
            // if (res.confirm) {
            //   console.log('用户点击确定')
            //   wx.showLoading()
            //   this.downloadFile()
            // } else if (res.cancel) {
            //   console.log('用户点击取消')
            // }
              
          }
        })
 
      })
      
    },
 
    createNewImg (e) {
      console.log('createNewImg',e)
      let posterCreate =  this.selectComponent('#posterCreate');
      let goodsItemName = this.data.goodsItemName;
      let context = e.detail.context;
      // let childComp = e.detail.childComp;
      // let context = wx.createCanvasContext('mycanvas');
      context.clearRect(0, 0, 375, 667);
      context.setFillStyle("#fff");
      context.fillRect(0, 0, 375, 667)
      // let path = "../../imgs/gobg.png";
      // //不知道是什么原因,手机环境能正常显示
      // context.drawImage(path, 0, 0, 375, 183);
      let avatarImg = this.data.avatarUrl;
      console.log(avatarImg,"avatarImg")
      // let path2 = "../../imgs/chengzhong.png";
      let goodsImg = this.data.goodsImg;
      let qrCodeImg = this.data.qrCodeImg;
      console.log('将生成好的图片保存到本地',avatarImg,goodsImg,qrCodeImg)
      // let path5 = "../../imgs/computer.png";
      // context.drawImage(path2, 126, 186, 120, 120);
      // context.save(); // 保存当前context的状态
 
      //绘制文字
      context.beginPath()
      context.setFontSize(16);
      context.setFillStyle('#333333');
      context.setTextAlign('left');
      context.fillText(this.data.posterDes, 140, 50);
      context.stroke();
      context.setFontSize(16);
      context.setFillStyle('#333333');
      context.setTextAlign('left');
      context.fillText(this.data.posterDes2, 140, 85);
      context.stroke();
      context.closePath();
      //绘制商品
      context.drawImage(goodsImg, 48, 125, 280, 280);
      // 商品文字
      context.beginPath();
      if (goodsItemName) {
        context.setFontSize(16);
        context.setFillStyle('#333333');
        context.setTextAlign('left');
        var result = posterCreate.breakLinesForCanvas(context,
          goodsItemName.slice(0,40),
          220, 14);
        var lineHeight = 25;
        result.forEach(function(line, index) {
            context.fillText(line, 50, lineHeight * index + 435);
            // context.fillText("商品描述", 50, 445);
        });
      }
      
      
      if (this.data.goodsItemPrice) {
        context.stroke();
        context.setFontSize(32);
        context.setFillStyle('#f23232');
        context.setTextAlign('left');
        context.fillText(this.data.goodsItemSign+this.data.goodsItemPrice, 45, 495);
        context.stroke();
      }
      
      
      context.closePath();
  
      // 绘制分割线
      context.beginPath()
      context.strokeStyle='#ddd'
      context.moveTo(10, 520)
      context.lineTo(365, 520)
      context.stroke()
      context.closePath();
      //绘制code码
      // context.setFontSize(40);
      // context.setFillStyle('#ffe200');
      // context.setTextAlign('center');
      // context.fillText(that.data.code, 185, 435);
      // context.stroke();
      //绘制左下角二维码
      context.drawImage(qrCodeImg, 20, 530, 120, 120);
  
      //绘制右下角扫码提示语
      // context.drawImage(path5, 248, 578, 90, 25);
      context.setFontSize(16);
      context.setFillStyle('#333');
      context.setTextAlign('left');
      context.fillText("长按识别小程序码 即可查看~",150, 600);
      context.stroke();
      //绘制头像
      context.arc(80, 60, 40, 0, 2 * Math.PI) //画出圆
      context.strokeStyle = "#ffe200";
      context.clip(); //裁剪上面的圆形
      context.drawImage(avatarImg, 40, 20, 80, 80); // 在刚刚裁剪的园上画图
      context.draw();
      //将生成好的图片保存到本地,需要延迟一会,绘制期间耗时
      setTimeout(()=> {
        posterCreate.createPoster();
        // console.log('将生成好的图片保存到本地',avatarImg,goodsImg,qrCodeImg)
      }, 300);
    },
    
  }
})