liveMiddle.vue 7.6 KB

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