Browse Source

新增商城相关页面切换新老接口按钮插件

chenguo 1 tuần trước cách đây
mục cha
commit
73b8e79fee

+ 7 - 4
src/components/SmartEndpointToggle/ScrmEndpointButton.vue

@@ -6,6 +6,7 @@
             添加组件方式:
             <smart-endpoint-toggle-button
             prefix="[your-prefix]"
+            regexGate="[regs]"  -- 正则匹配接口路径如/his/store/productList 可填写/his/store 支持多个逗号分割
             component-name="[your-component-name]">
             </smart-endpoint-toggle-button>
             提供可用的回调
@@ -38,6 +39,11 @@ export default {
       type: String,
       required: true
     },
+    //regexGate:只对特定路径使用
+    regexGate: {
+      type: String,
+      required: true
+    },
     // 组件名称,用于生成唯一标识
     componentName: {
       type: String,
@@ -56,25 +62,21 @@ export default {
     }
   },
   created() {
-    console.log('SmartEndpointToggleButton created');
     // 生成基于组件名和路由的唯一状态键
     this.stateKey = this.generateStateKey()
     // 初始化状态
     this.initializeState()
   },
   activated() {
-    console.log('SmartEndpointToggleButton activated,stateKey='+this.stateKey+',prefix='+this.prefix+',is='+this.isUsingNewEndpoint);
     setActive(this.stateKey);
     // 页面激活时恢复状态
     this.restoreState()
   },
   deactivated() {
-    console.log('SmartEndpointToggleButton deactivated');
     // 页面失活时保存状态
     this.saveState()
   },
   destroyed() {
-    console.log('SmartEndpointToggleButton destroyed');
     // 组件销毁
     resetEndpoint(this.stateKey)
   },
@@ -99,6 +101,7 @@ export default {
       try {
         const state = {
           prefix: this.prefix,
+          regexGate: this.regexGate,
           isUsingNewEndpoint: this.isUsingNewEndpoint,
           timestamp: Date.now()
         }

+ 3 - 1
src/utils/request.js

@@ -32,12 +32,14 @@ service.interceptors.request.use(config => {
   // 使用当前激活的组件状态来决定是否添加前缀
   let useNewEndpoint = false
   let routePrefix = ''
+  let canReg = false
   if (activeStateKey) {
     const state = JSON.parse(sessionStorage.getItem(activeStateKey));
     useNewEndpoint = state.isUsingNewEndpoint;
     routePrefix = state.prefix;
+    canReg= state.regexGate.split(',').any(reg => config.url.includes(reg));
   }
-  if (useNewEndpoint) {
+  if (useNewEndpoint && canReg) {
     config.url = routePrefix + config.url;
   }