Authored by 郝肖肖

'live-ctx'

/* eslint no-unused-vars: ["error", {"args": "none"}] */
'use strict';
const model = require('../models/live');
const liveModel = require('../models/live');
exports.index = (req, res, next) => {
liveModel.getAllList().then(result => {
req.ctx(liveModel).getAllList().then(result => {
res.render('live/entry', {
title: '直播列表',
module: 'activity',
... ... @@ -30,7 +29,7 @@ exports.main = (req, res, next) => {
res.locals.page = 'live-play';
res.locals.enableFoot = Number(req.query.no) !== 1;
model.fetchInfo(id, isReplay)
req.ctx(liveModel).fetchInfo(id, isReplay)
.then(result => {
if (!result.data) {
return next();
... ... @@ -44,7 +43,7 @@ exports.main = (req, res, next) => {
exports.barrage = (req, res, next) => {
const type = req.query.type;
model.getBarrageHost(type)
req.ctx(liveModel).getBarrageHost(type)
.then(result => {
if (result.code !== 200) {
next(result.message);
... ... @@ -61,7 +60,7 @@ exports.replayBarrage = (req, res, next) => {
const startTime = req.query.startTime;
const timeInterval = req.query.timeInterval;
model.getReplyBarrage(id, startTime, timeInterval)
req.ctx(liveModel).getReplyBarrage(id, startTime, timeInterval)
.then(result => {
res.json(result);
})
... ...
'use strict';
const moment = require('moment');
const service = global.yoho.ServiceAPI;
const serviceAPI = global.yoho.ServiceAPI;
const liveAPI = global.yoho.LiveAPI;
const contentCodeConfig = require('../../../config/content-code');
const resourcesProcess = require(`${global.utils}/resources-process`);
... ... @@ -47,130 +47,145 @@ const _getHumanDuration = (duration) => {
return `${duration[0]}:${duration[1]}:${duration[2]}`;
};
// 获取顶部bannel
let _getBannerData = () => {
return service.get('operations/api/v5/resource/get', {
content_code: contentCodeConfig.live.index,
platform: 'iphone'
}, {
code: 200,
cache: true
}).then((result) => {
return result && result.data ? resourcesProcess(result.data) : [];
});
};
// 获取精选视频
const _getBestList = () => {
return liveAPI.get('v1/living/best', {}, {
code: 200,
cache: false
}).then(result => {
let list = result && result.data || [];
if (result && result.data && result.data.length !== 2) {
result.data = [];
}
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
for (let item of list) {
switch (item.living) {
case 0:
item.pre_living = true;
break;
case 1:
default:
item.now_living = true;
break;
case 2:
// 直播结束不显示
result.data = [];
break;
// 获取顶部bannel
_getBannerData() {
return this.get({
api: serviceAPI,
url: 'operations/api/v5/resource/get',
data: {
content_code: contentCodeConfig.live.index,
platform: 'iphone'
},
param: {
code: 200,
cache: true
}
}).then((result) => {
return result && result.data ? resourcesProcess(result.data) : [];
});
}
// 格式化时间
item.starting_time = _formatTime(item.starting_time * 1000, true);
}
return result && result.data || [];
});
};
// 获取直播中所有视频
const _getLivingList = () => {
return liveAPI.get('v1/living/listing', {}, {
code: 200,
cache: false
}).then(result => {
return result && result.data || [];
});
};
// 获取直播预告列表
const _getPrelivingList = () => {
return liveAPI.get('v1/living/starting', {}, {
code: 200,
cache: false
}).then(result => {
let list = result && result.data || [];
// 获取精选视频
_getBestList() {
return liveAPI.get('v1/living/best', {}, {
code: 200,
cache: false
}).then(result => {
let list = result && result.data || [];
for (let item of list) {
item.starting_time = _formatTime(item.starting_time * 1000);
}
return result && result.data || [];
});
};
// 获取回看列表
const _getRecordList = () => {
return liveAPI.get('v1/living/replaying', {}, {
code: 200,
cache: false
}).then(result => {
return result && result.data || [];
});
};
if (result && result.data && result.data.length !== 2) {
result.data = [];
}
// 返回所有数据
const getAllList = () => {
return Promise.all([_getBestList(), _getLivingList(), _getPrelivingList(), _getRecordList(), _getBannerData()]);
};
for (let item of list) {
switch (item.living) {
case 0:
item.pre_living = true;
break;
case 1:
default:
item.now_living = true;
break;
case 2:
// 直播结束不显示
result.data = [];
break;
}
// 格式化时间
item.starting_time = _formatTime(item.starting_time * 1000, true);
}
// 获取 回放视屏 信息
const fetchReplayInfo = (videoID) => {
let url = 'v1/living/detail';
let data = { video_id: videoID };
let options = { cache: true };
return result && result.data || [];
});
}
return liveAPI.get(url, data, options)
.then(result => {
if (result && result.data) {
let d = result.data;
// 获取直播中所有视频
_getLivingList() {
return liveAPI.get('v1/living/listing', {}, {
code: 200,
cache: false
}).then(result => {
return result && result.data || [];
});
}
d.background = helpers.image(d.background, 640, 968);
d.pic = helpers.image(d.pic, 640, 968);
d.master_pic = helpers.image(d.master_pic, 180, 180);
d.humanTime = _formatTime(data.live_start_time * 1000);
d.video_src = d.url;
// 获取直播预告列表
_getPrelivingList() {
return liveAPI.get('v1/living/starting', {}, {
code: 200,
cache: false
}).then(result => {
let list = result && result.data || [];
// 自定义数据
d.duration = '00:00:00'; // 回看时长 前端JS根据video获取
d.living = 3; // 重播 状态
d.canPlay = true;
d.atEnd = false;
d.isReplay = true;
for (let item of list) {
item.starting_time = _formatTime(item.starting_time * 1000);
}
return result && result.data || [];
});
}
return result || {};
// 获取回看列表
_getRecordList() {
return liveAPI.get('v1/living/replaying', {}, {
code: 200,
cache: false
}).then(result => {
return result && result.data || [];
});
};
}
// 返回所有数据
getAllList() {
return Promise.all([
this._getBestList(),
this._getLivingList(),
this._getPrelivingList(),
this._getRecordList(),
this._getBannerData()
]);
}
// 获取 回放视屏 信息
fetchReplayInfo(videoID) {
let url = 'v1/living/detail';
let data = { video_id: videoID };
let options = { cache: true };
return liveAPI.get(url, data, options)
.then(result => {
if (result && result.data) {
let d = result.data;
d.background = helpers.image(d.background, 640, 968);
d.pic = helpers.image(d.pic, 640, 968);
d.master_pic = helpers.image(d.master_pic, 180, 180);
d.humanTime = _formatTime(data.live_start_time * 1000);
d.video_src = d.url;
// 自定义数据
d.duration = '00:00:00'; // 回看时长 前端JS根据video获取
d.living = 3; // 重播 状态
d.canPlay = true;
d.atEnd = false;
d.isReplay = true;
}
return result || {};
});
}
// 获取 直播视屏 信息
const fetchLiveInfo = (roomID) => {
let url = 'v1/living/detail';
let data = { room_id: roomID };
// 获取 直播视屏 信息
fetchLiveInfo(roomID) {
let url = 'v1/living/detail';
let data = { room_id: roomID };
return liveAPI.get(url, data)
.then(result => {
return liveAPI.get(url, data).then(result => {
if (result && result.data) {
let d = result.data;
... ... @@ -189,37 +204,30 @@ const fetchLiveInfo = (roomID) => {
return result || {};
});
};
const fetchInfo = (id, isReplay) => {
if (isReplay) {
return fetchReplayInfo(id);
} else {
return fetchLiveInfo(id);
}
};
// 获取 直播 弹幕 host
const getBarrageHost = (type) => {
return liveAPI.get('v1/system/gethosts', { type });
};
fetchInfo(id, isReplay) {
if (isReplay) {
return this.fetchReplayInfo(id);
} else {
return this.fetchLiveInfo(id);
}
}
const getReplyBarrage = (videoID, startTime, timeInterval) => {
const url = 'v1/living/getreplaybarrage';
const data = {
startTime,
timeInterval,
video_id: videoID
};
const options = { cache: true };
// 获取 直播 弹幕 host
getBarrageHost(type) {
return liveAPI.get('v1/system/gethosts', { type });
}
return liveAPI.get(url, data, options);
};
getReplyBarrage(videoID, startTime, timeInterval) {
const url = 'v1/living/getreplaybarrage';
const data = {
startTime,
timeInterval,
video_id: videoID
};
const options = { cache: true };
// 处理直播时间
module.exports = {
getAllList,
fetchInfo,
getBarrageHost,
getReplyBarrage
return liveAPI.get(url, data, options);
}
};
... ...