| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <template>
- <view class="share-page">
- <LoadingSkeleton v-if="loading" :rows="2" />
- <ErrorState v-else-if="error" message="资料加载失败" @retry="loadPoster" />
- <EmptyState
- v-else-if="!inviteCode"
- icon="📄"
- title="暂无资料编码"
- desc="分享资料生成后会在此展示"
- action-text="返回"
- @action="goBack"
- />
- <template v-else>
- <!-- 统一用 canvas 合成海报:品牌 + 资料编码 + 小程序码(生产由后端生成,test-mode 为占位框) -->
- <view class="poster-card">
- <canvas canvas-id="posterCanvas" id="posterCanvas" class="poster-canvas"></canvas>
- </view>
- <view class="invite-row">
- <text class="invite-label">资料编码</text>
- <text class="invite-code">{{ inviteCode }}</text>
- <button class="copy-btn" @click="copyCode" :disabled="!inviteCode">复制</button>
- </view>
- <view class="invite-row share-text-row" v-if="shareText">
- <text class="invite-label">分享文案</text>
- <text class="share-text-preview" @click="copyShareText">点击复制分享文案 ›</text>
- </view>
- <button class="save-btn" @click="savePoster" :loading="saving" :disabled="saving || !inviteCode">保存海报图片</button>
- </template>
- </view>
- </template>
- <script>
- import { getMyPoster } from '@/utils/api.js'
- import { resolveAssetUrl } from '@/utils/share.js'
- import { guardPage } from '@/utils/guard.js'
- import LoadingSkeleton from '@/components/loading-skeleton.vue'
- import ErrorState from '@/components/error-state.vue'
- import EmptyState from '@/components/empty-state.vue'
- var CANVAS_W = 300
- var CANVAS_H = 450
- var QR_X = 85
- var QR_Y = 228
- var QR_SIZE = 130
- export default {
- components: { LoadingSkeleton, ErrorState, EmptyState },
- data() {
- return {
- inviteCode: '',
- posterUrl: '',
- shareText: '',
- saving: false,
- loading: false,
- error: false
- }
- },
- onLoad() {
- if (!guardPage()) return
- this.loadPoster()
- },
- onReady() {
- // canvas 就绪后先绘制占位海报,邀请码/小程序码返回后再重绘(仅正常态可见时绘制)
- if (!this.loading && this.inviteCode) {
- this.drawPoster()
- }
- },
- methods: {
- goBack() {
- var pages = getCurrentPages()
- if (pages.length > 1) {
- uni.navigateBack()
- } else {
- uni.redirectTo({ url: '/pages/index/index' })
- }
- },
- loadPoster() {
- var self = this
- self.loading = true
- self.error = false
- getMyPoster().then(function(resp) {
- var data = resp.data || {}
- self.inviteCode = data.inviteCode || ''
- self.posterUrl = resolveAssetUrl(data.posterUrl || '')
- self.shareText = data.shareText || ''
- self.loading = false
- if (self.inviteCode) {
- // 重新绘制带资料编码/小程序码的海报(canvas 此时已渲染)
- setTimeout(function() {
- self.drawPoster()
- }, 300)
- }
- }).catch(function() {
- self.loading = false
- self.error = true
- })
- },
- drawPoster() {
- var self = this
- var ctx = uni.createCanvasContext('posterCanvas', self)
- // 背景
- var gradient = ctx.createLinearGradient(0, 0, 0, CANVAS_H)
- gradient.addColorStop(0, '#E8F3FC')
- gradient.addColorStop(1, '#C3D2DF')
- ctx.setFillStyle(gradient)
- ctx.fillRect(0, 0, CANVAS_W, CANVAS_H)
- // 顶部品牌区
- ctx.setFillStyle('#1F4E79')
- ctx.fillRect(0, 0, CANVAS_W, 8)
- ctx.setFillStyle('#1E3A5F')
- ctx.setFontSize(22)
- ctx.setTextAlign('center')
- ctx.fillText('爱伴-健康之旅', CANVAS_W / 2, 56)
- ctx.setFillStyle('#3B5A78')
- ctx.setFontSize(13)
- ctx.fillText('资料分享', CANVAS_W / 2, 84)
- // 邀请码卡片
- ctx.setFillStyle('#FFFFFF')
- this.roundRect(ctx, 40, 112, CANVAS_W - 80, 96, 12)
- ctx.setFillStyle('#2E6FA8')
- ctx.setFontSize(14)
- ctx.fillText('资料编码', CANVAS_W / 2, 144)
- ctx.setFillStyle('#1E3A5F')
- ctx.setFontSize(self.inviteCode.length > 8 ? 22 : 34)
- ctx.fillText(self.inviteCode || '------', CANVAS_W / 2, 188)
- // 小程序码(带资料编码 scene,扫码查看分享资料)
- this.drawQr(ctx, function() {
- ctx.setTextAlign('center')
- ctx.setFillStyle('#1F4E79')
- ctx.setFontSize(14)
- ctx.fillText('扫码查看分享资料', CANVAS_W / 2, 386)
- ctx.setFillStyle('#6B7B8C')
- ctx.setFontSize(11)
- ctx.setFillStyle('#3B5A78')
- ctx.setFontSize(12)
- ctx.fillText('爱伴-健康之旅', CANVAS_W / 2, 432)
- ctx.draw()
- })
- },
- drawQr(ctx, done) {
- var self = this
- if (!self.posterUrl) {
- self.drawQrPlaceholder(ctx)
- done()
- return
- }
- uni.getImageInfo({
- src: self.posterUrl,
- 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('#FFFFFF')
- this.roundRect(ctx, QR_X, QR_Y, QR_SIZE, QR_SIZE, 8)
- ctx.setStrokeStyle('#E3ECF4')
- ctx.setLineWidth(1)
- ctx.strokeRect(QR_X, QR_Y, QR_SIZE, QR_SIZE)
- ctx.setTextAlign('center')
- ctx.setFillStyle('#C3D2DF')
- ctx.setFontSize(11)
- 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)
- },
- roundRect(ctx, x, y, w, h, r) {
- ctx.beginPath()
- ctx.moveTo(x + r, y)
- ctx.lineTo(x + w - r, y)
- ctx.arcTo(x + w, y, x + w, y + r, r)
- ctx.lineTo(x + w, y + h - r)
- ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
- ctx.lineTo(x + r, y + h)
- ctx.arcTo(x, y + h, x, y + h - r, r)
- ctx.lineTo(x, y + r)
- ctx.arcTo(x, y, x + r, y, r)
- ctx.closePath()
- ctx.fill()
- },
- copyCode() {
- if (!this.inviteCode) return
- uni.setClipboardData({
- data: this.inviteCode,
- success: function() {
- uni.showToast({ title: '邀请码已复制', icon: 'success' })
- }
- })
- },
- copyShareText() {
- if (!this.shareText) {
- uni.showToast({ title: '分享文案暂未生成', icon: 'none' })
- return
- }
- uni.setClipboardData({
- data: this.shareText,
- success: function() {
- uni.showToast({ title: '分享文案已复制', icon: 'success' })
- }
- })
- },
- savePoster() {
- var self = this
- if (this.saving) return
- this.saving = true
- uni.canvasToTempFilePath({
- canvasId: 'posterCanvas',
- 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' })
- }
- })
- }
- }
- }
- </script>
- <style scoped>
- .share-page { min-height: 100vh; background: #F5FAFE; padding: 24rpx 32rpx; }
- .poster-card { background: #FFF; border-radius: 16rpx; padding: 24rpx; margin-bottom: 24rpx; display: flex; justify-content: center; }
- .poster-canvas { width: 300px; height: 450px; border-radius: 12rpx; }
- .invite-row { background: #FFF; border-radius: 16rpx; padding: 32rpx; margin-bottom: 24rpx; display: flex; align-items: center; }
- .share-text-row { padding: 20rpx 32rpx; }
- .share-text-preview { flex: 1; font-size: 24rpx; color: #2E6FA8; margin-left: 24rpx; line-height: 1.5; }
- .share-text-preview:active { opacity: 0.7; }
- .invite-label { font-size: 28rpx; color: #1E3A5F; font-weight: 600; flex-shrink: 0; }
- .invite-code { flex: 1; font-size: 32rpx; color: #2E6FA8; font-weight: 700; font-family: monospace; margin-left: 24rpx; letter-spacing: 4rpx; }
- .copy-btn { flex-shrink: 0; height: 60rpx; line-height: 60rpx; padding: 0 32rpx; background: #2E6FA8; color: #FFF; font-size: 26rpx; border-radius: 30rpx; border: none; }
- .copy-btn[disabled] { background: #B4C2D0; color: #FFF; }
- .save-btn { width: 100%; height: 88rpx; line-height: 88rpx; background: #2E6FA8; color: #FFF; font-size: 30rpx; font-weight: 600; border-radius: 44rpx; border: none; margin-top: 8rpx; }
- .save-btn:active { opacity: 0.85; }
- </style>
|