shareRoom.js 10 KB

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