shareRoom.js 12 KB

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