wp.vue 14 KB

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