shared.js 7.1 KB

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