feat: 添加pinia状态管理库和Tabbar组件

- 添加pinia状态管理库和Tabbar组件
- 导入新的组件和模块
- 更新页面布局和样式
- 删除无用的文件和代码
This commit is contained in:
Timothy Yin 2024-09-18 22:08:19 +08:00
parent 34222f94d8
commit cf77c72dfe
15 changed files with 216 additions and 49 deletions

6
components.d.ts vendored
View File

@ -7,9 +7,15 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
PageWrapper: typeof import('./src/components/page-wrapper.vue')['default']
Tabbar: typeof import('./src/components/Tabbar.vue')['default']
WdButton: typeof import('wot-design-uni/components/wd-button/wd-button.vue')['default']
WdCell: typeof import('wot-design-uni/components/wd-cell/wd-cell.vue')['default']
WdCellGroup: typeof import('wot-design-uni/components/wd-cell-group/wd-cell-group.vue')['default']
WdForm: typeof import('wot-design-uni/components/wd-form/wd-form.vue')['default']
WdInput: typeof import('wot-design-uni/components/wd-input/wd-input.vue')['default']
WdTabbar: typeof import('wot-design-uni/components/wd-tabbar/wd-tabbar.vue')['default']
WdTabbarItem: typeof import('wot-design-uni/components/wd-tabbar-item/wd-tabbar-item.vue')['default']
WdToast: typeof import('wot-design-uni/components/wd-toast/wd-toast.vue')['default']
}
}

View File

@ -2,6 +2,7 @@
import { ref } from 'vue';
import { useRouter, useRoute } from 'uni-mini-router'
import { onMounted, computed, watch } from 'vue';
import { useTabbar } from '@/stores/useTabbar';
const props = defineProps({
currentName: {
@ -10,18 +11,12 @@ const props = defineProps({
}
})
const route = useRoute()
const router = useRouter()
const tab = useTabbar()
const activeTab = ref('home')
onMounted(() => {
activeTab.value = props.currentName
})
watch(activeTab, (val) => {
router.pushTab({ name: val })
})
// onMounted(() => {
// tab.activeTab = props.currentName
// })
const nameLabelIconMap = {
home: {
@ -49,8 +44,8 @@ const tabList = computed(() => router.routes.map((route: { name: keyof typeof na
<template>
<div>
<wd-tabbar v-model="activeTab" fixed safe-area-inset-bottom bordered placeholder>
<wd-tabbar-item v-for="(tab, i) in tabList" :name="tab.name" :title="tab.title" :icon="tab.icon" :key="i" />
<wd-tabbar v-model="tab.activeTab" fixed safe-area-inset-bottom bordered placeholder>
<wd-tabbar-item v-for="(tab, i) in tabList" :name="tab.name" :title="tab.title" :icon="tab.icon" :key="i" @tap="router.pushTab({ name: tab.name })" />
</wd-tabbar>
</div>
</template>

View File

@ -0,0 +1,27 @@
<script lang="ts" setup>
import TabBar from '@/components/Tabbar.vue';
import { useUser } from '@/stores/useUser';
import { useRouter } from 'uni-mini-router';
import { onMounted } from 'vue';
const router = useRouter()
const user = useUser()
onMounted(() => {
if (!user.userinfo) {
router.replaceAll('/pages/login/index')
}
})
</script>
<template>
<div>
<slot></slot>
<wd-toast />
<tab-bar current-name="home" />
</div>
</template>
<style scoped>
</style>

View File

@ -0,0 +1,10 @@
import { defineStore } from "pinia";
import { ref } from "vue";
export const useConfig = defineStore('config', () => {
const BASE_URL = ref<string>("https://ppmp.fenshenzhike.com/api");
return {
BASE_URL
}
})

View File

@ -13,6 +13,13 @@
"style": {
"navigationBarTitleText": "我的"
}
},
{
"name": "login",
"path": "pages/login/index",
"style": {
"navigationBarTitleText": "登录"
}
}
],
"tabBar": {

View File

@ -1,30 +1,18 @@
<template>
<div class="content">
<img class="logo" src="/static/logo.png" />
<div class="flex flex-col items-center gap-4">
<p class="title text-red-500">
{{ title }}
</p>
<WdButton @click="onClick">Increment</WdButton>
<Tabbar current-name="home" />
<page-wrapper>
<div class="content">
<img class="logo" src="/static/logo.png" />
<div class="flex flex-col items-center gap-4">
<p class="title text-red-500">
Welcome
</p>
</div>
</div>
</div>
</page-wrapper>
</template>
<script setup lang="ts">
import { useUserStore } from '@/stores/user';
import { computed } from 'vue';
import Tabbar from '@/components/Tabbar.vue';
const user = useUserStore()
const onClick = () => {
user.count++
}
const title = computed(() => `Counter: ${user.count}`)
import pageWrapper from '@/components/page-wrapper.vue';
</script>
<style>

69
src/pages/login/index.vue Normal file
View File

@ -0,0 +1,69 @@
<script lang="ts" setup>
import { useConfig } from '@/composables/useConfig';
import { reactive, ref } from 'vue';
import { useToast } from 'wot-design-uni';
const toast = useToast()
const config = useConfig()
const model = reactive<{
email: string
password: string
remember: boolean
}>({
email: '',
password: '',
remember: false
})
const form = ref()
const handleSubmit = () => {
form.value.validate()
.then(({ valid, errors }: { valid: boolean; errors: any }) => {
if (valid) {
toast.loading({
msg: '登录中...'
})
fetch(`${config.BASE_URL}/login`, {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ email: 'root@cyqsd.cn', password: 'Abc123456', remember: 'false' })
})
.then(res => res.json())
.then(res => {
toast.info({
msg: res.token
})
})
.catch(err => {
toast.error({
msg: err || '登录失败,未知错误'
})
})
}
})
.catch((error: any) => {
console.log(error, 'error')
})
}
</script>
<template>
<div>
<wd-form ref="form" class="p-4" :model="model">
<wd-cell-group border>
<wd-input label="邮箱" label-width="100px" prop="email" clearable v-model="model.email" placeholder="请输入邮箱"
:rules="[{ required: true, message: '请填写邮箱' }]" />
<wd-input label="密码" label-width="100px" prop="password" show-password clearable v-model="model.password"
placeholder="请输入密码" :rules="[{ required: true, message: '请填写密码' }]" />
</wd-cell-group>
<view class="p-4">
<wd-button type="primary" size="large" @click="handleSubmit" block>登录</wd-button>
</view>
</wd-form>
<wd-toast />
</div>
</template>
<style scoped></style>

View File

@ -1,12 +1,21 @@
<script lang="ts" setup>
import Tabbar from '@/components/Tabbar.vue';
import pageWrapper from '@/components/page-wrapper.vue';
</script>
<template>
<div>
<h1>我的页面</h1>
<Tabbar current-name="my" />
</div>
<page-wrapper>
<div class="p-4 flex flex-col gap-4">
<WdCellGroup :border="true">
<WdCell title="用户名" value="test1" />
<WdCell title="单位" value="重庆眩生花科技有限公司" />
<WdCell title="角色" value="普通管理员" />
<WdCell title="手机" value="15023333333" />
</WdCellGroup>
<div class="px-4">
<wd-button plain hairline type="error">退出账号</wd-button>
</div>
</div>
</page-wrapper>
</template>
<style scoped>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 200 KiB

10
src/stores/useTabbar.ts Normal file
View File

@ -0,0 +1,10 @@
import { defineStore } from "pinia";
import { ref } from "vue";
export const useTabbar = defineStore('tabbar', () => {
const activeTab = ref('home')
return {
activeTab
}
})

11
src/stores/useUser.ts Normal file
View File

@ -0,0 +1,11 @@
import type { User } from "@/types/api/user";
import { defineStore } from "pinia";
import { ref } from "vue";
export const useUser = defineStore("user", () => {
const userinfo = ref<User | null>(null);
return {
userinfo,
};
});

View File

@ -1,10 +0,0 @@
import { defineStore } from "pinia";
import { ref } from "vue";
export const useUserStore = defineStore("user", () => {
const count = ref(0);
return {
count,
};
});

2
src/type.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
//type.d.ts
declare const ROUTES: [];

43
src/types/api/user.ts Normal file
View File

@ -0,0 +1,43 @@
export interface User {
id: number;
username: string;
email: string;
avatar: string;
department_id: null;
creator_id: number;
status: number;
login_ip: string;
login_at: number;
created_at: string;
updated_at: string;
deleted_at: Date;
permissions: Permission[];
roles: any[];
jobs: any[];
}
export interface Permission {
id: number;
parent_id: number;
permission_name: string;
route: string;
icon: string;
module: PermissionModule;
permission_mark: string;
component: string;
redirect: null | string;
keepalive: number;
type: number;
hidden: boolean;
sort: number;
active_menu: string;
creator_id: number;
created_at: string;
updated_at: string;
}
export enum PermissionModule {
Lesson = "lesson",
Permissions = "permissions",
User = "user",
}