wp.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <view class="box">
  3. <view style="padding-top: 100rpx;">
  4. <canvas canvas-id="mini_poster" style="width: 600rpx;height: 817rpx;margin-left: 73rpx;"></canvas><!-- :style="{ width: canvasW + 'px', height: canvasH + 'px' }" -->
  5. </view>
  6. <view style="height: 106rpx;"></view>
  7. <!-- <view class="footer">
  8. <view style="height: 106rpx; align-items: center; background-color: #333333;">
  9. <button style="background: transparent; height: 106rpx; line-height: 106rpx;" @tap="toSaveImage">保存名片</button>
  10. </view>
  11. </view> -->
  12. <view class="downloadBtn" @click="toSaveImage">保存图片</view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. mpWxQr: "http://dmsphoto.66km.com.cn/marketing/F520E658D4A649B88DF210EA41432A01.png",
  20. canvasW: 0,
  21. canvasH: 0,
  22. invest_areas: [{id: 1, name: "儿童教育"}, {id: 1, name: "少儿编程"}, {id: 1, name: "生物智能"}, {id: 1, name: "万物相连"}, {id: 1, name: "大数据"}],
  23. nickname: "",
  24. avatar: "",
  25. company: "",
  26. phone: "17610998099",
  27. email: "redoume@163.com",
  28. time: "2022.01.21~2022.01.2",
  29. finished: false,
  30. sellingPoint:'',
  31. }
  32. },
  33. computed: {
  34. showInvestAreas() {
  35. if (this.invest_areas) {
  36. const names = []
  37. this.invest_areas.forEach(item => {
  38. names.push(item.name)
  39. })
  40. return names.join(" ")
  41. }
  42. return '未设置'
  43. }
  44. },
  45. onLoad(opt) {
  46. //this.canvasW = uni.getSystemInfoSync().windowWidth
  47. //this.canvasH = this.calculateCanvasHeight()
  48. this.avatar=opt.img;
  49. this.nickname=opt.name;
  50. if(opt.sellingPoint){
  51. this.sellingPoint=opt.sellingPoint;
  52. }
  53. console.log(this.sellingPoint)
  54. if(this.nickname.length>18){
  55. this.nickname=this.nickname.substring(0,18)+'...'
  56. }
  57. this.canvasW =uni.upx2px(600)
  58. this.canvasH = uni.upx2px(827)
  59. this.toDrawCanvas()
  60. },
  61. methods: {
  62. toSaveImage() {
  63. if (!this.finished) {
  64. uni.showToast({
  65. title: '正在生成图片,稍后再试',
  66. icon: 'none'
  67. })
  68. return
  69. }
  70. const that = this
  71. uni.canvasToTempFilePath({
  72. canvasId: 'mini_poster',
  73. success: (res) => {
  74. uni.saveImageToPhotosAlbum({
  75. filePath: res.tempFilePath,
  76. success: () => {
  77. uni.showToast({
  78. title: '保存成功'
  79. })
  80. },
  81. fail() {
  82. uni.showToast({
  83. icon: 'none',
  84. title: '保存名片码失败'
  85. })
  86. }
  87. })
  88. },
  89. fail() {
  90. uni.showToast({
  91. icon: 'none',
  92. title: '保存名片码失败'
  93. })
  94. }
  95. })
  96. },
  97. async toDrawCanvas() {
  98. const padding = uni.upx2px(34)
  99. const cardHeight = uni.upx2px(380)
  100. const cw = this.canvasW - 2 * padding
  101. let ctx = uni.createCanvasContext('mini_poster', this)
  102. // 透明背景
  103. // ctx.setFillStyle('rgba(255, 255, 255, 0)')
  104. ctx.setFillStyle('#FFFFFF')
  105. ctx.fillRect(0, 0, this.canvasW, this.canvasH)
  106. // draw card round rect
  107. const r = uni.upx2px(12)
  108. this.drawRoundRect(ctx, padding, padding, cw, cardHeight, r, 2)
  109. // draw card content
  110. this.drawCard(ctx, padding, padding, cw, cardHeight)
  111. // draw avatar
  112. const av = uni.upx2px(540)
  113. const topimgh = uni.upx2px(317)
  114. //console.log(av)
  115. //console.log(topimgh)
  116. const hi = await this.downloadImage(this.avatar)
  117. if (hi.tempFilePath) {
  118. const x = uni.upx2px(30)
  119. const y = uni.upx2px(30)
  120. this.drawRoundRectAvatar(ctx, x, y, av, topimgh, r, hi.tempFilePath)
  121. }
  122. // draw hello info
  123. //this.drawHelloInfo(ctx, padding, padding + cardHeight + padding, cw)
  124. // draw qr code
  125. if (this.mpWxQr) {
  126. const hello = await this.downloadImage(this.mpWxQr)
  127. //const padding = uni.upx2px(34)
  128. //const av = uni.upx2px(180)
  129. //const totalH = this.calculateCanvasHeight()
  130. //console.log("totalH=="+totalH)
  131. console.log(hello.tempFilePath)
  132. var wxsx=uni.upx2px(68)
  133. var wxsy=uni.upx2px(649)
  134. var wxwidth=uni.upx2px(148)
  135. if (hello.tempFilePath) {
  136. ctx.drawImage(hello.tempFilePath, wxsx, wxsy, wxwidth,wxwidth)
  137. }
  138. }
  139. ctx.draw()
  140. this.finished = true
  141. },
  142. async drawCard(ctx, x, y, w, h) {
  143. // draw company
  144. //let vp = y + uni.upx2px(45)
  145. //const hp = x + uni.upx2px(52)
  146. let vp = uni.upx2px(400)
  147. const hp = uni.upx2px(52)
  148. ctx.setTextBaseline('center')
  149. const fz30 = uni.upx2px(32)
  150. ctx.fillStyle = '#212121'
  151. ctx.setFontSize(fz30)
  152. ctx.fillText(this.nickname, hp, vp)
  153. var timeX=hp;
  154. var timeY=vp+30
  155. const fz26 = uni.upx2px(26)
  156. ctx.setFillStyle('#3C3C3C')
  157. ctx.setFontSize(fz26)
  158. ctx.fillText(this.time, timeX, timeY)
  159. var sm=this.sellingPoint
  160. ctx.setFillStyle('#999999')
  161. ctx.setFontSize(fz26)
  162. var smy=timeY+20
  163. var maxwidth = uni.upx2px(550)
  164. if(sm.length>20){
  165. var sm1=sm.substring(0,20)
  166. ctx.fillText(sm1, timeX, smy)
  167. if(sm.length>40){
  168. var sm2=sm.substring(20,40)
  169. ctx.fillText(sm2, timeX, smy+20)
  170. var sm3=sm.substring(40,60)
  171. ctx.fillText(sm3, timeX, smy+40)
  172. }else{
  173. var sm2=sm.substring(20,40)
  174. ctx.fillText(sm2, timeX, smy+20)
  175. }
  176. }else{
  177. ctx.fillText(sm, timeX, smy)
  178. }
  179. var wemtis1='名额有限 先到先得'
  180. var wemtisx=uni.upx2px(240)
  181. var wemtisy=uni.upx2px(700)
  182. ctx.setFillStyle('#999999')
  183. const fz24 = uni.upx2px(24)
  184. ctx.setFontSize(fz24)
  185. ctx.fillText(wemtis1,wemtisx,wemtisy)
  186. var wemtis2='长按二维码识别参与活动'
  187. var wemtisy2=wemtisy+30
  188. ctx.fillText(wemtis2,wemtisx,wemtisy2)
  189. // draw nickname
  190. vp = vp + 30
  191. vp = vp + 42
  192. const iconW = uni.upx2px(30)
  193. const textH = hp + iconW + 6
  194. ctx.setFillStyle('#333333')
  195. ctx.setFontSize(fz30)
  196. vp = vp + 28
  197. ctx.setFillStyle('#333333')
  198. ctx.setFontSize(fz30)
  199. // ctx.fillText(this.showInvestAreas, textH, vp)
  200. const oPadding = uni.upx2px(34)
  201. const iPadding = uni.upx2px(40)
  202. const textW = w - textH + oPadding - iPadding
  203. //this.drawTextInOneLine(ctx, this.showInvestAreas, textH, vp, textW)
  204. vp = vp + 28
  205. ctx.setFillStyle('#333333')
  206. ctx.setFontSize(fz30)
  207. //ctx.fillText(this.phone ? this.phone : '', textH, vp)
  208. },
  209. async drawHelloInfo(ctx, x, y, w) {
  210. let vp = y + 12
  211. const hp = x
  212. ctx.setTextBaseline('top')
  213. const fz30 = uni.upx2px(30)
  214. ctx.setFillStyle('#333333')
  215. ctx.setFontSize(fz30)
  216. ctx.fillText('您好,', hp, vp)
  217. const text = "我是来自 " + this.company + ' 的投资人' + this.nickname
  218. const lines = this.drawTextInLines(ctx, text, hp, vp, w, 26)
  219. vp += 26 * lines + 26
  220. ctx.fillText('这是我的名片,请惠存。', hp, vp)
  221. vp += 26
  222. ctx.fillText('谢谢', hp, vp)
  223. vp += 26
  224. vp += 40
  225. ctx.setFillStyle('#F37231')
  226. ctx.fillRect(hp, vp, 20, 3)
  227. vp += 7
  228. ctx.setFillStyle('#333333')
  229. const fz28 = uni.upx2px(28)
  230. ctx.setFontSize(fz28)
  231. ctx.fillText('长按识别二维码', hp, vp)
  232. vp += 20
  233. const ttt = '在大天使中查看我的更多信息'
  234. ctx.fillText(ttt, hp, vp)
  235. },
  236. calculateCanvasHeight() {
  237. // 400 is card height
  238. const ctx = uni.createCanvasContext('test_poster')
  239. const padding = uni.upx2px(34)
  240. const text = "我是来自 " + this.company + ' 的投资人' + this.nickname
  241. const rows = this.drawTextInLines(ctx, text, padding, 0, this.canvasW - 2 * padding, 26)
  242. const cardH = uni.upx2px(380)
  243. const h = padding * 2 + cardH + padding + 12 + 26 * 3 + rows * 26
  244. return h + 90
  245. },
  246. _drawRoundRect(ctx, x, y, w, h, r) {
  247. ctx.beginPath()
  248. // 左上角
  249. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)
  250. // 右上角
  251. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)
  252. // 右下角
  253. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)
  254. // 左下角
  255. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)
  256. ctx.closePath()
  257. // ctx.strokeStyle = '#D8D8D8';
  258. ctx.fillStyle = '#FFFFFF';
  259. // ctx.stroke()
  260. ctx.fill()
  261. },
  262. drawRoundRect(ctx, x, y, w, h, r, lineWidth) {
  263. ctx.save()
  264. //this._drawRoundRect(ctx, x, y, w, h, r)
  265. ctx.clip()
  266. // draw left border
  267. ctx.fillStyle = '#EEEEEE'
  268. var y=uni.upx2px(619)
  269. var bWidth=uni.upx2px(600)
  270. ctx.fillRect(0, y, bWidth, 2)
  271. ctx.restore()
  272. },
  273. drawRoundRectAvatar(ctx, x, y, w, h, r, imgPath) {
  274. ctx.save()
  275. this._drawRoundRect(ctx, x, y, w, h, r)
  276. ctx.clip()
  277. ctx.drawImage(imgPath, x, y, w, h)
  278. ctx.restore()
  279. },
  280. // TODO: measureText not works in App
  281. drawTextInLines(ctx, t, x, y, w, lineHeight) {
  282. const chr = t.split("")
  283. let temp = ''
  284. let row = []
  285. for (let i = 0; i < chr.length; i++) {
  286. if (ctx.measureText(temp).width < w) {
  287. //
  288. } else {
  289. row.push(temp)
  290. temp = ''
  291. }
  292. temp += chr[i]
  293. }
  294. row.push(temp)
  295. for (const i in row) {
  296. // i + 1 will be 11, 21,...
  297. ctx.fillText(row[i], x, (y + (i * 1 + 1) * lineHeight))
  298. }
  299. return row.length
  300. },
  301. // TODO: measureText not works in app
  302. drawTextInOneLine(ctx, t, x, y, w) {
  303. const chr = t.split('')
  304. let temp = ''
  305. for (let i = 0; i < chr.length; i++) {
  306. if (ctx.measureText(temp + '...').width <= w) {
  307. temp += chr[i]
  308. } else {
  309. temp = temp.substring(0, temp.length - 1)
  310. temp += '...'
  311. break
  312. }
  313. }
  314. ctx.fillText(temp, x, y)
  315. },
  316. downloadImage(url) {
  317. return new Promise((resolve, reject) => {
  318. uni.downloadFile({
  319. url: url,
  320. success: (res) => {
  321. return resolve(res)
  322. },
  323. fail: (err) => {
  324. return reject(err)
  325. }
  326. })
  327. })
  328. }
  329. }
  330. }
  331. </script>
  332. <style scoped>
  333. .box{
  334. width: 100%;
  335. height: 100vh;
  336. background: url('http://dmsphoto.66km.com.cn/thFiles/83BDAB66-DD76-4F3A-92B3-F6AF0BA57A9E.png');
  337. background-size: 100% 100%;
  338. }
  339. .downloadBtn{
  340. width: 329rpx;
  341. height: 92rpx;
  342. background: url('http://dmsphoto.66km.com.cn/thFiles/A0CF4704-69A8-4DAC-8943-75F98D1349DD.png');
  343. background-size: 100% 100%;
  344. line-height: 92rpx;
  345. text-align: center;
  346. font-size: 28rpx;
  347. font-family: PingFangSC-Medium, PingFang SC;
  348. font-weight: 500;
  349. color: #DC490A;
  350. margin: 0 auto;
  351. }
  352. </style>