shareRoom.js 11 KB

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