discover.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // pages/discover/discover.js
  2. import socketApi from '../..//socket'
  3. import api from '../../config/api'
  4. const util = require('../../utils/util.js');
  5. import remote from '../../config.js'
  6. const initData = {
  7. showGuide: false,
  8. showCommodity: false,
  9. showInvitation: false,
  10. showEnd: false,
  11. showAlert: false,
  12. sendShare: false,
  13. showPacked: false,
  14. showIsEnd: false,
  15. showCoupon: false,
  16. showContact: false,
  17. showCommodityCtrl: false,
  18. goodsCount: 0,
  19. url: '',
  20. currTab: 'webview',
  21. showTab: true,
  22. }
  23. Page({
  24. ...socketApi,
  25. /**
  26. * 页面的初始数据
  27. */
  28. data: {
  29. live_base_url: 'rtmp://120.24.85.77:1935/hls',
  30. page: 1,
  31. size: 1000,
  32. roomList: [],
  33. bottom: 50,
  34. ...initData
  35. },
  36. loadWeb() {
  37. this.setData({
  38. ...initData, url: this.data.url,
  39. })
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. async onLoad () {
  45. getApp().checkNetStatu();
  46. this.getRoomList()
  47. this.socketOptions = await this.socketStart({sceneId: ''})
  48. this.base = remote.viewChildHost + '/discover.html?appname=shop&origin=4Dplaza'
  49. console.log(this.base)
  50. // this.base = 'https://test.4dkankan.com/discover.html?appname=shop'
  51. const url = this.getUrl(this.base, this.socketOptions, false)
  52. this.setData({ url, loadUrl: true, socketOptions: this.socketOptions })
  53. this.refers = false
  54. this.data.reload = true
  55. this.joinUrl()
  56. },
  57. async getRoomList() {
  58. let {data} = await util.request(api.RoomList + '?pageNum=1&pageSize=1000', {}, 'GET')
  59. // let aaa = true
  60. data.list.forEach(item => {
  61. // 101:直播中,102:未开始,103已结束,104禁播,105:暂停,106:异常,107:已过期
  62. item.state = item.liveStatus === 101
  63. // item.state = aaa
  64. // aaa = !aaa
  65. })
  66. this.setData({roomList: data.list})
  67. },
  68. async scene(data) {
  69. this.cacheData = data
  70. if (data.data.cmd === 'enter') {
  71. let {data: {brand}} = await util.request(api.BrandDetailByScene + '?sceneNum=' + data.data.num)
  72. if (brand) {
  73. this.options.id = brand.id
  74. this.setData({
  75. id: brand.id,
  76. newPicUrl: brand.listPicUrl,
  77. showCommodityCtrl: true
  78. })
  79. this.mcode = data.data.num
  80. this.setData({numCode: data.data.num})
  81. this.getBrand(brand.id, data.data.num)
  82. this.joinUrl()
  83. }
  84. } else if (data.data.cmd === 'exit') {
  85. this.data.navList = null
  86. this.setData({ ...initData, url: this.data.url, numCode: null, showTab: false })
  87. }
  88. wx.setStorageSync('lastSceneNum', data.data.num)
  89. },
  90. exit() {
  91. this.setData({webviewUrl: null})
  92. socketApi.exit.bind(this)()
  93. },
  94. onShareAppMessage: function (res) {
  95. let { id, newPicUrl } = this.data
  96. if (res.from === 'button') {
  97. this.setData({ sendShare: false })
  98. return {
  99. imageUrl: newPicUrl,
  100. path: `/pages/webview/index?id=${id}&join=true&roomId=${this.data.socketOptions.roomId}`,
  101. }
  102. }
  103. },
  104. gotoGood(ev) {
  105. let {goodsId} = ev.target.dataset.item
  106. wx.navigateTo({
  107. url: '/pages/goods/goods?id=' + goodsId,
  108. })
  109. },
  110. showTabs(data) {
  111. this.setData( {showTab: data.data} )
  112. },
  113. changeTab(ev) {
  114. this.setData({ showTab: false })
  115. wx.nextTick(() => {
  116. if (ev.target.dataset.tab === 'live') {
  117. // this.setData({ ...initData, showTab: true, currTab: ev.target.dataset.tab })
  118. this.setData({ showTab: true, currTab: ev.target.dataset.tab })
  119. this.pauseVideo = true
  120. this.joinUrl()
  121. } else {
  122. this.setData({ showTab: true, currTab: ev.target.dataset.tab })
  123. this.pauseVideo = false
  124. this.joinUrl()
  125. }
  126. })
  127. },
  128. gotoLive(ev) {
  129. let item = ev.currentTarget.dataset.item
  130. const goodPath = item.liveRoomGoodsVos.length ?
  131. encodeURIComponent(JSON.stringify({ path: 'pages/goods/goods', id: item.liveRoomGoodsVos[0].goodsId })) : null
  132. wx.navigateTo({
  133. url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${item.roomId}${goodPath ? `&custom_params=${goodPath}` : ''}`,
  134. })
  135. },
  136. /**
  137. * 生命周期函数--监听页面初次渲染完成
  138. */
  139. onReady: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面显示
  143. */
  144. onShow: async function (refer) {
  145. getApp().updateCardCount()
  146. if (this.cccache && this.cacheToken !== wx.getStorageSync('token')) {
  147. refer = true
  148. }
  149. this.cacheToken = wx.getStorageSync('token');
  150. this.cccache = true
  151. if (this.refers || refer) {
  152. this.socketOptions = await this.socketStart({sceneId: ''})
  153. this.base = remote.viewChildHost + '/discover.html?appname=shop&origin=4Dplaza'
  154. const url = this.getUrl(this.base, this.socketOptions, false)
  155. this.setData({ url, loadUrl: true, socketOptions: this.socketOptions, reload: true })
  156. this.refers = false
  157. if (refer) {
  158. console.error('激活视频?1')
  159. this.setData({ webviewUrl: null })
  160. setTimeout(() => {
  161. this.joinUrl()
  162. }, 500)
  163. } else {
  164. console.error('激活视频?2')
  165. this.joinUrl()
  166. socketApi.onShow.call(this)
  167. }
  168. } else {
  169. console.error('激活视频?3')
  170. socketApi.onShow.call(this)
  171. }
  172. },
  173. /**
  174. * 生命周期函数--监听页面隐藏
  175. */
  176. onHide: function () {
  177. setTimeout(() => {
  178. let refers = ['pages/index/index', 'pages/catalog/catalog', 'pages/cart/cart', 'pages/ucenter/index/index']
  179. // let refers = []
  180. let pages = getCurrentPages()
  181. let route = pages[pages.length - 1].route
  182. if (~refers.indexOf(route)) {
  183. this.refers = true
  184. this.setData({...initData})
  185. wx.nextTick(() => {
  186. this.socketSendMessage('stopCall', {})
  187. this.stopCall()
  188. this.socketStop()
  189. this.setData({ webviewUrl: null })
  190. })
  191. } else {
  192. socketApi.onHide.call(this)
  193. }
  194. }, 100)
  195. },
  196. /**
  197. * 生命周期函数--监听页面卸载
  198. */
  199. onUnload: function () {
  200. },
  201. /**
  202. * 页面相关事件处理函数--监听用户下拉动作
  203. */
  204. onPullDownRefresh: function () {
  205. },
  206. /**
  207. * 页面上拉触底事件的处理函数
  208. */
  209. onReachBottom: function () {
  210. },
  211. getUrl(...args) {
  212. let url = socketApi.getUrl.call(this, ...args)
  213. let num = wx.getStorageSync('lastSceneNum')
  214. url += num ? `&lastSceneNum=${num}` : ''
  215. return url
  216. },
  217. webMessage(e) {
  218. console.error('接收', e)
  219. let msgs = e.detail.data.filter(item => item.type === 'lastSceneNum')
  220. let msg = msgs[msgs.length - 1]
  221. if (msg) {
  222. wx.setStorageSync('lastSceneNum', msg.num)
  223. }
  224. }
  225. })