mtfosbot_web/src/store/actions.js

272 lines
6.8 KiB
JavaScript

import axios from 'axios'
import {
apiUrl,
chkObject
} from '@/tools'
const client = axios.create({
baseURL: apiUrl,
withCredentials: true
})
const errorCatch = function (err, act) {
let msg = ''
if ('response' in err && 'data' in err.response && 'message' in err.response.data) {
msg = err.response.data.message
} else {
msg = 'unknown error'
}
this('addDialog', {
msg,
act
})
}
export default {
async checkSession ({commit}, cb = null) {
commit('toggleLoading', true)
let flag = false
try {
let result = await client({
method: 'get',
url: '/api/session'
})
if ('data' in result) {
commit('setUserInfo', {
name: result.data.user.name || '',
type: result.data.user.type || ''
})
}
flag = true
} catch (err) {
flag = false
}
if (typeof cb === 'function') cb(flag)
commit('toggleLoading', false)
},
async sendLogin ({commit}, {account, password, cb = null}) {
let chk = chkObject.bind({body: {account, password}})
if (!chk('account', 'string') || !chk('password', 'string')) {
commit('addDialog', {
msg: '帳號密碼不能留空'
})
return
}
let data = {
account,
password
}
commit('toggleLoading', true)
try {
await client({
method: 'post',
url: '/api/login',
data
})
if (typeof cb === 'function') cb()
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async sendLogout ({commit}, {cb}) {
commit('toggleLoading', true)
try {
await client({
method: 'post',
url: '/api/logout'
})
} catch (err) {
}
if (typeof cb === 'function') cb()
commit('toggleLoading', false)
},
async getChannelList ({commit}, cb = null) {
commit('toggleLoading', true)
try {
let result = await client({
method: 'get',
url: '/api/twitch/channels'
})
let list = []
if ('data' in result && 'list' in result.data && Array.isArray(result.data.list)) list = result.data.list
commit('setChannels', list)
if (typeof cb === 'function') cb()
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async getChannel ({commit}, {id}) {
if (typeof id !== 'string' || id.trim().length === 0) {
commit('addDialog', {
msg: 'channel id read fail'
})
return
}
commit('toggleLoading', true)
try {
let result = await client({
method: 'get',
url: `/api/twitch/channel/${id}`
})
let channel = null
if ('data' in result && 'channel' in result.data && typeof result.data.channel === 'object') channel = result.data.channel
if (channel === null) {
commit('addDialog', {
msg: 'channel data read fail'
})
} else {
commit('setChannel', channel)
}
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async changeBotJoin ({commit}, {id, join, cb}) {
if (typeof id !== 'string' || id.trim().length === 0) {
commit('addDialog', {
msg: 'channel id read fail'
})
return
}
if (typeof join !== 'boolean') {
commit('addDialog', {
msg: 'join status read fail'
})
return
}
commit('toggleLoading', true)
try {
await client({
method: 'put',
url: `/api/twitch/channel/${id}/botjoin`,
data: {
join: join ? 1 : 0
}
})
if (typeof cb === 'function') cb()
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async changeOpayID ({commit}, {chid, opayid, cb}) {
if (typeof chid !== 'string' || chid.trim().length === 0) {
commit('addDialog', {
msg: 'channel id read fail'
})
return
}
if (typeof opayid !== 'string') {
commit('addDialog', {
msg: 'opay id type error'
})
return
}
commit('toggleLoading', true)
try {
await client({
method: 'put',
url: `/api/twitch/channel/${chid.trim()}/opay`,
data: {
opay: opayid
}
})
commit('getChannel', {id: chid})
if (typeof cb === 'function') cb()
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async getOpaySetting ({commit}, {id}) {
if (typeof id !== 'string' || id.trim().length === 0) {
commit('addDialog', {
msg: 'channel id read fail'
})
return
}
commit('toggleLoading', true)
try {
let result = await client({
method: 'get',
url: `/api/twitch/channel/${id.trim()}/opay/setting`
})
if (!('data' in result) || !('setting' in result.data) || typeof result.data.setting !== 'object') {
commit('addDialog', {
msg: 'get opay setting data fail'
})
} else {
commit('setOpaySetting', {
chid: id,
setting: result.data.setting || {}
})
}
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async changeOpaySetting ({commit, dispatch}, {id, data}) {
if (typeof id !== 'string' || id.trim().length === 0) {
commit('addDialog', {
msg: 'channel id read fail'
})
return
}
if (typeof data !== 'object') {
commit('addDialog', {
msg: 'data read fail'
})
return
}
commit('toggleLoading', true)
try {
await client({
method: 'put',
url: `/api/twitch/channel/${id.trim()}/opay/setting`,
data
})
dispatch('getOpaySetting', {id})
} catch (err) {
errorCatch.apply(commit, [err])
}
commit('toggleLoading', false)
},
async getLineGroups ({commit, dispatch}) {
try {
let res = await client({
method: 'get',
url: `/api/line/groups`
})
commit('setLineGroups', res.data.list)
} catch (err) {
errorCatch.call(commit, err)
}
},
async getLogList ({commit, dispatch}, {group, page, max = 20, cb = null}) {
console.log('get log list :::::: ', group, page, max)
let chk = chkObject.bind({ body: { group, page, max } })
if (!chk('group', 'string', true) || !chk('page', 'number') || !chk('max', 'number')) return
try {
let opts = {
method: 'get',
url: '/api/line/logs',
params: {
order: 'asc'
}
}
if (group.length > 0) opts.params.group = group
if (page > 1 || page < 0) opts.params.p = page
if (max > 0) opts.params.max = max
let res = await client(opts)
if (cb !== null && typeof cb === 'function') cb(res.data)
} catch (err) {
errorCatch.call(commit, err)
}
}
}