wp.vue 15 KB

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