From e2de8f5d85cbec7e4275c855b72228909d408e96 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 20 Aug 2018 00:31:11 +0800 Subject: [PATCH] update 1. add login action 2. add user info getter 3. add top menu ui --- jsconfig.json | 9 ++++ src/App.vue | 5 ++- .../ControlPanel/channelList/index.vue | 14 +++++++ src/components/ControlPanel/index.vue | 36 ++++++++++++++++ src/components/ControlPanel/topMenu.vue | 22 ++++++++++ src/components/Dialog/index.vue | 38 +++++++++++++++++ src/components/Login/index.vue | 18 ++++---- src/router.js | 19 +++++++++ src/store/actions.js | 41 ++++++++++++++++++- src/store/getters.js | 2 + src/store/index.js | 13 ++++-- src/store/mutations.js | 18 ++++++++ src/tools.js | 40 +++++++++++++++++- 13 files changed, 261 insertions(+), 14 deletions(-) create mode 100644 jsconfig.json create mode 100644 src/components/ControlPanel/channelList/index.vue create mode 100644 src/components/ControlPanel/index.vue create mode 100644 src/components/ControlPanel/topMenu.vue create mode 100644 src/components/Dialog/index.vue diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..044903e --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "moduleResolution": "classic", + "baseUrl": ".", + "paths": { + "@": ["src/*"] + } + } +} \ No newline at end of file diff --git a/src/App.vue b/src/App.vue index e91f8d6..4fb2127 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,15 +2,18 @@
+
diff --git a/src/components/ControlPanel/channelList/index.vue b/src/components/ControlPanel/channelList/index.vue new file mode 100644 index 0000000..6792b31 --- /dev/null +++ b/src/components/ControlPanel/channelList/index.vue @@ -0,0 +1,14 @@ + + + diff --git a/src/components/ControlPanel/index.vue b/src/components/ControlPanel/index.vue new file mode 100644 index 0000000..becac7c --- /dev/null +++ b/src/components/ControlPanel/index.vue @@ -0,0 +1,36 @@ + + + + + diff --git a/src/components/ControlPanel/topMenu.vue b/src/components/ControlPanel/topMenu.vue new file mode 100644 index 0000000..784fff0 --- /dev/null +++ b/src/components/ControlPanel/topMenu.vue @@ -0,0 +1,22 @@ + + + diff --git a/src/components/Dialog/index.vue b/src/components/Dialog/index.vue new file mode 100644 index 0000000..b11bf08 --- /dev/null +++ b/src/components/Dialog/index.vue @@ -0,0 +1,38 @@ + + + diff --git a/src/components/Login/index.vue b/src/components/Login/index.vue index cf87b02..caecf34 100644 --- a/src/components/Login/index.vue +++ b/src/components/Login/index.vue @@ -8,7 +8,7 @@ - + Login Login with Twitch @@ -49,18 +49,22 @@ export default { this.checkSession((flag) => { // check login session // true === isLogin - console.log(flag) + if (flag === true) { + this.$router.push('/cp') + } }) }, methods: { ...mapMutations(['toggleLoading']), - ...mapActions(['checkSession']), + ...mapActions(['checkSession', 'sendLogin']), login: function () { console.log('login submit') - this.toggleLoading(1) - setTimeout(() => { - this.toggleLoading(0) - }, 1000) + this.sendLogin({ + account: this.account, + password: this.password + }, () => { + this.$router.push('/cp') + }) }, go_oauth: function () { window.location.href = apiUrl.replace(/\/$/, '') + '/twitch/login?tourl=' + encodeURIComponent(window.location.href) diff --git a/src/router.js b/src/router.js index 086d36d..6302872 100644 --- a/src/router.js +++ b/src/router.js @@ -2,6 +2,8 @@ import Vue from 'vue' import Router from 'vue-router' // import HelloWorld from '@/components/HelloWorld' import Login from '@/components/Login' +import ControlPanel from '@/components/ControlPanel' +import ChannelList from '@/components/ControlPanel/channelList' Vue.use(Router) @@ -13,6 +15,23 @@ export default new Router({ path: '/', name: 'Login', component: Login + }, + { + path: '/cp', + name: 'ControlPanel', + component: ControlPanel, + children: [ + { + path: 'channels', + alias: '', + name: 'ChannelList', + component: ChannelList + }, + { + path: '*', + redirect: 'channels' + } + ] } ] }) diff --git a/src/store/actions.js b/src/store/actions.js index 7df911f..7e0bbaa 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -1,5 +1,5 @@ import axios from 'axios' -import {apiUrl} from '@/tools' +import {apiUrl, chkObject} from '@/tools' const client = axios.create({ baseURL: apiUrl, @@ -11,15 +11,52 @@ export default { commit('toggleLoading', true) let flag = false try { - await client({ + 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) } } diff --git a/src/store/getters.js b/src/store/getters.js index 61d8371..eef8eb6 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -1 +1,3 @@ export const getLoading = state => !!state.loading +export const dialogMsg = state => state.dialog[0] || null +export const userInfo = state => state.user diff --git a/src/store/index.js b/src/store/index.js index acbd03b..a8dc10e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -6,10 +6,17 @@ import * as getters from './getters' Vue.use(Vuex) +const state = { + loading: false, + dialog: [], + user: { + name: '', + type: '' + } +} + export default new Vuex.Store({ - state: { - loading: false - }, + state, mutations, getters, actions diff --git a/src/store/mutations.js b/src/store/mutations.js index c23c6cc..8cf5593 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -1,5 +1,23 @@ export default { toggleLoading (state, loading = null) { state.loading = !!loading + }, + setUserInfo (state, {name, type}) { + if (typeof name !== 'string' || name.trim().length === 0) return + if (typeof type !== 'string' || type.trim().length === 0) return + name = name.trim() + type = type.trim() + state.user = { + ...state.user, + name, + type + } + }, + addDialog (state, {msg, act = null}) { + if (typeof msg !== 'string' || msg.trim().length === 0) return + state.dialog = [...state.dialog, {msg, act}] + }, + removeDialog (state) { + state.dialog = state.dialog.slice(1) } } diff --git a/src/tools.js b/src/tools.js index c55365d..36e66f3 100644 --- a/src/tools.js +++ b/src/tools.js @@ -1 +1,39 @@ -export const apiUrl = 'https://bot.trj.tw' \ No newline at end of file +export const apiUrl = 'https://bot.trj.tw' + +export const chkObject = function (key = '', type = '', empty = false) { + const uuidChk = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i + if (!(key in this.body)) return false + + switch (type) { + case 'string': + if (typeof this.body[key] !== 'string' || (!empty && !this.body[key].trim())) return false + break + case 'number': + if (!isFinite(this.body[key])) return false + break + case 'boolean': + if (typeof this.body[key] !== 'boolean') return false + break + case 'array': + if (!Array.isArray(this.body[key]) || (!empty && this.body[key].length === 0)) return false + break + case 'uuid': + if (typeof this.body[key] !== 'string') return false + if (!empty && this.body[key] === '') return false + if (!empty && !uuidChk.test(this.body[key])) return false + break + case 'object': + if (typeof this.body[key] !== 'object') return false + try { + let str = JSON.stringify(this.body[key]) + JSON.parse(str) + } catch (err) { + return false + } + break + default: + return false + } + + return true +}