shareRoom.js 12 KB

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