update:登陆功能

This commit is contained in:
hrz 2025-03-07 00:37:13 +08:00
parent b7bf309b5b
commit fdc5b3f3ea
7 changed files with 447 additions and 24 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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()
}
}

View file

@ -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
}
}

View file

@ -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)
}

View file

@ -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)
})
}

View file

@ -15,7 +15,8 @@
<img src="@/assets/login/hi.png" alt="" style="width: 34px;height: 34px;"/>
<div class="login-text">登录</div>
<div class="login-welcome">
WELCOME TO LOGIN</div>
WELCOME TO LOGIN
</div>
</div>
<div style="padding: 0 30px;">
<div class="input-box">
@ -30,27 +31,32 @@
<img src="@/assets/login/shield.png" alt="" class="input-icon"/>
<el-input v-model="form.captcha" placeholder="请输入验证码"/>
</div>
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #5778ff;display: flex;justify-content: space-between;margin-top: 20px;">
<div
style="font-weight: 400;font-size: 14px;text-align: left;color: #5778ff;display: flex;justify-content: space-between;margin-top: 20px;">
<div style="cursor: pointer;">新用户注册</div>
</div>
</div>
<div class="login-btn">登陆</div>
<div class="login-btn" @click="login">登陆</div>
<div style="font-size: 14px;color: #979db1;">
登录即同意<div style="display: inline-block;color: #5778FF;cursor: pointer;">《用户协议》</div>和
登录即同意
<div style="display: inline-block;color: #5778FF;cursor: pointer;">《用户协议》</div>
和
<div style="display: inline-block;color: #5778FF;cursor: pointer;">《隐私政策》</div>
</div>
</div>
</el-main>
<el-footer>
<div style="font-size: 12px;font-weight: 400;color: #979db1;">
©2025 xiaozhi-esp32-server</div>
©2025 xiaozhi-esp32-server
</div>
</el-footer>
</el-container>
</div>
</template>
<script>
// @ is an alias to /src
import Api from '@/apis/api'
import {isNull, showDanger, showSuccess, goToPage} from '@/utils'
export default {
name: 'login',
@ -65,8 +71,23 @@ export default {
}
},
methods: {
handleClick(){
login() {
if (isNull(this.form.username)) {
showDanger('用户名不能为空')
return
}
if (isNull(this.form.password)) {
showDanger('密码不能为空')
return
}
if (isNull(this.form.captcha)) {
showDanger('验证码不能为空')
return
}
Api.user.login(this.form, ({data}) => {
showSuccess('登陆成功!')
goToPage('/home')
})
}
}
}
@ -86,12 +107,14 @@ export default {
-o-background-size: cover;
/* 兼容老版本Opera浏览器 */
}
.login-text {
font-weight: 700;
font-size: 32px;
text-align: left;
color: #3d4566;
}
.login-welcome {
font-weight: 400;
font-size: 9px;
@ -100,6 +123,7 @@ export default {
align-self: flex-end;
margin-bottom: 7px;
}
.login-box {
position: absolute;
top: 50%;
@ -111,17 +135,20 @@ export default {
width: 450px;
box-sizing: border-box;
}
.el-dropdown-link {
font-weight: 400;
font-size: 14px;
text-align: left;
color: #979db1;
}
.input-icon {
width: 19px;
height: 22px;
flex-shrink: 0;
}
.login-btn {
height: 35px;
background: #5778ff;
@ -133,6 +160,7 @@ export default {
line-height: 35px;
margin: 35px 15px 15px;
}
.code-send {
width: 70px;
height: 32px;
@ -145,6 +173,7 @@ export default {
flex-shrink: 0;
cursor: pointer;
}
.input-box {
display: flex;
margin-top: 20px;
@ -156,10 +185,12 @@ export default {
padding: 0 15px;
gap: 20px;
}
::v-deep {
.el-tabs__nav-wrap::after {
height: 1px;
}
.el-tabs__nav-wrap::before {
content: "";
position: absolute;
@ -170,18 +201,22 @@ export default {
background-color: #e4e7ed;
z-index: 1;
}
.el-tabs__item {
height: 65px;
line-height: 65px;
font-weight: 700;
color: #3d4566;
}
.el-tabs__item.is-active {
color: #5778ff;
}
.el-tabs__nav-scroll {
padding: 0 30px;
}
.el-input__inner {
border: none;
background-color: transparent;