内网走nginx代理访问高德webapi2.0

需求:客户的电脑都只能访问内,服务器可以访问外网,客户电脑使用的项目中用到了高德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.Geocoder"></script>
src="<%= BASE_URL %>loca?v=2.0.0&key=高德key">

如果是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 
 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'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'getMapUrl();
  const src =`${mapUrl}/loca?v=2.0&key=${key}${url}`;
   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");
} 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/;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,4}';
            sub_filter '{vdata,vdata01,vdata02,vdata03,vdata04}.amap.com' '10.200.31.45:32100/{vdata,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,4}.is.autonavi.com' '10.200.31.45:32100/wprd0{1,1)">
           sub_filter 'webrd0{1,1)">

           sub_filter 'https' 'http';
        }
        location /loca {
            proxy_set_header accept-Encoding "";
            proxy_pass   https://webapi.amap.com/loca;   
            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,4}.amap.com' '$proxyip/vdata0{1,4}';
            sub_filter '{vdata,vdata04}.amap.com' '$proxyip/{vdata,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,4}.is.autonavi.com' '$proxyip/wprd0{1,1)">;
           sub_filter 'webrd0{1,1)">;

           sub_filter 'https' 'http';
        }
        location /loca {
            set $proxyip "10.200.31.45:32100";
            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 对返回的内容进行替换,替换为服务器的地址+特定前缀,再根据前缀二次转发即可。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

本文链接:https://www.f2er.com/3188648.html

大家都在看

  • 飞码LowCode前端技术系列:如何便捷快速验证实现投产及飞码探索

    本篇文章从数据中心,事件中心如何协议工作、不依赖环境对vue2.x、vue3.x都可以支持、投产页面问题定位三个方面进行分析。
    2023-11-16 博文
  • 如何优雅使用 vuex

    大纲 本文内容更多的是讲讲使用 vuex 的一些心得想法,所以大概会讲述下面这些点: Q1:我为什么会想使用 vuex 来管理数据状态交互? Q2:使用 vuex 框架有哪些缺点或者说副作用? Q3:我是如何在项目里使用 vuex 的? 初识 vuex 对于 vuex,有人喜欢,有人反感 喜欢的人觉
    2023-11-16 博文
  • 第三方组件及计算属性传参的问题解决方式

    1. 前言 唉,好想玩滋嘣。 2. 计算属性直接传参接收不到 表格数据某一列需要用的计算属性时,模板中使用计算属性 fullName 就会直接调用 fullName 函数,而在模板中 fullName(item) 相当于fullName()(item),此处为函数柯里化。 &lt;el-table-
    2023-11-16 博文
  • 记录--Vue3基于Grid布局简单实现一个瀑布流组件

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 在学习Grid布局之时,我发现其是CSS中的一种强大的布局方案,它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,在刷某书和某宝首页时,我们发现其展示方式就是一种瀑布流,是一种流行的网站页面布局,视觉表……
    2023-11-16 博文
  • 用强数据类型保护你的表单数据-基于antd表单的类型约束

    接口数据类型与表单提交数据类型,在大多数情况下,大部分属性的类型是相同的,但很少能做到完全统一。我在之前的工作中经常为了方便,直接将接口数据类型复用为表单内数据类型,在遇到属性类型不一致的情况时会使用any强制忽略类型错误。后来经过自省与思考,这种工作模式会引起各种隐藏bug,一定有更……
    2023-11-16 博文
  • pinia的使用

    前言 最近新开了个项目,以前老项目都是vue2+vuex开发的,都说用vue3+pinia爽得多,那新项目就vue3+pinia吧。这里记录一下pinia的使用。 使用方法 安装pinia: npm i pinia main.js中引入pinia: //main.js import { create
    2023-11-16 博文
  • 记录--让我们来深入了解一下前端“三清”是什么

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前端“三清” 在前端开发中,我们经常听到关于“三清”的说法,即 window、document、Object。这三者分别代表了 BOM(浏览器对象模型)、DOM(文档对象模型)以及 JS 的顶层对象。在这个体系中,我们通过 JavaScr
    2023-11-16 博文
  • 记录--啊?Vue是有三种路由模式的?

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 众所周知,vue路由模式常见的有 history 和 hash 模式,但其实还有一种方式-abstract模式(了解一哈~) 别急,本文我们将重点逐步了解: 路由 + 几种路由模式 + 使用场景 + 思考 + freestyle 路由概念
    2023-11-16 博文