y100.js
9.72 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_Y100 = 'act_article_y100';
const playExpireTime = 45 * 1000;
const showExpireTime = 30 * 1000;
class Y100Model extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取Y100列表
* @returns {*}
*/
articleY100List({actId, pageNo, pageSize, tag}) {
const params = {
actId,
start: (pageNo - 1) * pageSize,
page: _.parseInt(pageSize)
};
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
if (tag) {
sql += ' and AAY.tag like :tag';
params.tag = `%${tag}%`;
sql += ' ORDER BY AA.good_count DESC';
} else {
sql += ' and AAY.top = 1';
}
sql += ' LIMIT :start, :page';
return mysqlCli.query(sql, params);
}
/**
* 获取Y100列表-随机
* @returns {*}
*/
async articleY100RandomList({actId, num}) {
const params = {
actId,
};
let maxSql = `
SELECT
max(\`index\`) as maxIndex
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
WHERE AAy.act_id = :actId`;
const maxResult = await mysqlCli.query(maxSql, params);
let maxIndex = maxResult[0].maxIndex;
if (num > maxIndex) {
throw '随机数量超过数据库大小';
}
const ids = [],
selectIds = [];
for (let i = 0; i <= maxIndex; i++) {
ids.push(i);
}
for (let i = 0; i < num; i++) {
const index = parseInt(Math.random() * ids.length, 10);
selectIds.push(ids[index]);
ids.splice(index, 1);
}
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
sql += ` and AAY.index in (${selectIds.join(',')})`;
sql += ` order by field(AAY.index,${selectIds.join(',')});`;
return mysqlCli.query(sql, params);
}
/**
* 获取Y100详情
* @returns {*}
*/
async articleY100Detail(id) {
let sql = `
SELECT
AAY.id,
AAY.act_id,
AAY.article_id,
AAY.img_url,
AAY.tag,
AAY.name,
AAY.career,
AAY.style,
AAY.interest,
AAY.skns,
AAY.create_time,
AA.good_count
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
INNER JOIN ${TABLE_ACT_ARTICLE} AA ON AAY.article_id = AA.id
where AAY.id = :id`;
const query = await mysqlCli.query(sql, {
id
});
if (query.length === 1) {
const result = query[0];
const {skns} = query[0];
if (skns) {
const productsResult = await this.get({
data: {
method: 'h5.product.batch',
limit: skns.split(',').length,
productSkn: skns,
contain_all: 'Y'
},
param: {
cache: true
}
});
if (productsResult.code === 200 && productsResult.data) {
result.products = productsResult.data.product_list;
}
}
return result;
}
}
/**
* 使用设备更新用户信息
* @returns {*}
*/
async deviceUse({openId, userName, avatar, deviceNo, actId}) {
const deviceQuery = await mysqlCli.query(
`SELECT
user_id,
user_name,
user_avatar,
user_image,
user_label,
expire_time,
status
FROM
act_devices_y100
WHERE
act_id = :actId
AND
device_no = :deviceNo`,
{
deviceNo,
actId,
});
if (deviceQuery.length !== 1) {
return {
code: 400,
message: '设备信息错误'
};
}
const device = _.head(deviceQuery);
if (device.expire_time && device.expire_time > Date.now()) {
return {
code: 401,
message: `用户${device.user_name}正在使用大屏,请稍等一会`
};
}
const expireTime = Date.now() + playExpireTime;
await mysqlCli.update(`
UPDATE
act_devices_y100
SET
user_id = :openId,
user_name = :userName,
user_avatar = :avatar,
user_image = '',
user_label = '',
expire_time = :expireTime,
status = 0
WHERE
act_id = :actId
AND
device_no = :deviceNo`, {
openId,
userName,
avatar,
expireTime,
actId,
deviceNo
});
return {
code: 200,
message: '扫描成功',
data: {
expireTime
}
};
}
/**
* 使用设备更新用户信息
* @returns {*}
*/
async devicePost({openId, deviceNo, actId, userImage, userLabel}) {
const deviceQuery = await mysqlCli.query(
`SELECT
user_id,
user_name,
user_avatar,
user_image,
user_label,
expire_time,
status
FROM
act_devices_y100
WHERE
act_id = :actId
AND
device_no = :deviceNo`,
{
deviceNo,
actId,
});
if (deviceQuery.length !== 1) {
return {
code: 400,
message: '设备信息错误'
};
}
const device = _.head(deviceQuery);
if (device.expire_time <= Date.now()) {
return {
code: 400,
message: '提交超时请再玩一次吧'
};
}
if (device.user_id !== openId) {
return {
code: 401,
message: `用户${device.user_name}正在使用大屏,请稍等一会`
};
}
const expireTime = Date.now() + showExpireTime;
await mysqlCli.update(`
UPDATE
act_devices_y100
SET
user_image = :userImage,
user_label = :userLabel,
expire_time = :expireTime,
status = 1
WHERE
act_id = :actId
AND
device_no = :deviceNo`, {
openId,
expireTime,
userImage,
userLabel,
actId,
deviceNo
});
return {
code: 200,
message: '提交成功',
};
}
/**
* 检查设备状态
* @returns {*}
*/
async checkDeviceStatus({deviceNo, actId}) {
const deviceQuery = await mysqlCli.query(
`SELECT
user_id,
user_name,
user_avatar,
user_image,
user_label,
expire_time,
status
FROM
act_devices_y100
WHERE
act_id = :actId
AND
device_no = :deviceNo`,
{
deviceNo,
actId,
});
if (deviceQuery.length !== 1) {
return {
code: 400,
message: '设备信息错误'
};
}
const device = _.head(deviceQuery);
if (device.expire_time > Date.now()) {
if (device.status === 0) {
return {
code: 1002,
message: 'playing',
data: {
openId: device.user_id,
userName: device.user_name,
avatar: device.user_avatar,
expireTime: device.expire_time
}
};
}
if (device.status === 1) {
return {
code: 10003,
message: 'show image',
data: {
openId: device.user_id,
userName: device.user_name,
avatar: device.user_avatar,
userImage: device.user_image,
userLabel: device.user_label,
expireTime: device.expire_time
}
};
}
}
return {
code: 1001,
message: 'show qrcode'
};
}
}
module.exports = Y100Model;