shared.js 6.3 KB

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