diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8d0b49b..cd45377 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -25,29 +25,17 @@ jobs: with: # 选择要使用的 node 版本 node-version: 20 - - # 设置密钥 - # Run the set secrets script - - name: Set secrets - env: - WEATHER_KEY: ${{ secrets.WEATHER_KEY }} - run: | - touch key.js - echo "export const WEATHER_KEY = '$WEATHER_KEY';" >> key.js # 安装依赖 # Install dependencies - name: Install Dependencies run: npm ci - - # 运行构建脚本 # Run the build script - name: Build site run: npm run build - # 查看 workflow 的文档来获取更多信息 # @see https://github.com/crazy-max/ghaction-github-pages - name: Deploy to GitHub Pages diff --git a/src/api/jsonp.js b/src/api/jsonp.js new file mode 100644 index 0000000..f6d3f83 --- /dev/null +++ b/src/api/jsonp.js @@ -0,0 +1,42 @@ +import { printLog } from '../utils/common' +export function createJsonp(url, callbackName, params) { + return new Promise((resolve, reject) => { + // Generate a unique ID for the script tag + const id = `jsonp_${Date.now()}_${Math.random().toString(36).substr(2, 5)}`; + // Define the callback function name + const uniqueCallbackName = `${callbackName}_${id}`; + + // Add the callback function to the window object + window[uniqueCallbackName] = (response) => { + resolve(response); + // Remove the script tag and the callback from the window object + document.body.removeChild(script); + delete window[uniqueCallbackName]; + }; + + // Construct the URL with the callback parameter + let finalUrl = url + (url.includes('?') ? '&' : '?') + 'callback=' + uniqueCallbackName; + Object.keys(params).forEach(key => { + finalUrl += '&' + key + '=' + encodeURIComponent(params[key]); + }); + + // Create a script element and append it to the document + const script = document.createElement('script'); + script.src = finalUrl; + script.id = id; + document.body.appendChild(script); + }); +} +export function useJsonpRequest(url, callbackName, params) { + const fetchJsonp = async () => { + try { + const response = await createJsonp(url, callbackName, params); + return response; + } catch (error) { + printLog('error', 'Error fetching JSONP:', error); + throw error; + } + }; + + return { fetchJsonp }; +} \ No newline at end of file diff --git a/src/api/search.js b/src/api/search.js index 01c77c8..efa6a57 100644 --- a/src/api/search.js +++ b/src/api/search.js @@ -1,39 +1,28 @@ -import axios from "axios" +import { useJsonpRequest } from './jsonp.js' -let searchRequestCancel = null let suggestResult = [] -export function getSearchSuggest(keyword) { +export const getSearchSuggest = async (keyword) => { return new Promise((resolve, reject) => { - if (searchRequestCancel !== null) { - searchRequestCancel(); - } - axios.get('/sug', { - params: { - prod: "pc", - wd: keyword - }, - cancelToken: new axios.CancelToken((cancel) => { - // 这里cancel就是取消当前请求的方法 - searchRequestCancel = cancel; - }) - }).then(res => { + const { fetchJsonp } = useJsonpRequest( + 'https://www.baidu.com/sugrec', // 确保这个 URL 支持 JSONP + 'jsonpCallback', // 这里指定的前缀会被用来生成回调函数名称 + { prod: "pc", wd: keyword } + ); + + fetchJsonp().then(data => { suggestResult.splice(0, suggestResult.length); - if (res.data.g !== undefined) { - res.data.g.forEach(element => { + if (data.g !== undefined) { + data.g.forEach(element => { suggestResult.push(element.q) }); } - resolve(suggestResult); + resolve(suggestResult) }).catch(err => { - if (axios.isCancel(err)) { - // 请求被取消 - } else { - reject(err.data); - } + reject(err.data); }) }) -} +}; export function doSearch(url, value, mode) { if (mode === 'current') { diff --git a/src/components/SettingItem.vue b/src/components/SettingItem.vue index 05d812c..a3fd75c 100644 --- a/src/components/SettingItem.vue +++ b/src/components/SettingItem.vue @@ -15,6 +15,7 @@ const props = defineProps({ checked: Boolean, inputValue: String, textValue: String, + disabled: Boolean, }) const settingInputRef = ref() @@ -40,7 +41,7 @@ function ensureSettingInput() {
{{ props.label }}
- +
{{ props.nextValue }}
diff --git a/src/components/SwitchOnoff.vue b/src/components/SwitchOnoff.vue index 5db1a2b..9b4177b 100644 --- a/src/components/SwitchOnoff.vue +++ b/src/components/SwitchOnoff.vue @@ -4,10 +4,14 @@ import { onMounted, ref, watch } from 'vue' const emit = defineEmits(['change']) const props = defineProps({ onoff: Boolean, + disabled: Boolean, }) const switchRoundRef = ref() function changeOnoff() { + if (props.disabled) { + return; + } emit('change'); } diff --git a/src/pages/SettingPage.vue b/src/pages/SettingPage.vue index 53a1105..058290b 100644 --- a/src/pages/SettingPage.vue +++ b/src/pages/SettingPage.vue @@ -681,8 +681,8 @@ onMounted(() => {
- - + @@ -697,6 +697,10 @@ onMounted(() => { {{ item + ' ' }}
+
+
+ 由于服务迁移,目前天气功能无法使用。 +
diff --git a/src/stores/settingStore.js b/src/stores/settingStore.js index 966f51d..e55a624 100644 --- a/src/stores/settingStore.js +++ b/src/stores/settingStore.js @@ -20,7 +20,7 @@ export const useSettingStore = defineStore('setting', { searchOpenMode: 'new', autoFocusSearchInput: false, autoCleanSearchInput: false, - showWeather: true, + showWeather: false, weatherLocationMode: 'custom', webAppGroupIndex: 0, circularSliding: true, diff --git a/vite.config.js b/vite.config.js index 3b0b795..989ac83 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,7 +1,6 @@ import { fileURLToPath, URL } from 'node:url' import { defineConfig } from 'vite' -import { WEATHER_KEY } from './key' import viteCompression from 'vite-plugin-compression' import vue from '@vitejs/plugin-vue' @@ -40,37 +39,5 @@ export default defineConfig({ server: { host: '0.0.0.0', port: 8000, - proxy: { // 这里设置代理仅对开发环境生效,生产环境需要单独设置,如nginx - '/sug': { - target: 'https://www.baidu.com/sugrec', - changeOrigin: true, // 允许跨域 - rewrite: (path) => path.replace(/^\/sug/, ''), - }, - '/geo': { - target: 'https://geoapi.qweather.com/v2/city/lookup', - changeOrigin: true, - rewrite: (path) => path.replace(/^\/geo\//, '?key=' + WEATHER_KEY + '&location='), - }, - '/nowWeather': { - target: 'https://devapi.qweather.com/v7/weather/now', - changeOrigin: true, - rewrite: (path) => path.replace(/^\/nowWeather\//, '?key=' + WEATHER_KEY + '&location='), - }, - '/nowAir': { - target: 'https://devapi.qweather.com/v7/air/now', - changeOrigin: true, - rewrite: (path) => path.replace(/^\/nowAir\//, '?key=' + WEATHER_KEY + '&location='), - }, - '/futureWeather': { - target: 'https://devapi.qweather.com/v7/weather/3d', - changeOrigin: true, - rewrite: (path) => path.replace(/^\/futureWeather\//, '?key=' + WEATHER_KEY + '&location='), - }, - '/futureAir': { - target: 'https://devapi.qweather.com/v7/air/5d', - changeOrigin: true, - rewrite: (path) => path.replace(/^\/futureAir\//, '?key=' + WEATHER_KEY + '&location='), - }, - }, }, })