liveMiddle.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view>
  3. <!--pages/liveMiddle/liveMiddle.wxml-->
  4. <image :src="date.cover_img" class="background"></image>
  5. <view class="button">
  6. <view class="btn" @tap="intoLive" v-if="flag">{{star?'进入直播间':'直播未开始'}}</view>
  7. <view class="btn" @tap="intoLive" v-else>查看回放</view>
  8. <view class="btn1" @tap="showShare">分享海报</view>
  9. </view>
  10. <v-share :hide-toast="hideShare" :product="date" :code-url="codeUrl" a-type="6" @onFather="click"></v-share>
  11. <!-- <v-share hide-toast="{{hideShare}}" product="{{pro}}" code-url="{{codeUrl}}" bg-url="{{bgUrl}}"></v-share> -->
  12. </view>
  13. </template>
  14. <script>
  15. // pages/liveMiddle/liveMiddle.js
  16. const req = require("../../utils/request.js");
  17. const util = require("../../utils/util.js");
  18. const app = getApp();
  19. import vShare from "../../components/share/share";
  20. export default {
  21. data() {
  22. return {
  23. flag: true,
  24. hideShare: true,
  25. star: true,
  26. date: '',
  27. codeUrl: ""
  28. };
  29. },
  30. components: {
  31. vShare
  32. },
  33. props: {},
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function(options) {
  38. this.isLoad = true;
  39. this.query = options;
  40. if (options.scene) {
  41. this.scene = options.scene;
  42. }
  43. if (options.isShare) {
  44. this.query.roomid = options.id;
  45. req.setStorage('pidCode', options.userId);
  46. }
  47. this.getList().then(resolve=>{
  48. // console.log("debug1", resolve);
  49. this.getStar();
  50. })
  51. // console.log(options);
  52. },
  53. //分享
  54. onShareAppMessage: function() {
  55. // console.log('直播间详情', this.date);
  56. // console.log('/pages/liveMiddle/liveMiddle?id=' + this.query.id + '&isShare=' + true + '&userId=' + req
  57. // .getStorage(
  58. // 'userInfo').id);
  59. return {
  60. title: req.getStorage('userInfo').nickName + '邀请您观看直播:' + this.date.name,
  61. imageUrl: this.date.cover_img,
  62. path: '/pages/liveMiddle/liveMiddle?id=' + this.query.id + '&isShare=' + true + '&userId=' + req
  63. .getStorage(
  64. 'userInfo').id
  65. };
  66. },
  67. /**
  68. * 生命周期函数--监听页面显示
  69. */
  70. onShow: async function() {
  71. await this.loadCodeParams();
  72. },
  73. methods: {
  74. click(e) {
  75. // console.log(e)
  76. this.hideShare = e
  77. },
  78. getList() {
  79. return new Promise((resolve, reject) => {
  80. let that = this;
  81. if (!that.isLoad) return false;
  82. that.isLoad = false;
  83. let form = {
  84. page: 1,
  85. limit: 100
  86. };
  87. let date = {};
  88. req.getRequest('/api/live/list', form, data => {
  89. if (data) {
  90. data.map(item => {
  91. item.time = util.transTime(item.start_time);
  92. return item;
  93. }); // console.log(data)
  94. data.forEach(res => {
  95. if (res.roomid == this.query.id) {
  96. date = res;
  97. }
  98. });
  99. that.setData({
  100. date: date
  101. });
  102. resolve(date);
  103. }
  104. });
  105. });
  106. },
  107. onShareTimeline() {
  108. return {
  109. title: '直播分享' + this.date.name,
  110. imageUrl: this.date.cover_img
  111. };
  112. },
  113. // 分享
  114. showShare() {
  115. // #ifndef H5
  116. req.isLogin().then(success => {
  117. if (success) {
  118. this.getCodeUrl();
  119. this.setData({
  120. hideShare: false
  121. });
  122. }
  123. });
  124. // #endif
  125. },
  126. // 解析路由参数
  127. loadCodeParams() {
  128. let _ts = this;
  129. // console.log(_ts.scene);
  130. return new Promise((resolve, reject) => {
  131. if (!_ts.scene) {
  132. resolve();
  133. return false;
  134. }
  135. req.getRequest('/api/code/params', {
  136. scene: _ts.scene
  137. }, data => {
  138. // console.log(data);
  139. this.query.id = data.scene;
  140. req.setStorage('pidCode', data.userId);
  141. resolve();
  142. });
  143. });
  144. },
  145. getCodeUrl() {
  146. let that = this; //获取小程序码
  147. const params = {
  148. page: 'pages/liveMiddle/liveMiddle',
  149. scene: this.query.id
  150. };
  151. // console.log("pa--", params);
  152. req.getRequest('/api/program/codev', params, url => {
  153. // console.log(url);
  154. that.setData({
  155. codeUrl: url
  156. });
  157. });
  158. },
  159. //获取当前直播间的状态码 101:直播中,102:未开始, 可以直接进入直播间
  160. // 103已结束, 只能查看回放
  161. // 104禁播,105:暂停,106:异常,107:已过期 未处理
  162. getStar() {
  163. // console.log(this.date.time);
  164. let status = this.date.live_status;
  165. this.getDate(this.date);
  166. this.date.month = this.year;
  167. this.date.hour = this.hour;
  168. let str = this.date.share_img;
  169. if (str.indexOf("https") == -1) {
  170. this.date.share_img = str.replace(/http/, 'https');
  171. }
  172. let res = this.date;
  173. this.setData({
  174. date: res
  175. }); //状态码为101
  176. if (status == 101) {
  177. this.setData({
  178. flag: true,
  179. star: true,
  180. date: res
  181. });
  182. } //状态码为102
  183. if (status == 102) {
  184. this.setData({
  185. flag: true,
  186. star: false,
  187. date: res
  188. });
  189. } //状态码为103
  190. if (status == 103) {
  191. this.setData({
  192. flag: false
  193. });
  194. }
  195. },
  196. // 进入直播间
  197. intoLive(event) {
  198. let roomId = this.date.roomid;
  199. let customParams = encodeURIComponent(JSON.stringify({
  200. isShare: true,
  201. userId: req.getStorage(
  202. 'userInfo').id
  203. }))
  204. // let redirect=`plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
  205. // uni.navigateToMiniProgram({
  206. // appId:'wxae03e9c5b5fcb1fb',
  207. // path:redirect
  208. // })
  209. uni.navigateTo({
  210. url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
  211. });
  212. },
  213. /**
  214. *
  215. * 直播开始时间转换 year-month-day hour:minute:00
  216. *
  217. */
  218. getDate(str) {
  219. // console.log(str)
  220. if (str.time) {
  221. // str = this.date.time;
  222. var strArr = str.time.split(' ');
  223. // console.log(strArr)
  224. var time = strArr[0].toString().split('-');
  225. var hose = strArr[1].toString().split(':');
  226. var hour = hose[0];
  227. var minute = hose[1];
  228. var day = time[2];
  229. var month = time[1];
  230. var year = time[0];
  231. var date = (month + '月' + day + '日').toString();
  232. var date1 = (hour + ':' + minute).toString();
  233. var date2 = strArr[0];
  234. this.year = date
  235. this.hour = date1
  236. }
  237. },
  238. getLive(id) {
  239. return new Promise((resolve, reject) => {
  240. req.getRequest('/api/live/playBack', {
  241. page: 1,
  242. limit: 10,
  243. room_id: id
  244. }, data => {
  245. this.videoList = data;
  246. resolve();
  247. });
  248. });
  249. },
  250. async playBack(event) {
  251. let roomId = this.date.roomid;
  252. await this.getLive(roomId);
  253. let videoList = JSON.stringify(this.videoList);
  254. if (videoList.length < 1) return req.msg('回放生成中');
  255. uni.navigateTo({
  256. url: '/pages/video/video?videoList=' + videoList
  257. });
  258. }
  259. }
  260. };
  261. </script>
  262. <style>
  263. @import "./liveMiddle.css";
  264. </style>