share-poster.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <view v-if="visible" class="poster-mask" @click="close">
  3. <view class="poster-panel" @click.stop>
  4. <canvas canvas-id="salonPosterCanvas" id="salonPosterCanvas" class="poster-canvas"></canvas>
  5. <view class="poster-tip">长按图片可保存或转发给好友</view>
  6. <view class="poster-actions">
  7. <button class="poster-btn poster-btn-ghost" @click="close">关闭</button>
  8. <button class="poster-btn poster-btn-primary" :loading="saving" :disabled="saving" @click="save">保存海报</button>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. /**
  15. * 沙龙分享海报:canvas 绘制标题/时间/地点/邀请码 + 小程序码。
  16. * 小程序码由后端 /api/share/qrcode 生成(scene 携带沙龙 id 与邀请码);
  17. * test-mode 或生成失败时 qrUrl 为空,降级为占位框,不阻塞保存。
  18. */
  19. var CANVAS_W = 300
  20. var CANVAS_H = 480
  21. var QR_SIZE = 140
  22. var QR_X = (CANVAS_W - QR_SIZE) / 2
  23. var QR_Y = 225
  24. export default {
  25. name: 'SharePoster',
  26. props: {
  27. visible: { type: Boolean, default: false },
  28. title: { type: String, default: '' },
  29. time: { type: String, default: '' },
  30. place: { type: String, default: '' },
  31. inviteCode: { type: String, default: '' },
  32. qrUrl: { type: String, default: '' }
  33. },
  34. data() {
  35. return { saving: false }
  36. },
  37. watch: {
  38. visible: function(v) {
  39. if (v) this.redraw()
  40. },
  41. qrUrl: function() {
  42. if (this.visible) this.redraw()
  43. }
  44. },
  45. methods: {
  46. redraw() {
  47. var self = this
  48. this.$nextTick(function() {
  49. setTimeout(function() {
  50. self.draw()
  51. }, 150)
  52. })
  53. },
  54. draw() {
  55. var self = this
  56. var ctx = uni.createCanvasContext('salonPosterCanvas', self)
  57. // 背景
  58. ctx.setFillStyle('#FFFFFF')
  59. ctx.fillRect(0, 0, CANVAS_W, CANVAS_H)
  60. // 顶部品牌区
  61. var gradient = ctx.createLinearGradient(0, 0, CANVAS_W, 0)
  62. gradient.addColorStop(0, '#2E6FA8')
  63. gradient.addColorStop(1, '#1F4E79')
  64. ctx.setFillStyle(gradient)
  65. ctx.fillRect(0, 0, CANVAS_W, 70)
  66. ctx.setTextAlign('center')
  67. ctx.setFillStyle('#FFFFFF')
  68. ctx.setFontSize(18)
  69. ctx.fillText('爱伴-健康之旅', CANVAS_W / 2, 32)
  70. ctx.setFontSize(11)
  71. ctx.fillText('沙龙活动邀请', CANVAS_W / 2, 52)
  72. // 标题(最多两行)
  73. ctx.setTextAlign('left')
  74. ctx.setFillStyle('#1E3A5F')
  75. ctx.setFontSize(17)
  76. this.drawTitle(ctx, self.title || '沙龙活动', 24, 108, CANVAS_W - 48, 24, 2)
  77. // 时间 / 地点
  78. ctx.setFillStyle('#3B5A78')
  79. ctx.setFontSize(12)
  80. ctx.fillText('时间:' + (self.time || '待定'), 24, 168)
  81. ctx.fillText('地点:' + (self.place || '待定'), 24, 190)
  82. // 分割线
  83. ctx.setStrokeStyle('#E3ECF4')
  84. ctx.setLineWidth(1)
  85. ctx.beginPath()
  86. ctx.moveTo(24, 206)
  87. ctx.lineTo(CANVAS_W - 24, 206)
  88. ctx.stroke()
  89. // 小程序码
  90. this.drawQr(ctx, function() {
  91. // 邀请码
  92. ctx.setTextAlign('center')
  93. ctx.setFillStyle('#2E6FA8')
  94. ctx.setFontSize(13)
  95. ctx.fillText(self.inviteCode ? '邀请码:' + self.inviteCode : '扫码查看活动详情', CANVAS_W / 2, 400)
  96. ctx.setFillStyle('#6B7B8C')
  97. ctx.setFontSize(11)
  98. ctx.fillText('长按识别小程序码,一起来成长', CANVAS_W / 2, 428)
  99. ctx.setFillStyle('#C3D2DF')
  100. ctx.setFontSize(10)
  101. ctx.fillText('爱伴-健康之旅', CANVAS_W / 2, 456)
  102. ctx.draw()
  103. })
  104. },
  105. drawTitle(ctx, text, x, y, maxWidth, lineHeight, maxLines) {
  106. var chars = String(text).split('')
  107. var line = ''
  108. var lineY = y
  109. var lines = 0
  110. for (var i = 0; i < chars.length; i++) {
  111. var test = line + chars[i]
  112. if (ctx.measureText(test).width > maxWidth && line) {
  113. lines++
  114. if (lines >= maxLines) {
  115. ctx.fillText(line.substring(0, line.length - 1) + '…', x, lineY)
  116. return
  117. }
  118. ctx.fillText(line, x, lineY)
  119. line = chars[i]
  120. lineY += lineHeight
  121. } else {
  122. line = test
  123. }
  124. }
  125. if (line) ctx.fillText(line, x, lineY)
  126. },
  127. drawQr(ctx, done) {
  128. var self = this
  129. if (!self.qrUrl) {
  130. self.drawQrPlaceholder(ctx)
  131. done()
  132. return
  133. }
  134. uni.getImageInfo({
  135. src: self.qrUrl,
  136. success: function(res) {
  137. ctx.drawImage(res.path, QR_X, QR_Y, QR_SIZE, QR_SIZE)
  138. done()
  139. },
  140. fail: function() {
  141. self.drawQrPlaceholder(ctx)
  142. done()
  143. }
  144. })
  145. },
  146. drawQrPlaceholder(ctx) {
  147. ctx.setFillStyle('#F5FAFE')
  148. ctx.fillRect(QR_X, QR_Y, QR_SIZE, QR_SIZE)
  149. ctx.setStrokeStyle('#E3ECF4')
  150. ctx.setLineWidth(1)
  151. ctx.strokeRect(QR_X, QR_Y, QR_SIZE, QR_SIZE)
  152. ctx.setTextAlign('center')
  153. ctx.setFillStyle('#94A3B2')
  154. ctx.setFontSize(12)
  155. ctx.fillText('小程序码', CANVAS_W / 2, QR_Y + QR_SIZE / 2 - 4)
  156. ctx.setFontSize(10)
  157. ctx.fillText('生成中 / 暂不可用', CANVAS_W / 2, QR_Y + QR_SIZE / 2 + 16)
  158. },
  159. close() {
  160. this.$emit('close')
  161. },
  162. save() {
  163. var self = this
  164. if (this.saving) return
  165. this.saving = true
  166. uni.canvasToTempFilePath({
  167. canvasId: 'salonPosterCanvas',
  168. success: function(res) {
  169. uni.saveImageToPhotosAlbum({
  170. filePath: res.tempFilePath,
  171. success: function() {
  172. self.saving = false
  173. uni.showToast({ title: '海报已保存到相册', icon: 'success' })
  174. },
  175. fail: function(err) {
  176. self.saving = false
  177. var msg = (err && err.errMsg) || ''
  178. if (msg.indexOf('auth') > -1 || msg.indexOf('deny') > -1) {
  179. uni.showModal({
  180. title: '需要相册权限',
  181. content: '请在设置中开启「保存到相册」权限',
  182. confirmText: '去设置',
  183. success: function(r) {
  184. if (r.confirm) uni.openSetting({})
  185. }
  186. })
  187. } else {
  188. uni.showToast({ title: '保存失败,请稍后重试', icon: 'none' })
  189. }
  190. }
  191. })
  192. },
  193. fail: function() {
  194. self.saving = false
  195. uni.showToast({ title: '海报生成失败', icon: 'none' })
  196. }
  197. }, this)
  198. }
  199. }
  200. }
  201. </script>
  202. <style scoped>
  203. .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; }
  204. .poster-panel { background: #FFF; border-radius: 20rpx; padding: 24rpx; display: flex; flex-direction: column; align-items: center; }
  205. .poster-canvas { width: 300px; height: 480px; border-radius: 12rpx; }
  206. .poster-tip { font-size: 22rpx; color: #6B7B8C; margin: 16rpx 0 20rpx; }
  207. .poster-actions { display: flex; align-items: center; width: 100%; }
  208. .poster-btn { flex: 1; height: 76rpx; line-height: 76rpx; font-size: 28rpx; border-radius: 38rpx; border: none; padding: 0; }
  209. .poster-btn-ghost { background: #E3ECF4; color: #3B5A78; margin-right: 20rpx; }
  210. .poster-btn-primary { background: #2E6FA8; color: #FFF; }
  211. .poster-btn:active { opacity: 0.85; }
  212. </style>