|
@@ -0,0 +1,215 @@
|
|
|
|
|
+<template>
|
|
|
|
|
+ <view v-if="visible" class="poster-mask" @click="close">
|
|
|
|
|
+ <view class="poster-panel" @click.stop>
|
|
|
|
|
+ <canvas canvas-id="salonPosterCanvas" id="salonPosterCanvas" class="poster-canvas"></canvas>
|
|
|
|
|
+ <view class="poster-tip">长按图片可保存或转发给好友</view>
|
|
|
|
|
+ <view class="poster-actions">
|
|
|
|
|
+ <button class="poster-btn poster-btn-ghost" @click="close">关闭</button>
|
|
|
|
|
+ <button class="poster-btn poster-btn-primary" :loading="saving" :disabled="saving" @click="save">保存海报</button>
|
|
|
|
|
+ </view>
|
|
|
|
|
+ </view>
|
|
|
|
|
+ </view>
|
|
|
|
|
+</template>
|
|
|
|
|
+
|
|
|
|
|
+<script>
|
|
|
|
|
+/**
|
|
|
|
|
+ * 沙龙分享海报:canvas 绘制标题/时间/地点/邀请码 + 小程序码。
|
|
|
|
|
+ * 小程序码由后端 /api/share/qrcode 生成(scene 携带沙龙 id 与邀请码);
|
|
|
|
|
+ * test-mode 或生成失败时 qrUrl 为空,降级为占位框,不阻塞保存。
|
|
|
|
|
+ */
|
|
|
|
|
+var CANVAS_W = 300
|
|
|
|
|
+var CANVAS_H = 480
|
|
|
|
|
+var QR_SIZE = 140
|
|
|
|
|
+var QR_X = (CANVAS_W - QR_SIZE) / 2
|
|
|
|
|
+var QR_Y = 225
|
|
|
|
|
+
|
|
|
|
|
+export default {
|
|
|
|
|
+ name: 'SharePoster',
|
|
|
|
|
+ props: {
|
|
|
|
|
+ visible: { type: Boolean, default: false },
|
|
|
|
|
+ title: { type: String, default: '' },
|
|
|
|
|
+ time: { type: String, default: '' },
|
|
|
|
|
+ place: { type: String, default: '' },
|
|
|
|
|
+ inviteCode: { type: String, default: '' },
|
|
|
|
|
+ qrUrl: { type: String, default: '' }
|
|
|
|
|
+ },
|
|
|
|
|
+ data() {
|
|
|
|
|
+ return { saving: false }
|
|
|
|
|
+ },
|
|
|
|
|
+ watch: {
|
|
|
|
|
+ visible: function(v) {
|
|
|
|
|
+ if (v) this.redraw()
|
|
|
|
|
+ },
|
|
|
|
|
+ qrUrl: function() {
|
|
|
|
|
+ if (this.visible) this.redraw()
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ methods: {
|
|
|
|
|
+ redraw() {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ this.$nextTick(function() {
|
|
|
|
|
+ setTimeout(function() {
|
|
|
|
|
+ self.draw()
|
|
|
|
|
+ }, 150)
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ draw() {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ var ctx = uni.createCanvasContext('salonPosterCanvas', self)
|
|
|
|
|
+ // 背景
|
|
|
|
|
+ ctx.setFillStyle('#FFFFFF')
|
|
|
|
|
+ ctx.fillRect(0, 0, CANVAS_W, CANVAS_H)
|
|
|
|
|
+ // 顶部品牌区
|
|
|
|
|
+ var gradient = ctx.createLinearGradient(0, 0, CANVAS_W, 0)
|
|
|
|
|
+ gradient.addColorStop(0, '#FC7A57')
|
|
|
|
|
+ gradient.addColorStop(1, '#F9494F')
|
|
|
|
|
+ ctx.setFillStyle(gradient)
|
|
|
|
|
+ ctx.fillRect(0, 0, CANVAS_W, 70)
|
|
|
|
|
+ ctx.setTextAlign('center')
|
|
|
|
|
+ ctx.setFillStyle('#FFFFFF')
|
|
|
|
|
+ ctx.setFontSize(18)
|
|
|
|
|
+ ctx.fillText('爱伴·AI之旅', CANVAS_W / 2, 32)
|
|
|
|
|
+ ctx.setFontSize(11)
|
|
|
|
|
+ ctx.fillText('沙龙活动邀请', CANVAS_W / 2, 52)
|
|
|
|
|
+ // 标题(最多两行)
|
|
|
|
|
+ ctx.setTextAlign('left')
|
|
|
|
|
+ ctx.setFillStyle('#2A2326')
|
|
|
|
|
+ ctx.setFontSize(17)
|
|
|
|
|
+ this.drawTitle(ctx, self.title || '沙龙活动', 24, 108, CANVAS_W - 48, 24, 2)
|
|
|
|
|
+ // 时间 / 地点
|
|
|
|
|
+ ctx.setFillStyle('#5A4F4B')
|
|
|
|
|
+ ctx.setFontSize(12)
|
|
|
|
|
+ ctx.fillText('时间:' + (self.time || '待定'), 24, 168)
|
|
|
|
|
+ ctx.fillText('地点:' + (self.place || '待定'), 24, 190)
|
|
|
|
|
+ // 分割线
|
|
|
|
|
+ ctx.setStrokeStyle('#F1F5F9')
|
|
|
|
|
+ ctx.setLineWidth(1)
|
|
|
|
|
+ ctx.beginPath()
|
|
|
|
|
+ ctx.moveTo(24, 206)
|
|
|
|
|
+ ctx.lineTo(CANVAS_W - 24, 206)
|
|
|
|
|
+ ctx.stroke()
|
|
|
|
|
+ // 小程序码
|
|
|
|
|
+ this.drawQr(ctx, function() {
|
|
|
|
|
+ // 邀请码
|
|
|
|
|
+ ctx.setTextAlign('center')
|
|
|
|
|
+ ctx.setFillStyle('#FC7A57')
|
|
|
|
|
+ ctx.setFontSize(13)
|
|
|
|
|
+ ctx.fillText(self.inviteCode ? '邀请码:' + self.inviteCode : '扫码查看活动详情', CANVAS_W / 2, 400)
|
|
|
|
|
+ ctx.setFillStyle('#94877F')
|
|
|
|
|
+ ctx.setFontSize(11)
|
|
|
|
|
+ ctx.fillText('长按识别小程序码,一起来成长', CANVAS_W / 2, 428)
|
|
|
|
|
+ ctx.setFillStyle('#CBD5E1')
|
|
|
|
|
+ ctx.setFontSize(10)
|
|
|
|
|
+ ctx.fillText('爱伴·AI之旅训练营', CANVAS_W / 2, 456)
|
|
|
|
|
+ ctx.draw()
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ drawTitle(ctx, text, x, y, maxWidth, lineHeight, maxLines) {
|
|
|
|
|
+ var chars = String(text).split('')
|
|
|
|
|
+ var line = ''
|
|
|
|
|
+ var lineY = y
|
|
|
|
|
+ var lines = 0
|
|
|
|
|
+ for (var i = 0; i < chars.length; i++) {
|
|
|
|
|
+ var test = line + chars[i]
|
|
|
|
|
+ if (ctx.measureText(test).width > maxWidth && line) {
|
|
|
|
|
+ lines++
|
|
|
|
|
+ if (lines >= maxLines) {
|
|
|
|
|
+ ctx.fillText(line.substring(0, line.length - 1) + '…', x, lineY)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ ctx.fillText(line, x, lineY)
|
|
|
|
|
+ line = chars[i]
|
|
|
|
|
+ lineY += lineHeight
|
|
|
|
|
+ } else {
|
|
|
|
|
+ line = test
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (line) ctx.fillText(line, x, lineY)
|
|
|
|
|
+ },
|
|
|
|
|
+ drawQr(ctx, done) {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ if (!self.qrUrl) {
|
|
|
|
|
+ self.drawQrPlaceholder(ctx)
|
|
|
|
|
+ done()
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ uni.getImageInfo({
|
|
|
|
|
+ src: self.qrUrl,
|
|
|
|
|
+ success: function(res) {
|
|
|
|
|
+ ctx.drawImage(res.path, QR_X, QR_Y, QR_SIZE, QR_SIZE)
|
|
|
|
|
+ done()
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: function() {
|
|
|
|
|
+ self.drawQrPlaceholder(ctx)
|
|
|
|
|
+ done()
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ drawQrPlaceholder(ctx) {
|
|
|
|
|
+ ctx.setFillStyle('#F8FAFC')
|
|
|
|
|
+ ctx.fillRect(QR_X, QR_Y, QR_SIZE, QR_SIZE)
|
|
|
|
|
+ ctx.setStrokeStyle('#E2E8F0')
|
|
|
|
|
+ ctx.setLineWidth(1)
|
|
|
|
|
+ ctx.strokeRect(QR_X, QR_Y, QR_SIZE, QR_SIZE)
|
|
|
|
|
+ ctx.setTextAlign('center')
|
|
|
|
|
+ ctx.setFillStyle('#94A3B8')
|
|
|
|
|
+ ctx.setFontSize(12)
|
|
|
|
|
+ ctx.fillText('小程序码', CANVAS_W / 2, QR_Y + QR_SIZE / 2 - 4)
|
|
|
|
|
+ ctx.setFontSize(10)
|
|
|
|
|
+ ctx.fillText('生成中 / 暂不可用', CANVAS_W / 2, QR_Y + QR_SIZE / 2 + 16)
|
|
|
|
|
+ },
|
|
|
|
|
+ close() {
|
|
|
|
|
+ this.$emit('close')
|
|
|
|
|
+ },
|
|
|
|
|
+ save() {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ if (this.saving) return
|
|
|
|
|
+ this.saving = true
|
|
|
|
|
+ uni.canvasToTempFilePath({
|
|
|
|
|
+ canvasId: 'salonPosterCanvas',
|
|
|
|
|
+ success: function(res) {
|
|
|
|
|
+ uni.saveImageToPhotosAlbum({
|
|
|
|
|
+ filePath: res.tempFilePath,
|
|
|
|
|
+ success: function() {
|
|
|
|
|
+ self.saving = false
|
|
|
|
|
+ uni.showToast({ title: '海报已保存到相册', icon: 'success' })
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: function(err) {
|
|
|
|
|
+ self.saving = false
|
|
|
|
|
+ var msg = (err && err.errMsg) || ''
|
|
|
|
|
+ if (msg.indexOf('auth') > -1 || msg.indexOf('deny') > -1) {
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: '需要相册权限',
|
|
|
|
|
+ content: '请在设置中开启「保存到相册」权限',
|
|
|
|
|
+ confirmText: '去设置',
|
|
|
|
|
+ success: function(r) {
|
|
|
|
|
+ if (r.confirm) uni.openSetting({})
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ } else {
|
|
|
|
|
+ uni.showToast({ title: '保存失败,请稍后重试', icon: 'none' })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: function() {
|
|
|
|
|
+ self.saving = false
|
|
|
|
|
+ uni.showToast({ title: '海报生成失败', icon: 'none' })
|
|
|
|
|
+ }
|
|
|
|
|
+ }, this)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+</script>
|
|
|
|
|
+
|
|
|
|
|
+<style scoped>
|
|
|
|
|
+.poster-mask { position: fixed; left: 0; top: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; z-index: 999; }
|
|
|
|
|
+.poster-panel { background: #FFF; border-radius: 20rpx; padding: 24rpx; display: flex; flex-direction: column; align-items: center; }
|
|
|
|
|
+.poster-canvas { width: 300px; height: 480px; border-radius: 12rpx; }
|
|
|
|
|
+.poster-tip { font-size: 22rpx; color: #94877F; margin: 16rpx 0 20rpx; }
|
|
|
|
|
+.poster-actions { display: flex; align-items: center; width: 100%; }
|
|
|
|
|
+.poster-btn { flex: 1; height: 76rpx; line-height: 76rpx; font-size: 28rpx; border-radius: 38rpx; border: none; padding: 0; }
|
|
|
|
|
+.poster-btn-ghost { background: #F1F5F9; color: #5A4F4B; margin-right: 20rpx; }
|
|
|
|
|
+.poster-btn-primary { background: #FC7A57; color: #FFF; }
|
|
|
|
|
+.poster-btn:active { opacity: 0.85; }
|
|
|
|
|
+</style>
|