index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //获取应用实例
  2. const
  3. app = getApp(),
  4. Bmob = require('../../utils/Bmob-1.6.1.min.js'),
  5. Storage = require('../../utils/storage.js'),
  6. API = require('../../api/API.js'),
  7. utils = require('../../utils/util.js'),
  8. wxApi = require('../../api/wxApi.js')
  9. ;
  10. Page({
  11. data: { // 初始化页面渲染数据
  12. },
  13. onLoad: function (options) {
  14. let data = JSON.parse(options.data)
  15. console.error(data)
  16. this._draw(data)
  17. },
  18. onReady:function () {
  19. let me = this;
  20. //监听页面初次渲染完成
  21. },
  22. onShow:function () {
  23. //监听页面显示
  24. },
  25. onHide:function () {
  26. //监听页面隐藏
  27. },
  28. onReachBottom:function () {
  29. //页面上拉触底事件的处理函数
  30. },
  31. onPullDownRefresh:function () {
  32. //监听用户下拉动作
  33. },
  34. onShareAppMessage:function () {
  35. //用户点击右上角转发
  36. return {
  37. title: '回到你的幸福時光',
  38. path: '../canvas/index'
  39. }
  40. },
  41. _draw:function (data) {
  42. let me = this;
  43. let systemInfo = app.globalData.systemInfo;
  44. let drawInfo = {
  45. width:app.globalData.systemInfo.windowWidth || 375,
  46. height: 250,
  47. pixelRatio:app.globalData.systemInfo.pixelRatio || 2,
  48. };
  49. //
  50. let imageInfo = data.imgInfo;
  51. let ctx = wx.createCanvasContext('myCanvas');
  52. // wxApi.getImageInfo({
  53. // src: data.imgInfo.path
  54. // }).then(res => {
  55. // // console.error(res);
  56. // //
  57. // let imgInfo = {
  58. // height:res.height,
  59. // width:res.width,
  60. // type:res.type,
  61. // path:res.path
  62. // }
  63. //
  64. // me.data.formToSend.imgInfo = imgInfo;
  65. //
  66. drawInfo.height = (data.imgInfo.height * drawInfo.width)/(data.imgInfo.width);
  67. // console.error(drawInfo);
  68. //
  69. ctx.drawImage(imageInfo.path, 0, 0, drawInfo.width, drawInfo.height);
  70. //
  71. //文字
  72. ctx.setFillStyle('#000'); // 文字颜色:黑色
  73. ctx.setFontSize(10); // 文字字号:14px
  74. ctx.fillText(`${data.date}|${data.city}` ,20,drawInfo.height+20);
  75. //文字
  76. ctx.setFillStyle('#000'); // 文字颜色:黑色
  77. ctx.setFontSize(14); // 文字字号:14px
  78. ctx.fillText(data.title,20,drawInfo.height+50);
  79. //
  80. ctx.setFillStyle('#999'); // 文字颜色:黑色
  81. ctx.setFontSize(14); // 文字字号:14px
  82. ctx.fillText(data.author,20,drawInfo.height+70);
  83. //
  84. ctx.setFillStyle('#000'); // 文字颜色:黑色
  85. ctx.setFontSize(18); // 文字字号:18px
  86. let sectiongArr = data.section.split('\n');
  87. let maxFontNum = Math.floor((drawInfo.width-40)/18) || 20;
  88. // // console.error(maxFontNum);
  89. // // console.error(sectiongArr);
  90. let Allheight= drawInfo.height+80;
  91. sectiongArr.forEach((val,index,arr)=>{
  92. let temp = val.StrCut2Arr(maxFontNum);
  93. let sectiongIndex = index +1;
  94. let sectiongArrTemp = [...temp];
  95. sectiongArrTemp.forEach((e,i)=>{
  96. Allheight += 20;
  97. ctx.fillText(e,20,drawInfo.height+60+(sectiongIndex+i+1)*20);
  98. })
  99. });
  100. //绘制二维码
  101. if(data.imageBytes){
  102. ctx.drawImage(data.imageBytes, drawInfo.width-25-70, Allheight+80, 74, 74);
  103. }
  104. //绘制头像
  105. let avatarUrl = app.globalData.userInfo.avatarUrl;
  106. if(avatarUrl){
  107. ctx.drawImage(avatarUrl, 20, Allheight+80+17, 40, 40);
  108. }
  109. // //绘制微信名称
  110. // let wxname = '@'+app.globalData.userInfo.nickName;
  111. // //
  112. // ctx.setFillStyle('#999'); // 文字颜色:黑色
  113. // ctx.setFontSize(12); // 文字字号:14px
  114. // ctx.fillText(wxname,20,Allheight+80+40);
  115. //
  116. ctx.draw();
  117. wx.showToast({
  118. title: '已保存,可截图',
  119. icon: 'success',
  120. duration: 4000
  121. })
  122. me.setData({
  123. height:Allheight+200+'px'
  124. })
  125. //
  126. // wx.hideLoading();
  127. // me.setData({
  128. // height: Allheight+200+'px',
  129. // btn_height:Allheight+200+50+'px'
  130. // },function () {
  131. // wx.hideLoading();
  132. // console.log(me.data.formToSend);
  133. // //保存数据
  134. // saveContent(me.data.formToSend);
  135. // wx.showToast({
  136. // title: '已保存,可截图分享',
  137. // icon: 'success',
  138. // duration: 3000
  139. // })
  140. // })
  141. // }) .catch(err =>{
  142. // console.error(err);
  143. // wx.hideLoading();
  144. // })
  145. }
  146. });
  147. function saveContent(data) {
  148. console.log(data)
  149. }