- this is user
-
+
+
+ {user.isLogin && (
+
+
+
用户名:
+
+ {user.profile.username}
+
+
+
+
生日:
{user.profile.birthday}
+
+
+
性别:
+
+ {user.profile.gender === 1 ? '男' : '女'}
+
+
+
+ )}
+
+
+ {user.isLogin ? (
+ <>
+
+
+ }
+ onClick={() => {
+ Modal.confirm({
+ title: '退出登录',
+ content: '确定要退出登录吗?',
+ onOk: () => {
+ router.path = ''
+ user.logout()
+ message.success('已退出登录')
+ }
+ })
+ }}
+ >
+
+ >
+ ) : (
+ }
+ size="large"
+ onClick={() => {
+ router.path = 'global-login'
+ }}
+ >
+ 登录
+
+ )}
+
)
})
diff --git a/src/user/useUserStore.ts b/src/user/useUserStore.ts
index 475a2a9..20aacd7 100644
--- a/src/user/useUserStore.ts
+++ b/src/user/useUserStore.ts
@@ -1,43 +1,48 @@
import request from '@/utils/request'
import { defineStore } from 'pinia'
-import { computed, ref, watch } from 'vue'
+import { computed, reactive, ref, watch } from 'vue'
interface UserInfo {
id: string
username: string
- created: string
- vipUntil: string
gender: number
birthday: string
- brief: string
- email: string
- password: string
- type: string
- updated: string
avatar: string
openId: string
}
-
+const defaultUserInfo: UserInfo = {
+ id: '',
+ username: '',
+ gender: 0,
+ birthday: '',
+ avatar: '',
+ openId: ''
+}
export default defineStore('user', () => {
const token = ref(localStorage.getItem('token') || '')
watch(token, (val) => {
localStorage.setItem('token', val)
})
- const profile = ref
(null)
+ const profile = reactive(defaultUserInfo)
watch(
token,
(val) => {
if (!val) return
request('GET', '/api/profile').then((res) => {
- profile.value = res
+ Object.assign(profile, res)
})
},
{ immediate: true }
)
- const isLogin = computed(() => !!token.value && !!profile.value)
+ const isLogin = computed(() => !!token.value && !!profile.id)
+ const logout = () => {
+ token.value = ''
+ Object.assign(profile, defaultUserInfo)
+ }
return {
token,
profile,
- isLogin
+ isLogin,
+ logout
}
})
diff --git a/src/utils/upload.ts b/src/utils/upload.ts
index 10945d2..8c7f7ae 100644
--- a/src/utils/upload.ts
+++ b/src/utils/upload.ts
@@ -1,24 +1,20 @@
-import { ossBase, ossKeyUrl } from './../config'
+import { ossBase } from './../config'
import OSS from 'ali-oss'
import { v4 as uuid } from 'uuid'
+import request from './request'
export default async function upload(file: File, root: string) {
- const config = await fetch(ossKeyUrl, {
- method: 'POST',
- headers: {
- Authorization:
- 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NjlkZjA3Y2RhMjkyZTNiZTc0OGM5MmMifQ.9k9-C_Im2r2fONfT6rdAZDxapkqtwGtiVBSen59JDXY'
- }
- }).then(
- (res) =>
- res.json() as Promise<{
- region: string
- accessKeyId: string
- accessKeySecret: string
- securityToken: string
- bucket: string
- path: string
- }>
- )
+ const token = localStorage.getItem('token')
+ if (!token) {
+ throw new Error('尚未登录,无法上传')
+ }
+ const config = await request<{
+ region: string
+ accessKeyId: string
+ accessKeySecret: string
+ securityToken: string
+ bucket: string
+ path: string
+ }>('POST', '/api/ossKey')
const ext = file.name.split('.').pop()
const path = `${config.path}/${root}/${uuid()}.${ext}`
const client = new OSS({
@@ -28,13 +24,11 @@ export default async function upload(file: File, root: string) {
stsToken: config.securityToken,
bucket: config.bucket
})
- console.log(path)
const { name } = await client.put(path, file, {
mime: file.type,
headers: {
'Content-Type': file.type
}
})
- console.log(name)
return ossBase + '/' + name
}