shared.js 7.5 KB

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