wp.vue 15 KB

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