diff --git a/pages/goodsDetail/index.vue b/pages/goodsDetail/index.vue index ff82838..9b8ceec 100644 --- a/pages/goodsDetail/index.vue +++ b/pages/goodsDetail/index.vue @@ -163,19 +163,22 @@ methods: { getBackground() { let _this = this - _this.$http(_this.API.API_BILLBACKGROUND, { - type: 'goods' - }).then(res => { - let img = res.data.img_url - if (img) { - wx.getImageInfo({ - src: img, - success: function(sres) { - _this.backgroundImg = sres.path; - _this.isShowButtonCount++; - } - }) - } + return new Promise((resolve, reject) => { + _this.$http(_this.API.API_BILLBACKGROUND, { + type: 'goods' + }).then(res => { + let img = res.data.img_url + if (img) { + wx.getImageInfo({ + src: img, + success: function(sres) { + _this.backgroundImg = sres.path; + _this.isShowButtonCount++; + resolve(); // 成功回调 + } + }) + } + }).catch(err => reject(err)); }) }, //获取商品分享图 @@ -220,33 +223,36 @@ }) }, getWxCode() { - const token = this.$shared.createToken({ - user_id: this.userInfo.id, - goods_id: this.goods_id - }); - let options = { - route: 'goods', - pt: 2, - id: this.goods_id, - share_id: this.userInfo.id, - scene_code: this.$scene.V2_GOODS_SHAREPOSTER - } - this.generateKooken(token, options); // 谁分享了就生成一个token - - this.$http(this.API.API_WXBILL, { - scene: token, - page: 'pages/route/index', - width: '2800' - }).then(res => { - this.wxCode = res.data.base_url - if (this.wxCode) { - this.getwxCodeImg() + let that = this; + return new Promise((resolve, reject) => { + const token = that.$shared.createToken({ + user_id: that.userInfo.id, + goods_id: that.goods_id + }); + let options = { + route: 'goods', + pt: 2, + id: that.goods_id, + share_id: that.userInfo.id, + scene_code: that.$scene.V2_GOODS_SHAREPOSTER } + that.generateKooken(token, options); // 谁分享了就生成一个token + + that.$http(that.API.API_WXBILL, { + scene: token, + page: 'pages/route/index', + width: '2800' + }).then(res => { + that.wxCode = res.data.base_url + if (that.wxCode) { + that.getwxCodeImg(resolve, reject); // 将成功/失败回调传到下一个函数 + } + }).catch(err => reject()); }) }, //海报开始 //保存头像 - getwxCodeImg() { + getwxCodeImg(resolve, reject) { var imgSrc = this.wxCode; //base64编码 var save = wx.getFileSystemManager(); var number = Math.random(); @@ -257,10 +263,12 @@ success: res => { this.onceCode = wx.env.USER_DATA_PATH + '/pic' + number + '.jpg'; this.isShowButtonCount++; + resolve(); // 成功回调 console.log('二维码临时路径',this.onceCode) }, fail: err => { - console.log(err) + console.log(err); + reject(); // 失败回调 } }) }, @@ -317,7 +325,7 @@ context.draw(); //将生成好的图片保存到本地,需要延迟一会,绘制期间耗时 setTimeout(function() { - wx.hideToast(); + uni.hideLoading(); // 关闭loading wx.canvasToTempFilePath({ canvasId: 'mycanvas', success: function(res) { @@ -361,13 +369,19 @@ }, formSubmit() { var that = this; - that.getWxCode() - that.getBackground() - if(that.isShowButtonCount >= 2) { - that.createNewImg() - }else { - this.$msg('请稍后重试!') - } + uni.showLoading({title: '正在生成中'}); + Promise.all([that.getWxCode(), that.getBackground()]).then(res => { + that.createNewImg(); + }).catch(err => { + this.$msg('生成失败,请稍后重试!'); + }) + + + // if(that.isShowButtonCount >= 2) { + + // }else { + // this.$msg('请稍后重试!') + // } // that.$u.throttle(() => { // wx.showToast({ // title: '生成海报中...',