wp.vue 12 KB

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