需求:客戶的電腦都只能訪問內,伺服器可以訪問外網,客戶電腦使用的項目中用到了高德webapi2.0。10.200.31.45:32100是我們的web伺服器。 網上基本上都是對高德webapi1.4的配置方式,而web2.0有一些差別。 1.前端修改高德地圖的js應用 如果是index.html引入 ...
需求:客戶的電腦都只能訪問內,伺服器可以訪問外網,客戶電腦使用的項目中用到了高德webapi2.0。
10.200.31.45:32100是我們的web伺服器。
網上基本上都是對高德webapi1.4的配置方式,而web2.0有一些差別。
1.前端修改高德地圖的js應用
如果是index.html引入,
修改之前的應用:
<script type="text/javascript" crossorigin src="https://webapi.amap.com/maps?v=2.0&key=高德key&plugin=AMap.ToolBar, AMap.ControlBar,AMap.MoveAnimation,AMap.RangingTool,AMap.Geocoder"></script> <script src="https://webapi.amap.com/loca?v=2.0.0&key=高德key"></script>
修改之後的配置:
<script type="text/javascript" crossorigin src="<%= BASE_URL %>maps?v=2.0&key=高德key&plugin=AMap.ToolBar, AMap.ControlBar,AMap.MoveAnimation,AMap.RangingTool,AMap.Geocoder"></script> <script src="<%= BASE_URL %>loca?v=2.0.0&key=高德key"></script>
如果是js動態載入,代碼如下:
/** * 創建高德秘鑰 * @param secret */ export function createSecret(secret?:string){ secret=secret||'高德秘鑰'; // 拼裝腳本字元串 const script = document.createElement('script'); // 高德 Web API 初始化配置 script.innerHTML = ` window._AMapSecurityConfig = { securityJsCode: '${secret}', };`; // 將腳本插入到頁面頭部 document.head.appendChild(script); } //獲取地圖地址 const getMapUrl=()=>{ const {map_offline}=Local.getAmapConfig(); let mapUrl='https://webapi.amap.com'; if(map_offline){ mapUrl=''; } if (process.env.NODE_ENV === "development"){ mapUrl='https://webapi.amap.com'; } return mapUrl; } /** * 創建高德script引入腳本 * @param key * @param url */ export function loadAMapScript(url:string,key?:string) { key=key||'高德key'; const mapUrl=getMapUrl(); const src =`${mapUrl}/maps?v=2.0&key=${key}${url}`; console.log('我的src',src); return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = src; script.async = true; script.onerror = reject; script.onload = resolve; document.head.appendChild(script); }); } /** * 創建高德local script引入腳本 * @param key * @param url */ export function loadAMapLocaScript(key?:string,url?:string=''){ key=key||'高德key'; const mapUrl=getMapUrl(); const src =`${mapUrl}/loca?v=2.0&key=${key}${url}`; return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = src; script.async = true; script.onerror = reject; script.onload = resolve; document.head.appendChild(script); }); }
然後在main.ts中:
import {DefaultAmapConfig} from "@/xd-common-web/xdConst"; import {loadAMapLocaScript,loadAMapScript,createSecret} from "@/xd-common-web/utils/map/amapLink"; try{ console.log('開始載入地圖'); const mapConfig=Local.getAmapConfig()||DefaultAmapConfig; createSecret(mapConfig.map_secret); await loadAMapScript("&&plugin=AMap.LineSearch,AMap.StationSearch,AMap.PolylineEditor,AMap.Geocoder,AMap.PlaceSearch,AMap.AutoComplete,Amap.ElasticMarker,AMap.PolygonEditor",mapConfig.map_key); await loadAMapLocaScript(mapConfig.map_key); console.log('地圖載入成功'); } catch{ console.log('地圖載入異常'); }
2.nginx配置如下:
location /restapi/ { proxy_pass https://restapi.amap.com/; } location /restapi/v3/ { proxy_pass https://restapi.amap.com/v3/; } location /webapi/ { proxy_pass https://webapi.amap.com/; } location /webapi/count{ proxy_pass https://webapi.amap.com/count; } location /vdata/ { proxy_pass https://vdata.amap.com/; } location /vdata/style_icon/ { proxy_pass http://vdata.amap.com/style_icon/; } location /vdata/style/{ proxy_pass https://vdata.amap.com/style/; } location /vdata01/ { proxy_pass https://vdata01.amap.com/; } location /vdata02/ { proxy_pass https://vdata02.amap.com/; } location /vdata03/ { proxy_pass https://vdata03.amap.com/; } location /vdata04/ { proxy_pass https://vdata04.amap.com/; } location /wprd01/ { proxy_pass https://wprd01.is.autonavi.com/; } location /wprd02/ { proxy_pass https://wprd02.is.autonavi.com/; } location /wprd03/ { proxy_pass https://wprd03.is.autonavi.com/; } location /wprd04/ { proxy_pass https://wprd04.is.autonavi.com/; } location /webrd01/ { proxy_pass https://webrd01.is.autonavi.com/; } location /webrd02/ { proxy_pass https://webrd02.is.autonavi.com/; } location /webrd03/ { proxy_pass https://webrd03.is.autonavi.com/; } location /webrd04/ { proxy_pass https://webrd04.is.autonavi.com/; } #代理獲取js api文件並修改文件內容 location /maps { proxy_set_header Accept-Encoding ""; proxy_pass https://webapi.amap.com/maps; sub_filter_types *; sub_filter_once off; sub_filter 'http://webapi.amap.com' 'http://10.200.31.45:32100/webapi'; sub_filter 'https://webapi.amap.com' 'http://10.200.31.45:32100/webapi'; sub_filter 'http://restapi.amap.com' 'http://10.200.31.45:32100/restapi'; sub_filter 'https://restapi.amap.com' 'http://10.200.31.45:32100/restapi'; sub_filter 'http://vdata.amap.com' 'http://10.200.31.45:32100/vdata'; sub_filter 'https://vdata.amap.com' 'http://10.200.31.45:32100/vdata'; sub_filter 'webapi.amap.com' '10.200.31.45:32100/webapi'; sub_filter 'vdata.amap.com' '10.200.31.45:32100/vdata'; sub_filter 'restapi.amap.com' '10.200.31.45:32100/restapi'; sub_filter 'vdata0{1,2,3,4}.amap.com' '10.200.31.45:32100/vdata0{1,2,3,4}'; sub_filter '{vdata,vdata01,vdata02,vdata03,vdata04}.amap.com' '10.200.31.45:32100/{vdata,vdata01,vdata02,vdata03,vdata04}'; sub_filter 'webapi.amap.com/count' '10.200.31.45:32100/webapi/count'; sub_filter 'webapi.amap.com/theme' '10.200.31.45:32100/webapi/theme'; sub_filter 'restapi.amap.com/v3' '10.200.31.45:32100/restapi/v3'; sub_filter 'webapi.amap.com/style' '10.200.31.45:32100/webapi/style'; sub_filter 'wprd0{1,2,3,4}.is.autonavi.com' '10.200.31.45:32100/wprd0{1,2,3,4}'; sub_filter 'webrd0{1,2,3,4}.is.autonavi.com' '10.200.31.45:32100/wprd0{1,2,3,4}'; sub_filter 'https' 'http'; } location /loca { proxy_set_header Accept-Encoding ""; proxy_pass https://webapi.amap.com/loca; sub_filter_types *; sub_filter_once off; sub_filter 'webapi.amap.com/count' '10.200.31.45:32100/webapi/count'; sub_filter 'https' 'http'; }
至此,就配置好了。註意:實際項目中nginx配置文件中的10.200.31.45:32100可使用變數代替。例如:
#代理獲取js api文件並修改文件內容 location /maps { set $proxyip "10.200.31.45:32100"; proxy_set_header Accept-Encoding ""; proxy_pass https://webapi.amap.com/maps; sub_filter_types *; sub_filter_once off; sub_filter 'http://webapi.amap.com' 'http://$proxyip/webapi'; sub_filter 'https://webapi.amap.com' 'http://$proxyip/webapi'; sub_filter 'http://restapi.amap.com' 'http://$proxyip/restapi'; sub_filter 'https://restapi.amap.com' 'http://$proxyip/restapi'; sub_filter 'http://vdata.amap.com' 'http://$proxyip/vdata'; sub_filter 'https://vdata.amap.com' 'http://$proxyip/vdata'; sub_filter 'webapi.amap.com' '$proxyip/webapi'; sub_filter 'vdata.amap.com' '$proxyip/vdata'; sub_filter 'restapi.amap.com' '$proxyip/restapi'; sub_filter 'vdata0{1,2,3,4}.amap.com' '$proxyip/vdata0{1,2,3,4}'; sub_filter '{vdata,vdata01,vdata02,vdata03,vdata04}.amap.com' '$proxyip/{vdata,vdata01,vdata02,vdata03,vdata04}'; sub_filter 'webapi.amap.com/count' '$proxyip/webapi/count'; sub_filter 'webapi.amap.com/theme' '$proxyip/webapi/theme'; sub_filter 'restapi.amap.com/v3' '$proxyip/restapi/v3'; sub_filter 'webapi.amap.com/style' '$proxyip/webapi/style'; sub_filter 'wprd0{1,2,3,4}.is.autonavi.com' '$proxyip/wprd0{1,2,3,4}'; sub_filter 'webrd0{1,2,3,4}.is.autonavi.com' '$proxyip/wprd0{1,2,3,4}'; sub_filter 'https' 'http'; } location /loca { set $proxyip "10.200.31.45:32100"; proxy_set_header Accept-Encoding ""; proxy_pass https://webapi.amap.com/loca; sub_filter_types *; sub_filter_once off; sub_filter 'webapi.amap.com/count' '$proxyip/webapi/count'; sub_filter 'https' 'http'; }
實現思路:通過特定的首碼maps和loca把高德webapi的引用進行轉發,轉發之後,高德載入地圖時,自動會調用一些介面,使用sub_filter 對返回的內容進行替換,替換為伺服器的地址+特定首碼,再根據首碼二次轉發即可。
博客地址: | http://www.cnblogs.com/jiekzou/ | |
博客版權: | 本文以學習、研究和分享為主,歡迎轉載,但必須在文章頁面明顯位置給出原文連接。 如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起討論,共同進步! 再次感謝您耐心的讀完本篇文章。 |
|
其它: |
.net-QQ群4:612347965
java-QQ群:805741535
H5-QQ群:773766020 我的拙作 《Vue3.x+TypeScript實踐指南》 《ASP.NET MVC企業級實戰》 《H5+移動應用實戰開發》 《Vue.js 2.x實踐指南》 《JavaScript實用教程 》 《Node+MongoDB+React 項目實戰開發》 已經出版,希望大家多多支持! |