shared.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. console.log(options)
  14. const { img_url, companyId, vrLink, shareImg } = options
  15. this.companyId = companyId
  16. this.vrLink = vrLink
  17. this.shareCodeImg = decodeURIComponent(shareImg)
  18. this.setData({
  19. shared_img: decodeURIComponent(img_url)
  20. })
  21. await this.getCompanyDetail()
  22. const query = wx.createSelectorQuery()
  23. query.select('.canvas').boundingClientRect().exec(res => {
  24. this.canvas_width = res[0].width
  25. this.canvas_height = res[0].height
  26. this.drawImage()
  27. })
  28. },
  29. onReady() {
  30. },
  31. getCompanyDetail () {
  32. CompanyApi.getCompanyDetail(this.companyId).then(res => {
  33. console.log(res, 'company')
  34. this.setData({
  35. company: res.data
  36. })
  37. })
  38. },
  39. showEdit () {
  40. this.setData({
  41. editShowStatus: true
  42. })
  43. },
  44. hideEdit () {
  45. this.setData({
  46. editShowStatus: false
  47. })
  48. },
  49. bindinput (e) {
  50. const { value } = e.detail
  51. this.setData({
  52. recommend_text: value
  53. })
  54. },
  55. onShareAppMessage () {
  56. return {
  57. title: this.data.company.companyName,
  58. path: `/pages/web/web?vr_link=${this.vrLink}`,
  59. imageUrl: this.data.shared_img + '?x-oss-process=image/resize,m_fill,h_510,w_510'
  60. }
  61. },
  62. copyLink () {
  63. wx.setClipboardData({
  64. data: decodeURIComponent(this.vrLink),
  65. success (res) {
  66. wx.showToast({
  67. title: '复制成功',
  68. })
  69. }
  70. })
  71. },
  72. savePhoto () {
  73. wx.saveImageToPhotosAlbum({
  74. filePath: this.data.img_url,
  75. success(res) {
  76. wx.showModal({
  77. title: '图片保存成功',
  78. content: '图片成功保存到相册了,去发圈噻~',
  79. showCancel: false,
  80. confirmText: '好哒',
  81. confirmColor: '#72B9C3',
  82. success: (res) => {
  83. if (res.confirm) {
  84. }
  85. }
  86. })
  87. }
  88. })
  89. },
  90. async drawImage(recommend_text) {
  91. this.context = wx.createCanvasContext('content')
  92. this.context.lineJoin = 'miter'
  93. this.context.font = '15px bold'
  94. this.context.setFillStyle('#fff')
  95. this.context.fillRect(0, 0, this.canvas_width, this.canvas_height)
  96. const dpr = wx.getSystemInfoSync().pixelRatio
  97. let cover = await this.downloadFile(this.data.shared_img + '?x-oss-process=image/resize,m_fill,h_510,w_510')
  98. let shareCode = await this.downloadFile(this.shareCodeImg)
  99. const img_width = this.canvas_width * 0.8644,
  100. img_height = this.canvas_width * 0.8644
  101. const left = (this.canvas_width - img_width) / 2
  102. // 画圆弧矩形
  103. this.strokeRoundRect((this.canvas_width - img_width) / 2, (this.canvas_width - img_width) / 2, img_width, img_height, 4)
  104. this.context.clip()
  105. this.context.drawImage(cover, 0, 0, img_width, img_height)
  106. this.context.restore()
  107. this.context.fillStyle = '#131D34'
  108. this.context.font = '15px bold'
  109. this.context.fillText(this.data.company.companyName, left, img_width + left + this.canvas_height * 15 / 460 + 21)
  110. this.context.restore()
  111. this.context.font = '13px bold'
  112. this.context.fillStyle = '#79868F'
  113. let recommend_text1, recommend_text2
  114. if (recommend_text) {
  115. recommend_text1 = recommend_text.slice(0,11)
  116. recommend_text2 = recommend_text.slice(11)
  117. }
  118. this.context.fillText(recommend_text1 || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21)
  119. if (recommend_text2) {
  120. this.context.fillText(recommend_text2 || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21 + 21)
  121. }
  122. this.context.font = '12px bold'
  123. this.context.fillStyle = '#000'
  124. this.context.drawImage(shareCode, left, img_width + left + this.canvas_height * 92 / 460 + 10, 60, 60)
  125. this.context.fillText('3D实景拍摄', left + 80, img_width + left + this.canvas_height * 92 / 460 + 20)
  126. this.context.fillText('掌上全方位了解企业', left + 80, img_width + left + this.canvas_height * 92 / 460 + 40)
  127. this.context.fillText('长按识别二维码进入展厅', left + 80, img_width + left + this.canvas_height * 140 / 460 + 17)
  128. this.context.draw(false, () => {
  129. wx.canvasToTempFilePath({
  130. x: 0,
  131. y: 0,
  132. width: this.canvas_width,
  133. height: this.canvas_height,
  134. canvasId: 'content',
  135. success: (res) => {
  136. this.setData({
  137. img_url: res.tempFilePath
  138. })
  139. }
  140. })
  141. })
  142. this.context.fill()
  143. this.context.restore()
  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. downloadFile (url) {
  183. return new Promise(resolve => {
  184. wx.downloadFile({
  185. url: url,
  186. success (res) {
  187. resolve(res.tempFilePath)
  188. }
  189. })
  190. })
  191. }
  192. }
  193. })