index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view class="share-page">
  3. <LoadingSkeleton v-if="loading" :rows="2" />
  4. <ErrorState v-else-if="error" message="资料加载失败" @retry="loadPoster" />
  5. <EmptyState
  6. v-else-if="!inviteCode"
  7. icon="📄"
  8. title="暂无资料编码"
  9. desc="分享资料生成后会在此展示"
  10. action-text="返回"
  11. @action="goBack"
  12. />
  13. <template v-else>
  14. <!-- 统一用 canvas 合成海报:品牌 + 资料编码 + 小程序码(生产由后端生成,test-mode 为占位框) -->
  15. <view class="poster-card">
  16. <canvas canvas-id="posterCanvas" id="posterCanvas" class="poster-canvas"></canvas>
  17. </view>
  18. <view class="invite-row">
  19. <text class="invite-label">资料编码</text>
  20. <text class="invite-code">{{ inviteCode }}</text>
  21. <button class="copy-btn" @click="copyCode" :disabled="!inviteCode">复制</button>
  22. </view>
  23. <view class="invite-row share-text-row" v-if="shareText">
  24. <text class="invite-label">分享文案</text>
  25. <text class="share-text-preview" @click="copyShareText">点击复制分享文案 ›</text>
  26. </view>
  27. <button class="save-btn" @click="savePoster" :loading="saving" :disabled="saving || !inviteCode">保存海报图片</button>
  28. </template>
  29. </view>
  30. </template>
  31. <script>
  32. import { getMyPoster } from '@/utils/api.js'
  33. import { resolveAssetUrl } from '@/utils/share.js'
  34. import { guardPage } from '@/utils/guard.js'
  35. import LoadingSkeleton from '@/components/loading-skeleton.vue'
  36. import ErrorState from '@/components/error-state.vue'
  37. import EmptyState from '@/components/empty-state.vue'
  38. var CANVAS_W = 300
  39. var CANVAS_H = 450
  40. var QR_X = 85
  41. var QR_Y = 228
  42. var QR_SIZE = 130
  43. export default {
  44. components: { LoadingSkeleton, ErrorState, EmptyState },
  45. data() {
  46. return {
  47. inviteCode: '',
  48. posterUrl: '',
  49. shareText: '',
  50. saving: false,
  51. loading: false,
  52. error: false
  53. }
  54. },
  55. onLoad() {
  56. if (!guardPage()) return
  57. this.loadPoster()
  58. },
  59. onReady() {
  60. // canvas 就绪后先绘制占位海报,邀请码/小程序码返回后再重绘(仅正常态可见时绘制)
  61. if (!this.loading && this.inviteCode) {
  62. this.drawPoster()
  63. }
  64. },
  65. methods: {
  66. goBack() {
  67. var pages = getCurrentPages()
  68. if (pages.length > 1) {
  69. uni.navigateBack()
  70. } else {
  71. uni.redirectTo({ url: '/pages/index/index' })
  72. }
  73. },
  74. loadPoster() {
  75. var self = this
  76. self.loading = true
  77. self.error = false
  78. getMyPoster().then(function(resp) {
  79. var data = resp.data || {}
  80. self.inviteCode = data.inviteCode || ''
  81. self.posterUrl = resolveAssetUrl(data.posterUrl || '')
  82. self.shareText = data.shareText || ''
  83. self.loading = false
  84. if (self.inviteCode) {
  85. // 重新绘制带资料编码/小程序码的海报(canvas 此时已渲染)
  86. setTimeout(function() {
  87. self.drawPoster()
  88. }, 300)
  89. }
  90. }).catch(function() {
  91. self.loading = false
  92. self.error = true
  93. })
  94. },
  95. drawPoster() {
  96. var self = this
  97. var ctx = uni.createCanvasContext('posterCanvas', self)
  98. // 背景
  99. var gradient = ctx.createLinearGradient(0, 0, 0, CANVAS_H)
  100. gradient.addColorStop(0, '#E8F3FC')
  101. gradient.addColorStop(1, '#C3D2DF')
  102. ctx.setFillStyle(gradient)
  103. ctx.fillRect(0, 0, CANVAS_W, CANVAS_H)
  104. // 顶部品牌区
  105. ctx.setFillStyle('#1F4E79')
  106. ctx.fillRect(0, 0, CANVAS_W, 8)
  107. ctx.setFillStyle('#1E3A5F')
  108. ctx.setFontSize(22)
  109. ctx.setTextAlign('center')
  110. ctx.fillText('爱伴-健康之旅', CANVAS_W / 2, 56)
  111. ctx.setFillStyle('#3B5A78')
  112. ctx.setFontSize(13)
  113. ctx.fillText('资料分享', CANVAS_W / 2, 84)
  114. // 邀请码卡片
  115. ctx.setFillStyle('#FFFFFF')
  116. this.roundRect(ctx, 40, 112, CANVAS_W - 80, 96, 12)
  117. ctx.setFillStyle('#2E6FA8')
  118. ctx.setFontSize(14)
  119. ctx.fillText('资料编码', CANVAS_W / 2, 144)
  120. ctx.setFillStyle('#1E3A5F')
  121. ctx.setFontSize(self.inviteCode.length > 8 ? 22 : 34)
  122. ctx.fillText(self.inviteCode || '------', CANVAS_W / 2, 188)
  123. // 小程序码(带资料编码 scene,扫码查看分享资料)
  124. this.drawQr(ctx, function() {
  125. ctx.setTextAlign('center')
  126. ctx.setFillStyle('#1F4E79')
  127. ctx.setFontSize(14)
  128. ctx.fillText('扫码查看分享资料', CANVAS_W / 2, 386)
  129. ctx.setFillStyle('#6B7B8C')
  130. ctx.setFontSize(11)
  131. ctx.setFillStyle('#3B5A78')
  132. ctx.setFontSize(12)
  133. ctx.fillText('爱伴-健康之旅', CANVAS_W / 2, 432)
  134. ctx.draw()
  135. })
  136. },
  137. drawQr(ctx, done) {
  138. var self = this
  139. if (!self.posterUrl) {
  140. self.drawQrPlaceholder(ctx)
  141. done()
  142. return
  143. }
  144. uni.getImageInfo({
  145. src: self.posterUrl,
  146. success: function(res) {
  147. ctx.drawImage(res.path, QR_X, QR_Y, QR_SIZE, QR_SIZE)
  148. done()
  149. },
  150. fail: function() {
  151. self.drawQrPlaceholder(ctx)
  152. done()
  153. }
  154. })
  155. },
  156. drawQrPlaceholder(ctx) {
  157. ctx.setFillStyle('#FFFFFF')
  158. this.roundRect(ctx, QR_X, QR_Y, QR_SIZE, QR_SIZE, 8)
  159. ctx.setStrokeStyle('#E3ECF4')
  160. ctx.setLineWidth(1)
  161. ctx.strokeRect(QR_X, QR_Y, QR_SIZE, QR_SIZE)
  162. ctx.setTextAlign('center')
  163. ctx.setFillStyle('#C3D2DF')
  164. ctx.setFontSize(11)
  165. ctx.fillText('小程序码', CANVAS_W / 2, QR_Y + QR_SIZE / 2 - 4)
  166. ctx.setFontSize(10)
  167. ctx.fillText('生成中 / 暂不可用', CANVAS_W / 2, QR_Y + QR_SIZE / 2 + 16)
  168. },
  169. roundRect(ctx, x, y, w, h, r) {
  170. ctx.beginPath()
  171. ctx.moveTo(x + r, y)
  172. ctx.lineTo(x + w - r, y)
  173. ctx.arcTo(x + w, y, x + w, y + r, r)
  174. ctx.lineTo(x + w, y + h - r)
  175. ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
  176. ctx.lineTo(x + r, y + h)
  177. ctx.arcTo(x, y + h, x, y + h - r, r)
  178. ctx.lineTo(x, y + r)
  179. ctx.arcTo(x, y, x + r, y, r)
  180. ctx.closePath()
  181. ctx.fill()
  182. },
  183. copyCode() {
  184. if (!this.inviteCode) return
  185. uni.setClipboardData({
  186. data: this.inviteCode,
  187. success: function() {
  188. uni.showToast({ title: '邀请码已复制', icon: 'success' })
  189. }
  190. })
  191. },
  192. copyShareText() {
  193. if (!this.shareText) {
  194. uni.showToast({ title: '分享文案暂未生成', icon: 'none' })
  195. return
  196. }
  197. uni.setClipboardData({
  198. data: this.shareText,
  199. success: function() {
  200. uni.showToast({ title: '分享文案已复制', icon: 'success' })
  201. }
  202. })
  203. },
  204. savePoster() {
  205. var self = this
  206. if (this.saving) return
  207. this.saving = true
  208. uni.canvasToTempFilePath({
  209. canvasId: 'posterCanvas',
  210. success: function(res) {
  211. uni.saveImageToPhotosAlbum({
  212. filePath: res.tempFilePath,
  213. success: function() {
  214. self.saving = false
  215. uni.showToast({ title: '海报已保存到相册', icon: 'success' })
  216. },
  217. fail: function(err) {
  218. self.saving = false
  219. var msg = (err && err.errMsg) || ''
  220. if (msg.indexOf('auth') > -1 || msg.indexOf('deny') > -1) {
  221. uni.showModal({
  222. title: '需要相册权限',
  223. content: '请在设置中开启「保存到相册」权限',
  224. confirmText: '去设置',
  225. success: function(r) {
  226. if (r.confirm) {
  227. uni.openSetting({})
  228. }
  229. }
  230. })
  231. } else {
  232. uni.showToast({ title: '保存失败,请稍后重试', icon: 'none' })
  233. }
  234. }
  235. })
  236. },
  237. fail: function() {
  238. self.saving = false
  239. uni.showToast({ title: '海报生成失败', icon: 'none' })
  240. }
  241. })
  242. }
  243. }
  244. }
  245. </script>
  246. <style scoped>
  247. .share-page { min-height: 100vh; background: #F5FAFE; padding: 24rpx 32rpx; }
  248. .poster-card { background: #FFF; border-radius: 16rpx; padding: 24rpx; margin-bottom: 24rpx; display: flex; justify-content: center; }
  249. .poster-canvas { width: 300px; height: 450px; border-radius: 12rpx; }
  250. .invite-row { background: #FFF; border-radius: 16rpx; padding: 32rpx; margin-bottom: 24rpx; display: flex; align-items: center; }
  251. .share-text-row { padding: 20rpx 32rpx; }
  252. .share-text-preview { flex: 1; font-size: 24rpx; color: #2E6FA8; margin-left: 24rpx; line-height: 1.5; }
  253. .share-text-preview:active { opacity: 0.7; }
  254. .invite-label { font-size: 28rpx; color: #1E3A5F; font-weight: 600; flex-shrink: 0; }
  255. .invite-code { flex: 1; font-size: 32rpx; color: #2E6FA8; font-weight: 700; font-family: monospace; margin-left: 24rpx; letter-spacing: 4rpx; }
  256. .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; }
  257. .copy-btn[disabled] { background: #B4C2D0; color: #FFF; }
  258. .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; }
  259. .save-btn:active { opacity: 0.85; }
  260. </style>