shared.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {
  2. VueLikePage
  3. } from '../../utils/page'
  4. VueLikePage([], {
  5. data: {
  6. shared_img: '',
  7. recommend_text: '',
  8. editShowStatus: false
  9. },
  10. methods: {
  11. onLoad: function (options) {
  12. this.setData({
  13. shared_img: decodeURIComponent(options.img_url)
  14. })
  15. },
  16. onReady() {
  17. const query = wx.createSelectorQuery()
  18. query.select('.canvas').boundingClientRect().exec(res => {
  19. this.canvas_width = res[0].width
  20. this.canvas_height = res[0].height
  21. this.drawImage()
  22. })
  23. },
  24. showEdit () {
  25. this.setData({
  26. editShowStatus: true
  27. })
  28. },
  29. hideEdit () {
  30. this.setData({
  31. editShowStatus: false
  32. })
  33. },
  34. bindinput (e) {
  35. const { value } = e.detail
  36. this.setData({
  37. recommend_text: value
  38. })
  39. },
  40. onShareAppMessage () {
  41. return {
  42. imageUrl: this.data.shared_img + '?x-oss-process=image/resize,m_fill,h_510,w_510'
  43. }
  44. },
  45. copyLink () {
  46. wx.setClipboardData({
  47. data: 'data',
  48. success (res) {
  49. wx.showToast({
  50. title: '复制成功',
  51. })
  52. }
  53. })
  54. },
  55. savePhoto () {
  56. wx.saveImageToPhotosAlbum({
  57. filePath: this.data.img_url,
  58. success(res) {
  59. wx.showModal({
  60. title: '图片保存成功',
  61. content: '图片成功保存到相册了,去发圈噻~',
  62. showCancel: false,
  63. confirmText: '好哒',
  64. confirmColor: '#72B9C3',
  65. success: (res) => {
  66. if (res.confirm) {
  67. }
  68. }
  69. })
  70. }
  71. })
  72. },
  73. drawImage(recommend_text) {
  74. this.context = wx.createCanvasContext('content')
  75. this.context.lineJoin = 'miter'
  76. this.context.font = '15px bold'
  77. this.context.setFillStyle('#fff')
  78. this.context.fillRect(0, 0, this.canvas_width, this.canvas_height)
  79. const dpr = wx.getSystemInfoSync().pixelRatio
  80. wx.downloadFile({
  81. url: this.data.shared_img + '?x-oss-process=image/resize,m_fill,h_510,w_510',
  82. success: res => {
  83. const img_width = this.canvas_width * 0.8644,
  84. img_height = this.canvas_width * 0.8644
  85. const left = (this.canvas_width - img_width) / 2
  86. // 画圆弧矩形
  87. this.strokeRoundRect((this.canvas_width - img_width) / 2, (this.canvas_width - img_width) / 2, img_width, img_height, 4)
  88. this.context.clip()
  89. this.context.drawImage(res.tempFilePath, 0, 0, img_width, img_height)
  90. this.context.restore()
  91. this.context.fillStyle = '#131D34'
  92. this.context.fillText('格力新天地 3室2厅', left, img_width + left + this.canvas_height * 15 / 460 + 21)
  93. this.context.restore()
  94. this.context.font = '13px bold'
  95. this.context.fillStyle = '#79868F'
  96. this.context.fillText(recommend_text || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21)
  97. this.context.font = '12px bold'
  98. this.context.fillStyle = '#000'
  99. this.context.fillText('3D实景拍摄', left + 80, img_width + left + this.canvas_height * 92 / 460 + 20)
  100. this.context.fillText('语音与带看,专人在线答疑', left + 80, img_width + left + this.canvas_height * 92 / 460 + 40)
  101. this.context.fillText('长按识别二维码进入房源', left + 80, img_width + left + this.canvas_height * 140 / 460 + 17)
  102. this.context.draw(false, () => {
  103. wx.canvasToTempFilePath({
  104. x: 0,
  105. y: 0,
  106. width: this.canvas_width,
  107. height: this.canvas_height,
  108. canvasId: 'content',
  109. success: (res) => {
  110. this.setData({
  111. img_url: res.tempFilePath
  112. })
  113. }
  114. })
  115. })
  116. this.context.fill()
  117. this.context.restore()
  118. },
  119. fail: err => {
  120. console.log(err)
  121. }
  122. })
  123. },
  124. submitRecommend() {
  125. this.drawImage(this.data.recommend_text)
  126. this.hideEdit()
  127. },
  128. strokeRoundRect(x, y, width, height, radius, /*optional*/ lineWidth, /*optional*/ strokeColor) {
  129. //圆的直径必然要小于矩形的宽高
  130. if (2 * radius > width || 2 * radius > height) {
  131. return false;
  132. }
  133. this.context.save();
  134. this.context.translate(x, y);
  135. //绘制圆角矩形的各个边
  136. this.drawRoundRectPath(width, height, radius);
  137. this.context.lineWidth = 0; //若是给定了值就用给定的值否则给予默认值2
  138. this.context.strokeStyle = strokeColor || "#fff";
  139. this.context.stroke();
  140. },
  141. drawRoundRectPath(width, height, radius) {
  142. this.context.beginPath(0);
  143. //从右下角顺时针绘制,弧度从0到1/2PI
  144. this.context.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  145. //矩形下边线
  146. this.context.lineTo(radius, height);
  147. //左下角圆弧,弧度从1/2PI到PI
  148. this.context.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  149. //矩形左边线
  150. this.context.lineTo(0, radius);
  151. //左上角圆弧,弧度从PI到3/2PI
  152. this.context.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);
  153. //上边线
  154. this.context.lineTo(width - radius, 0);
  155. //右上角圆弧
  156. this.context.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);
  157. //右边线
  158. this.context.lineTo(width, height - radius);
  159. this.context.closePath();
  160. }
  161. }
  162. })