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