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
| <script> import {base64src} from '../util'
export default { name: 'Poster', props: { value: { type: Boolean, default: false } }, data () { return { bg: 'https://img09.zhaopin.cn/2012/other/mobile/standout/renmai-speed/bg_wechat_post@2x.png', codeImg: '', localBg: '', posterTemp: '' } }, computed:{ btnText () { return this.authSavePoster ? '保存图片' : '去授权' }, openType () { return this.authSavePoster ? '' : 'openSetting' } }, watch: { value (val) { val && (this.posterTemp ? this.getAuth() : this.generatorPoster()) } },
methods: { downloadFile () { return new Promise((resolve, reject) => wx.downloadFile({ url: this.bg, success: (res) => { console.log('TCL: downloadFile -> res', res) this.localBg = res.tempFilePath resolve(res) }, fail (err) { reject(err) } })) }, getCode () { return new Promise(async (resolve, reject) => { const res = await getWXacode({ scene: `share=1234`, page: '' }) console.log('TCL: getCode -> res', res)
base64src(`data:image/png;base64,${res.data}`).then(res => { this.codeImg = res resolve() }).catch(err => { console.log('TCL: getCode -> err', err) }) }) }, generatorPoster () { wx.showLoading({ title: '下载海报模版中...', mask: true }) Promise.all([ this.downloadFile(), this.getCode() ]).then(res => { wx.showLoading({ title: '合成海报中...', mask: true }) const ctx = wx.createCanvasContext('myCanvas', this) ctx.drawImage(this.localBg, 0, 0, 300, 495) ctx.drawImage(this.codeImg, 100, 280, 100, 100) ctx.draw(false, (e) => { console.log('TCL: generatorPoster -> e', e) this.canvasToTemp() }) }).catch(() => { wx.hideLoading() wx.showToast({ title: '海报模版下载失败', icon: 'none' }) }) }, canvasToTemp () { wx.canvasToTempFilePath({ x: 0, y: 0, width: 300, height: 495, canvasId: 'myCanvas', success: (res) => { console.log('TCL: success -> res', res) this.posterTemp = res.tempFilePath this.getAuth() }, fail (err) { console.log('TCL: fail -> err', err) wx.showToast({ title: err.errMsg, icon: 'none' }) } }) },
savePoster () { wx.saveImageToPhotosAlbum({ filePath: this.posterTemp, success: res => { wx.showToast({ title: '海报已成功保存到相册', icon: 'none' }) this.$emit('input', false) }, fail (err) { this.authSavePoster = false wx.showToast({ title: '海报保存失败', icon: 'none' }) } }) }, getAuth () { wx.getSetting({ success: (res) => { console.log('TCL: getAuth -> res', res) const { authSetting } = res if (authSetting['scope.writePhotosAlbum'] === false) { console.log('去授权') this.authSavePoster = false wx.hideLoading() } else { this.savePoster() } } }) }, openSetting ({mp: {detail}}) { console.log('TCL: openSetting -> detail', detail) detail.authSetting['scope.writePhotosAlbum'] && (this.authSavePoster = true) }, btnTap () { this.openType || this.savePoster() } } } </script>
|