diff --git a/main/manager-web/src/apis/api.js b/main/manager-web/src/apis/api.js new file mode 100755 index 000000000..43d5f90ae --- /dev/null +++ b/main/manager-web/src/apis/api.js @@ -0,0 +1,22 @@ +// 引入各个模块的请求 +import user from './module/user.js' +/** + * 接口地址 + * 在开发阶段,如果地址写的是相对路径,请与vue.config.js的devServer配置相结合,方便跨域请求 + * + */ +const DEV_API_SERVICE = 'https://apifoxmock.com/m1/5931378-5618560-default' + +/** + * 根据开发环境返回接口url + * @returns {string} + */ +export function getServiceUrl () { + return DEV_API_SERVICE +} + +/** request服务封装 */ +export default { + getServiceUrl, + user +} diff --git a/main/manager-web/src/apis/httpRequest.js b/main/manager-web/src/apis/httpRequest.js new file mode 100755 index 000000000..aeddd1881 --- /dev/null +++ b/main/manager-web/src/apis/httpRequest.js @@ -0,0 +1,132 @@ +import {goToPage, showDanger, showWarning} from '../utils/index' +import Constant from '../utils/constant' +import Fly from 'flyio/dist/npm/fly'; + +const fly = new Fly() +// 设置超时 +fly.config.timeout = 30000 + +/** + * Request服务封装 + */ +export default { + sendRequest, + reAjaxFun, + clearRequestTime +} + +function sendRequest() { + return { + _sucCallback: null, + _failCallback: null, + _method: 'GET', + _data: {}, + _header: {'content-type': 'application/json; charset=utf-8'}, + _url: '', + 'send'() { + this._header.token = localStorage.getItem(Constant.STORAGE_KEY.TOKEN) + fly.request(this._url, this._data, { + method: this._method, + headers: this._header + }).then((res) => { + const error = httpHandlerError(res, this._failCallback) + if (error) { + return + } + if (this._sucCallback) { + this._sucCallback(res) + } + }).catch((res) => { + console.log(1111, res) + httpHandlerError(res, this._failCallback) + }) + return this + }, + 'success'(callback) { + this._sucCallback = callback + return this + }, + 'fail'(callback) { + this._failCallback = callback + return this + }, + 'url'(url) { + if (url) { + url = url.replaceAll('$', '/') + } + this._url = url + return this + }, + 'data'(data) { + this._data = data + return this + }, + 'method'(method) { + this._method = method + return this + }, + 'header'(header) { + this._header = header + return this + }, + 'showLoading'(showLoading) { + this._showLoading = showLoading + return this + }, + 'async'(flag) { + this.async = flag + } + } +} + +/** + * Info 请求完成后返回信息 + * callBack 回调函数 + * errTip 自定义错误信息 + */ +function httpHandlerError(info, callBack) { + /** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */ + let networkError = false + if (info.status === 200) { + if (info.data.code === 'success' || info.data.code === 0) { + return networkError + } else if (info.data.code === 401) { + goToPage(Constant.PAGE.LOGIN, true) + return true + } else { + showDanger(info.data.msg) + return true + } + } + if (callBack) { + callBack(info) + } else { + showDanger(`网络请求出现了错误【${info.status}】`) + } + return true +} + +let requestTime = 0 +let reAjaxSec = 2 + +function reAjaxFun(fn) { + let nowTimeSec = new Date().getTime() / 1000 + if (requestTime === 0) { + requestTime = nowTimeSec + } + let ajaxIndex = parseInt((nowTimeSec - requestTime) / reAjaxSec) + if (ajaxIndex > 10) { + showWarning('似乎无法连接服务器') + } else { + showWarning('正在连接服务器(' + ajaxIndex + ')') + } + if (fn) { + setTimeout(() => { + fn() + }, reAjaxSec * 1000) + } +} + +function clearRequestTime() { + requestTime = 0 +} \ No newline at end of file diff --git a/main/manager-web/src/apis/module/user.js b/main/manager-web/src/apis/module/user.js new file mode 100755 index 000000000..6f2df2679 --- /dev/null +++ b/main/manager-web/src/apis/module/user.js @@ -0,0 +1,20 @@ +import RequestService from '../httpRequest' +import {getServiceUrl} from '../api' + + +export default { + // 登录 + login(loginForm, callback) { + RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/login`).method('POST') + .data(loginForm) + .success((res) => { + RequestService.clearRequestTime() + callback(res) + }) + .fail(() => { + RequestService.reAjaxFun(() => { + this.login(loginForm, callback) + }) + }).send() + } +} diff --git a/main/manager-web/src/utils/constant.js b/main/manager-web/src/utils/constant.js new file mode 100755 index 000000000..f34abb32f --- /dev/null +++ b/main/manager-web/src/utils/constant.js @@ -0,0 +1,19 @@ +const HAVE_NO_RESULT = '暂无' +export default { + HAVE_NO_RESULT, // 项目的配置信息 + STORAGE_KEY: { + TOKEN: 'TOKEN', + PUBLIC_KEY: 'PUBLIC_KEY', + USER_TYPE: 'USER_TYPE' + }, + Lang: { + 'zh_cn': 'zh_cn', 'zh_tw': 'zh_tw', 'en': 'en' + }, + FONT_SIZE: { + 'big': 'big', + 'normal': 'normal', + }, // 获取map中的某key + get(map, key) { + return map[key] || HAVE_NO_RESULT + } +} diff --git a/main/manager-web/src/utils/date.js b/main/manager-web/src/utils/date.js new file mode 100755 index 000000000..c6210bc0d --- /dev/null +++ b/main/manager-web/src/utils/date.js @@ -0,0 +1,60 @@ +export const toDate = (date) => { + return isDate(date) ? new Date(date) : null +} + +export const isDate = (date) => { + if (date === null || date === undefined) return false + if (isNaN(new Date(date).getTime())) return false + return true +} + +export const isDateObject = (val) => { + return val instanceof Date +} + +export const formatAddDate = (date, format, addDay) => { + date = toDate(date) + if (!date) { + return '' + } + if (!addDay) { + date.setDate(date.getDate() + addDay) + } + return formatDateTool(date, format || 'yyyy-MM-dd HH:mm:ss') +} + +export const formatDate = (date, format) => { + date = toDate(date) + if (!date) return '' + return formatDateTool(date, format || 'yyyy-MM-dd HH:mm:ss') +} + +function formatDateTool(date, fmt) { + if (/(y+)/.test(fmt)) { + fmt = fmt.replace( + RegExp.$1, + (date.getFullYear() + '').substr(4 - RegExp.$1.length) + ) + } + const o = { + 'M+': date.getMonth() + 1, + 'd+': date.getDate(), + 'h+': date.getHours(), + 'm+': date.getMinutes(), + 's+': date.getSeconds() + } + for (const k in o) { + if (new RegExp(`(${k})`).test(fmt)) { + const str = o[k] + '' + fmt = fmt.replace( + RegExp.$1, + RegExp.$1.length === 1 ? str : padLeftZero(str) + ) + } + } + return fmt +} + +function padLeftZero(str) { + return ('00' + str).substr(str.length) +} diff --git a/main/manager-web/src/utils/index.js b/main/manager-web/src/utils/index.js new file mode 100755 index 000000000..b9c22fbc9 --- /dev/null +++ b/main/manager-web/src/utils/index.js @@ -0,0 +1,135 @@ +import router from '../router' +import Constant from '../utils/constant' +import { Message } from 'element-ui' + +/** + * 判断用户是否登录 + */ +export function checkUserLogin(fn) { + let token = localStorage.getItem(Constant.STORAGE_KEY.TOKEN) + let userType = localStorage.getItem(Constant.STORAGE_KEY.USER_TYPE) + if (isNull(token) || isNull(userType)) { + goToPage('console', true) + return + } + if (fn) { + fn() + } +} + +/** + * 判断是否为空 + * @param data + * @returns {boolean} + */ +export function isNull(data) { + if (data === undefined) { + return true + } else if (data === null) { + return true + } else if (typeof data === 'string' && (data.length === 0 || data === '' || data === 'undefined' || data === 'null')) { + return true + } else if ((data instanceof Array) && data.length === 0) { + return true + } + return false +} + +/** + * 判断不为空 + * @param data + * @returns {boolean} + */ +export function isNotNull(data) { + return !isNull(data) +} + +/** + * 显示顶部红色通知 + * @param msg + */ +export function showDanger(msg) { + if (isNull(msg)) { + return + } + Message({ + message: msg, + type: 'error' + }) +} + +/** + * 显示顶部橙色通知 + * @param msg + */ +export function showWarning(msg) { + if (isNull(msg)) { + return + } + Message({ + message: msg, + type: 'warning' + }); +} + + + +/** + * 显示顶部绿色通知 + * @param msg + */ +export function showSuccess(msg) { + Message({ + message: msg, + type: 'success' + }) +} + + + +/** + * 页面跳转 + * @param path + * @param isRepalce + */ +export function goToPage(path, isRepalce) { + if (isRepalce) { + router.replace(path) + } else { + router.push(path) + } +} + +/** + * 获取当前vue页面名称 + * @param path + * @param isRepalce + */ +export function getCurrentPage() { + let hash = location.hash.replace('#', '') + if (hash.indexOf('?') > 0) { + hash = hash.substring(0, hash.indexOf('?')) + } + return hash +} + +/** + * 生成从[min,max]的随机数 + * @param min + * @param max + * @returns {number} + */ +export function randomNum(min, max) { + return Math.round(Math.random() * (max - min) + min) +} + + +/** + * 获取uuid + */ +export function getUUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { + return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16) + }) +} + diff --git a/main/manager-web/src/views/login.vue b/main/manager-web/src/views/login.vue index 5bc9e6b02..6f6759e7a 100644 --- a/main/manager-web/src/views/login.vue +++ b/main/manager-web/src/views/login.vue @@ -3,70 +3,91 @@
- - + style="display: flex;align-items: center;margin-top: 15px;margin-left: 10px;gap: 10px;"> + +
- - + +
- - + +
- - + +
-
+
新用户注册
- +
- 登录即同意
《用户协议》
和 + 登录即同意 +
《用户协议》
+ 和
《隐私政策》
- ©2025 xiaozhi-esp32-server
+ ©2025 xiaozhi-esp32-server +