From 0f479b1f9b9a7c0d3fc7977d45fd7625ba10910a Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 4 Dec 2018 23:25:41 +0800 Subject: [PATCH] add msg item --- .../lineLog/components/message-item.vue | 21 ++++- src/components/ControlPanel/lineLog/index.vue | 78 ++++++++++++++----- src/store/actions.js | 30 +++++++ src/store/getters.js | 1 + src/store/index.js | 3 + src/store/mutations.js | 3 + src/tools.js | 77 ++++++++++++------ 7 files changed, 169 insertions(+), 44 deletions(-) diff --git a/src/components/ControlPanel/lineLog/components/message-item.vue b/src/components/ControlPanel/lineLog/components/message-item.vue index 839700a..a588ff3 100644 --- a/src/components/ControlPanel/lineLog/components/message-item.vue +++ b/src/components/ControlPanel/lineLog/components/message-item.vue @@ -1,21 +1,38 @@ + + diff --git a/src/components/ControlPanel/lineLog/index.vue b/src/components/ControlPanel/lineLog/index.vue index 5c24e3e..9a9634c 100644 --- a/src/components/ControlPanel/lineLog/index.vue +++ b/src/components/ControlPanel/lineLog/index.vue @@ -1,19 +1,37 @@ diff --git a/src/store/actions.js b/src/store/actions.js index 78d5469..753f26a 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -235,5 +235,35 @@ export default { 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: {} + } + if (group.length > 0) opts.params.group = group + if (page > 1) 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.list) + } catch (err) { + errorCatch.call(commit, err) + } } } diff --git a/src/store/getters.js b/src/store/getters.js index e768dea..61ec206 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -6,3 +6,4 @@ export const getChannel = state => id => { let arr = state.twitch.channels.filter(t => t.id === id) return arr.length > 0 ? arr[0] : null } +export const lineGroups = state => state.line.groups || [] diff --git a/src/store/index.js b/src/store/index.js index 65fbdf3..a14389a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -15,6 +15,9 @@ const state = { }, twitch: { channels: [] + }, + line: { + groups: [] } } diff --git a/src/store/mutations.js b/src/store/mutations.js index dce6d55..f7b9b01 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -39,5 +39,8 @@ export default { return t }) state.twitch.channels = [...list] + }, + setLineGroups (state, list) { + state.line.groups = [...list] } } diff --git a/src/tools.js b/src/tools.js index 9c8b471..a68b3dd 100644 --- a/src/tools.js +++ b/src/tools.js @@ -1,21 +1,70 @@ export const apiUrl = 'https://bot.trj.tw' -export const chkObject = function (key = '', type = '', empty = false) { +export const padleft = (str, len, pad = '0') => { + if (typeof str !== 'string') str = str.toString() + if (str.length < len) return padleft(`${pad}${str}`, len, pad) + return str +} + +/** + * check is number + * @param {any} v input value + * @return {boolean} + */ +export const isNumber = (v) => { + if (!isFinite(v) || v === true || v === false) return false + return true +} + +/** + * src value to int + * @param {object} v src value + * @param {number} defVal default value + * @param {number} min range min + * @param {number} max range max + */ +export const toInt = (v, defVal = 0, min = null, max = null) => { + if (!isNumber(defVal)) defVal = 0 + if (typeof defVal === 'string') defVal = parseInt(defVal) + min = !isNumber(min) ? null : (typeof min === 'string' ? parseInt(min) : min) + max = !isNumber(max) ? null : (typeof max === 'string' ? parseInt(max) : max) + if (!isNumber(v)) return defVal + if (typeof v === 'string') v = parseInt(v) + if (min !== null && v < min) v = min + if (max !== null && v > max) v = max + return v +} +/** + * check Object + * @param {string} key key name + * @param {string} type type name + * @param {boolean} empty can empty + * @param {number} max max value + */ +export const chkObject = function (key = '', type = '', empty = false, max = null) { 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 + if (isFinite(empty) && (empty !== false && empty !== true)) { + max = toInt(empty, 0) + empty = false + } + switch (type) { case 'string': - if (typeof this.body[key] !== 'string' || (!empty && !this.body[key].trim())) return false + if (typeof this.body[key] !== 'string' || (!empty && !this.body[key])) return false + if (max !== null && this.body[key].length > max) return false break case 'number': - if (!isFinite(this.body[key])) return false + if (!isNumber(this.body[key])) return false + if (max !== null && this.body[key] > max) 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 + if (max !== null && this.body[key].length > max) return false break case 'uuid': if (typeof this.body[key] !== 'string') return false @@ -23,7 +72,7 @@ export const chkObject = function (key = '', type = '', empty = false) { if (!empty && !uuidChk.test(this.body[key])) return false break case 'object': - if (typeof this.body[key] !== 'object') return false + if (typeof this.body[key] !== 'object' || this.body[key] === null || this.body[key] === undefined) return false try { let str = JSON.stringify(this.body[key]) JSON.parse(str) @@ -37,23 +86,3 @@ export const chkObject = function (key = '', type = '', empty = false) { return true } - -/** - * parse number - * @param {any} v input number - * @param {number} def default number - * @param {number} min min number - * @param {number} max max number - * @return {number} - */ -export const toInt = (v, def = 0, min = null, max = null) => { - if (!isFinite(def)) def = 0 - if (typeof def === 'string') def = parseInt(def) - min = isFinite(min) ? (typeof min === 'string' ? parseInt(min) : min) : null - max = isFinite(max) ? (typeof max === 'string' ? parseInt(max) : max) : null - if (!isFinite(v)) return def - if (typeof v === 'string') v = parseInt(v) - if (min !== null && v < min) v = min - if (max !== null && v > max) v = max - return v -}