63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
import axios from 'axios'
|
|
import {apiUrl, chkObject} from '@/tools'
|
|
|
|
const client = axios.create({
|
|
baseURL: apiUrl,
|
|
withCredentials: true
|
|
})
|
|
|
|
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) {
|
|
let msg = ''
|
|
if ('response' in err && 'data' in err.response && 'message' in err.response.data) {
|
|
msg = err.response.data.message
|
|
} else {
|
|
msg = 'unknown error'
|
|
}
|
|
commit('addDialog', {msg})
|
|
}
|
|
commit('toggleLoading', false)
|
|
}
|
|
}
|