@jetthai/cuora 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/plugin/game.js ADDED
@@ -0,0 +1,439 @@
1
+ const api = {
2
+ getGameSHA: () => ({ method: 'get', url: 'game/list/v2/sha' }),
3
+ getGameMap: (code) => ({ method: 'get', url: 'game/list/v2/options', params: { code } }),
4
+ getGameStatus: () => ({ method: 'get', url: 'game/list/v2/status' }),
5
+ getGameCategory: ({ id, first }) => ({ method: 'get', url: 'game/list/v2/category', params: { id, first } }),
6
+ addFavoriteGame: (code) => ({ method: 'post', url: 'game/myfavorite/add', params: code }),
7
+ removeFavoriteGame: (code) => ({ method: 'post', url: 'game/myfavorite/remove', params: code }),
8
+ getStatements: () => ({ url: 'game/statements', method: 'get' }),
9
+ setPreference: ({ defaultCategory }) => ({ url: 'settings/preference', method: 'post', params: { defaultCategory } }),
10
+ playGame: (params) => ({ url: 'game/play', method: 'get', params }),
11
+ logoutGame: ({ uid_site, provider_id }) => ({ url: 'game/logout', method: 'get', params: { uid_site, provider_id } }),
12
+ }
13
+
14
+ function createStore(request, loadingRequest) {
15
+ return {
16
+ namespaced: true,
17
+ state: () => ({
18
+ categoryId: null,
19
+ gameList: [],
20
+ virtualCategoryList: [],
21
+ gameMap: {},
22
+ inverseGameMap: {},
23
+ selectedPrimaryCategory: 'slot',
24
+ primaryCategoryList: [
25
+ { category: 'SYSTEM_BIG_CATEGORY_SLOT', code: null, type: 'slot' },
26
+ { category: 'SYSTEM_BIG_CATEGORY_TABLECARD', code: null, type: 'tablegame' },
27
+ { category: 'SYSTEM_BIG_CATEGORY_FISHING', code: null, type: 'fish' },
28
+ { category: 'SYSTEM_BIG_CATEGORY_VIP', code: null, type: 'vip' },
29
+ { category: 'SYSTEM_BIG_CATEGORY_ARCADE', code: null, type: 'arcade' },
30
+ { category: 'SYSTEM_BIG_CATEGORY_OLD', code: null, type: 'old' },
31
+ ],
32
+ favorite: { category: 'SYSTEM_MY_FAVORITE', code: null, list: [] },
33
+ //
34
+ gameURL: null,
35
+ currentProvider: null,
36
+ currentGameId: null,
37
+ currentGameType: null,
38
+ isMatching: false,
39
+ statements: [],
40
+ gameTimer: null,
41
+ }),
42
+ getters: {
43
+ getCurrentProvider(state) {
44
+ return state.currentProvider || null
45
+ },
46
+ getCurrentGameId(state) {
47
+ return state.currentGameId || null
48
+ },
49
+ getCurrentPlaying(state, getters) {
50
+ return `${getters.getCurrentProvider}.${getters.getCurrentGameId}`
51
+ },
52
+ getCurrentGameType(state, getters) {
53
+ return state.currentGameType
54
+ },
55
+ currentGameURL(state) {
56
+ return state.gameURL
57
+ },
58
+ //
59
+ getCategoryId(state, getters) {
60
+ return state.categoryId
61
+ },
62
+ getGameList(state, getters) {
63
+ return state.gameList
64
+ },
65
+ getVirtualCategoryList(state, getters) {
66
+ return state.virtualCategoryList
67
+ },
68
+ getGameMap(state, getters) {
69
+ return state.gameMap
70
+ },
71
+ getInverseGameMap(state, getters) {
72
+ return state.inverseGameMap
73
+ },
74
+ getFavoriteList(state, getters) {
75
+ return state.favorite?.list || []
76
+ },
77
+ getFavoriteId(state, getters) {
78
+ return state.favorite.code
79
+ },
80
+ getPrimaryCategoryList(state) {
81
+ return state.primaryCategoryList
82
+ },
83
+ getSelectedPrimaryCategory(state, getters) {
84
+ return state.selectedPrimaryCategory
85
+ },
86
+ isMatching(state) {
87
+ return state.isMatching
88
+ },
89
+ getUserStatements(state) {
90
+ return state.statements
91
+ },
92
+ getGameTimer(state) {
93
+ return state.gameTimer
94
+ },
95
+ },
96
+ mutations: {
97
+ setCurrentProvider(state, data) {
98
+ state.currentProvider = data
99
+ },
100
+ setCurrentGameId(state, data) {
101
+ state.currentGameId = data
102
+ },
103
+ setCurrentGameType(state, data) {
104
+ state.currentGameType = data
105
+ },
106
+ setGameURL(state, url) {
107
+ if (url) {
108
+ url = url.replace('isAPP=false', 'isAPP=true')
109
+ }
110
+ state.gameURL = url
111
+ },
112
+ //
113
+ reset(state) {
114
+ state.categoryId = null
115
+ state.gameList = null
116
+ state.virtualCategoryList = []
117
+ state.gameMap = null
118
+ state.favorite = { category: 'SYSTEM_MY_FAVORITE', code: null, list: [] }
119
+ state.inverseGameMap = null
120
+ state.primaryCategoryList = [
121
+ { category: 'SYSTEM_BIG_CATEGORY_SLOT', code: null, type: 'slot' },
122
+ { category: 'SYSTEM_BIG_CATEGORY_TABLECARD', code: null, type: 'tablegame' },
123
+ { category: 'SYSTEM_BIG_CATEGORY_FISHING', code: null, type: 'fish' },
124
+ { category: 'SYSTEM_BIG_CATEGORY_VIP', code: null, type: 'vip' },
125
+ { category: 'SYSTEM_BIG_CATEGORY_ARCADE', code: null, type: 'arcade' },
126
+ { category: 'SYSTEM_BIG_CATEGORY_OLD', code: null, type: 'old' },
127
+ ]
128
+ },
129
+ _setCategoryId(state, data) {
130
+ state.categoryId = data
131
+ },
132
+ setGameList(state, data) {
133
+ state.gameList = data
134
+ },
135
+ _setGameMap(state, data) {
136
+ state.gameMap = data
137
+ },
138
+ _appendGameMap(state, { key, value }) {
139
+ state.gameMap = { ...state.gameMap, [key]: value }
140
+ },
141
+ _setInverseGameMap(state, data) {
142
+ const inverseOption = {}
143
+ for (const key in data) {
144
+ try {
145
+ const arr = data[key]?.id.split('|')
146
+ const composedKey = `${arr[0]}_${arr[3]}`
147
+ inverseOption[composedKey] = key
148
+ } catch (err) {
149
+ console.log(`[Gamehall] Error: { key: ${key}, data: ${data[key]} }`)
150
+ }
151
+ }
152
+ state.inverseGameMap = inverseOption
153
+ },
154
+ _appendInverseGameMap(state, { key, value }) {
155
+ const arr = value.id.split('|')
156
+ const composedKey = `${arr[0]}_${arr[3]}`
157
+ state.inverseGameMap = { ...state.inverseGameMap, [composedKey]: key }
158
+ },
159
+ _setFavorite(state, favorite) {
160
+ state.favorite = favorite
161
+ },
162
+ _setFavoriteList(state, codeList) {
163
+ state.favorite = { ...state.favorite, list: codeList }
164
+ },
165
+ _setPrimaryCategoryList(state, categories) {
166
+ const primaryCategories = [
167
+ 'SYSTEM_BIG_CATEGORY_OLD',
168
+ 'SYSTEM_BIG_CATEGORY_SLOT',
169
+ 'SYSTEM_BIG_CATEGORY_ARCADE',
170
+ 'SYSTEM_BIG_CATEGORY_FISHING',
171
+ 'SYSTEM_BIG_CATEGORY_VIP',
172
+ 'SYSTEM_BIG_CATEGORY_TABLECARD',
173
+ ]
174
+ const validCategories = categories.filter((category) => {
175
+ return primaryCategories.includes(category.category)
176
+ })
177
+
178
+ const updatedCategories = state.primaryCategoryList.map((category) => {
179
+ const matchCategory = validCategories.find((item) => item.category === category.category)
180
+ return { ...category, code: matchCategory?.id }
181
+ })
182
+
183
+ state.primaryCategoryList = updatedCategories
184
+ },
185
+ _setVirtualCategoryList(state, virualCategoryList) {
186
+ const virtual = virualCategoryList?.filter((item) => item.virtual === 1) || []
187
+ state.virtualCategoryList = virtual.filter((item) => item.category !== 'SYSTEM_MY_FAVORITE')
188
+ },
189
+ setSelectedPrimaryCategory(state, data) {
190
+ state.selectedPrimaryCategory = data
191
+ },
192
+ gameMatching(state, data) {
193
+ state.isMatching = data
194
+ },
195
+ setStatements(state, data) {
196
+ state.statements = data
197
+ },
198
+ _setGameTimer(state, timer) {
199
+ state.gameTimer = timer
200
+ },
201
+ },
202
+ actions: {
203
+ async examineGame({ state, getters, commit, dispatch, rootState, rootGetters }) {
204
+ const currentPath = this.$router.history.current.fullPath
205
+ if (isPlayingGame(currentPath)) return
206
+
207
+ const oldSHA = rootGetters['localStorage/getGameV2SHA']
208
+ const newSHA = await request(api.getGameSHA())
209
+ const isGameChanged = newSHA?.status !== oldSHA?.status
210
+ const isCategoryChanged = newSHA?.category !== oldSHA?.category
211
+
212
+ if (isGameChanged) {
213
+ console.log(`[Gamehall] game changed`, newSHA?.status)
214
+ commit(`localStorage/setGameV2SHA`, newSHA, { root: true })
215
+ const oldStatus = rootGetters['localStorage/getGameV2Status']
216
+ const newStatus = await request(api.getGameStatus())
217
+ const infoChangedList = extractInfoChangedGames(oldStatus, newStatus)
218
+ commit(`localStorage/setGameV2Status`, newStatus, { root: true })
219
+
220
+ if (infoChangedList.length > 0) {
221
+ const options = JSON.parse(JSON.stringify(rootState.game.gameMap))
222
+ for (const code of infoChangedList) {
223
+ if (options[code]) {
224
+ delete options[code]
225
+ }
226
+ }
227
+ commit(`_setGameMap`, options)
228
+ }
229
+
230
+ dispatch('fetchGameList', getters.getCategoryId)
231
+ } else if (isCategoryChanged) {
232
+ console.log(`[Gamehall] category changed`, newSHA?.category)
233
+ commit(`localStorage/setGameV2SHA`, newSHA, { root: true })
234
+
235
+ dispatch('fetchGameList', getters.getCategoryId)
236
+ }
237
+
238
+ // check for saving to localstorage
239
+ const optionInRAM = state.gameMap
240
+ const optionInLocal = rootState.localStorage.gameV2PersistedOption
241
+ if (Object.keys(optionInRAM || {}).length !== Object.keys(optionInLocal || {}).length) {
242
+ console.log('[Gamehall] save to local')
243
+ commit('localStorage/setGameV2PersistedOption', optionInRAM, { root: true })
244
+ }
245
+ },
246
+ async addGameOption({ getters, commit, dispatch }, code) {
247
+ const option = await request(api.getGameMap(code))
248
+ if (Object.keys(option || {}).length > 0) {
249
+ commit('_appendGameMap', { key: code, value: option })
250
+ commit('_appendInverseGameMap', { key: code, value: option })
251
+ }
252
+ },
253
+ updateGameOption({ state, getters, commit, dispatch, rootState, rootGetters }, list) {
254
+ const games = list.filter((item) => !!item.g) || []
255
+ games.forEach((game) => {
256
+ const code = game.g
257
+ const options = state.gameMap || {}
258
+ if (!options[code]) {
259
+ dispatch('addGameOption', code)
260
+ }
261
+ })
262
+ },
263
+ async checkGameListPreset({ state, commit, getters, dispatch, rootState, rootGetters }, fullScan) {
264
+ const oldSHA = rootGetters['localStorage/getGameV2SHA']
265
+ const isInLocalStorage = !!oldSHA
266
+ if (!isInLocalStorage || fullScan === true) {
267
+ // 首次使用
268
+ const [newSHA, newStatus, option] = await Promise.all([
269
+ request(api.getGameSHA()),
270
+ request(api.getGameStatus()),
271
+ request(api.getGameMap()),
272
+ ])
273
+ commit('localStorage/setGameV2SHA', newSHA, { root: true })
274
+ commit('localStorage/setGameV2Status', newStatus, { root: true })
275
+ commit('_setGameMap', option)
276
+ commit('_setInverseGameMap', option)
277
+ } else if (!state.gameMap && rootState.localStorage.gameV2PersistedOption) {
278
+ // 重新整理
279
+ console.log('[Gamehall] copy from local')
280
+ commit('_setGameMap', rootState.localStorage.gameV2PersistedOption)
281
+ commit('_setInverseGameMap', rootState.localStorage.gameV2PersistedOption)
282
+ }
283
+ },
284
+ async fetchGameList({ state, commit, getters, dispatch, rootState, rootGetters }, id) {
285
+ await dispatch('checkGameListPreset')
286
+ const currentPath = this.$router.history.current.fullPath
287
+
288
+ if (isInGamehall(currentPath)) {
289
+ commit('_setCategoryId', id)
290
+ const category = await request(api.getGameCategory({ id }))
291
+ const categoryChildren = category?.list || []
292
+
293
+ if (state.primaryCategoryList.map((item) => item.category).includes(category.name)) {
294
+ commit('_setVirtualCategoryList', categoryChildren)
295
+ }
296
+
297
+ if (id) {
298
+ commit('setGameList', categoryChildren)
299
+ } else {
300
+ commit('game/_setPrimaryCategoryList', categoryChildren, { root: true })
301
+ const favoriteId = categoryChildren.find((item) => item.category === 'SYSTEM_MY_FAVORITE').id
302
+ const category = await request(api.getGameCategory({ id: favoriteId }))
303
+ const list = category?.list || []
304
+ commit('_setFavorite', { ...state.favorite, code: favoriteId, list: list.map((item) => item.g) })
305
+ }
306
+ }
307
+ },
308
+ async addFavoriteGame({ state, commit, getters, dispatch, rootState, rootGetters }, code) {
309
+ try {
310
+ const appendedList = [...getters.getFavoriteList, code]
311
+ commit('_setFavoriteList', appendedList)
312
+ await request(api.addFavoriteGame({ games: [code] }))
313
+ } catch (err) {
314
+ console.log(err)
315
+ const restoredList = getters.getFavoriteList.filter((item) => item !== code)
316
+ commit('_setFavoriteList', restoredList)
317
+ }
318
+ },
319
+ async removeFavoriteGame({ state, commit, getters, dispatch, rootState, rootGetters }, code) {
320
+ try {
321
+ const reservedList = getters.getFavoriteList.filter((item) => item !== code)
322
+ commit('_setFavoriteList', reservedList)
323
+ await request(api.removeFavoriteGame({ games: [code] }))
324
+ } catch (err) {
325
+ console.log(err)
326
+ const restoredList = [...getters.getFavoriteList, code]
327
+ commit('_setFavoriteList', restoredList)
328
+ }
329
+ },
330
+ async getStatements({ state, commit, getters, dispatch, rootState, rootGetters }) {
331
+ try {
332
+ commit(`core/setLoading`, true, { root: true })
333
+ const data = await request(api.getStatements())
334
+ commit(`setStatements`, data)
335
+ commit(`core/setLoading`, false, { root: true })
336
+ return data
337
+ } catch (err) {
338
+ commit(`core/setLoading`, false, { root: true })
339
+ throw err
340
+ }
341
+ },
342
+ async setPreference({}, defaultCategory) {
343
+ return await request(api.setPreference({ defaultCategory }))
344
+ },
345
+ executeGameTimer({ state, commit, dispatch }) {
346
+ clearInterval(state.gameTimer)
347
+ dispatch('examineGame')
348
+ const timer = setInterval(() => {
349
+ dispatch('examineGame')
350
+ }, 20000)
351
+ commit('_setGameTimer', timer)
352
+ },
353
+ async exitGame({ commit, rootGetters }) {
354
+ const uid_site = rootGetters['user/userInfo'].uid_site
355
+ const provider_id = rootGetters['gameStore/getCurrentProvider']
356
+ if (provider_id) {
357
+ await loadingRequest(api.logoutGame({ uid_site, provider_id }))
358
+ }
359
+ commit('setCurrentProvider', null)
360
+ commit('setCurrentGameId', null)
361
+ },
362
+ async play({ getters, rootGetters, commit, dispatch }, { game, isDemo = false }) {
363
+ try {
364
+ if (
365
+ (await dispatch('core/checkStatus', null, { root: true })) === true &&
366
+ rootGetters['core/isAuthorized'] &&
367
+ game.system_enable !== 0 &&
368
+ (await dispatch('core/checkVerification', null, { root: true })) === true
369
+ ) {
370
+ commit('gameMatching', true)
371
+ const returnProviders = ['RSG', 'KP', 'EPICWIN', 'KA']
372
+ const lang = rootGetters['core/getLocale'](
373
+ { shorthand: getters.getLocale, isUnderline: true },
374
+ { root: true }
375
+ )
376
+ const params = {
377
+ provider_id: game.provider || game.provider_id,
378
+ game_id: game.id || game.game_id,
379
+ gametype_id: game.game_type || game.gametype_id,
380
+ mapping_id: game.mapping_id || game.mapping_id,
381
+ is_demo: isDemo,
382
+ is_mobile: rootGetters['core/isMobileDevice'],
383
+ lang,
384
+ return_url:
385
+ returnProviders.includes(game.provider) || returnProviders.includes(game.provider_id)
386
+ ? `${window.location.origin}/return`
387
+ : window.location.href, // For providers to return.
388
+ }
389
+ const data = await request(api.playGame(params))
390
+ commit('gameMatching', false)
391
+ const targetUrl = data.redirect_url
392
+ commit('setGameURL', targetUrl)
393
+ commit('setCurrentProvider', params.provider_id)
394
+ commit('setCurrentGameId', params.game_id)
395
+ commit('setCurrentGameType', params.mapping_id)
396
+ return targetUrl
397
+ } else {
398
+ commit('gameMatching', false)
399
+ throw new Error('not authorized')
400
+ }
401
+ } catch (err) {
402
+ commit('gameMatching', false)
403
+ throw err
404
+ }
405
+ },
406
+ },
407
+ }
408
+ }
409
+
410
+ export default (ctx, inject) => {
411
+ const { store, $request, $loadingRequest } = ctx
412
+ store.registerModule('game', createStore($request, $loadingRequest))
413
+ }
414
+
415
+ function extractInfoChangedGames(oldStatus, newStatus) {
416
+ const list = []
417
+ const oldKeys = Object.keys(oldStatus || {})
418
+
419
+ // timestamp changed
420
+ for (const key of oldKeys) {
421
+ const isInfoChanged = oldStatus[key]?.[1] !== newStatus[key]?.[1]
422
+ if (isInfoChanged) {
423
+ list.push(key)
424
+ }
425
+ }
426
+
427
+ return list
428
+ }
429
+
430
+ function isInGamehall(url) {
431
+ const gamehallRegex = /^\/main(\/?|(\/[a-zA-Z0-g]+\/?))?$/g
432
+ const playingRegex = /^\/?main\/game\/?$/g
433
+ return gamehallRegex.test(url) && !playingRegex.test(url)
434
+ }
435
+
436
+ function isPlayingGame(url) {
437
+ const playingRegex = /^\/?main\/game\/?$/g
438
+ return playingRegex.test(url)
439
+ }
@@ -0,0 +1,211 @@
1
+ const api = {
2
+ getMessageTypes: () => ({ url: 'mailbox/getMessageTypes', method: 'get' }),
3
+ getMessageList: ({ type, limit = 10, page }) => ({
4
+ url: 'mailbox/getMessageList?include=cl',
5
+ method: 'post',
6
+ params: { type, limit, page },
7
+ }),
8
+ updateMessage: ({ groupId, isRead }) => ({
9
+ url: 'mailbox/updateMessage',
10
+ method: 'post',
11
+ params: { groupId, isRead },
12
+ }),
13
+ sendMessage: () => ({ url: 'mailbox/sendMessage', method: 'post' }),
14
+ getMessage: ({ groupId, limit = 10, pageIdx }) => ({
15
+ url: 'mailbox/getMessage',
16
+ method: 'post',
17
+ params: { groupId, limit, pageIdx },
18
+ }),
19
+ deleteMessage: ({ mid, isDelete }) => ({ url: 'mailbox/deleteMessage', method: 'post', params: { mid, isDelete } }),
20
+ }
21
+
22
+ function createStore(request) {
23
+ return {
24
+ namespaced: true,
25
+ state: () => ({
26
+ inbox: [],
27
+ tabList: [],
28
+ tab: '',
29
+ message: null,
30
+ isCrash: false,
31
+ systemPrompt: false,
32
+ summary: {},
33
+ newMessageLevel: null, // 'system' | 'normal'
34
+ }),
35
+ getters: {
36
+ getSummary(state) {
37
+ return state.summary
38
+ },
39
+ getInbox(state) {
40
+ return state.inbox
41
+ },
42
+ getTabList(state, getters) {
43
+ return state.tabList
44
+ },
45
+ getTab(state, getters) {
46
+ return state.tab
47
+ },
48
+ getMessage(state) {
49
+ return state.message
50
+ },
51
+ getCrash(state, getters) {
52
+ return state.isCrash
53
+ },
54
+ hasSystemMessage(state, getters) {
55
+ return !!state.summary?.detailRows?.SYSTEM
56
+ },
57
+ getSystemPrompt(state, getters) {
58
+ return state.systemPrompt
59
+ },
60
+ _getTabContent(state, getters) {
61
+ return state.inbox.find((item) => item.tab === state.tab) || {}
62
+ },
63
+ },
64
+ mutations: {
65
+ _setSummary(state, data) {
66
+ state.summary = data
67
+ },
68
+ _setNewMessageLevel(state, data) {
69
+ state.newMessageLevel = data
70
+ },
71
+ setSystemPrompt(state, data) {
72
+ state.systemPrompt = data
73
+ },
74
+ setTab(state, data) {
75
+ state.tab = data
76
+ },
77
+ _setInbox(state, data) {
78
+ state.inbox = data
79
+ },
80
+ _setTabList(state, data) {
81
+ state.tabList = data
82
+ },
83
+ _setCrash(state, bool) {
84
+ state.isCrash = bool
85
+ },
86
+ _setMessage(state, message) {
87
+ state.message = message
88
+ },
89
+ },
90
+ actions: {
91
+ async initialize({ state, getters, commit, dispatch }) {
92
+ const { data = [] } = await request(api.getMessageTypes())
93
+ const tabList = data?.map((item) => item.type)
94
+ const inbox = tabList.map((type) => ({ tab: type, data: [], page: -1 }))
95
+ commit('_setTabList', tabList)
96
+ commit('_setInbox', inbox)
97
+ if (!state.tab) {
98
+ commit('setTab', tabList[0])
99
+ }
100
+ },
101
+ async loadNextMessages({ state, getters, commit, dispatch }, { loaded, complete }) {
102
+ // 'loaded' and 'complete' are 'infinite-loading' component's methods, just bring in
103
+ const { tab } = state
104
+ const { totalPage, page } = getters._getTabContent
105
+ const nextIndex = page + 1
106
+ const maxIndex = totalPage - 1
107
+
108
+ if (!totalPage || maxIndex > nextIndex) {
109
+ try {
110
+ const data = await request(api.getMessageList({ type: tab, page: nextIndex }))
111
+ if (data.data.length === 0) {
112
+ complete()
113
+ return
114
+ }
115
+ dispatch('_appendMessages', data)
116
+ dispatch('_handleUnreadSystemMessage')
117
+ loaded()
118
+ } catch (err) {
119
+ console.log(err)
120
+ // 因為 api 有時會壞掉,要幫他們防呆...
121
+ commit('_setCrash', true)
122
+ }
123
+ } else {
124
+ complete()
125
+ }
126
+ },
127
+ selectMessage({ state, getters, commit, dispatch }, message) {
128
+ if (message && !message.isRead) {
129
+ dispatch('_updateStatusRead', message)
130
+ }
131
+ commit('_setMessage', message)
132
+ },
133
+ updateRewardMessage({ state, getters, commit, dispatch }) {
134
+ const clone = JSON.parse(JSON.stringify(state.inbox))
135
+ clone.forEach((tab) => {
136
+ if (tab.tab !== state.tab) {
137
+ return
138
+ }
139
+ tab.data.forEach((message) => {
140
+ if (message.groupId !== state.message.groupId) {
141
+ return
142
+ }
143
+ message.receiveStatus = 1
144
+ })
145
+ })
146
+ commit('_setInbox', clone)
147
+ commit('_setMessage', { ...state.message, receiveStatus: 1 })
148
+ },
149
+ setSummary({ state, getters, commit, dispatch }, data) {
150
+ let level = null
151
+ if (data.rows > state.summary.rows) {
152
+ if (data.detailRows.SYSTEM > state.summary.detailRows.SYSTEM) {
153
+ commit('_setNewMessageLevel', 'system')
154
+ level = 'system'
155
+ } else {
156
+ commit('_setNewMessageLevel', 'normal')
157
+ level = 'normal'
158
+ }
159
+ } else {
160
+ commit('_setNewMessageLevel', null)
161
+ }
162
+ commit('_setSummary', data)
163
+ return level
164
+ },
165
+ _handleUnreadSystemMessage({ state, getters, commit, dispatch }) {
166
+ const { tab, systemPrompt } = state
167
+ if (tab === 'SYSTEM' && systemPrompt) {
168
+ const tabContent = getters._getTabContent
169
+ commit('setSystemPrompt', false)
170
+ // 系統信要強制玩家閱讀
171
+ const unread = tabContent.data.find((item) => !item.isRead)
172
+ if (unread) {
173
+ dispatch('selectMessage', unread)
174
+ }
175
+ }
176
+ },
177
+ _appendMessages({ state, getters, commit, dispatch }, res) {
178
+ const { inbox, tab } = state
179
+ const { data, ...rest } = res
180
+ const clone = JSON.parse(JSON.stringify(inbox))
181
+ const index = clone.findIndex((inboxTab) => inboxTab.tab === tab)
182
+ const messages = [...clone[index].data, ...data]
183
+ clone[index] = { ...clone[index], ...rest, data: messages }
184
+ commit('_setInbox', clone)
185
+ },
186
+ _updateStatusRead({ state, getters, commit, dispatch }, message) {
187
+ const { inbox, tab, summary } = state
188
+ const cloneInbox = JSON.parse(JSON.stringify(inbox))
189
+ cloneInbox
190
+ .find((inboxTab) => inboxTab.tab === tab)
191
+ .data.forEach((item) => {
192
+ if (item.groupId === message.groupId) {
193
+ item.isRead = true
194
+ // 更新未讀數量
195
+ const cloneSummary = JSON.parse(JSON.stringify(summary))
196
+ cloneSummary.rows = cloneSummary.rows - 1 // 總數減1
197
+ cloneSummary.detailRows = { ...cloneSummary.detailRows, [tab]: cloneSummary.detailRows[tab] - 1 } // 分類減1
198
+ commit('_setSummary', cloneSummary)
199
+ }
200
+ })
201
+ commit('_setInbox', cloneInbox)
202
+ request(api.updateMessage({ groupId: message.groupId, isRead: true }))
203
+ },
204
+ },
205
+ }
206
+ }
207
+
208
+ export default (ctx, inject) => {
209
+ const { store, $request } = ctx
210
+ store.registerModule('inbox', createStore($request))
211
+ }