Skip to content

Commit

Permalink
chore: 简化代理
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanEichDisciple committed Aug 9, 2024
1 parent 6421b6b commit 420aff3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
// 瓦片图层
const tileLayer1 = new TileLayer({
source: new XYZ({
url: '/tile/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}'
url: `https://t{0-7}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${import.meta.env.VITE_TK}`
})
});
const tileLayer2 = new TileLayer({
source: new XYZ({
url: '/tile/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}'
url: `https://t{0-7}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${import.meta.env.VITE_TK}`
})
});
tileLayer1.set('name', 'tileLayer1');
Expand Down
34 changes: 2 additions & 32 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,6 @@ import dotenv from 'dotenv';
dotenv.config();

export default defineConfig({
base: process.env.VITE_BASE || '/',
plugins: [sveltekit()],
server: {
proxy: {
'/tile': {
target: 'https://t0.tianditu.gov.cn',
changeOrigin: true,
rewrite: (path) => {
if (path && process.env.VITE_TK) {
const url = `${path.replace(/^\/tile/, '')}&tk=${process.env.VITE_TK}`;
return url;
}
throw new Error('Path or VITE_TK is missing');
},
configure: (proxy, options) => {
if (options) {
options.headers = options.headers || {};
options.headers.Referer = 'https://t0.tianditu.gov.cn';
}
},
bypass: (req, res, proxyOptions) => {
if (req && req.url && proxyOptions) {
const t = Math.floor(Math.random() * 8); // Randomly select a value between 0 and 7
req.headers.Host = `t${t}.tianditu.gov.cn`;
req.url = req.url.replace(/t{0-7}/, `t${t}`);
} else {
throw new Error('Invalid arguments for bypass function');
}
}
}
}
}
base: process.env.VITE_BASE || '/',
plugins: [sveltekit()]
});

0 comments on commit 420aff3

Please sign in to comment.