shared.js 7.2 KB

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