|
@@ -1,4 +1,5 @@
|
|
|
import pinia from '~src/store/init'
|
|
|
+import website from '~src/config'
|
|
|
import appConfig from '~src/config/app'
|
|
|
import { useAppStore } from '~src/store'
|
|
|
import { getRoutes } from '~api/menu'
|
|
@@ -6,22 +7,24 @@ import themeData from '~src/config/theme'
|
|
|
import tokenData from '~src/router/modules/token'
|
|
|
import { logout, refreshToken, userLogin } from '~api/user'
|
|
|
import { userConfigInfo, userConfigSave } from '~api/other'
|
|
|
-import { setStoreValue } from '~src/utils/storage'
|
|
|
-import { ArrToOneObj, arrIndex, getArrValue, getObjValue, isNullES } from 'js-fast-way'
|
|
|
+import { getStoreValue, setStoreValue } from '~src/utils/storage'
|
|
|
+import { ArrToOneObj, arrIndex, getArrValue, getObjValue } from 'js-fast-way'
|
|
|
import { useOsTheme } from 'hc-vue3-ui'
|
|
|
-
|
|
|
+import { setAppName } from '~uti/tools'
|
|
|
+import logoIcon from '~src/assets/logo/icon.png'
|
|
|
+import logoName from '~src/assets/logo/name.png'
|
|
|
|
|
|
//初始变量
|
|
|
const store = useAppStore(pinia)
|
|
|
|
|
|
//登录
|
|
|
export const useAppLogin = async (form) => {
|
|
|
- const { error, code, res } = await userLogin(form)
|
|
|
+ const { error, code, res, msg } = await userLogin(form)
|
|
|
if (!error && code === 200) {
|
|
|
- await setUserAppInfo(res)
|
|
|
- return { error, code, res }
|
|
|
+ const info = await setUserAppInfo(res)
|
|
|
+ return { error: info.error, msg: info.msg }
|
|
|
} else {
|
|
|
- return { error, code, res }
|
|
|
+ return { error: true, msg: msg }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -36,43 +39,49 @@ export const setUserAppInfo = async (res) => {
|
|
|
if (routerRes.length <= 0) {
|
|
|
return { error: true, msg: '路由异常' }
|
|
|
}
|
|
|
- //获取路由首页
|
|
|
- const isHomeUrl = await getHomeRouter(routerRes)
|
|
|
- if (isNullES(isHomeUrl)) {
|
|
|
- return { error: true, msg: '菜单权限异常' }
|
|
|
- }
|
|
|
+ await getHomeRouter(routerRes)
|
|
|
//获取配置数据
|
|
|
await initUserConfigInfo()
|
|
|
- return true
|
|
|
+ return { error: false, msg: '成功' }
|
|
|
}
|
|
|
|
|
|
+//设置租户信息
|
|
|
+export const setUserTenantInfo = async (res = {}) => {
|
|
|
+ const { tenantAvatar, tenantAvatarText, tenantTitle, tenantId } = res
|
|
|
+
|
|
|
+ //设置标题
|
|
|
+ const title = tenantTitle ? tenantTitle : website.title
|
|
|
+ store.setTitle(title)
|
|
|
+ setAppName(title)
|
|
|
+
|
|
|
+ //设置logo图标
|
|
|
+ const icon = tenantAvatar ? tenantAvatar : logoIcon
|
|
|
+ store.setLogoIcon(icon)
|
|
|
+
|
|
|
+ //设置logo文字
|
|
|
+ const name = tenantAvatarText ? tenantAvatarText : logoName
|
|
|
+ store.setLogoName(name)
|
|
|
+
|
|
|
+ //设置租户ID
|
|
|
+ const id = tenantId ? tenantId : '000000'
|
|
|
+ store.setTenantId(id)
|
|
|
+
|
|
|
+ //返回数据
|
|
|
+ return { title, icon, name, id }
|
|
|
+}
|
|
|
|
|
|
//获取路由菜单的主入口路径
|
|
|
-const getHomeRouter = async (arr) => {
|
|
|
- //判断 arr 数组中的 children 中是否有数据,没数据的话,取下一个元素
|
|
|
- const menus = arr.filter((item) => {
|
|
|
- if (item?.children.length > 0) {
|
|
|
- const menua = item.children.filter((items) => {
|
|
|
- const children = getArrValue(items?.children)
|
|
|
- return children.length > 0
|
|
|
- })
|
|
|
- return menua.length > 0
|
|
|
- } else {
|
|
|
- return false
|
|
|
- }
|
|
|
- })
|
|
|
- //处理数据
|
|
|
- if (menus.length <= 0) {
|
|
|
- store.setHomeUrl('home-index')
|
|
|
- return false
|
|
|
- }
|
|
|
- const code = menus[0]?.children[0]?.children[0]?.code
|
|
|
- if (isNullES(code)) {
|
|
|
- store.setHomeUrl('home-index')
|
|
|
- return false
|
|
|
+const getHomeRouter = async () => {
|
|
|
+ const routes = getStoreValue('routesArr') ?? []
|
|
|
+ //没有菜单路由数据
|
|
|
+ if (routes.length <= 0) {
|
|
|
+ store.setHomeUrl('home')
|
|
|
+ return 'home'
|
|
|
}
|
|
|
+ //设置主入口
|
|
|
+ const code = routes[0]
|
|
|
store.setHomeUrl(code)
|
|
|
- return true
|
|
|
+ return code
|
|
|
}
|
|
|
|
|
|
//用户信息初始化
|
|
@@ -111,9 +120,7 @@ export const initUserConfigInfo = async () => {
|
|
|
export const setRouterData = async () => {
|
|
|
//请求接口,获取路由数据
|
|
|
const { error, data } = await getRoutes()
|
|
|
- if (error) {
|
|
|
- return Promise.reject(false)
|
|
|
- }
|
|
|
+ if (error) return []
|
|
|
const resData = getArrValue(data)
|
|
|
//数据转换
|
|
|
let routesObj = {}, routesArr = []
|
|
@@ -123,6 +130,7 @@ export const setRouterData = async () => {
|
|
|
store.setMenus(resData)
|
|
|
setStoreValue('route', routesObj)
|
|
|
setStoreValue('routes', routes)
|
|
|
+ setStoreValue('routesArr', routesArr)
|
|
|
return resData
|
|
|
}
|
|
|
|