shared.js 7.4 KB

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