|
|
const YoLuckApi = require('./yoluck-api-back');
|
|
|
const _ = require('lodash');
|
|
|
const logger = global.yoho.logger;
|
|
|
|
|
|
const YOLUCK_LIST_TYPE = {
|
|
|
running: 0,
|
|
|
ready: 1,
|
|
|
finished: 2,
|
|
|
joined: 3
|
|
|
};
|
|
|
|
|
|
const YOLUCK_MYLIST_TYPE = {
|
|
|
running: 0,
|
|
|
finished: 1
|
|
|
};
|
|
|
|
|
|
const ACTIVITY = {
|
|
|
UNKNOWN: 0,
|
|
|
READY: 1, // 活动未开始
|
|
|
START: 2, // 活动开始
|
|
|
END: 3, // 活动结束
|
|
|
LUCK: 4, // 抽奖结束
|
|
|
END_LESS_PEOPLE: 5 // 人数不足
|
|
|
};
|
|
|
|
|
|
const STEP = {
|
|
|
ZERO: 0,
|
|
|
FIRST: 1,
|
|
|
SECOND: 2,
|
|
|
THIRD: 3,
|
|
|
FOURTH: 4,
|
|
|
};
|
|
|
|
|
|
const ACTION_BAR_STATUS = {
|
|
|
UNKNOWN: 0,
|
|
|
READY: 1, // 未开始
|
|
|
START: 2, // 参加
|
|
|
ALEADY: 3, // 已参加活动
|
|
|
WAIT: 4, // 等待抽奖中
|
|
|
LUCK: 5, // 抽奖结束
|
|
|
END: 6, // 活动结束
|
|
|
};
|
|
|
|
|
|
class YoLuckService extends global.yoho.BaseModel {
|
|
|
constructor(ctx) {
|
|
|
super(ctx);
|
|
|
this.api = new YoLuckApi(ctx);
|
|
|
}
|
|
|
|
|
|
async index(type = YOLUCK_LIST_TYPE.running, uid) {
|
|
|
try {
|
|
|
const [r1, r2] = await Promise.all([
|
|
|
this.getList({page: 1, type, uid}),
|
|
|
this.getBottomBanner()
|
|
|
]);
|
|
|
|
|
|
if (r1.error) {
|
|
|
return r1;
|
|
|
}
|
|
|
|
|
|
const desc = {
|
|
|
[YOLUCK_LIST_TYPE.running]: '进行中',
|
|
|
[YOLUCK_LIST_TYPE.ready]: '即将开始',
|
|
|
[YOLUCK_LIST_TYPE.finished]: '已结束',
|
|
|
[YOLUCK_LIST_TYPE.joined]: '已参加',
|
|
|
};
|
|
|
|
|
|
const pages = {
|
|
|
tabs: [{
|
|
|
type: YOLUCK_LIST_TYPE.running,
|
|
|
active: false,
|
|
|
key: desc[YOLUCK_LIST_TYPE.running]
|
|
|
}, {
|
|
|
type: YOLUCK_LIST_TYPE.ready,
|
|
|
active: false,
|
|
|
key: desc[YOLUCK_LIST_TYPE.ready]
|
|
|
}, {
|
|
|
type: YOLUCK_LIST_TYPE.finished,
|
|
|
active: false,
|
|
|
key: desc[YOLUCK_LIST_TYPE.finished]
|
|
|
}, {
|
|
|
type: YOLUCK_LIST_TYPE.joined,
|
|
|
active: false,
|
|
|
key: desc[YOLUCK_LIST_TYPE.joined]
|
|
|
}],
|
|
|
panels: [{
|
|
|
type: YOLUCK_LIST_TYPE.running,
|
|
|
active: false,
|
|
|
list: [],
|
|
|
key: desc[YOLUCK_LIST_TYPE.running]
|
|
|
}, {
|
|
|
type: YOLUCK_LIST_TYPE.ready,
|
|
|
active: false,
|
|
|
list: []
|
|
|
}, {
|
|
|
type: YOLUCK_LIST_TYPE.finished,
|
|
|
active: false,
|
|
|
list: [],
|
|
|
key: desc[YOLUCK_LIST_TYPE.running]
|
|
|
}, {
|
|
|
type: YOLUCK_LIST_TYPE.joined,
|
|
|
active: false,
|
|
|
list: [],
|
|
|
key: desc[YOLUCK_LIST_TYPE.joined]
|
|
|
}],
|
|
|
resource: r2,
|
|
|
currentType: type
|
|
|
};
|
|
|
|
|
|
for (let t of Object.keys(pages.tabs)) {
|
|
|
let tab = pages.tabs[t];
|
|
|
let panel = pages.panels[t];
|
|
|
|
|
|
if (tab.type === type) {
|
|
|
tab.active = true;
|
|
|
panel.active = true;
|
|
|
panel.list = r1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return pages;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: e
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async getList({page, type, uid}) {
|
|
|
try {
|
|
|
let result;
|
|
|
|
|
|
if (type === YOLUCK_LIST_TYPE.joined) {
|
|
|
if (!uid) {
|
|
|
return {
|
|
|
error: 400,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
const [resRunning, resFinished] = await Promise.all([
|
|
|
this.getMyListNext({page: 1, limit: 1000, type: YOLUCK_MYLIST_TYPE.running, uid}),
|
|
|
await this.getMyListNext({page: 1, limit: 1000, type: YOLUCK_MYLIST_TYPE.finished, uid})
|
|
|
]);
|
|
|
|
|
|
result = [];
|
|
|
|
|
|
if (resRunning.error) {
|
|
|
result.push([]);
|
|
|
} else {
|
|
|
result.push(resRunning);
|
|
|
}
|
|
|
|
|
|
if (resFinished.error) {
|
|
|
result.push([]);
|
|
|
} else {
|
|
|
result.push(resFinished);
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
} else {
|
|
|
result = await this.api.getList({page, type});
|
|
|
}
|
|
|
|
|
|
if (result.code !== 200) {
|
|
|
return {
|
|
|
error: '出错了'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
return result.data;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: e
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async _getDetail(actPrizeId, uid) {
|
|
|
try {
|
|
|
const result = await this.api.getDetail({actPrizeId, uid});
|
|
|
|
|
|
if (result.code !== 200) {
|
|
|
return {
|
|
|
error: 'error'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
return result.data;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: e
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async _getRecentAvatars(actPrizeId) {
|
|
|
try {
|
|
|
const result = await this.api.getRecentAvatars({actPrizeId});
|
|
|
|
|
|
if (result.code !== 200) {
|
|
|
return {
|
|
|
error: 'error'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
return result.data;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: '出错了'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async getDetail(actPrizeId, uid) {
|
|
|
let userName = uid && _.get(this.ctx.req, 'user.NAME');
|
|
|
|
|
|
const [r1, r2, r3, r4] = await Promise.all([
|
|
|
this._getDetail(actPrizeId, uid),
|
|
|
this._getRecentAvatars(actPrizeId),
|
|
|
this.getDetailBanner(),
|
|
|
userName ? Promise.resolve({}) : this.api._getUsreInfo(uid)
|
|
|
]);
|
|
|
|
|
|
const result = {};
|
|
|
|
|
|
if (r1.error) {
|
|
|
result.error = r1.error;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
result.userName = userName || _.get(r4, 'data.nickname', '') || '';
|
|
|
result.actPrizeId = actPrizeId;
|
|
|
result.product = !r1.error ? r1 : {};
|
|
|
result.avatars = !r2.error ? r2.map(i => {
|
|
|
i.user_name = this.wrapperName(i.user_name);
|
|
|
i.user_thumb = i.user_thumb || 'http://img12.static.yhbimg.com/sns/2018/08/02/15/0237a5305f921865764e8409fcffbd3299.png';
|
|
|
return i;
|
|
|
}) : [];
|
|
|
|
|
|
result.myPrizeCount = !r1.error ? r1.myCodeNum : 0;
|
|
|
result.product.myPrizeCount = result.myPrizeCount;
|
|
|
|
|
|
let participantCount = !r1.error ? r1.joinNum : 0;
|
|
|
|
|
|
if (r1.status === ACTIVITY.END || r1.status === ACTIVITY.LUCK) {
|
|
|
participantCount = r1.limit;
|
|
|
}
|
|
|
|
|
|
let notice;
|
|
|
|
|
|
try {
|
|
|
notice = JSON.parse(result.product.notice);
|
|
|
} catch (e) {} // eslint-disable-line
|
|
|
|
|
|
notice = _.assign({}, notice);
|
|
|
notice.content = notice.content || '关注公众号“潮流有货”,发送“开奖”查看中奖结果';
|
|
|
notice.h5BtnName = notice.h5BtnName || '关注';
|
|
|
|
|
|
result.product.notice = notice;
|
|
|
|
|
|
result.product.participantCount = participantCount;
|
|
|
result.actionStatus = this.setActionStatus(result.product);
|
|
|
result.resource = r3;
|
|
|
|
|
|
this.setStep(result);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
wrapperName(name) {
|
|
|
if (!name) {
|
|
|
return '';
|
|
|
}
|
|
|
|
|
|
let nL = name.length;
|
|
|
let newName = [];
|
|
|
|
|
|
if (nL > 5) {
|
|
|
newName.push(name[0]);
|
|
|
newName.push(name[1]);
|
|
|
newName.push('*');
|
|
|
newName.push('*');
|
|
|
newName.push(name[nL - 1]);
|
|
|
|
|
|
return newName.join('');
|
|
|
} else {
|
|
|
return name;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
formatN(format, num) {
|
|
|
let n = num + '';
|
|
|
let nList = n.split('');
|
|
|
let l = nList.length;
|
|
|
let fl = format.length;
|
|
|
|
|
|
if (format.length < nList.length) {
|
|
|
return nList;
|
|
|
}
|
|
|
|
|
|
let el = fl - l;
|
|
|
|
|
|
for (let i = 0; i < el; i++) {
|
|
|
nList.splice(0, 0, '0');
|
|
|
}
|
|
|
|
|
|
return nList.join('');
|
|
|
}
|
|
|
|
|
|
setActionStatus(product) {
|
|
|
let status = ACTION_BAR_STATUS.START;
|
|
|
let activityStatus = product.status;
|
|
|
|
|
|
if (activityStatus === ACTIVITY.UNKNOWN) {
|
|
|
status = ACTION_BAR_STATUS.END;
|
|
|
} else if (activityStatus === ACTIVITY.READY) {
|
|
|
status = ACTION_BAR_STATUS.READY;
|
|
|
} else if (activityStatus >= ACTIVITY.END) {
|
|
|
if (this.isInActivity(product)) {
|
|
|
if (activityStatus === ACTIVITY.LUCK) {
|
|
|
status = ACTION_BAR_STATUS.LUCK;
|
|
|
} else {
|
|
|
status = ACTION_BAR_STATUS.WAIT;
|
|
|
}
|
|
|
} else {
|
|
|
status = ACTION_BAR_STATUS.END;
|
|
|
}
|
|
|
} else {
|
|
|
if (this.isInActivity(product)) {
|
|
|
status = ACTION_BAR_STATUS.ALEADY;
|
|
|
} else {
|
|
|
status = ACTION_BAR_STATUS.START;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
isInActivity(p) {
|
|
|
return p.myPrizeCount > 0;
|
|
|
}
|
|
|
|
|
|
setStep(result) {
|
|
|
let step = STEP.ZERO;
|
|
|
let activityStatus = result.product.status;
|
|
|
let myPrizeCount = result.product.myPrizeCount;
|
|
|
|
|
|
if (myPrizeCount === 0) {
|
|
|
step = STEP.ZERO;
|
|
|
} else {
|
|
|
if (activityStatus === ACTIVITY.START) {
|
|
|
myPrizeCount = result.myPrizeCount;
|
|
|
|
|
|
if (myPrizeCount === 0) {
|
|
|
step = STEP.ZERO;
|
|
|
} else if (myPrizeCount === 1) {
|
|
|
step = STEP.FIRST;
|
|
|
} else if (myPrizeCount >= 2) {
|
|
|
step = STEP.SECOND;
|
|
|
} else {
|
|
|
step = STEP.ZERO;
|
|
|
}
|
|
|
} else if (activityStatus === ACTIVITY.END || activityStatus === ACTIVITY.LUCK) {
|
|
|
step = STEP.THIRD;
|
|
|
} else if (activityStatus === ACTIVITY.END_LESS_PEOPLE) {
|
|
|
step = STEP.FOURTH;
|
|
|
} else {
|
|
|
step = STEP.ZERO;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
result.step = step;
|
|
|
}
|
|
|
|
|
|
|
|
|
async getMyListNext({page, type, limit, uid}) {
|
|
|
try {
|
|
|
const result = await this.api.getMyList({page, type, uid, limit});
|
|
|
|
|
|
if (result.code !== 200) {
|
|
|
return {
|
|
|
error: '出错了'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
return result.data;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: e
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async getResourceCode(params) {
|
|
|
const result = await this.api._getResourceCode(params);
|
|
|
const url = _.get(result, '[0].data.list[0]', {});
|
|
|
|
|
|
url.width = _.get(result, '[0].data.imageWidth', 0);
|
|
|
url.height = _.get(result, '[0].data.imageHeight', 0);
|
|
|
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
async getDetailBanner() {
|
|
|
try {
|
|
|
const result = await this.getResourceCode({
|
|
|
contentCode: 'ccc32dbedf164a52b4efa34383878860'
|
|
|
});
|
|
|
|
|
|
if (!result.width) {
|
|
|
result.width = 750;
|
|
|
result.height = 140;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: '出错了'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async getBottomBanner() {
|
|
|
try {
|
|
|
const result = await this.getResourceCode({
|
|
|
contentCode: '5a2203f5656fbc9788bd8af70f2823d3'
|
|
|
});
|
|
|
|
|
|
if (!result.width) {
|
|
|
result.width = 750;
|
|
|
result.height = 234;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
} catch (e) {
|
|
|
return {
|
|
|
error: '出错了'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async getCode({shareUid, uid, actPrizeId}) {
|
|
|
try {
|
|
|
let userInfo = await this.api._getUsreInfo(uid);
|
|
|
|
|
|
let userName = _.get(userInfo, 'data.nickname', '');
|
|
|
let userThumb = _.get(userInfo, 'data.head_ico', '');
|
|
|
|
|
|
const result = await this.api.fetchCode({
|
|
|
shareUid,
|
|
|
uid,
|
|
|
actPrizeId,
|
|
|
userThumb,
|
|
|
userName
|
|
|
});
|
|
|
|
|
|
if (result.code !== 200) {
|
|
|
logger.error(result);
|
|
|
|
|
|
return {
|
|
|
error: '错误'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
if (userThumb.indexOf('?') > 0) {
|
|
|
userThumb = _.split(userThumb, '?')[0] + '?imageView2/2/w/70/h/70/q/60';
|
|
|
}
|
|
|
|
|
|
return _.assign({userThumb}, result.data);
|
|
|
} catch (e) {
|
|
|
logger.error(e);
|
|
|
|
|
|
return {
|
|
|
error: '出错了'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async getActivityCodeList(uid, actPrizeId) {
|
|
|
let result = await this.api.getMyListWithActid(uid, actPrizeId);
|
|
|
|
|
|
let codeList = [];
|
|
|
let nearAvatar = '//img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100'; // eslint-disable-line
|
|
|
|
|
|
_.forEach(_.get(result, 'data', []), value => {
|
|
|
if (value.prize_code) {
|
|
|
if (value.user_thumb.indexOf('headimg') > 0) {
|
|
|
value.user_thumb = nearAvatar;
|
|
|
} else {
|
|
|
if (value.user_thumb.indexOf('?') > 0) {
|
|
|
value.user_thumb = _.split(value.user_thumb, '?')[0] + '?imageView2/2/w/70/h/70/q/60';
|
|
|
}
|
|
|
nearAvatar = value.user_thumb;
|
|
|
}
|
|
|
|
|
|
codeList.push({
|
|
|
prizeCode: value.prize_code,
|
|
|
userThumb: value.user_thumb
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
return codeList;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
|
YOLUCK_LIST_TYPE,
|
|
|
YoLuckService
|
|
|
}; |
...
|
...
|
|