wp.vue 15 KB

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