HellWhisper
HellWhisper
  • 发布:2019-10-29 09:58
  • 更新:2020-03-01 01:54
  • 阅读:3821

uni-app 公众号h5自动登陆 问题

分类:uni-app

uniapp 打包后
调用微信的登陆接口,没办法在redirecturl 中添加#, 比如设置回调地址是

www.xxxx.com/#/pages/index/index

那么微信回调之后的域名只有 www.xxx.com?code=aaaaaaaaaaa&state=123

但是如果按照微信的回调访问的话,是没办法获取code参数的,请问怎么处理

2019-10-29 09:58 负责人:无 分享
已邀请:
陈晨1

陈晨1

www.xxxx.com/#/pages/index/index?code=aaaaaaaaaaa&state=123
这样微信一样可以获取参数啊,#是hash,没影响的

  • HellWhisper (作者)

    如果在redirect-url 的网址里边加#,微信回调回来的网址是没有#的

    2019-10-30 16:48

  • 陈晨1

    回复 HellWhisper: 那你用history模式吧,history模式不带#

    2019-10-30 16:54

jtshushu

jtshushu - 擅长:PHP-Thinkphp Python MUI UniApp Vuejs Bootstrap Jq Logo设计 广告设计 网页设计 有项目上需要联系我微信号:jtshushu66

你好解决了吗
我也出现这个问题

/**  
 * 根据参数名 获取 URL 路径中的参数  
 * @param {String} name 要读取的参数名称  
 */  
function getUrlParam (name) {   
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')  
  let url = window.location.href.split('#')[0]   
  let search = url.split('?')[1]  
  if (search) {  
    var r = search.substr(0).match(reg)  
    if (r !== null) return unescape(r[2])  
    return null  
  } else {  
    return null  
  }  
}  

//getUrlParam('code') 调用一下就能拿到 code 结果,然后用这个 code 去调后台的接口让后台去微信后台拿openID。  

function wxAuthorize() {  
    let link = window.location.href;  
   // let params = this._getUrlParams(link);  // 地址解析  
   //console.log(link);  

    let params =getUrlParam('code');  // 地址解析  
        console.log(params);  
       // 已经授权登录过的就不用再授权了  
    // if (store.state.token) return;  

    // 如果拿到code,调用授权接口,没有拿到就跳转微信授权链接获取   
   // if (params.code) {  
    if (params) {  
        console.log("121212");  
        api.wxAuth(params.code); // 调用后台接口,授权   
    }else {  
        console.log("22221");  

        let appid = 'xxxxxxxx';  
        //1.使用encodeURIComponent以及JSON.stringify()方法对对象进行字符串化和编码,这样可以控制url参数的长度,参考示例代码(uni-app书写方式,微信小程序自己改。)  

        let uri = encodeURIComponent(link);  
        //2.接受信息的页面使用JSON.parse()以及decodeURIComponent()接收和解码参数。    

        //snsapi_base   //snsapi_base    scope = snsapi_base(不弹出授权页面,直接跳转,只能获取用户 openid )。  
        //snsapi_userinfo  弹出  
        let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`;  

        window.location.href = authURL;  

    }  
}

我怎么调转不了呢

2***@qq.com

2***@qq.com

老哥你这个回调域名的问题解决了吗

9***@qq.com

9***@qq.com

提交之前把#过滤掉或者H5配置为history模式访问

  • 2***@qq.com

    我用history 模式,为什么还是会刷新整个界面呢

    2020-03-01 14:22

  • 蹦豆儿

    回复 2***@qq.com: window.location.href = authURL; 这句话就是刷新整页

    2020-06-08 15:46

该问题目前已经被锁定, 无法添加新回复