poster.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * 名片海报生成工具
  3. * 使用 Canvas 2D 绘制名片海报
  4. */
  5. const W = 750
  6. const H = 780
  7. const PADDING = 40
  8. const roundRect = (ctx, x, y, w, h, r) => {
  9. ctx.beginPath()
  10. ctx.moveTo(x + r, y)
  11. ctx.lineTo(x + w - r, y)
  12. ctx.quadraticCurveTo(x + w, y, x + w, y + r)
  13. ctx.lineTo(x + w, y + h - r)
  14. ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h)
  15. ctx.lineTo(x + r, y + h)
  16. ctx.quadraticCurveTo(x, y + h, x, y + h - r)
  17. ctx.lineTo(x, y + r)
  18. ctx.quadraticCurveTo(x, y, x + r, y)
  19. ctx.closePath()
  20. }
  21. const loadImage = (src) => {
  22. return new Promise((resolve) => {
  23. if (!src) return resolve(null)
  24. const img = uni.createImage()
  25. img.src = src
  26. img.onload = () => resolve(img)
  27. img.onerror = () => resolve(null)
  28. })
  29. }
  30. const downloadImage = async (url) => {
  31. try {
  32. if (!url || !url.startsWith('http')) return url
  33. const res = await uni.downloadFile({ url })
  34. if (res.statusCode === 200) return res.tempFilePath
  35. return null
  36. } catch { return null }
  37. }
  38. const drawCircleAvatar = (ctx, img, cx, cy, r) => {
  39. ctx.save()
  40. ctx.beginPath()
  41. ctx.arc(cx, cy, r, 0, Math.PI * 2)
  42. ctx.clip()
  43. if (img) ctx.drawImage(img, cx - r, cy - r, r * 2, r * 2)
  44. ctx.restore()
  45. }
  46. const drawAvatarFallback = (ctx, cardInfo, cx, cy, r) => {
  47. ctx.save()
  48. ctx.beginPath()
  49. ctx.arc(cx, cy, r, 0, Math.PI * 2)
  50. ctx.fillStyle = '#4080FF'
  51. ctx.fill()
  52. ctx.strokeStyle = 'rgba(255,255,255,0.6)'
  53. ctx.lineWidth = 4
  54. ctx.stroke()
  55. const initial = cardInfo.nickName ? cardInfo.nickName.charAt(0) : '?'
  56. ctx.fillStyle = '#FFFFFF'
  57. ctx.font = `bold ${r}px sans-serif`
  58. ctx.textAlign = 'center'
  59. ctx.textBaseline = 'middle'
  60. ctx.fillText(initial, cx, cy)
  61. ctx.restore()
  62. }
  63. const drawContactRow = (ctx, emoji, text, x, y) => {
  64. if (!text) return
  65. // 图标背景圆
  66. ctx.save()
  67. ctx.beginPath()
  68. ctx.arc(x + 20, y + 20, 20, 0, Math.PI * 2)
  69. ctx.fillStyle = 'rgba(64, 128, 255, 0.08)'
  70. ctx.fill()
  71. ctx.restore()
  72. // Emoji
  73. ctx.font = '22px sans-serif'
  74. ctx.textAlign = 'center'
  75. ctx.textBaseline = 'middle'
  76. ctx.fillText(emoji, x + 20, y + 20)
  77. // 文字
  78. ctx.font = '26px sans-serif'
  79. ctx.textAlign = 'left'
  80. ctx.textBaseline = 'middle'
  81. ctx.fillStyle = '#555555'
  82. let display = text
  83. if (text.length > 24) display = text.substring(0, 22) + '...'
  84. ctx.fillText(display, x + 52, y + 20)
  85. }
  86. const fillTextWrap = (ctx, text, x, y, maxWidth, lineHeight) => {
  87. if (!text) return y
  88. ctx.textAlign = 'center'
  89. ctx.textBaseline = 'top'
  90. const chars = text.split('')
  91. let line = '', cy = y
  92. for (let i = 0; i < chars.length; i++) {
  93. const test = line + chars[i]
  94. if (ctx.measureText(test).width > maxWidth && i > 0) {
  95. ctx.fillText(line, x, cy)
  96. line = chars[i]
  97. cy += lineHeight
  98. } else {
  99. line = test
  100. }
  101. }
  102. ctx.fillText(line, x, cy)
  103. return cy + lineHeight
  104. }
  105. export const generateCardPoster = async (cardInfo = {}) => {
  106. return new Promise((resolve, reject) => {
  107. const query = uni.createSelectorQuery()
  108. query.select('#posterCanvas')
  109. .fields({ node: true, size: true })
  110. .exec(async (res) => {
  111. if (!res || !res[0]) {
  112. reject(new Error('Canvas 节点未找到'))
  113. return
  114. }
  115. const canvas = res[0].node
  116. const ctx = canvas.getContext('2d')
  117. const dpr = uni.getSystemInfoSync().pixelRatio || 2
  118. canvas.width = W * dpr
  119. canvas.height = H * dpr
  120. ctx.scale(dpr, dpr)
  121. // ===== 1. 渐变背景 =====
  122. const bgGrad = ctx.createLinearGradient(0, 0, 0, H)
  123. bgGrad.addColorStop(0, '#4A90E2')
  124. bgGrad.addColorStop(0.5, '#6FB3F2')
  125. bgGrad.addColorStop(1, '#B0E0E6')
  126. ctx.fillStyle = bgGrad
  127. ctx.fillRect(0, 0, W, H)
  128. // ===== 2. 装饰圆点 =====
  129. ctx.fillStyle = 'rgba(255,255,255,0.10)'
  130. ctx.beginPath(); ctx.arc(620, 160, 100, 0, Math.PI * 2); ctx.fill()
  131. ctx.beginPath(); ctx.arc(680, 320, 60, 0, Math.PI * 2); ctx.fill()
  132. ctx.beginPath(); ctx.arc(-20, 640, 80, 0, Math.PI * 2); ctx.fill()
  133. // ===== 3. 白色卡片 =====
  134. const cardX = PADDING, cardY = 60
  135. const cardW = W - PADDING * 2, cardH = 660
  136. ctx.save()
  137. ctx.shadowColor = 'rgba(0,0,0,0.08)'
  138. ctx.shadowBlur = 24
  139. ctx.shadowOffsetY = 6
  140. ctx.fillStyle = '#FFFFFF'
  141. roundRect(ctx, cardX, cardY, cardW, cardH, 24)
  142. ctx.fill()
  143. ctx.restore()
  144. // ===== 4. 内部名片卡片 =====
  145. const innerX = cardX + 32
  146. const innerY = cardY + 32
  147. const innerW = cardW - 64
  148. const innerH = cardH - 64
  149. ctx.save()
  150. ctx.shadowColor = 'rgba(0,0,0,0.05)'
  151. ctx.shadowBlur = 16
  152. ctx.shadowOffsetY = 3
  153. ctx.fillStyle = '#FFFFFF'
  154. roundRect(ctx, innerX, innerY, innerW, innerH, 16)
  155. ctx.fill()
  156. ctx.restore()
  157. // 背景图
  158. try {
  159. const cardBg = await loadImage('/static/image/home/usecard-bg.png')
  160. if (cardBg) {
  161. ctx.save()
  162. roundRect(ctx, innerX, innerY, innerW, innerH, 16)
  163. ctx.clip()
  164. ctx.drawImage(cardBg, innerX, innerY, innerW, innerH)
  165. ctx.restore()
  166. }
  167. } catch (e) {}
  168. // ===== 5. 头像(右上角)=====
  169. const avatarCx = innerX + innerW - 100
  170. const avatarCy = innerY + 70
  171. const avatarR = 60
  172. ctx.save()
  173. ctx.beginPath()
  174. ctx.arc(avatarCx, avatarCy, avatarR + 4, 0, Math.PI * 2)
  175. ctx.fillStyle = 'rgba(255,255,255,0.5)'
  176. ctx.fill()
  177. ctx.restore()
  178. let avatarImg = null
  179. if (cardInfo.avatar) {
  180. const localPath = await downloadImage(cardInfo.avatar)
  181. avatarImg = await loadImage(localPath)
  182. }
  183. if (avatarImg) {
  184. drawCircleAvatar(ctx, avatarImg, avatarCx, avatarCy, avatarR)
  185. } else {
  186. drawAvatarFallback(ctx, cardInfo, avatarCx, avatarCy, avatarR)
  187. }
  188. // ===== 6. 姓名 + 职位 + 公司 =====
  189. const nameX = innerX + 44
  190. const nameY = innerY + 50
  191. ctx.fillStyle = '#202020'
  192. ctx.font = 'bold 44px sans-serif'
  193. ctx.textAlign = 'left'
  194. ctx.textBaseline = 'top'
  195. const name = cardInfo.nickName || '用户'
  196. ctx.fillText(name, nameX, nameY)
  197. const nameW = ctx.measureText(name).width
  198. // 职位标签
  199. if (cardInfo.postName) {
  200. const tagX = nameX + nameW + 14
  201. ctx.font = '22px sans-serif'
  202. const tagTextW = ctx.measureText(cardInfo.postName).width
  203. const tagW = tagTextW + 26
  204. ctx.fillStyle = 'rgba(68, 110, 255, 0.10)'
  205. roundRect(ctx, tagX, nameY + 6, tagW, 34, 8)
  206. ctx.fill()
  207. ctx.fillStyle = '#446EFF'
  208. ctx.textAlign = 'left'
  209. ctx.textBaseline = 'top'
  210. ctx.fillText(cardInfo.postName, tagX + 13, nameY + 11)
  211. }
  212. // 公司名称(最大宽度避开右上方头像)
  213. ctx.fillStyle = '#666666'
  214. ctx.font = '26px sans-serif'
  215. ctx.textAlign = 'left'
  216. ctx.textBaseline = 'top'
  217. const companyMaxW = innerW - 160
  218. const companyName = cardInfo.companyName || '公司名称'
  219. // 如果公司名太长,手动截断
  220. ctx.font = '26px sans-serif'
  221. if (ctx.measureText(companyName).width > companyMaxW) {
  222. let display = companyName
  223. while (ctx.measureText(display + '...').width > companyMaxW && display.length > 0) {
  224. display = display.substring(0, display.length - 1)
  225. }
  226. ctx.fillText(display + '...', innerX + 44, nameY + 60)
  227. } else {
  228. ctx.fillText(companyName, innerX + 44, nameY + 60)
  229. }
  230. // ===== 7. 联系方式 =====
  231. const contactY = innerY + 160
  232. const contactX = innerX + 44
  233. const contactGap = 60
  234. ctx.strokeStyle = '#EEEEEE'
  235. ctx.lineWidth = 1
  236. ctx.beginPath()
  237. ctx.moveTo(contactX, contactY)
  238. ctx.lineTo(innerX + innerW - 44, contactY)
  239. ctx.stroke()
  240. drawContactRow(ctx, '📞', cardInfo.phonenumber, contactX, contactY + 20)
  241. drawContactRow(ctx, '✉', cardInfo.email, contactX, contactY + 20 + contactGap)
  242. drawContactRow(ctx, '📍', cardInfo.companyAddress, contactX, contactY + 20 + contactGap * 2)
  243. // ===== 8. 导出 =====
  244. setTimeout(() => {
  245. uni.canvasToTempFilePath({
  246. canvas,
  247. success: (res) => resolve(res.tempFilePath),
  248. fail: (err) => reject(err)
  249. })
  250. }, 400)
  251. })
  252. })
  253. }
  254. export const savePosterToAlbum = (tempFilePath) => {
  255. return new Promise((resolve, reject) => {
  256. uni.authorize({
  257. scope: 'scope.writePhotosAlbum',
  258. success: () => {
  259. uni.saveImageToPhotosAlbum({
  260. filePath: tempFilePath,
  261. success: () => resolve(),
  262. fail: (err) => reject(err)
  263. })
  264. },
  265. fail: () => {
  266. uni.showModal({
  267. title: '提示',
  268. content: '需要授权相册权限才能保存名片',
  269. confirmText: '去设置',
  270. success: (res) => {
  271. if (res.confirm) uni.openSetting({})
  272. reject(new Error('未授权'))
  273. }
  274. })
  275. }
  276. })
  277. })
  278. }
  279. export default { generateCardPoster, savePosterToAlbum }