shared.js 7.0 KB

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