Skip to content

Commit

Permalink
Adapt to github page
Browse files Browse the repository at this point in the history
  • Loading branch information
SingleMoonlight committed Sep 21, 2024
1 parent 114a8bb commit 3ba84fd
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 74 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/api/jsonp.js
Original file line number Diff line number Diff line change
@@ -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 };
}
39 changes: 14 additions & 25 deletions src/api/search.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion src/components/SettingItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const props = defineProps({
checked: Boolean,
inputValue: String,
textValue: String,
disabled: Boolean,
})
const settingInputRef = ref()
Expand All @@ -40,7 +41,7 @@ function ensureSettingInput() {
<div class="setting-item" @click="clickSettingItem">
<div class="setting-item-label">{{ props.label }}</div>
<div v-if="props.type === 'switch'" class="setting-switch">
<SwitchOnoff :onoff="props.onoff" @change="emit('turnSwitch')"></SwitchOnoff>
<SwitchOnoff :onoff="props.onoff" :disabled="props.disabled" @change="emit('turnSwitch')"></SwitchOnoff>
</div>
<div v-else-if="props.type === 'next'" class="setting-next">
<div class="setting-next-value">{{ props.nextValue }}</div>
Expand Down
4 changes: 4 additions & 0 deletions src/components/SwitchOnoff.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
8 changes: 6 additions & 2 deletions src/pages/SettingPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ onMounted(() => {
</div>
</div>
<div class="setting-pane-body">
<CardContainer :card-name="'天气'" :card-des="'开启时可以在导航页面右上角查看当前位置或者指定位置的天气信息。'">
<SettingItem :label="'显示天气'" :type="'switch'" :onoff="settingStore.showWeather"
<CardContainer :card-name="'天气'" :card-des="'开启时可以在导航页面左上角查看当前位置或者指定位置的天气信息。'">
<SettingItem :label="'显示天气'" :type="'switch'" :onoff="settingStore.showWeather" :disabled="true"
@turn-switch="settingStore.showWeather = !settingStore.showWeather">
</SettingItem>
</CardContainer>
Expand All @@ -697,6 +697,10 @@ onMounted(() => {
{{ item + ' ' }}
</a>
</div>
<br>
<div>
由于服务迁移,目前天气功能无法使用。
</div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/stores/settingStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 0 additions & 33 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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='),
},
},
},
})

0 comments on commit 3ba84fd

Please sign in to comment.