企业微信对接:回调地址带#时返回地址参数位置不对的问题

复制代码
// 需要企微回调的地址
http://localhost:1024/app/#/SsoLogin?loginMethod=qw

// 企微配置的地址
https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxx&redirect_uri=http%3A%2F%2Flocalhost%3A1024%2Fapp%2F%23%2FSsoLogin%3FloginMethod%3Dqw&response_type=code&scope=snsapi_base&state=STATE&agentid=AGENTID#wechat_redirect

// 企微返回的地址
http://localhost:1024/app/?code=xxxxxxxxxxxxxxxxxxstate=000000#/SsoLogin?loginMethod=qw

返回结果并没有在#/SsoLogin后拼接参数

方法一:

企微返回的地址虽然参数顺序不对,但还是访问到/SsoLogin这个地址

只需要在这个页面中处理一下字符串取出参数即可

复制代码
const getUrlParamsRegex = url => {
  const params = {};
  // 处理 URL 中的参数
  const urlParts = url.split("?");
  if (urlParts[1]) {
    const searchParams = new URLSearchParams(urlParts[1].split("#")[0]);
    for (const [key, value] of searchParams.entries()) {
      params[key] = value;
    }
  }
  // 处理 hash 中的参数
  const hashParts = url.split("#");
  if (hashParts[1]) {
    const hashParams = new URLSearchParams(hashParts[1].split("?")[1] || "");
    for (const [key, value] of hashParams.entries()) {
      params[key] = value;
    }
  }
  return params;
};

// 返回结果取值  const urlParamsRegex = getUrlParamsRegex(window.location.href);
// urlParamsRegex.code

方法二

1、修改vue路由模式,去掉#

复制代码
// createWebHashHistory改为createWebHistory,并可以在配置额文件指定路由后缀VITE_PUBLIC_PATH

// 原代码
const router = createRouter({
  history: createWebHashHistory(),
...
});

// 修改后
const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_PATH),
...
});

2、修改nginx配置

复制代码
// 原配置
        location /app {
            root  /usr/share/nginx;
            index index.html;
        }

// 修改后
        location /app {
            root  /usr/share/nginx;
            index index.html;
            try_files $uri $uri/ /cloud/index.html;
        }
相关推荐
五仁火烧12 小时前
生产环境中配置了接口3000后,不能启动,改成8080后就可以
linux·网络·安全·vue
我的golang之路果然有问题19 小时前
实习中遇到的 CORS 同源策略自己的理解分析
前端·javascript·vue·reactjs·同源策略·cors
刘联其19 小时前
Vue3+Vite +dotenvx读取.env文件参数
vue
莫非王土也非王臣20 小时前
Prometheus监控多个网站状态并推送到企业微信群机器人的完整方案
机器人·企业微信·prometheus
椰果uu2 天前
vue-virtual-scroller-虚拟滚动列表:渲染不定高度长列表+可控跳转
前端·javascript·typescript·vue
小和尚敲木头2 天前
记录一次vue3中this引发的开发没有问题,生产发生问题的分析
前端·vue
PieroPc2 天前
用FastAPI 后端 和 Vue3 前端写一个博客系统 例
前端·vue·fastapi
vx-bot5556662 天前
企业微信接口在自动化工作流中的关键角色与设计模式
设计模式·自动化·企业微信
天空属于哈夫克32 天前
利用 RPA 实现企业微信外部群自动化的架构逻辑
自动化·企业微信·rpa
s018962 天前
易飞ERP对接企业微信审批
企业微信