discover.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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: 0,
  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. if (!wx.getStorageSync('token')) {
  47. return;
  48. }
  49. this.getRoomList()
  50. this.socketOptions = await this.socketStart({sceneId: ''})
  51. this.base = remote.viewChildHost + '/discover.html?appname=shop&origin=4Dplaza'
  52. console.log(this.base)
  53. // this.base = 'https://test.4dkankan.com/discover.html?appname=shop'
  54. const url = this.getUrl(this.base, this.socketOptions, false)
  55. this.setData({ url, loadUrl: true, socketOptions: this.socketOptions })
  56. this.refers = false
  57. this.data.reload = true
  58. this.joinUrl()
  59. },
  60. async getRoomList() {
  61. let {data} = await util.request(api.RoomList + '?pageNum=1&pageSize=1000', {}, 'GET')
  62. // let aaa = true
  63. data.list.forEach(item => {
  64. // 101:直播中,102:未开始,103已结束,104禁播,105:暂停,106:异常,107:已过期
  65. item.state = item.liveStatus === 101
  66. // item.state = aaa
  67. // aaa = !aaa
  68. })
  69. this.setData({roomList: data.list})
  70. },
  71. gotoShop(ev) {
  72. let id = ev.currentTarget.dataset.item.brandId
  73. wx.navigateTo({
  74. url: '/pages/webview/index?id=' + id,
  75. })
  76. },
  77. async scene(data) {
  78. this.cacheData = data
  79. console.log('进入场景', data)
  80. if (data.data.cmd === 'enter') {
  81. let {data: {brand}} = await util.request(api.BrandDetailByScene + '?sceneNum=' + data.data.num)
  82. if (brand) {
  83. this.options.id = brand.id
  84. this.setData({
  85. id: brand.id,
  86. newPicUrl: brand.listPicUrl,
  87. showCommodityCtrl: true
  88. })
  89. this.pauseVideo = true
  90. this.mcode = data.data.num
  91. console.log('设置', data.data.num)
  92. this.setData({numCode: data.data.num})
  93. this.getBrand(brand.id, data.data.num)
  94. this.joinUrl()
  95. }
  96. } else if (data.data.cmd === 'exit') {
  97. this.data.navList = null
  98. this.pauseVideo = false
  99. this.setData({ ...initData, url: this.data.url, numCode: null })
  100. // , showTab: false
  101. }
  102. wx.setStorageSync('lastSceneNum', data.data.num)
  103. },
  104. exit() {
  105. this.setData({webviewUrl: null})
  106. socketApi.exit.bind(this)()
  107. },
  108. onShareAppMessage: function (res) {
  109. let { id, newPicUrl } = this.data
  110. if (res.from === 'button') {
  111. this.setData({ sendShare: false })
  112. return {
  113. imageUrl: newPicUrl,
  114. path: `/pages/webview/index?id=${id}&join=true&roomId=${this.data.socketOptions.roomId}`,
  115. }
  116. }
  117. },
  118. gotoGood(ev) {
  119. let {goodsId} = ev.target.dataset.item
  120. wx.navigateTo({
  121. url: '/pages/goods/goods?id=' + goodsId,
  122. })
  123. },
  124. showTabs(data) {
  125. this.setData( {showTab: data.data} )
  126. },
  127. changeTab(ev) {
  128. this.setData({ showTab: false })
  129. wx.nextTick(() => {
  130. if (ev.target.dataset.tab === 'live') {
  131. // this.setData({ ...initData, showTab: true, currTab: ev.target.dataset.tab })
  132. this.setData({ showTab: true, currTab: ev.target.dataset.tab })
  133. this.pauseVideo = true
  134. this.joinUrl()
  135. } else {
  136. this.setData({ showTab: true, currTab: ev.target.dataset.tab })
  137. this.pauseVideo = false
  138. this.joinUrl()
  139. }
  140. })
  141. },
  142. gotoLive(ev) {
  143. let item = ev.currentTarget.dataset.item
  144. const goodPath = item.liveRoomGoodsVos.length ?
  145. encodeURIComponent(JSON.stringify({ path: 'pages/goods/goods', id: item.liveRoomGoodsVos[0].goodsId })) : null
  146. wx.navigateTo({
  147. url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${item.roomId}${goodPath ? `&custom_params=${goodPath}` : ''}`,
  148. })
  149. },
  150. /**
  151. * 生命周期函数--监听页面初次渲染完成
  152. */
  153. onReady: function () {
  154. },
  155. /**
  156. * 生命周期函数--监听页面显示
  157. */
  158. onShow: async function (refer) {
  159. getApp().updateCardCount()
  160. if (!wx.getStorageSync('token')) {
  161. return;
  162. }
  163. console.error(wx.getStorageSync('token'))
  164. if (this.cccache && this.cacheToken !== wx.getStorageSync('token')) {
  165. refer = true
  166. }
  167. this.cacheToken = wx.getStorageSync('token');
  168. this.cccache = true
  169. if (this.refers || refer) {
  170. this.socketOptions = await this.socketStart({sceneId: ''})
  171. this.base = remote.viewChildHost + '/discover.html?appname=shop&origin=4Dplaza'
  172. const url = this.getUrl(this.base, this.socketOptions, false)
  173. this.setData({ url, loadUrl: true, socketOptions: this.socketOptions, reload: true })
  174. this.refers = false
  175. if (refer) {
  176. this.setData({ webviewUrl: null })
  177. setTimeout(() => {
  178. console.error('1111')
  179. this.joinUrl()
  180. }, 500)
  181. } else {
  182. console.error('2222')
  183. this.joinUrl()
  184. socketApi.onShow.call(this)
  185. }
  186. } else {
  187. this.setData({
  188. showComtypesAllTab: false
  189. })
  190. if (this.socketSendMessage) {
  191. let pages = getCurrentPages()
  192. this.pauseVideo = !(pages[pages.length - 1].route === 'pages/discover/discover' && this.data.currTab === 'webview' && !this.data.numCode)
  193. console.error('3333')
  194. this.joinUrl()
  195. this.socketSendMessage('changeOnlineStatus', { status: true })
  196. }
  197. }
  198. },
  199. /**
  200. * 生命周期函数--监听页面隐藏
  201. */
  202. onHide: function () {
  203. setTimeout(() => {
  204. let refers = ['pages/index/index', 'pages/catalog/catalog', 'pages/cart/cart', 'pages/ucenter/index/index']
  205. // let refers = []
  206. let pages = getCurrentPages()
  207. let route = pages[pages.length - 1].route
  208. if (~refers.indexOf(route)) {
  209. this.refers = true
  210. this.setData({...initData})
  211. wx.nextTick(() => {
  212. this.socketSendMessage('stopCall', {})
  213. this.stopCall()
  214. this.socketStop()
  215. this.setData({ webviewUrl: null })
  216. })
  217. } else {
  218. console.error('aaaaa')
  219. socketApi.onHide.call(this)
  220. }
  221. }, 100)
  222. },
  223. /**
  224. * 生命周期函数--监听页面卸载
  225. */
  226. onUnload: function () {
  227. },
  228. /**
  229. * 页面相关事件处理函数--监听用户下拉动作
  230. */
  231. onPullDownRefresh: function () {
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom: function () {
  237. },
  238. getUrl(...args) {
  239. console.error('bbbbbbb')
  240. let url = socketApi.getUrl.call(this, ...args)
  241. let num = wx.getStorageSync('lastSceneNum')
  242. let params = {
  243. lastSceneNum: num,
  244. nickname: encodeURI(encodeURI(this.socketOptions.nickname)),
  245. avatar: encodeURI(encodeURI(this.socketOptions.avatar))
  246. }
  247. let p = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
  248. url = url + '&' + p
  249. console.error(p)
  250. return url
  251. },
  252. webMessage(e) {
  253. console.error('接收', e)
  254. let msgs = e.detail.data.filter(item => item.type === 'lastSceneNum')
  255. let msg = msgs[msgs.length - 1]
  256. if (msg) {
  257. wx.setStorageSync('lastSceneNum', msg.num)
  258. }
  259. }
  260. })