From 26097d571027ce753e6bcb3ec4841e7768065c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=8C=E7=B3=96=E5=8C=85=E5=AD=90?= Date: Sat, 21 Mar 2026 14:20:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=93=8D=E5=BA=94=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/yilia/source-src/js/mobile.js | 32 ++++++++++++------------ themes/yilia/source-src/js/photo-wall.js | 10 ++++---- themes/yilia/source-src/js/request.js | 25 ++++++++++++++++++ themes/yilia/source-src/js/slider.js | 22 ++++++++-------- 4 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 themes/yilia/source-src/js/request.js diff --git a/themes/yilia/source-src/js/mobile.js b/themes/yilia/source-src/js/mobile.js index 60c48a3..47440ea 100644 --- a/themes/yilia/source-src/js/mobile.js +++ b/themes/yilia/source-src/js/mobile.js @@ -20,23 +20,23 @@ function tabActive() { }) } -function getElementLeft(element) {     - var actualLeft = element.offsetLeft     - var current = element.offsetParent     - while (current !== null) {       - actualLeft += current.offsetLeft     - current = current.offsetParent   - }   +function getElementLeft(element) { + var actualLeft = element.offsetLeft + var current = element.offsetParent + while (current !== null) { + actualLeft += current.offsetLeft + current = current.offsetParent + } return actualLeft -}   -function getElementTop(element) {     - var actualTop = element.offsetTop     - var current = element.offsetParent     - while (current !== null) {       - actualTop += current.offsetTop       - current = current.offsetParent     - }     - return actualTop   +} +function getElementTop(element) { + var actualTop = element.offsetTop + var current = element.offsetParent + while (current !== null) { + actualTop += current.offsetTop + current = current.offsetParent + } + return actualTop } function scrollStop($dom, top, limit, zIndex, diff) { diff --git a/themes/yilia/source-src/js/photo-wall.js b/themes/yilia/source-src/js/photo-wall.js index 741781f..fa05dd9 100644 --- a/themes/yilia/source-src/js/photo-wall.js +++ b/themes/yilia/source-src/js/photo-wall.js @@ -1,4 +1,4 @@ -import axios from 'axios' +import http from './request' import PhotoSwipe from 'photoswipe' import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default' @@ -27,10 +27,10 @@ function loadMoreItems(step) { scrollLock = true //加载过程中锁定滚动加载 loadTip.style.display = 'block' // 滚动到底部时调用 - axios.get(`${themeConfig.root}api/v2/common/photos`, {params: {start:totalIndex, limit:step}}).then(res => { + http.get('/api/v2/common/photos', {params: {start:totalIndex, limit:step}}).then(res => { var itemContainer = document.createElement('div') - for(let index = 0 ; index= res.data.total ) { + if( totalIndex >= res.total ) { loadTip.textContent = '没有更多图片啦/(ㄒoㄒ)/~~' } else { setTimeout(()=>{ diff --git a/themes/yilia/source-src/js/request.js b/themes/yilia/source-src/js/request.js new file mode 100644 index 0000000..ea4e018 --- /dev/null +++ b/themes/yilia/source-src/js/request.js @@ -0,0 +1,25 @@ +import axios from 'axios' + +const http = axios.create({ + timeout: 10000, + paramsSerializer: { + indexes: null + } +}) + +http.interceptors.response.use(res => { + const responseBody = res.data + // 统一响应格式处理 + switch (responseBody.code) { + case 0: + // 成功,直接返回数据 + return responseBody.data + case -1: + return Promise.reject(new Error(responseBody.message || '请求失败')) + default: + // 其他情况,兼容没有包装格式的响应 + return res.data + } +}, err => Promise.reject(err)) + +export default http \ No newline at end of file diff --git a/themes/yilia/source-src/js/slider.js b/themes/yilia/source-src/js/slider.js index df24ca7..047284f 100644 --- a/themes/yilia/source-src/js/slider.js +++ b/themes/yilia/source-src/js/slider.js @@ -1,4 +1,4 @@ -import axios from 'axios' +import http from './request' import Vue from 'vue/dist/vue.esm' import waifuTips from '../config/waifu-tip.json' @@ -99,16 +99,15 @@ const vm = new Vue({ limit: this.fullTextSearch.limit, words: this.fullTextSearchWords } - axios.get(window.themeConfig.root + 'api/v2/common/search', {params}).then(res => { + http.get('/api/v2/common/search', {params}).then(res => { this.fullTextSearch.isLoading = false fullTextSearchTimer = null - let result = res.data - if(!Array.isArray(result.data) || !result.data.length) { + if(!Array.isArray(res.list) || !res.list.length) { this.fullTextSearch.tip = '未搜索到匹配文章' } else { - this.fullTextSearchItems.push(...result.data) + this.fullTextSearchItems.push(...res.list) } - this.fullTextSearch.hasMore = (result.total > this.fullTextSearch.pageNum * this.fullTextSearch.limit) + this.fullTextSearch.hasMore = (res.total > this.fullTextSearch.pageNum * this.fullTextSearch.limit) }).catch(err => { this.fullTextSearch.tip = '加载失败, 请刷新重试' this.fullTextSearch.isLoading = false @@ -169,8 +168,8 @@ const vm = new Vue({ } }, mounted () { - axios.get(window.themeConfig.root + 'content.json').then((res)=>{ - this.items = res.data + fetch(window.themeConfig.root + 'content.json').then(res => res.json()).then(resJson=>{ + this.items = resJson }).catch(err => { console.warn('加载文章列表失败') }) @@ -225,8 +224,7 @@ function handleSearch(searchItems) { async function welcomeMessage() { let now = new Date().getHours() - return axios.get(`${window.themeConfig.root}api/v2/common/config/waifu_tip`).then(res => { - let textTimes = res.data + return http.get('/api/v2/common/config/waifu_tip').then(textTimes => { let text = null Array.prototype.sort.call(textTimes, (item1, item2) => { if(item1.start>item2.start) { @@ -273,8 +271,8 @@ const waifuTools = { }, 'tools.chart'() { // 一言 - axios.get(`${window.themeConfig.root}api/v2/common/hitokoto?length=40&format=json`).then(res => { - this.showMessage(res.data.hitokoto + (res.data.from?`  ——${res.data.from}`:'')) + http.get('/api/v2/common/hitokoto?length=40&format=json').then(res => { + this.showMessage(res.hitokoto + (res.from?`  ——${res.from}`:'')) }) }, 'tools.search'() {