Skip to content

Commit

Permalink
chore: add pinia, unocss, auth
Browse files Browse the repository at this point in the history
  • Loading branch information
NMTuan committed Jun 30, 2023
1 parent a5df4f0 commit 0af3e6d
Show file tree
Hide file tree
Showing 11 changed files with 624 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div>
<NuxtWelcome />
</div>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
89 changes: 89 additions & 0 deletions composables/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-27 15:59:25
* @LastEditTime: 2023-06-30 20:55:06
* @LastEditors: NMTuan
* @Description:
* @FilePath: \laf_curd\composables\request.js
*/
export const request = (params, disabledLoading) => {
// let loading
const runtimeConfig = useRuntimeConfig()
const userStore = useUserStore()

const defaultParams = {
url: undefined,
method: 'GET',
path: undefined,
query: {},
body: {},
auth: true
}

params = { ...defaultParams, ...params }

// 如果传了url则使用,否则用env中配置项。
const url = (params.url || runtimeConfig.public.requestUrl) + params.path
delete params.url
delete params.path

// get请求不需要body
if (params.method === 'GET') {
delete params.body
}

// 鉴权
if (params.auth !== false) {
params.headers = {
Authorization: 'Bearer ' + userStore.token
}
}
delete params.auth

return new Promise((resolve, reject) => {
if (!url) {
reject({
code: 40000,
message: 'no url'
})
}
// if (!disabledLoading) {
// loading = ElLoading.service()
// }
$fetch(url, params)
// .finally(() => {
// if (!disabledLoading) {
// loading.close()
// }
// })
.then((res) => {
if (res instanceof Blob) {
resolve(res)
} else if (!res.error) {
if (res.message) {
// ElMessage({
// message: res.message,
// type: 'success'
// })
}
resolve(res)
} else {
if (res.code && res.message) {
// ElMessageBox.alert(res.message, {
// title: `Error ${res.code}`
// })
}
reject(res)
}
})
.catch((res) => {
if (res.response && res.response.status) {
// ElMessageBox.alert('网络异常,请稍后重试!', {
// title: `Error ${res.response.status}`
// })
}
reject(res)
})
})
}
19 changes: 19 additions & 0 deletions middleware/auth.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-29 21:14:49
* @LastEditTime: 2023-06-29 21:28:45
* @LastEditors: NMTuan
* @Description:
* @FilePath: \ezMemos\middleware\auth.global.ts
*/
export default defineNuxtRouteMiddleware((to, from) => {
const userStore = useUserStore()
console.log('to', to)
console.log('userStore', userStore.whiteList)

// 不在白名单, 而且没token
if (!userStore.whiteList.includes(to.path) && !userStore.token) {
return navigateTo('/welcome')
}
})
25 changes: 24 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
/*
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-30 19:16:42
* @LastEditTime: 2023-06-30 20:30:13
* @LastEditors: NMTuan
* @Description:
* @FilePath: \laf_curd\nuxt.config.ts
*/
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true }
css: ['@unocss/reset/normalize.css'],
modules: ['@unocss/nuxt', '@pinia/nuxt'],
devtools: { enabled: true },
runtimeConfig: {
public: {
requestUrl: 'https://api.laf.run',
pat: ''
}
},
experimental: {
viewTransition: true
},
imports: {
dirs: ['stores']
}
})
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"devDependencies": {
"@nuxt/devtools": "latest",
"@types/node": "^18",
"@unocss/nuxt": "^0.53.4",
"nuxt": "^3.6.1"
},
"dependencies": {
"@pinia/nuxt": "^0.4.11",
"pinia": "^2.1.4"
}
}
14 changes: 14 additions & 0 deletions pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-30 20:33:19
* @LastEditTime: 2023-06-30 20:34:11
* @LastEditors: NMTuan
* @Description:
* @FilePath: \laf_curd\pages\index.vue
-->
<template>
<div>
index !
</div>
</template>
14 changes: 14 additions & 0 deletions pages/login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-30 20:41:21
* @LastEditTime: 2023-06-30 20:43:25
* @LastEditors: NMTuan
* @Description:
* @FilePath: \laf_curd\pages\login.vue
-->
<template>
<div>
login !
</div>
</template>
52 changes: 52 additions & 0 deletions pages/welcome.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-30 20:33:22
* @LastEditTime: 2023-06-30 20:53:15
* @LastEditors: NMTuan
* @Description:
* @FilePath: \laf_curd\pages\welcome.vue
-->
<template>
<div>
<div v-if="error">error</div>
welcome !

<p>requestUrl:{{ requestUrl }}</p>

<p>pat:{{ pat }}</p>

<p><button @click="handlerLogin">login</button></p>
</div>
</template>
<script setup>
const runtimeConfig = useRuntimeConfig()
const userStore = useUserStore()
const { requestUrl, pat } = runtimeConfig.public
const error = ref(false)

const handlerLogin = async () => {
if (!requestUrl || !pat) {
error.value = true
return
}
error.value = false
request({
path: '/v1/auth/pat2token',
method: 'POST',
body: {
pat
}
})
.then(res => {
if (res.data) {
userStore.update_token(res.data)
navigateTo({ name: 'index' })
}
})
.catch(err => {
error.value = true
})
}

</script>
25 changes: 25 additions & 0 deletions stores/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* @Author: NMTuan
* @Email: NMTuan@qq.com
* @Date: 2023-06-30 20:30:22
* @LastEditTime: 2023-06-30 20:59:01
* @LastEditors: NMTuan
* @Description:
* @FilePath: \laf_curd\stores\user.ts
*/
import { defineStore } from 'pinia'

export const useUserStore = defineStore('useUserStore', () => {
const token = useCookie('laf_curd_token')
const whiteList = ['/welcome'] // 不需要鉴权的url路径

const update_token = (val: string) => {
token.value = val
}

return {
token,
whiteList,
update_token
}
})
16 changes: 16 additions & 0 deletions uno.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig, presetAttributify, presetUno } from 'unocss'
import transformerDirectives from '@unocss/transformer-directives'
import transformerVariantGroup from '@unocss/transformer-variant-group'
import presetIcons from '@unocss/preset-icons'

export default defineConfig({
presets: [
presetUno(), // default
presetAttributify(), // attr mode: text="sm white"
presetIcons() // icon: i-ri-home-fill
],
transformers: [
transformerDirectives(), // @apply or --at-apply
transformerVariantGroup() // hover:(x x)
]
})
Loading

0 comments on commit 0af3e6d

Please sign in to comment.