shared.js 7.3 KB

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