wp.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <template>
  2. <view class="box">
  3. <view style="padding-top: 100rpx;padding-left: 73rpx;padding-right: 75rpx;">
  4. <view style="border-radius: 20rpx;">
  5. <canvas canvas-id="mini_poster" style="width: 600rpx;" :style="{ height: canvasH + 'px' }"></canvas><!-- :style="{ width: canvasW + 'px', height: canvasH + 'px' }" -->
  6. </view>
  7. </view>
  8. <view style="height: 106rpx;"></view>
  9. <!-- <view class="footer">
  10. <view style="height: 106rpx; align-items: center; background-color: #333333;">
  11. <button style="background: transparent; height: 106rpx; line-height: 106rpx;" @tap="toSaveImage">保存名片</button>
  12. </view>
  13. </view> -->
  14. <view class="downloadBtn" @click="toSaveImage">保存图片</view>
  15. <!-- <img :src="mpWxQr2" alt="" ref="image" class="wcl"> -->
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. mpWxQr: "https://img.qdbtl.cn/thFiles//material/85996BA2-24C5-4251-937D-2BFBCF19E6DD.png",
  23. canvasW: 0,
  24. canvasH: 0,
  25. invest_areas: [{id: 1, name: "儿童教育"}, {id: 1, name: "大数据"}],
  26. nickname: "",
  27. avatar: "",
  28. company: "",
  29. phone: "17610998099",
  30. email: "redoume@163.com",
  31. time: "2022.01.21~2022.01.2",
  32. finished: false,
  33. sellingPoint:'',
  34. userInfo:'',
  35. ext:'',
  36. mpWxQr2:'',
  37. mpWxQr3:'',
  38. mpWxQr4:'',
  39. ptImg:'https://img.qdbtl.cn/thFiles//material/85996BA2-24C5-4251-937D-2BFBCF19E6DD.png',
  40. ptImg2:'',
  41. groupType:'',
  42. }
  43. },
  44. computed: {
  45. showInvestAreas() {
  46. if (this.invest_areas) {
  47. const names = []
  48. this.invest_areas.forEach(item => {
  49. names.push(item.name)
  50. })
  51. return names.join(" ")
  52. }
  53. return '未设置'
  54. }
  55. },
  56. onLoad(opt) {
  57. //this.canvasW = uni.getSystemInfoSync().windowWidth
  58. //this.canvasH = this.calculateCanvasHeight()
  59. this.avatar=opt.img;
  60. //this.avatar='http://dmsphoto.66km.com.cn/marketing/B7511CA2-CC50-4F2C-867D-DB15783CE3C5.jpg';
  61. console.log(this.avatar.slice(0,5))
  62. /* if(this.avatar.slice(0,5)=='https'){
  63. }else{
  64. this.avatar=this.avatar.replace('http','https');
  65. } */
  66. this.time=opt.time
  67. console.log(this.avatar)
  68. this.nickname=opt.name;
  69. if(opt.sellingPoint&&opt.sellingPoint!='undefined'){
  70. this.sellingPoint=opt.sellingPoint;
  71. }
  72. console.log('this.sellingPoint==='+this.sellingPoint)
  73. //console.log(this.nickname+this.nickname.length)
  74. if(this.nickname.length>16){
  75. this.nickname=this.nickname.substring(0,16)+'...'
  76. }
  77. if(opt.groupType!=0){
  78. console.log(opt.groupType)
  79. this.groupType=opt.groupType;
  80. if(this.nickname.length>12){
  81. this.nickname=this.nickname.substring(0,12)+'...'
  82. }
  83. }
  84. //console.log(this.nickname+this.nickname.length)
  85. this.userInfo=this.$store.state.userInfo;
  86. this.ext=this.$common.getExtStoreId();
  87. this.canvasW =uni.upx2px(600)
  88. if(this.sellingPoint){
  89. this.canvasH = uni.upx2px(840)
  90. }else{
  91. this.canvasH = uni.upx2px(740)
  92. }
  93. var that=this;
  94. var type=opt.type
  95. if(type==2){
  96. var bizType=2;
  97. var path='pages/activity/jkDetail'
  98. }else{
  99. var bizType=1;
  100. var path='pages/shop/goodsDetail'
  101. }
  102. uni.showLoading({
  103. title: '加载中'
  104. })
  105. this.$request.httpBinary('openMall/shareQrCode', {
  106. bizType:bizType,
  107. path:path,//?id+this.id+'&shareId='+this.userInfo.openId,
  108. goodsId:opt.id,
  109. openId:this.userInfo.openId,
  110. unionId:this.ext.unionId
  111. },'GET').then(res => {
  112. //console.log(res)
  113. this.mpWxQr3=res;
  114. uni.hideLoading();
  115. //this.mpWxQr2= 'data:image/png;base64,'+uni.arrayBufferToBase64(res)
  116. // this.mpWxQr4= uni.arrayBufferToBase64(res)
  117. this.toDrawCanvas()
  118. })
  119. // this.$request.httpBinary('openMall/shareQrCode', {
  120. // bizType:1,
  121. // path:'pages/shop/goodsDetail',//?id+this.id+'&shareId='+this.userInfo.openId,
  122. // goodsId:opt.id,
  123. // openId:this.userInfo.openId,
  124. // unionId:this.ext.unionId
  125. // },'GET').then(res => {
  126. // //console.log(res)
  127. // this.mpWxQr2= 'data:image/png;base64,'+uni.arrayBufferToBase64(res)
  128. // // let theNode=uni.createSelectorQuery().select(".wcl")
  129. // // theNode.boundingClientRect((data)=>{
  130. // // console.log(data)
  131. // // // that.mpWxQr=data
  132. // // }).exec()
  133. // // this.mpWxQr = uni.createSelectorQuery().select('wcl')
  134. // this.toDrawCanvas()
  135. // })
  136. //return false;
  137. },
  138. methods: {
  139. toSaveImage() {
  140. if (!this.finished) {
  141. uni.showToast({
  142. title: '正在生成图片,稍后再试',
  143. icon: 'none'
  144. })
  145. return
  146. }
  147. const that = this
  148. uni.canvasToTempFilePath({
  149. canvasId: 'mini_poster',
  150. success: (res) => {
  151. uni.saveImageToPhotosAlbum({
  152. filePath: res.tempFilePath,
  153. success: () => {
  154. uni.showToast({
  155. title: '保存成功'
  156. })
  157. },
  158. fail() {
  159. uni.showToast({
  160. icon: 'none',
  161. title: '保存名片码失败'
  162. })
  163. }
  164. })
  165. },
  166. fail() {
  167. uni.showToast({
  168. icon: 'none',
  169. title: '保存名片码失败'
  170. })
  171. }
  172. })
  173. },
  174. async toDrawCanvas() {
  175. const padding = uni.upx2px(34)
  176. const cardHeight = uni.upx2px(420)
  177. const cw = this.canvasW - 2 * padding
  178. const r = uni.upx2px(12)
  179. let ctx = uni.createCanvasContext('mini_poster', this)
  180. // 左上角
  181. var cx = uni.upx2px(30)
  182. var cy = uni.upx2px(30)
  183. //this._drawRoundRect(ctx, 30, 30, this.canvasW, this.canvasH, r)
  184. // ctx.arc(this.canvasW, 0, this.canvasW,this.canvasH, cx)
  185. // 透明背景
  186. ctx.setFillStyle('rgba(255, 255, 255, 0)')
  187. //ctx.setFillStyle('#FFFFFF')
  188. //ctx.setFillStyle('#D53533')
  189. ctx.fillRect(0, 0, this.canvasW, this.canvasH)
  190. // draw card round rect
  191. this.drawback(ctx)
  192. this.drawRoundRect(ctx, padding, padding, cw, cardHeight, r, 2)
  193. // draw card content
  194. this.drawCard(ctx, padding, padding, cw, cardHeight)
  195. // draw avatar
  196. const av = uni.upx2px(540)
  197. const topimgh = uni.upx2px(360)
  198. //const topimgh ='auto'
  199. //console.log(av)
  200. //console.log(topimgh)
  201. const hi = await this.downloadImage(this.avatar)
  202. if (hi.tempFilePath) {
  203. const x = uni.upx2px(30)
  204. const y = uni.upx2px(30)
  205. this.drawRoundRectAvatar(ctx, x, y, av, topimgh, r, hi.tempFilePath)
  206. }
  207. if(this.groupType){
  208. var ptw=uni.upx2px(60)
  209. var pth=uni.upx2px(32)
  210. var ptIcon=await this.downloadImage(this.ptImg);
  211. if (ptIcon.tempFilePath) {
  212. const x = uni.upx2px(30)
  213. const y = uni.upx2px(370)
  214. this.drawRoundRectAvatar(ctx, x, y, ptw, pth, r, ptIcon.tempFilePath)
  215. }
  216. }
  217. // draw hello info
  218. //this.drawHelloInfo(ctx, padding, padding + cardHeight + padding, cw)
  219. // draw qr code
  220. if (this.mpWxQr) {
  221. var fsm = uni.getFileSystemManager();
  222. //var filePath='../../static/timg/CS.png'
  223. var times = new Date().getTime();
  224. var codeimg = wx.env.USER_DATA_PATH + '/' + times + '.png';
  225. var that=this;
  226. fsm.writeFile({
  227. filePath: codeimg, // 要写入的文件路径 (本地路径)
  228. data: this.mpWxQr3, // 要写入的文本或二进制数据
  229. encoding:"base64", // 指定写入文件的字符编码
  230. success(e) {
  231. console.log(e)
  232. console.log(codeimg)
  233. var wxsx=uni.upx2px(48)
  234. if(that.sellingPoint){
  235. var wxsy=uni.upx2px(649)
  236. }else{
  237. var wxsy=uni.upx2px(549)
  238. }
  239. var wxwidth=uni.upx2px(158)
  240. ctx.drawImage(codeimg, wxsx, wxsy, wxwidth,wxwidth)
  241. },
  242. fail(err) {
  243. console.log(err)
  244. }
  245. });
  246. const hello = await this.downloadImage(this.mpWxQr)
  247. //const padding = uni.upx2px(34)
  248. //const av = uni.upx2px(180)
  249. //const totalH = this.calculateCanvasHeight()
  250. //console.log("totalH=="+totalH)
  251. //console.log(hello.tempFilePath)
  252. // var wxsx=uni.upx2px(68)
  253. // var wxsy=uni.upx2px(649)
  254. // var wxwidth=uni.upx2px(148)
  255. // ctx.drawImage(codeimg, wxsx, wxsy, wxwidth,wxwidth)
  256. if (hello.tempFilePath) {
  257. //ctx.drawImage(hello.tempFilePath, wxsx, wxsy, wxwidth,wxwidth)
  258. }
  259. }
  260. ctx.draw()
  261. this.finished = true
  262. },
  263. async drawCard(ctx, x, y, w, h) {
  264. // draw company
  265. //let vp = y + uni.upx2px(45)
  266. //const hp = x + uni.upx2px(52)
  267. let vp = uni.upx2px(440)
  268. var hp = uni.upx2px(36)
  269. ctx.setTextBaseline('center')
  270. const fz30 = uni.upx2px(32)
  271. ctx.fillStyle = '#212121'
  272. ctx.setFontSize(fz30)
  273. if(this.groupType){
  274. var namehp = uni.upx2px(100)
  275. ctx.fillText(this.nickname, namehp, vp)
  276. }else{
  277. ctx.fillText(this.nickname, hp, vp)
  278. }
  279. var timeX=hp;
  280. var timeY=vp+20
  281. const fz26 = uni.upx2px(26)
  282. ctx.setFillStyle('#3C3C3C')
  283. ctx.setFontSize(fz26)
  284. ctx.fillText(this.time, timeX, timeY)
  285. var sm=this.sellingPoint
  286. ctx.setFillStyle('#999999')
  287. ctx.setFontSize(fz26)
  288. var smy=timeY+20
  289. var maxwidth = uni.upx2px(550)
  290. if(sm.length>20){
  291. var sm1=sm.substring(0,20)
  292. ctx.fillText(sm1, timeX, smy)
  293. if(sm.length>40){
  294. var sm2=sm.substring(20,40)
  295. ctx.fillText(sm2, timeX, smy+20)
  296. var sm3=sm.substring(40,60)
  297. ctx.fillText(sm3, timeX, smy+40)
  298. }else{
  299. var sm2=sm.substring(20,40)
  300. ctx.fillText(sm2, timeX, smy+20)
  301. }
  302. }else{
  303. ctx.fillText(sm, timeX, smy)
  304. }
  305. var wemtis1='名额有限 先到先得'
  306. var wemtisx=uni.upx2px(240)
  307. if(this.sellingPoint){
  308. var wemtisy=uni.upx2px(700)
  309. }else{
  310. var wemtisy=uni.upx2px(600)
  311. }
  312. ctx.setFillStyle('#999999')
  313. const fz24 = uni.upx2px(24)
  314. ctx.setFontSize(fz24)
  315. ctx.fillText(wemtis1,wemtisx,wemtisy)
  316. var wemtis2='长按二维码识别参与活动'
  317. var wemtisy2=wemtisy+30
  318. ctx.fillText(wemtis2,wemtisx,wemtisy2)
  319. // draw nickname
  320. vp = vp + 30
  321. vp = vp + 42
  322. const iconW = uni.upx2px(30)
  323. const textH = hp + iconW + 6
  324. ctx.setFillStyle('#333333')
  325. ctx.setFontSize(fz30)
  326. vp = vp + 28
  327. ctx.setFillStyle('#333333')
  328. ctx.setFontSize(fz30)
  329. // ctx.fillText(this.showInvestAreas, textH, vp)
  330. const oPadding = uni.upx2px(34)
  331. const iPadding = uni.upx2px(40)
  332. const textW = w - textH + oPadding - iPadding
  333. //this.drawTextInOneLine(ctx, this.showInvestAreas, textH, vp, textW)
  334. vp = vp + 28
  335. ctx.setFillStyle('#333333')
  336. ctx.setFontSize(fz30)
  337. //ctx.fillText(this.phone ? this.phone : '', textH, vp)
  338. },
  339. async drawHelloInfo(ctx, x, y, w) {
  340. let vp = y + 12
  341. const hp = x
  342. ctx.setTextBaseline('top')
  343. const fz30 = uni.upx2px(30)
  344. ctx.setFillStyle('#333333')
  345. ctx.setFontSize(fz30)
  346. ctx.fillText('您好,', hp, vp)
  347. const text = "我是来自 " + this.company + ' 的投资人' + this.nickname
  348. const lines = this.drawTextInLines(ctx, text, hp, vp, w, 26)
  349. vp += 26 * lines + 26
  350. ctx.fillText('这是我的名片,请惠存。', hp, vp)
  351. vp += 26
  352. ctx.fillText('谢谢', hp, vp)
  353. vp += 26
  354. vp += 40
  355. ctx.setFillStyle('#F37231')
  356. ctx.fillRect(hp, vp, 20, 3)
  357. vp += 7
  358. ctx.setFillStyle('#333333')
  359. const fz28 = uni.upx2px(28)
  360. ctx.setFontSize(fz28)
  361. ctx.fillText('长按识别二维码', hp, vp)
  362. vp += 20
  363. const ttt = '在大天使中查看我的更多信息'
  364. ctx.fillText(ttt, hp, vp)
  365. },
  366. calculateCanvasHeight() {
  367. // 400 is card height
  368. const ctx = uni.createCanvasContext('test_poster')
  369. const padding = uni.upx2px(34)
  370. const text = "我是来自 " + this.company + ' 的投资人' + this.nickname
  371. const rows = this.drawTextInLines(ctx, text, padding, 0, this.canvasW - 2 * padding, 26)
  372. const cardH = uni.upx2px(380)
  373. const h = padding * 2 + cardH + padding + 12 + 26 * 3 + rows * 26
  374. return h + 90
  375. },
  376. _drawRoundRect(ctx, x, y, w, h, r) {
  377. ctx.beginPath()
  378. // 左上角
  379. console.log("x="+x+'y='+y+'w='+w+'h='+h+'r='+r)
  380. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)
  381. //ctx.arc(0, 0, 15, Math.PI, Math.PI * 1.5)
  382. // 右上角
  383. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)
  384. //ctx.arc(0, 0, 0, Math.PI, Math.PI * 1.5)
  385. // 右下角
  386. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)
  387. // 左下角
  388. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)
  389. ctx.closePath()
  390. // ctx.strokeStyle = '#D8D8D8';
  391. ctx.fillStyle = '#FFFFFF';
  392. // ctx.stroke()
  393. ctx.fill()
  394. },
  395. drawRoundRect(ctx, x, y, w, h, r, lineWidth) {
  396. //return false;
  397. ctx.save()
  398. //this._drawRoundRect(ctx, x, y, w, h, r)
  399. ctx.clip()
  400. // draw left border
  401. ctx.fillStyle = '#EEEEEE'
  402. if(this.sellingPoint){
  403. var y=uni.upx2px(619)
  404. }else{
  405. var y=uni.upx2px(519)
  406. }
  407. ctx.arc(0, 0, 15, Math.PI, Math.PI * 1.5)
  408. var bWidth=uni.upx2px(600)
  409. ctx.fillRect(0, y, bWidth, 2)
  410. ctx.restore()
  411. },
  412. drawback(ctx){
  413. ctx.save()
  414. var bWidth=uni.upx2px(12)
  415. this._drawRoundRect(ctx, 0, 0, this.canvasW, this.canvasH,bWidth )
  416. ctx.clip()
  417. // draw left border
  418. ctx.fillStyle = '#FFFFFF'
  419. var bWidth=uni.upx2px(600)
  420. ctx.fillRect(0, 0, bWidth, this.canvasH)
  421. ctx.restore()
  422. },
  423. drawRoundRectAvatar(ctx, x, y, w, h, r, imgPath) {
  424. ctx.save()
  425. this._drawRoundRect(ctx, x, y, w, h, r)
  426. ctx.clip()
  427. ctx.drawImage(imgPath, x, y, w, h)
  428. ctx.restore()
  429. },
  430. // TODO: measureText not works in App
  431. drawTextInLines(ctx, t, x, y, w, lineHeight) {
  432. const chr = t.split("")
  433. let temp = ''
  434. let row = []
  435. for (let i = 0; i < chr.length; i++) {
  436. if (ctx.measureText(temp).width < w) {
  437. //
  438. } else {
  439. row.push(temp)
  440. temp = ''
  441. }
  442. temp += chr[i]
  443. }
  444. row.push(temp)
  445. for (const i in row) {
  446. // i + 1 will be 11, 21,...
  447. ctx.fillText(row[i], x, (y + (i * 1 + 1) * lineHeight))
  448. }
  449. return row.length
  450. },
  451. // TODO: measureText not works in app
  452. drawTextInOneLine(ctx, t, x, y, w) {
  453. const chr = t.split('')
  454. let temp = ''
  455. for (let i = 0; i < chr.length; i++) {
  456. if (ctx.measureText(temp + '...').width <= w) {
  457. temp += chr[i]
  458. } else {
  459. temp = temp.substring(0, temp.length - 1)
  460. temp += '...'
  461. break
  462. }
  463. }
  464. ctx.fillText(temp, x, y)
  465. },
  466. downloadImage(url) {
  467. return new Promise((resolve, reject) => {
  468. uni.downloadFile({
  469. url: url,
  470. success: (res) => {
  471. return resolve(res)
  472. },
  473. fail: (err) => {
  474. return reject(err)
  475. }
  476. })
  477. })
  478. }
  479. }
  480. }
  481. </script>
  482. <style scoped>
  483. .box{
  484. width: 100%;
  485. height: 100vh;
  486. background: url('http://dmsphoto.66km.com.cn/thFiles/83BDAB66-DD76-4F3A-92B3-F6AF0BA57A9E.png');
  487. background-size: 100% 100%;
  488. }
  489. .downloadBtn{
  490. width: 329rpx;
  491. height: 92rpx;
  492. background: url('http://dmsphoto.66km.com.cn/thFiles/A0CF4704-69A8-4DAC-8943-75F98D1349DD.png');
  493. background-size: 100% 100%;
  494. line-height: 92rpx;
  495. text-align: center;
  496. font-size: 28rpx;
  497. font-family: PingFangSC-Medium, PingFang SC;
  498. font-weight: 500;
  499. color: #DC490A;
  500. margin: 0 auto;
  501. }
  502. </style>