Skip to content

Commit

Permalink
Merge pull request #1945 from Azir-11/main
Browse files Browse the repository at this point in the history
优化user store部分写法
  • Loading branch information
pixelmaxQm authored Dec 2, 2024
2 parents a6b73c2 + 30a39be commit 3df9faa
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 69 deletions.
30 changes: 20 additions & 10 deletions web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
FROM node:20
# 如果需要用 cicd ,请设置环境变量:
# variables:
# DOCKER_BUILDKIT: 1

WORKDIR /gva_web/
COPY . .
FROM node:20-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
COPY . /app
WORKDIR /app

RUN yarn && yarn build

FROM nginx:alpine
LABEL MAINTAINER="SliverHorn@[email protected]"
FROM base AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod

FROM base AS build
COPY --from=prod-deps /app/node_modules /app/node_modules
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install && pnpm run build

COPY .docker-compose/nginx/conf.d/my.conf /etc/nginx/conf.d/my.conf
COPY --from=0 /gva_web/dist /usr/share/nginx/html
RUN cat /etc/nginx/nginx.conf
RUN cat /etc/nginx/conf.d/my.conf

FROM nginx:alpine
LABEL MAINTAINER="[email protected]"
COPY --from=base /app/.docker-compose/nginx/conf.d/my.conf /etc/nginx/conf.d/my.conf
COPY --from=build /app/dist /usr/share/nginx/html
RUN ls -al /usr/share/nginx/html
3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@vue-office/excel": "^1.7.11",
"@vue-office/pdf": "^2.0.2",
"@vueuse/core": "^11.0.3",
"@vueuse/integrations": "^12.0.0",
"@wangeditor/editor": "^5.1.23",
"@wangeditor/editor-for-vue": "^5.1.12",
"ace-builds": "^1.36.4",
Expand All @@ -27,7 +28,6 @@
"echarts": "5.5.1",
"element-plus": "^2.8.5",
"highlight.js": "^11.10.0",
"js-cookie": "^3.0.5",
"marked": "14.1.1",
"marked-highlight": "^2.1.4",
"mitt": "^3.0.1",
Expand All @@ -39,6 +39,7 @@
"sortablejs": "^1.15.3",
"spark-md5": "^3.0.2",
"tailwindcss": "^3.4.10",
"universal-cookie": "^7",
"vform3-builds": "^3.0.10",
"vite-auto-import-svg": "^1.1.0",
"vue": "^3.5.7",
Expand Down
8 changes: 7 additions & 1 deletion web/src/components/exportExcel/exportExcel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</template>

<script setup>
import {getUrl} from "@/utils/image";
const props = defineProps({
templateId: {
type: String,
Expand Down Expand Up @@ -35,7 +37,10 @@
ElMessage.error('组件未设置模板ID')
return
}
const baseUrl = import.meta.env.VITE_BASE_API
let baseUrl = import.meta.env.VITE_BASE_API
if (baseUrl === "/"){
baseUrl = ""
}
const paramsCopy = JSON.parse(JSON.stringify(props.condition))
if (props.limit) {
paramsCopy.limit = props.limit
Expand All @@ -52,6 +57,7 @@
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`
)
.join('&')
const url = `${baseUrl}/sysExportTemplate/exportExcel?templateID=${props.templateId}${params ? '&' + params : ''}`
window.open(url, '_blank')
Expand Down
5 changes: 4 additions & 1 deletion web/src/components/exportExcel/exportTemplate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
ElMessage.error('组件未设置模板ID')
return
}
const baseUrl = import.meta.env.VITE_BASE_API
let baseUrl = import.meta.env.VITE_BASE_API
if (baseUrl === "/"){
baseUrl = ""
}
const url = `${baseUrl}/sysExportTemplate/exportTemplate?templateID=${props.templateId}`
window.open(url, '_blank')
}
Expand Down
5 changes: 4 additions & 1 deletion web/src/components/exportExcel/importExcel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
<script setup>
import { ElMessage } from 'element-plus'
const baseUrl = import.meta.env.VITE_BASE_API
let baseUrl = import.meta.env.VITE_BASE_API
if (baseUrl === "/"){
baseUrl = ""
}
const props = defineProps({
templateId: {
Expand Down
104 changes: 49 additions & 55 deletions web/src/pinia/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { jsonInBlacklist } from '@/api/jwt'
import router from '@/router/index'
import { ElLoading, ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { ref, watch } from 'vue'
import { ref, computed } from 'vue'
import { useRouterStore } from './router'
import cookie from 'js-cookie'
import { useCookies } from '@vueuse/integrations/useCookies'
import { useStorage } from '@vueuse/core'

import { useAppStore } from '@/pinia'

Expand All @@ -19,9 +20,10 @@ export const useUserStore = defineStore('user', () => {
headerImg: '',
authority: {}
})
const token = ref(
window.localStorage.getItem('token') || cookie.get('x-token') || ''
)
const token = useStorage('token', '')
const xToken = useCookies('x-token')
const currentToken = computed(() => token.value || xToken.value || '')

const setUserInfo = (val) => {
userInfo.value = val
if (val.originSetting) {
Expand All @@ -33,11 +35,11 @@ export const useUserStore = defineStore('user', () => {

const setToken = (val) => {
token.value = val
xToken.value = val
}

const NeedInit = async () => {
token.value = ''
window.localStorage.removeItem('token')
await ClearStorage()
await router.push({ name: 'Init', replace: true })
}

Expand All @@ -57,49 +59,49 @@ export const useUserStore = defineStore('user', () => {
}
/* 登录*/
const LoginIn = async (loginInfo) => {
loadingInstance.value = ElLoading.service({
fullscreen: true,
text: '登录中,请稍候...'
})

const res = await login(loginInfo)

// 登陆失败,直接返回
if (res.code !== 0) {
loadingInstance.value.close()
return false
}

// 登陆成功,设置用户信息和权限相关信息
setUserInfo(res.data.user)
setToken(res.data.token)
try {
loadingInstance.value = ElLoading.service({
fullscreen: true,
text: '登录中,请稍候...'
})

// 初始化路由信息
const routerStore = useRouterStore()
await routerStore.SetAsyncRouter()
const asyncRouters = routerStore.asyncRouters
const res = await login(loginInfo)

if (res.code !== 0) {
ElMessage.error(res.message || '登录失败')
return false
}
// 登陆成功,设置用户信息和权限相关信息
setUserInfo(res.data.user)
setToken(res.data.token)

// 初始化路由信息
const routerStore = useRouterStore()
await routerStore.SetAsyncRouter()
const asyncRouters = routerStore.asyncRouters

// 注册到路由表里
asyncRouters.forEach((asyncRouter) => {
router.addRoute(asyncRouter)
})

// 注册到路由表里
asyncRouters.forEach((asyncRouter) => {
router.addRoute(asyncRouter)
})
if (!router.hasRoute(userInfo.value.authority.defaultRouter)) {
ElMessage.error('请联系管理员进行授权')
} else {
await router.replace({ name: userInfo.value.authority.defaultRouter })
}

if (!router.hasRoute(userInfo.value.authority.defaultRouter)) {
ElMessage.error('请联系管理员进行授权')
} else {
await router.replace({ name: userInfo.value.authority.defaultRouter })
}
const isWindows = /windows/i.test(navigator.userAgent)
window.localStorage.setItem('osType', isWindows ? 'WIN' : 'MAC')

const isWin = ref(/windows/i.test(navigator.userAgent))
if (isWin.value) {
window.localStorage.setItem('osType', 'WIN')
} else {
window.localStorage.setItem('osType', 'MAC')
// 全部操作均结束,关闭loading并返回
return true
} catch (error) {
console.error('LoginIn error:', error)
return false
} finally {
loadingInstance.value?.close()
}

// 全部操作均结束,关闭loading并返回
loadingInstance.value.close()
return true
}
/* 登出*/
const LoginOut = async () => {
Expand All @@ -119,22 +121,14 @@ export const useUserStore = defineStore('user', () => {
/* 清理数据 */
const ClearStorage = async () => {
token.value = ''
xToken.value = ''
sessionStorage.clear()
window.localStorage.removeItem('token')
cookie.remove('x-token')
localStorage.removeItem('originSetting')
}

watch(
() => token.value,
() => {
window.localStorage.setItem('token', token.value)
}
)

return {
userInfo,
token,
token: currentToken,
NeedInit,
ResetUserInfo,
GetUserInfo,
Expand Down

0 comments on commit 3df9faa

Please sign in to comment.