wp.vue 14 KB

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