index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const App = getApp();
  2. // 工具类
  3. import Util from '../../utils/util.js';
  4. // 直播状态
  5. const LiveStatus = {
  6. 101: {
  7. 'name': '直播中',
  8. 'value': 101,
  9. },
  10. 102: {
  11. 'name': '未开始',
  12. 'value': 102,
  13. },
  14. 103: {
  15. 'name': '已结束',
  16. 'value': 103,
  17. },
  18. 104: {
  19. 'name': '禁播',
  20. 'value': 104,
  21. },
  22. 105: {
  23. 'name': '暂停中',
  24. 'value': 105,
  25. },
  26. 106: {
  27. 'name': '异常',
  28. 'value': 106,
  29. },
  30. 107: {
  31. 'name': '已过期',
  32. 'value': 107,
  33. },
  34. };
  35. Page({
  36. /**
  37. * 页面的初始数据
  38. */
  39. data: {
  40. scrollHeight: 0,
  41. list: [], // 列表数据
  42. page: 1, // 当前页码
  43. isLoading: true, // 是否正在加载中
  44. isLastPage: false, // 当前是最后一页
  45. },
  46. /**
  47. * 生命周期函数--监听页面加载
  48. */
  49. onLoad(options) {
  50. let _this = this;
  51. // 设置列表容器高度
  52. _this.setListHeight();
  53. // 获取直播间列表
  54. _this.getLiveRoomList();
  55. },
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow() {
  60. },
  61. /**
  62. * 获取直播间列表
  63. */
  64. getLiveRoomList(isPage, page) {
  65. let _this = this;
  66. App._get('live.room/lists', {
  67. page: page || 1
  68. }, result => {
  69. let resList = result.data.list,
  70. dataList = _this.data.list;
  71. if (isPage == true) {
  72. _this.setData({
  73. 'list.data': dataList.data.concat(resList.data),
  74. isLoading: false,
  75. });
  76. } else {
  77. _this.setData({
  78. list: resList,
  79. isLoading: false,
  80. isLastPage: false,
  81. });
  82. }
  83. // 刷新直播间状态 (体验不佳, 暂不使用)
  84. // _this.setLiveStatusText(resList);
  85. });
  86. },
  87. /**
  88. * 刷新直播间状态
  89. * mix: 因livePlayer.getLiveStatus接口需要间隔1分钟频率轮询, 用户二次进入时体验不佳, 暂不调用
  90. */
  91. setLiveStatusText(list) {
  92. // 引用直播组件
  93. const livePlayer = requirePlugin('live-player-plugin');
  94. let _this = this;
  95. let startIndex = _this.data.list.data.length - list.data.length;
  96. list.data.forEach((itm, idx) => {
  97. let index = startIndex + idx;
  98. let item = _this.data.list.data[index];
  99. let dataKey = 'list.data[' + index + ']';
  100. // 首次获取立马返回直播状态,往后间隔1分钟或更慢的频率去轮询获取直播状态
  101. livePlayer.getLiveStatus({
  102. room_id: item['room_id']
  103. })
  104. .then(res => {
  105. // 101: 直播中, 102: 未开始, 103: 已结束, 104: 禁播, 105: 暂停中, 106: 异常,107:已过期
  106. let liveStatus = res.liveStatus,
  107. liveStatusText1 = LiveStatus[liveStatus]['name'],
  108. liveStatusText2 = liveStatusText1;
  109. if (liveStatus == 101) {
  110. liveStatusText1 = '正在直播中';
  111. } else if (liveStatus == 102) {
  112. liveStatusText1 = _this.semanticStartTime(item.start_time) + ' 开播';
  113. }
  114. _this.setData({
  115. [dataKey + '.live_status']: liveStatus,
  116. [dataKey + '.live_status_text_1']: liveStatusText1,
  117. [dataKey + '.live_status_text_2']: liveStatusText2,
  118. // test
  119. // [dataKey + '.test']: `test: ${item['room_id']}`,
  120. });
  121. console.log(`getLiveStatus: ${item['room_id']}`);
  122. })
  123. .catch(err => {
  124. console.log(`getLiveStatus: ${item['room_id']}`);
  125. });
  126. });
  127. return list;
  128. },
  129. /**
  130. * 语义化开播时间
  131. */
  132. semanticStartTime(startTime) {
  133. // 转换为 YYYYMMDD 格式
  134. let startTimeObj = new Date(Util.format_date(startTime));
  135. let $startDate = Util.dateFormat("YYYYmmdd", startTimeObj);
  136. // 获取今天的 YYYY-MM-DD 格式
  137. let $todyDate = Util.dateFormat("YYYYmmdd", new Date());
  138. // 获取明天的 YYYY-MM-DD 格式
  139. var tomorrowTimeObj = new Date();
  140. tomorrowTimeObj.setTime(tomorrowTimeObj.getTime() + 24 * 60 * 60 * 1000);
  141. let $tomorrowDate = Util.dateFormat("YYYYmmdd", tomorrowTimeObj);
  142. // 使用IF当作字符串判断是否相等
  143. if ($startDate == $todyDate) {
  144. return Util.dateFormat('今天HH:MM', startTimeObj);
  145. } else if ($startDate == $tomorrowDate) {
  146. return Util.dateFormat('明天HH:MM', startTimeObj);
  147. }
  148. // 常规日期格式
  149. return Util.dateFormat('mm/dd HH:MM', startTimeObj);
  150. },
  151. /**
  152. * 下拉到底加载数据
  153. */
  154. onPageDown() {
  155. let _this = this;
  156. // 已经是最后一页
  157. if (_this.data.page >= _this.data.list.last_page) {
  158. _this.setData({
  159. isLastPage: true
  160. });
  161. return false;
  162. }
  163. // 加载下一页列表
  164. _this.getLiveRoomList(true, ++_this.data.page);
  165. },
  166. /**
  167. * 设置列表容器高度
  168. */
  169. setListHeight() {
  170. let _this = this,
  171. systemInfo = wx.getSystemInfoSync();
  172. _this.setData({
  173. scrollHeight: systemInfo.windowHeight * 0.98
  174. });
  175. },
  176. /**
  177. * 进入直播间
  178. */
  179. onTargetLiveRoomIndex(e) {
  180. let roomId = e.currentTarget.dataset.id;
  181. let customParams = {
  182. path: 'pages/index/index',
  183. referee_id: App.getUserId(), // 分销推荐人
  184. };
  185. wx.navigateTo({
  186. url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${encodeURIComponent(JSON.stringify(customParams))}`
  187. });
  188. },
  189. /**
  190. * 分享当前页面
  191. */
  192. onShareAppMessage() {
  193. return {
  194. title: '直播列表',
  195. path: "/pages/live/index?" + App.getShareUrlParams()
  196. };
  197. },
  198. /**
  199. * 分享到朋友圈
  200. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  201. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  202. */
  203. onShareTimeline() {
  204. return {
  205. title: '直播列表',
  206. path: "/pages/live/index?" + App.getShareUrlParams()
  207. };
  208. },
  209. })