第一次提交
This commit is contained in:
113
src/stores/api.js
Normal file
113
src/stores/api.js
Normal file
@ -0,0 +1,113 @@
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { useSSRContext } from 'vue'
|
||||
import axios from 'axios'
|
||||
|
||||
import { objectToQueryString, getCookie, setCookie } from '@/utils.js'
|
||||
|
||||
const apiMapping = {
|
||||
'': ['http://localhost:28001/','/api/']
|
||||
}
|
||||
|
||||
function replaceUrl(url) {
|
||||
return url
|
||||
.replace('{{hostname}}',window.location.hostname)
|
||||
.replace('{{port}}',window.location.port)
|
||||
.replace('{{protocol}}',window.location.protocol)
|
||||
}
|
||||
|
||||
export const useApiStore = defineStore('api', () => {
|
||||
let host = import.meta.env.SSR ? useSSRContext().host : window.location.host
|
||||
let entry = apiMapping[host] ? apiMapping[host] : apiMapping['']
|
||||
let endpoint = import.meta.env.SSR ? entry[0] : replaceUrl(entry[1])
|
||||
//console.log('Entry point:', endpoint)
|
||||
var inited = false
|
||||
var initializing = false
|
||||
async function apiGet(url, data) {
|
||||
const realURL = data
|
||||
? `${endpoint}${url}?${objectToQueryString(data)}`
|
||||
: `${endpoint}${url}`
|
||||
try {
|
||||
let start = Date.now()
|
||||
const response = await axios.get(realURL)
|
||||
let stop = Date.now()
|
||||
return {
|
||||
status: response.status,
|
||||
data: response.data,
|
||||
start, stop,
|
||||
duration: stop - start,
|
||||
isSSR: import.meta.env.SSR
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
return {
|
||||
status: error.response.status,
|
||||
data: error.response.data,
|
||||
isSSR: import.meta.env.SSR
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
status: -1,
|
||||
data: null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
async function apiPost(url, data) {
|
||||
const realURL = `${endpoint}${url}`;
|
||||
try {
|
||||
let start = Date.now()
|
||||
const response = await axios.post(realURL, data, {
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
})
|
||||
let stop = Date.now()
|
||||
return {
|
||||
status: response.status,
|
||||
data: response.data,
|
||||
start, stop,
|
||||
duration: stop - start,
|
||||
isSSR: import.meta.env.SSR
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
return {
|
||||
status: error.response.status,
|
||||
data: error.response.data,
|
||||
isSSR: import.meta.env.SSR
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
status: -1,
|
||||
data: null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
async function init() {
|
||||
if (inited) return
|
||||
if (initializing) {
|
||||
while (initializing) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||
}
|
||||
return
|
||||
}
|
||||
initializing = true
|
||||
inited = true
|
||||
initializing = false
|
||||
if (!import.meta.env.SSR) {
|
||||
console.log(`[API] Inited! endpoint: ${endpoint}`)
|
||||
}
|
||||
}
|
||||
async function reInit(){
|
||||
inited = false
|
||||
await init()
|
||||
}
|
||||
async function getWork(workId) {
|
||||
return await apiGet('work',{ workId })
|
||||
}
|
||||
return {
|
||||
init,
|
||||
reInit,
|
||||
getWork
|
||||
}
|
||||
})
|
58
src/stores/db.js
Normal file
58
src/stores/db.js
Normal file
@ -0,0 +1,58 @@
|
||||
import { ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { openDB } from 'idb';
|
||||
|
||||
export const useDB = defineStore('_db', () => {
|
||||
const dbPromise = openDB('data', 1, {
|
||||
upgrade(db) {
|
||||
const bookmarkStore = db.createObjectStore('bookmarks', {
|
||||
keyPath: 'id',
|
||||
autoIncrement: true,
|
||||
})
|
||||
bookmarkStore.createIndex('by-workId', 'workId');
|
||||
},
|
||||
})
|
||||
return {
|
||||
db: dbPromise
|
||||
}
|
||||
})
|
||||
|
||||
export const useBookmarkStore = defineStore('bookmark', () => {
|
||||
const db = useDB().db
|
||||
async function getAll(workId) {
|
||||
return (await db).getAllFromIndex('bookmarks', 'by-workId', workId);
|
||||
}
|
||||
async function get(id) {
|
||||
return (await db).get('bookmarks', id);
|
||||
}
|
||||
async function add(workId, index, para, name ) {
|
||||
return (await db).add('bookmarks', {
|
||||
workId, name, para, index
|
||||
});
|
||||
}
|
||||
async function del(id) {
|
||||
(await db).delete('bookmarks', id);
|
||||
}
|
||||
async function delByWork(workId) {
|
||||
(await getAll(workId)).forEach(async (item) => {
|
||||
del(item.id)
|
||||
})
|
||||
}
|
||||
async function updateName(id, name) {
|
||||
const raw = await get(id)
|
||||
if (raw) {
|
||||
raw.name = name
|
||||
console.log(name)
|
||||
await (await db).put('bookmarks', raw);
|
||||
}
|
||||
}
|
||||
return {
|
||||
get,
|
||||
add,
|
||||
del,
|
||||
getAll,
|
||||
delByWork,
|
||||
updateName
|
||||
}
|
||||
})
|
25
src/stores/device.js
Normal file
25
src/stores/device.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { breakpoint } from 'mdui/functions/breakpoint.js'
|
||||
import { observeResize } from 'mdui/functions/observeResize.js'
|
||||
|
||||
export const useMobileScreen = defineStore('deviceMobileScreen', () => {
|
||||
function _() {
|
||||
if (import.meta.env.SSR) { return false }
|
||||
else { return breakpoint().down('md') ? true : false }
|
||||
}
|
||||
const isMobile = ref(_())
|
||||
if (!import.meta.env.SSR) {
|
||||
const observer = observeResize(document.body, (entry, obs) => {
|
||||
isMobile.value = _()
|
||||
})
|
||||
}
|
||||
function reCal() {
|
||||
isMobile.value = _()
|
||||
}
|
||||
return {
|
||||
isMobile,
|
||||
reCal
|
||||
}
|
||||
})
|
71
src/stores/route.js
Normal file
71
src/stores/route.js
Normal file
@ -0,0 +1,71 @@
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
|
||||
export const useRouteStore = defineStore('route', () => {
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const allRoutes = ref(router.getRoutes()
|
||||
.filter(route => route.meta.hidden !== true)
|
||||
.map(route => ({
|
||||
path: route.path,
|
||||
name: route.name,
|
||||
order: route.meta.order || Number.MAX_SAFE_INTEGER
|
||||
}))
|
||||
.sort((a, b) => (a.order - b.order))
|
||||
)
|
||||
const lastFromDrawer = ref(0)
|
||||
const customTitle = ref(null)
|
||||
const title = computed(() => customTitle.value || route.meta.title || route.name)
|
||||
function drawerPress(target) {
|
||||
if (lastFromDrawer.value == 0) {
|
||||
lastFromDrawer.value = 1
|
||||
router.push(target)
|
||||
} else {
|
||||
router.replace(target)
|
||||
}
|
||||
}
|
||||
const progress = ref(0)
|
||||
const progressMax = ref(1)
|
||||
const showProgress = ref(false)
|
||||
if (!import.meta.env.SSR) {
|
||||
watch(title, title => document.title = title)
|
||||
let progressTimer = null
|
||||
router.beforeEach((to, from) => {
|
||||
if (lastFromDrawer.value == 2) {
|
||||
lastFromDrawer.value = 0
|
||||
} else if (lastFromDrawer.value == 1) {
|
||||
lastFromDrawer.value = 2
|
||||
}
|
||||
progress.value = 0
|
||||
progressMax.value = 1
|
||||
showProgress.value = true
|
||||
if (!progressTimer) {
|
||||
progressTimer = setInterval(() => {
|
||||
progress.value += progressMax.value / 10
|
||||
if (progressMax.value <= progress.value) progressMax.value = progressMax.value * 3
|
||||
}, 300)
|
||||
}
|
||||
return true
|
||||
})
|
||||
router.afterEach((to, from) => {
|
||||
if (progressTimer) {
|
||||
showProgress.value = false
|
||||
clearInterval(progressTimer)
|
||||
progressTimer = null
|
||||
}
|
||||
customTitle.value = null
|
||||
if (!import.meta.env.SSR) window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
|
||||
})
|
||||
}
|
||||
return {
|
||||
allRoutes,
|
||||
lastFromDrawer,
|
||||
title,
|
||||
drawerPress,
|
||||
showProgress,
|
||||
progress,
|
||||
progressMax,
|
||||
customTitle
|
||||
}
|
||||
})
|
38
src/stores/themeScheme.js
Normal file
38
src/stores/themeScheme.js
Normal file
@ -0,0 +1,38 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { setTheme } from 'mdui/functions/setTheme.js'
|
||||
import { setColorScheme } from 'mdui/functions/setColorScheme.js'
|
||||
|
||||
export const useThemeStore = defineStore('homePage', () => {
|
||||
const mode = ref('auto')
|
||||
const color = ref('#890000')
|
||||
function setColor(target) {
|
||||
if (color.value != target) {
|
||||
color.value = target
|
||||
}
|
||||
setColorScheme(color.value)
|
||||
}
|
||||
function setMode(target) {
|
||||
if (mode.value != target) {
|
||||
mode.value = target
|
||||
}
|
||||
setTheme(mode.value)
|
||||
}
|
||||
function switchMode(callback) {
|
||||
if (mode.value === 'auto' || mode.value === 'light') {
|
||||
mode.value = 'dark'
|
||||
} else {
|
||||
mode.value = 'light'
|
||||
}
|
||||
setMode(mode.value)
|
||||
if (callback) {
|
||||
callback(mode.value)
|
||||
}
|
||||
}
|
||||
function applyTheme() {
|
||||
setColorScheme(color.value)
|
||||
setTheme(mode.value)
|
||||
}
|
||||
return { setColor, setMode, switchMode, applyTheme }
|
||||
})
|
47
src/stores/workRead.js
Normal file
47
src/stores/workRead.js
Normal file
@ -0,0 +1,47 @@
|
||||
import { ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { escapeAndFormatText } from '../utils.js'
|
||||
|
||||
import { useApiStore } from '@/stores/api.js'
|
||||
|
||||
export const useWorkReadState = defineStore('workRead', () => {
|
||||
const api = useApiStore()
|
||||
const id = ref(null)
|
||||
const summary = ref(null)
|
||||
const pesud = ref(null)
|
||||
const title = ref(null)
|
||||
const text = ref(null)
|
||||
const publishedTime = ref(null)
|
||||
const state = ref('')
|
||||
function setData(data) {
|
||||
id.value = data.workId
|
||||
title.value = data.title
|
||||
summary.value = [escapeAndFormatText(data.summary)]
|
||||
pesud.value = data.pesud
|
||||
text.value = data.text.split('\n\n')
|
||||
}
|
||||
async function loadWork(target) {
|
||||
if (target == id.value || state.value == 'loading') return
|
||||
state.value = 'loading'
|
||||
const result = await api.getWork(target)
|
||||
if (result.status == 200) {
|
||||
setData(result.data)
|
||||
state.value = 'ready'
|
||||
} else {
|
||||
id.value = target
|
||||
state.value = import.meta.env.SSR ? 'ssrnotfound' : 'notfound'
|
||||
}
|
||||
}
|
||||
return {
|
||||
id,
|
||||
title,
|
||||
summary,
|
||||
pesud,
|
||||
text,
|
||||
publishedTime,
|
||||
state,
|
||||
setData,
|
||||
loadWork
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user