message.js
7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 消息 by acgpiano
'use strict';
const _ = require('lodash');
const moment = require('moment');
const helpers = global.yoho.helpers;
// 格式化时间
const _formatTime = (time) => {
return moment(time).format('HH:mm:ss');
};
// 格式年月日
const _formatDay = (day) => {
return moment(day).format('YYYY.MM.DD');
};
class messageModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
// 获取列表页
_list(result) {
let final = [];
if (result.data && result.data.list) {
// PHP排序了,应该不用
for (let item of result.data.list) {
let obj = {
id: item.id,
isNotReaded: item.is_read === 'Y' ? false : true,
title: item.title,
time: `${item.create_date} ${_formatTime(item.create_time * 1000)}`,
};
if (item.body && item.type === 'pullCoupon') {
Object.assign(obj, {
birthType: true,
// name: item.body.name,
// collarTime: item.body.collar_time,
content: _.get(item, 'body.content', ''),
isCollar: item.body.is_collar === 'Y' ? true : false,
isOverTime: item.body.is_over_time === 'Y' ? true : false
});
}
final.push(obj);
}
}
return final;
}
// 获取生日信息
_getBirthCouponById(uid) {
return this.get({data: {
method: 'app.promotion.queryBirthCoupon',
uid: uid,
couponType: 4,
}}).then(result => {
if (result && result.data) {
let final = {
ticket: []
};
final.birthType = true;
if (result.data.list && result.data.list[0].body.is_collar === 'Y') {
final = {
isCollar: true
};
} else {
if (result.data && !result.data[0].err) {
for (let coupon of result.data) {
final.ticket.push({
id: coupon.id ? coupon.id : '',
couponAmount: coupon.couponAmount,
couponName: coupon.couponName.split('-')[0],
startTime: _formatDay(coupon.startTime),
endTime: _formatDay(coupon.endTime),
useLimit: coupon.useLimit
});
}
} else {
final = {
err: result.data[0].err
};
}
}
return final;
} else {
if (result.code === 410) {
return [{err: result.message}];
} else if (result.code === 411) {
return [{err: result.message}];
} else {
return [];
}
}
});
}
// 获取详情页
_detail(result, id) {
let final = {};
if (result.data && result.data.list) {
for (let item of result.data.list) {
if (item.id === Number(id) && item.type !== 'showGetCoin' && item.type !== 'notice') {
final.sender = item.from;
final.title = item.title;
final.time = `${item.create_date} ${_formatTime(item.create_time * 1000)}`; // 时间
// 判断消息类型
switch (item.type) {
case 'button':
// 促销活动
if (item.body) {
final.sale = {
image: item.body.image ? item.body.image : '',
content: item.body.text ? item.body.text : '',
btnLink: item.body.pc_link ? item.body.pc_link : '',
btnName: item.body.button_text ? item.body.button_text : '',
};
}
break;
case 'pushCoupon':
// 查看优惠券
if (item.body) {
final.coupons = [];
final.coupons.push({
remark: item.body.coupon_name ? item.body.coupon_name : '',
useTime: item.body.time ? item.body.time : '',
id: item.body.inboxId ? item.body.inboxId : '',
price: item.body.price ? item.body.price : '',
url: helpers.urlFormat('/home/coupons', {
t: (new Date().getTime()) / 1000,
}),
});
}
break;
default:
// 普通文本
final.text = {
content: item.body.content ? item.body.content : '',
};
break;
}
}
}
}
return final;
}
// 获取列表或详情
getList(params) {
return this.get({data: {
method: 'app.inbox.getlist',
page: params.page || 1,
size: params.size || 10,
uid: params.uid,
}}).then(result => {
if (params.msgid) {
return this._detail(result, params.msgid);
} else {
return this._list(result);
}
});
}
// 删除消息
delMsg(params) {
return this.get({data: {
method: 'app.inbox.delmessage',
uid: params.uid,
id: params.msgid,
}}).then(result => {
if (result && result.code === 200) {
return {
code: 200
};
} else {
return {
code: 400,
message: '出错啦~',
};
}
});
}
// 领取优惠券
pickCoupon(params) {
return this.get({data: {
method: 'app.promotion.getCoupon',
uid: params.uid,
couponId: params.couponId,
}}).then(result => {
if (result && result.code === 200) {
return {
code: 200
};
} else {
return {
code: result.code,
message: result.message,
};
}
});
}
}
module.exports = messageModel;