-
Notifications
You must be signed in to change notification settings - Fork 130
/
store.js
597 lines (532 loc) · 13.7 KB
/
store.js
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* 条目
* @Params: { _ningMoeId, _jp, _cn, _image, _summary }
* @Author: czy0729
* @Date: 2019-03-22 08:49:20
* @Last Modified by: czy0729
* @Last Modified time: 2019-12-20 16:08:53
*/
import { observable, computed } from 'mobx'
import bangumiData from 'bangumi-data'
import {
subjectStore,
discoveryStore,
userStore,
collectionStore
} from '@stores'
import { open, getTimestamp } from '@utils'
import { t, xhrCustom, queue } from '@utils/fetch'
import { appNavigate, getBangumiUrl, getCoverMedium } from '@utils/app'
import store from '@utils/store'
import { info, showActionSheet } from '@utils/ui'
import { IOS, USERID_TOURIST, USERID_IOS_AUTH, HOST_NING_MOE } from '@constants'
import { MODEL_SUBJECT_TYPE, MODEL_EP_STATUS } from '@constants/model'
const namespace = 'ScreenSubject'
const sites = ['bilibili', 'qq', 'iqiyi', 'acfun', 'youku']
const ningmoeCorret = {
炎炎消防队: '炎炎之消防队'
}
export default class ScreenSubject extends store {
state = observable({
visible: false, // 是否显示管理模态框
epsReverse: false, // 章节是否倒序
chap: '', // 书籍
vol: '',
bangumiInfo: {
sites: [], // 动画在线地址
type: '' // 动画类型
},
// 播放源
epsData: {
_loaded: false
},
_loaded: true
})
init = async () => {
const state = await this.getStorage(undefined, this.namespace)
this.setState({
...state,
visible: false,
chap: '',
vol: '',
_loaded: true
})
const res = subjectStore.fetchSubject(this.subjectId)
const data = await res
const item = bangumiData.items.find(item => item.title === data.name)
if (item) {
this.setState({
bangumiInfo: {
sites: item.sites,
type: item.type
}
})
}
// 获取其他源头eps在线地址
if (this.type === '动画') {
const { _ningMoeId } = this.params
if (_ningMoeId) {
discoveryStore.fetchNingMoeDetail({
id: _ningMoeId,
bgmId: this.subjectId
})
} else {
// 柠萌瞬间有时候条目名会有差异, 比如bgm叫炎炎消防队, 柠萌就叫炎炎之消防队
const name = data.name_cn || data.name
discoveryStore.fetchNingMoeDetailBySearch({
keyword: ningmoeCorret[name] ? ningmoeCorret[name] : name
})
}
}
queue([
() => collectionStore.fetchCollection(this.subjectId),
() => userStore.fetchUserProgress(this.subjectId),
() => subjectStore.fetchSubjectEp(this.subjectId),
async () => {
const res = subjectStore.fetchSubjectFormHTML(this.subjectId)
const { book } = await res
this.setState({
chap: book.chap || '0',
vol: book.vol || '0'
})
return res
},
() => this.fetchEpsData(),
() => this.fetchSubjectComments(true)
])
return res
}
// -------------------- fetch --------------------
/**
* 条目留言
*/
fetchSubjectComments = (refresh, reverse) =>
subjectStore.fetchSubjectComments(
{ subjectId: this.subjectId },
refresh,
reverse
)
/**
* 获取单集播放源
* https://github.com/ekibun/bangumi_onair/blob/master/onair/100/100444.json
*/
fetchEpsData = async () => {
if (this.type === '动画') {
try {
/**
* 旧源头
* https://raw.githubusercontent.com/ekibun/bangumi_onair/master
*/
const { _response } = await xhrCustom({
url: `https://cdn.jsdelivr.net/gh/ekibun/bangumi_onair@latest/onair/${parseInt(
parseInt(this.subjectId) / 1000
)}/${this.subjectId}.json`
})
const epsData = {
_loaded: getTimestamp()
}
sites.forEach(item => (epsData[item] = {}))
JSON.parse(_response).eps.forEach((item, index) => {
item.sites.forEach(i => {
if (sites.includes(i.site)) {
epsData[i.site][index] = i.url
}
})
})
this.setState({
epsData
})
this.setStorage(undefined, undefined, this.namespace)
} catch (error) {
warn(namespace, 'fetchEpsData', error)
}
}
}
// -------------------- get --------------------
@computed get subjectId() {
const { subjectId } = this.params
return subjectId
}
/**
* 命名空间
*/
@computed get namespace() {
return `${namespace}|${this.subjectId}`
}
/**
* 是否登陆(token)
*/
@computed get isLogin() {
return userStore.isLogin
}
/**
* 用户id
*/
@computed get userId() {
return userStore.userInfo.id
}
/**
* 条目信息
*/
@computed get subject() {
return subjectStore.subject(this.subjectId)
}
/**
* 柠萌瞬间ep数据
*/
@computed get ningMoeDetail() {
return discoveryStore.ningMoeDetail(this.subjectId)
}
/**
* 条目信息(来自网页)
*/
@computed get subjectFormHTML() {
return subjectStore.subjectFormHTML(this.subjectId)
}
/**
* 章节信息
*/
@computed get subjectEp() {
return subjectStore.subjectEp(this.subjectId)
}
/**
* 条目留言
*/
@computed get subjectComments() {
return subjectStore.subjectComments(this.subjectId)
}
/**
* 条目收藏信息
*/
@computed get collection() {
return collectionStore.collection(this.subjectId)
}
/**
* 用户章节记录
*/
@computed get userProgress() {
return userStore.userProgress(this.subjectId)
}
/**
* 条目类型中文
*/
@computed get type() {
const { _loaded, type: _type } = this.subject
if (!_loaded) {
return ''
}
return MODEL_SUBJECT_TYPE.getTitle(_type)
}
// Ep偏移
@computed get ningMoeEpOffset() {
return (
this.subjectEp.eps
.filter(item => item.type === 0)
.sort((a, b) => a.sort - b.sort)[0].sort - 1
)
}
/**
* 章节在线播放源
*/
@computed get onlinePlayActionSheetData() {
const data = []
if (this.ningMoeDetail.id) {
data.push('柠萌瞬间')
}
const { epsData } = this.state
sites.forEach(item => {
if (epsData[item] && Object.keys(epsData[item]).length) {
data.push(item)
}
})
data.push('取消')
return data
}
/**
* 条目动作
*/
@computed get action() {
switch (this.type) {
case '音乐':
return '听'
case '游戏':
return '玩'
default:
return '看'
}
}
/**
* 应付iOS审核, iOS且不登陆、游客模式、提供给审核人员的userId不显示
*/
@computed get showOnlinePlay() {
if (!IOS) {
return true
}
if (!this.isLogin) {
return false
}
if (
!this.userId ||
this.userId == USERID_TOURIST ||
this.userId == USERID_IOS_AUTH
) {
return false
}
return true
}
// -------------------- page --------------------
/**
* 显示收藏管理
*/
showManageModel = () => {
t('条目.显示收藏管理', {
subjectId: this.subjectId
})
this.setState({
visible: true
})
}
/**
* 隐藏管理进度信息弹窗
*/
closeManageModal = () =>
this.setState({
visible: false
})
/**
* 章节倒序
*/
toggleReverseEps = () => {
t('条目.章节倒序', {
subjectId: this.subjectId
})
const { epsReverse } = this.state
this.setState({
epsReverse: !epsReverse
})
this.setStorage(undefined, undefined, this.namespace)
}
/**
* 吐槽箱倒序
*/
toggleReverseComments = () => {
t('条目.吐槽箱倒序', {
subjectId: this.subjectId
})
const { _reverse } = this.subjectComments
this.fetchSubjectComments(true, !_reverse)
}
/**
* 书籍章节输入框改变
* @params {*} name 字段
* @params {*} text 文字
*/
changeText = (name, text) => {
t('条目.书籍章节输入框改变', {
subjectId: this.subjectId
})
this.setState({
[name]: String(text)
})
}
// -------------------- action --------------------
/**
* 章节菜单操作
*/
doEpsSelect = async (value, item, navigation) => {
// iOS是本集讨论, 安卓是(+N)...
if (value.includes('本集讨论') || value.includes('(+')) {
t('条目.章节菜单操作', {
title: '本集讨论',
subjectId: this.subjectId
})
// 数据占位
appNavigate(
item.url,
navigation,
{
_title: `ep${item.sort}.${item.name || item.name_cn}`,
_group: this.subject.name || this.subject.name_cn,
_groupThumb: getCoverMedium((this.subject.images || {}).medium),
_desc: `时长:${item.duration} / 首播:${item.airdate}<br />${(
item.desc || ''
).replace(/\r\n/g, '<br />')}`
},
{
id: '条目.跳转',
data: {
from: '章节',
subjectId: this.subjectId
}
}
)
return
}
if (value === '在线播放') {
t('条目.章节菜单操作', {
title: '在线播放',
subjectId: this.subjectId
})
// @todo 查找视频数据源地址
// const find = this.ningMoeDetail.eps.find(i => i.sort === item.sort)
// if (find && find.bakUrl) {
// const realUrl = await discoveryStore.fetchNingMoeRealYunUrl({
// url: find.bakUrl
// })
// if (realUrl) {
// navigation.push('Video', {
// url: realUrl
// })
// return
// }
// }
setTimeout(() => {
showActionSheet(this.onlinePlayActionSheetData, index => {
const isSp = item.type === 1
let url
if (this.onlinePlayActionSheetData[index] === '柠萌瞬间') {
// @notice 像一拳超人第二季这种 要处理EP偏移
if (isSp) {
url = `${HOST_NING_MOE}/detail?line=1&eps=1&bangumi_id=${this.ningMoeDetail.id}`
} else {
url = `${HOST_NING_MOE}/detail?line=1&eps=${item.sort -
this.ningMoeEpOffset}&bangumi_id=${this.ningMoeDetail.id}`
}
} else {
// @todo 逻辑比较复杂, 暂时不处理EP偏移
const { epsData } = this.state
const { eps } = this.subjectEp
const site = this.onlinePlayActionSheetData[index]
let epIndex
if (sites.includes(site)) {
if (isSp) {
url = getBangumiUrl({
id: item.id,
site
})
} else {
epIndex = eps
.filter(item => item.type === 0)
.findIndex(i => i.id === item.id)
url =
epsData[site][epIndex] ||
getBangumiUrl({
id: item.id,
site
})
}
}
}
if (url) {
open(url)
}
})
}, 320)
return
}
// 未收藏不能更改进度
const { status = { name: '未收藏' } } = this.collection
if (status.name !== '未收藏') {
const status = MODEL_EP_STATUS.getValue(value)
if (status) {
t('条目.章节菜单操作', {
title: '更新收视进度',
subjectId: this.subjectId,
status
})
// 更新收视进度
await userStore.doUpdateEpStatus({
id: item.id,
status
})
userStore.fetchUserCollection(this.subjectId)
userStore.fetchUserProgress(this.subjectId)
}
if (value === '看到') {
t('条目.章节菜单操作', {
title: '批量更新收视进度',
subjectId: this.subjectId
})
// 批量更新收视进度
await userStore.doUpdateSubjectWatched({
subjectId: this.subjectId,
sort: item.sort
})
userStore.fetchUserCollection(this.subjectId)
userStore.fetchUserProgress(this.subjectId)
}
return
}
info('收藏了才能管理哦')
}
/**
* 管理收藏
*/
doUpdateCollection = async values => {
t('条目.管理收藏', {
subjectId: this.subjectId
})
await collectionStore.doUpdateCollection(values)
collectionStore.fetchCollection(this.subjectId)
this.closeManageModal()
}
/**
* 更新书籍下一个章节
*/
doUpdateNext = async name => {
t('条目.更新书籍下一个章节', {
subjectId: this.subjectId
})
const { chap, vol } = this.state
// eslint-disable-next-line react/no-access-state-in-setstate
const next = String(parseInt(this.state[name] || 0) + 1)
await collectionStore.doUpdateBookEp({
subjectId: this.subjectId,
chap,
vol,
[name]: next
})
this.setState({
[name]: next
})
info('更新成功')
}
/**
* 更新书籍章节
*/
doUpdateBookEp = async () => {
t('条目.更新书籍章节', {
subjectId: this.subjectId
})
const { chap, vol } = this.state
await collectionStore.doUpdateBookEp({
subjectId: this.subjectId,
chap,
vol
})
info('更新成功')
}
/**
* 章节按钮长按
*/
doEpsLongPress = async ({ id }) => {
t('条目.章节按钮长按', {
subjectId: this.subjectId
})
const userProgress = this.userProgress
let status
if (userProgress[id]) {
// 已观看 -> 撤销
status = MODEL_EP_STATUS.getValue('撤销')
} else {
// 未观看 -> 看过
status = MODEL_EP_STATUS.getValue('看过')
}
await userStore.doUpdateEpStatus({
id,
status
})
userStore.fetchUserCollection(this.subjectId)
userStore.fetchUserProgress(this.subjectId)
}
}