引言:
最近開發(fā)一個微信公眾號項目,需要授權(quán)登錄,開發(fā)過這種項目的都知道疾嗅,需要跳轉(zhuǎn)到公眾號后臺配置的安全域名才能獲取到code,而對于本地調(diào)式來說冕象,這顯而不適合代承,所以我們需要將本地項目運行的地址映射到線上,話不多說渐扮,上代碼:
1.配置uniapp的manifest.json文件论悴,代碼如下:
"h5": {
? ? "router": {
? ? ? "mode": "history"
? ? },
? ? "domain": "http://example.com",//服務(wù)器地址(復(fù)制粘貼請去掉這段)
? ? "devServer": {
? ? ? "port": 80,
? ? ? "disableHostCheck": true
? ? }
? }
2.本地HOSTS文件(一般地址:C:\Windows\System32\drivers\etc)掖棉,用管理員身份運行一個IED(代碼編輯器)打開,添加一段配置:
127.0.0.1 example.com
example.com為你的回調(diào)地址膀估,不要加http或者h(yuǎn)ttps協(xié)議頭啊片,一定記得不要在這行前面加#號,#號是注釋玖像,不生效的齐饮;
3.若是有vite.config.ts文件捐寥,代碼如下:
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
// https://vitejs.dev/config/
export default defineConfig({
? build: {
? ? // 開發(fā)階段啟用源碼映射:https://uniapp.dcloud.net.cn/tutorial/migration-to-vue3.html#需主動開啟-sourcemap
? ? sourcemap: process.env.NODE_ENV === 'development',
? },
? plugins: [uni()],
? server: {
? ? host: '127.0.0.1',
? ? port: 8080,
? ? proxy: {
? ? ? '/api': {
????????target: yourexample.com',?//自定義域名(一定要配置到安全域名,不然無法回調(diào))
? ? ? ? changeOrigin: true,
? ? ? ? rewrite: (path) => path.replace(/^\/api/, 'http://example.com的IP形式 '), // 這里直接寫http://example.com的ip形式祖驱,這樣就不用去弄兩個域名了握恳,讓后端幫配置,能用ip訪問到接口地址就行捺僻,記得請求接口的時候換成IP來請求乡洼,打包的時候換成域名形式就行;
? ? ? },
? ? },
? },
})
以上就是全部內(nèi)容匕坯,若有一口或者錯誤束昵,歡迎各位大佬指正!