3 天以前 8217c63d1610a9d89419c69947beb4fb3bb7c7e4
style(frontend): 格式化代码缩进并增强API调用安全性

- 统一调整了src/views/index.vue中的代码缩进格式
- 在src/api/login.js中为getInfo请求添加了授权头验证
- 优化了src/utils/request.js中的请求拦截器逻辑
- 改进了src/store/modules/user.js中的令牌处理机制
- 增强了用户信息获取的安全性验证
- 在src/api/viewIndex.js中添加了生产相关API接口
- 统一了代码中的缩进和空格规范
已修改5个文件
99 ■■■■ 文件已修改
src/api/login.js 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/api/viewIndex.js 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/store/modules/user.js 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/request.js 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/index.vue 补丁 | 查看 | 原始文档 | blame | 历史
src/api/login.js
@@ -1,4 +1,5 @@
import request from '@/utils/request'
import { getToken } from '@/utils/auth'
// 登录方法
export function login(username, password, code, uuid) {
@@ -33,8 +34,10 @@
// 获取用户详细信息
export function getInfo() {
  const token = getToken()
  return request({
    url: '/getInfo',
    headers: token ? { Authorization: `Bearer ${token}` } : {},
    method: 'get'
  })
}
src/api/viewIndex.js
@@ -326,3 +326,45 @@
    method: "get",
  });
};
export const productionOverview = () => {
  return request({
    url: "/home/productionOverview",
    method: "get",
    headers: {
      handleAuthError: false,
    },
  });
};
export const productionRealtimeBoard = () => {
  return request({
    url: "/home/productionRealtimeBoard",
    method: "get",
    headers: {
      handleAuthError: false,
    },
  });
};
export const productionOrderProgress = (params) => {
  return request({
    url: "/home/productionOrderProgress",
    method: "get",
    params,
    headers: {
      handleAuthError: false,
    },
  });
};
export const todayProductionPlan = (params) => {
  return request({
    url: "/home/todayProductionPlan",
    method: "get",
    params,
    headers: {
      handleAuthError: false,
    },
  });
};
src/store/modules/user.js
@@ -25,8 +25,13 @@
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(username, password, code, uuid).then(res => {
            setToken(res.token)
            this.token = res.token
            const token = res?.token || res?.data?.token
            if (!token) {
              reject(new Error('未获取到登录令牌'))
              return
            }
            setToken(token)
            this.token = token
            resolve()
          }).catch(error => {
            reject(error)
@@ -47,7 +52,7 @@
      getInfo() {
        return new Promise((resolve, reject) => {
          getInfo().then(res => {
            const user = res.user
            const user = res.user || {}
            let avatar = user.avatar || ""
            avatar = import.meta.env.VITE_APP_BASE_API + '/profile/' + avatar
            if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
@@ -56,13 +61,13 @@
            } else {
              this.roles = ['ROLE_DEFAULT']
            }
            this.id = user.userId
            this.name = user.userName
            this.id = user.userId || ''
            this.name = user.userName || ''
            this.avatar = avatar
            this.currentFactoryName = user.currentFactoryName
            this.nickName = user.nickName
            this.roleName = user.roles[0].roleName
            this.currentDeptId = user.tenantId
            this.currentFactoryName = user.currentFactoryName || ''
            this.nickName = user.nickName || ''
            this.roleName = Array.isArray(user.roles) && user.roles.length > 0 ? (user.roles[0].roleName || '') : ''
            this.currentDeptId = user.tenantId || ''
            this.currentLoginTime = this.getCurrentTime()
            this.aiEnabled = Number(res.aiEnabled) === 1 ? 1 : 0
            resolve(res)
@@ -104,8 +109,13 @@
        const password = userInfo.password
        return new Promise((resolve, reject) => {
          loginCheckFactory(username, password).then(res => {
            setToken(res.token)
            this.token = res.token
            const token = res?.token || res?.data?.token
            if (!token) {
              reject(new Error('未获取到登录令牌'))
              return
            }
            setToken(token)
            this.token = token
            resolve()
          }).catch(error => {
            reject(error)
@@ -116,10 +126,15 @@
        return new Promise((resolve, reject) => {
          tideLogin(code)
              .then((res) => {
                setToken(res.token);
                this.token = res.token
                const token = res?.token || res?.data?.token
                if (!token) {
                  reject(new Error('未获取到登录令牌'))
                  return
                }
                setToken(token);
                this.token = token
                Vue.prototype.uploadHeader = {
                  Authorization: "Bearer " + res.token,
                  Authorization: "Bearer " + token,
                };
                resolve();
              })
src/utils/request.js
@@ -22,10 +22,11 @@
// request拦截器
service.interceptors.request.use(config => {
  config.headers = config.headers || {}
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  const isToken = config.headers.isToken === false
  // 是否需要防止数据重复提交
  const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
  const isRepeatSubmit = config.headers.repeatSubmit === false
  if (getToken() && !isToken) {
    config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
@@ -68,13 +69,14 @@
  return config
}, error => {
    console.log(error)
    Promise.reject(error)
    return Promise.reject(error)
})
// 响应拦截器
service.interceptors.response.use(res => {
    // 未设置状态码则默认成功状态
    const code = res.data.code || 200
    const handleAuthError = (res.config && res.config.headers && res.config.headers.handleAuthError) !== false
    // 获取错误信息
    const msg = errorCode[code] || res.data.msg || errorCode['default']
    // 二进制数据则直接返回
@@ -82,6 +84,9 @@
      return res.data
    }
    if (code === 401) {
      if (!handleAuthError) {
        return Promise.reject(new Error(msg))
      }
      if (!isRelogin.show) {
        isRelogin.show = true
        ElMessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
src/views/index.vue