shareRoom.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. import remote from '../../config.js'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. shared_img: '',
  12. recommend_text: '',
  13. editShowStatus: false,
  14. loadHot: false
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: async function (options) {
  20. let {
  21. // companyName,
  22. roomId,
  23. many,
  24. vrLink,
  25. id,
  26. type
  27. } = options
  28. //测试用
  29. // companyName = '1111'
  30. // id = 1046450
  31. // vrLink = 'L3BhZ2VzL3dlYnZpZXcvaW5kZXg'
  32. //测试用
  33. vrLink = atob(vrLink)
  34. // this.vrLink = vrLink + `?id=${id}&type=${type}`
  35. // 1046450,32,1,4370970,1
  36. // [id_type_join_roomId_many]
  37. this.vrLink = vrLink + `?id=${id}&type=${type}&join=true&roomId=${roomId}&many=${many}`;
  38. console.log(this.vrLink)
  39. // this.companyName = companyName
  40. let data = await this.getBrandDetail(id, type)
  41. this.sceneUrl = data.sceneUrl
  42. this.shareCodeImg = data.shareWxQrCode
  43. this.sceneName = data.sceneName
  44. const version = wx.getAccountInfoSync().miniProgram.envVersion;
  45. console.log('version', version)
  46. let params = {
  47. scene: `${id}_${type}_1_${roomId}_1`,
  48. page: vrLink.substr(1, vrLink.length - 1), //截掉page前的 /
  49. envVersion: version || 'release'
  50. }
  51. // this.getMIniCode =remote.requestHost+'/statics/tmp/images/1634032649766.png'
  52. // trackRoom share
  53. // 分享房间进入统计
  54. util.request(api.trackRoom, {
  55. roomId: roomId,
  56. type: 1,
  57. }, 'POST', 'application/json')
  58. try {
  59. let codeData = await this.generateMicroAppCode(params)
  60. if (codeData) {
  61. this.getMIniCode = remote.requestHost + codeData
  62. console.log('****')
  63. console.log(this.getMIniCode)
  64. } else {
  65. this.getMIniCode = null
  66. wx.showToast({
  67. title: '小程序码生成失败',
  68. })
  69. }
  70. } catch (err) {
  71. this.getMIniCode = null
  72. // this.getMIniCode =remote.requestHost+'/statics/tmp/images/1634032649766.png'
  73. }
  74. this.setData({
  75. shared_img: data.appListPicUrl || 'https://plaza.4dkankan.com/statics/img/pic_bg@2x.png',
  76. layoutHeight: wx.getSystemInfoSync().windowHeight - 170
  77. })
  78. const query = wx.createSelectorQuery()
  79. query.select('.canvas').boundingClientRect().exec(res => {
  80. this.canvas_width = res[0].width
  81. this.canvas_height = res[0].height
  82. this.drawImage()
  83. })
  84. },
  85. onShow() {},
  86. getCompanyDetail() {
  87. // CompanyApi.getCompanyDetail(this.companyId).then(res => {
  88. // console.log(res, 'company')
  89. // this.setData({
  90. // company: res.data
  91. // })
  92. // })
  93. },
  94. showEdit() {
  95. this.setData({
  96. editShowStatus: true
  97. })
  98. },
  99. hideEdit() {
  100. this.setData({
  101. editShowStatus: false
  102. })
  103. },
  104. bindinput(e) {
  105. const {
  106. value
  107. } = e.detail
  108. value.substr(0, 25)
  109. this.setData({
  110. recommend_text: value
  111. })
  112. },
  113. onShareAppMessage() {
  114. console.log(this.vrLink)
  115. return {
  116. path: this.vrLink,
  117. imageUrl: this.data.shared_img
  118. }
  119. },
  120. copyLink() {
  121. wx.setClipboardData({
  122. data: decodeURIComponent(this.sceneUrl),
  123. success(res) {
  124. wx.showToast({
  125. title: '复制成功',
  126. })
  127. }
  128. })
  129. },
  130. getBrandDetail: async function (id, type, cb) {
  131. let res = await util.request(api.BrandDetail, {
  132. id: id,
  133. type: type
  134. })
  135. return res.data.brand
  136. },
  137. generateMicroAppCode: async function (data) {
  138. let res = await util.request(api.generateMicroAppCode, data, 'POST', 'application/json')
  139. if (res.errno == 0) {
  140. return res.data
  141. } else if (res.errno == 1) {
  142. return false
  143. }
  144. },
  145. savePhoto() {
  146. wx.saveImageToPhotosAlbum({
  147. filePath: this.data.img_url,
  148. success(res) {
  149. wx.showModal({
  150. title: '图片保存成功',
  151. content: '图片成功保存到相册了,去发圈噻~',
  152. showCancel: false,
  153. confirmText: '好哒',
  154. confirmColor: '#72B9C3',
  155. success: (res) => {
  156. if (res.confirm) {}
  157. }
  158. })
  159. }
  160. })
  161. },
  162. async drawImage(recommend_text) {
  163. this.context = wx.createCanvasContext('content')
  164. this.context.lineJoin = 'miter'
  165. this.context.font = 'bold 15px sans-serif'
  166. this.context.setFillStyle('#fff')
  167. this.context.fillRect(0, 0, this.canvas_width, this.canvas_height)
  168. const dpr = wx.getSystemInfoSync().pixelRatio
  169. let cover = await this.downloadFile(this.data.shared_img)
  170. // if (this.shareCodeImg) {
  171. // var shareCode = await this.downloadFile(this.shareCodeImg)
  172. // } else {
  173. // var shareCode = null
  174. // }
  175. var shareCode = null
  176. if (this.getMIniCode) {
  177. try {
  178. shareCode = await this.downloadFile(this.getMIniCode)
  179. } catch (err) {
  180. shareCode = null
  181. }
  182. }
  183. const img_width = this.canvas_width * 0.8644,
  184. img_height = this.canvas_width * 0.8644
  185. const left = (this.canvas_width - img_width) / 2
  186. // 画圆弧矩形
  187. this.strokeRoundRect((this.canvas_width - img_width) / 2, (this.canvas_width - img_width) / 2, img_width, img_height, 4)
  188. this.context.clip()
  189. this.context.drawImage(cover, 0, 0, img_width, img_height)
  190. this.context.restore()
  191. this.context.fillStyle = '#fff'
  192. this.context.font = 'normal 300 11px sans-serif'
  193. // this.sceneName =
  194. // let title ='MOOST·理想服饰MOOST·理想服饰MOOST'
  195. if (this.sceneName && this.sceneName.length > 15) {
  196. this.sceneName = this.sceneName.substring(0, 15) + '...'
  197. } else if (!this.sceneName) {
  198. this.sceneName = ''
  199. }
  200. console.log(this.sceneName)
  201. if (this.sceneName != '') {
  202. this.titleWidth = this.context.measureText(this.sceneName).width + 53 + 20
  203. } else {
  204. this.titleWidth = 20
  205. }
  206. this.roundRectColor(this.context, left + 10, 243, this.titleWidth, 22, 6, 'rgba(0, 0, 0, 0.6)')
  207. this.context.fillStyle = '#fff'
  208. this.context.font = 'normal 300 11px sans-serif'
  209. this.context.fillText(this.sceneName, left + 10 + 53 + 10, 243 + 15)
  210. this.roundRectColor(this.context, left + 10, 243, 53, 22, 6, '#ED5D18')
  211. this.context.fillStyle = "#fff";
  212. this.context.beginPath();
  213. this.context.arc(left + 10 + 6, 243 + 11, 3, Math.PI * 2, 0, true);
  214. this.context.closePath();
  215. this.context.fill();
  216. this.context.fillStyle = '#fff'
  217. this.context.font = 'normal 300 11px sans-serif'
  218. this.context.fillText('直播中', left + 10 + 12, 243 + 15)
  219. // this.context.fillStyle = '#131D34'
  220. // this.context.font = 'normal bold 15px sans-serif'
  221. // this.context.fillText(this.companyName, left, img_width + left + this.canvas_height * 15 / 460 + 21)
  222. // this.context.restore()
  223. // this.context.font = 'normal 300 13px sans-serif'
  224. // this.context.fillStyle = '#79868F'
  225. // let recommend_text1, recommend_text2
  226. // if (recommend_text) {
  227. // recommend_text1 = recommend_text.slice(0, 11)
  228. // recommend_text2 = recommend_text.slice(11)
  229. // }
  230. // this.context.fillText(recommend_text1 || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21)
  231. // if (recommend_text2) {
  232. // this.context.font = 'normal 300 13px sans-serif'
  233. // this.context.fillStyle = '#79868F'
  234. // this.context.fillText(recommend_text2 || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21 + 21)
  235. // }
  236. this.context.font = 'bold 300 12px sans-serif'
  237. this.context.fillStyle = '#000'
  238. shareCode && this.context.drawImage(shareCode, left, img_height + 40, 60, 60)
  239. this.context.fillText('为爱筑家', left + 80, img_height + 50)
  240. this.context.fillText('满足您对家的一切美好想象', left + 80, img_height + 70)
  241. this.context.font = 'normal 300 12px sans-serif'
  242. this.context.fillStyle = '#000'
  243. this.context.fillText('长按识别二维码', left + 80, img_height + 95)
  244. this.context.fillStyle = '#ED5D18'
  245. this.context.fillText('进入直播间', left + 170, img_height + 95)
  246. this.context.draw(false, () => {
  247. wx.canvasToTempFilePath({
  248. x: 0,
  249. y: 0,
  250. width: this.canvas_width,
  251. height: this.canvas_height,
  252. canvasId: 'content',
  253. success: (res) => {
  254. this.setData({
  255. img_url: res.tempFilePath
  256. })
  257. setTimeout(() => {
  258. this.setData({
  259. loadHot: true
  260. })
  261. }, 100)
  262. }
  263. })
  264. })
  265. this.context.fill()
  266. this.context.restore()
  267. },
  268. submitRecommend() {
  269. this.drawImage(this.data.recommend_text)
  270. this.hideEdit()
  271. },
  272. strokeRoundRect(x, y, width, height, radius, /*optional*/ lineWidth, /*optional*/ strokeColor) {
  273. //圆的直径必然要小于矩形的宽高
  274. if (2 * radius > width || 2 * radius > height) {
  275. return false;
  276. }
  277. this.context.save();
  278. this.context.translate(x, y);
  279. //绘制圆角矩形的各个边
  280. this.drawRoundRectPath(width, height, radius);
  281. this.context.lineWidth = 0; //若是给定了值就用给定的值否则给予默认值2
  282. this.context.strokeStyle = strokeColor || "#fff";
  283. this.context.stroke();
  284. },
  285. drawRoundRectPath(width, height, radius) {
  286. this.context.beginPath(0);
  287. //从右下角顺时针绘制,弧度从0到1/2PI
  288. this.context.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  289. //矩形下边线
  290. this.context.lineTo(radius, height);
  291. //左下角圆弧,弧度从1/2PI到PI
  292. this.context.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  293. //矩形左边线
  294. this.context.lineTo(0, radius);
  295. //左上角圆弧,弧度从PI到3/2PI
  296. this.context.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);
  297. //上边线
  298. this.context.lineTo(width - radius, 0);
  299. //右上角圆弧
  300. this.context.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);
  301. //右边线
  302. this.context.lineTo(width, height - radius);
  303. this.context.closePath();
  304. },
  305. downloadFile(url) {
  306. return new Promise(resolve => {
  307. wx.downloadFile({
  308. url: url,
  309. success(res) {
  310. resolve(res.tempFilePath)
  311. },
  312. async fail(res) {
  313. console.log('下载失败,再触发下载一次', res)
  314. wx.downloadFile({
  315. url: url,
  316. success(res) {
  317. resolve(res.tempFilePath)
  318. },
  319. fail() {
  320. console.log('二次下载失败', res)
  321. resolve('')
  322. }
  323. })
  324. }
  325. })
  326. })
  327. },
  328. /**
  329. * 绘制圆角矩形
  330. * @param {*} x 起始点x坐标
  331. * @param {*} y 起始点y坐标
  332. * @param {*} w 矩形宽
  333. * @param {*} h 矩形高
  334. * @param {*} r 圆角半径
  335. * @param {*} context 画板上下文
  336. */
  337. roundRectColor(context, x, y, w, h, r, color) { //绘制圆角矩形(纯色填充)
  338. context.save();
  339. // context.setFillStyle("#ED5D18");
  340. // context.setStrokeStyle('#ED5D18')
  341. context.setFillStyle(color);
  342. context.setStrokeStyle(color)
  343. context.setLineJoin('round'); //交点设置成圆角
  344. context.setLineWidth(r);
  345. context.strokeRect(x + r / 2, y + r / 2, w - r, h - r);
  346. context.fillRect(x + r, y + r, w - r * 2, h - r * 2);
  347. // context.stroke();
  348. // context.closePath();
  349. }
  350. })