• 【微信h5】获取用户openid:基于vue3+springBoot


    一、前言

    开发网页端小程序,需要用到用户openid,所以通过查找资料,现在将完整的获取流程分享给大家。

    前端页面非.vue格式,而是.html格式

    该项目框架为:vue3+elementPlus+springboot

    二、步骤

    1、引入所需js

    微信js:

    vue3.js以及axios.js

    
    
    

    js所在目录位置: 

     

    1、vue3.js:

    链接:https://pan.baidu.com/s/1qk03G1QEd-E8IN6lnF-mjg 
    提取码:gblc

    2、axios.js:

    链接:https://pan.baidu.com/s/1JM2EPmg-evCCun4ycI1Few 
    提取码:txkk

     2、html代码开发--- vue写法

    1. mounted() {
    2. var appid = "xxxxxxx"; //个人公众号appid
    3. var redirect = encodeURIComponent(window.location.href); //重定向回来的地址
    4. var wx_code = this.getUrlParam("code"); // 截取url中的code
    5. //获取code的地址。获取成功重定向后地址栏中将会带有code,判断没有code的话,就跳转到微信官方链接上获取,获取成功后会再重定向回来,注意url是需要使用encodeURIComponent处理一下编码的
    6. if (!wx_code) {
    7. // scope: 必传;应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
    8. // 静默授权
    9. window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + redirect + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
    10. } else {
    11. // 获取到了code
    12. this.getOpenId(wx_code); //把code传给后台获取用户信息
    13. }
    14. },
    15. methods: {
    16. //getUrlParam方法就是使用正则截取地址栏里的code
    17. getUrlParam: function (name) {
    18. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    19. var r = window.location.search.substr(1).match(reg);
    20. if (r != null) return unescape(r[2]);
    21. return null;
    22. },
    23. //获取用户的openid
    24. getOpenId(code) {
    25. let result = axios({
    26. method: 'post',
    27. url: "/api/getOpenid",
    28. data: {code: code},
    29. header: {
    30. 'Content-Type': 'application/x-www-form-urlencoded'
    31. }
    32. }).then(function (result) {
    33. var wxResult = result.data.data;
    34. var openid = wxResult.openid;
    35. }).catch(error => {
    36. //待完善弹窗
    37. });
    38. },
    39. }

    3、controller层

    1. @RequestMapping("/getOpenid")
    2. public JSONObject getOpenid(@RequestBody DataObject para) throws AppException {
    3. return wxh5BusinessService.getOpenid(para);
    4. }

    4、service层

    1. //获取openid
    2. public JSONObjectgetOpenid(DataObject para) throws AppException {
    3. String code = para.getString("code", "");
    4. //在配置文件中配置上appid及secret,然后wxh5Properties为编写的配置类
    5. String appid = wxh5Properties.getAppid();
    6. String appsecret = wxh5Properties.getAppsecret();
    7. JSONObject result =new JSONObject();
    8. String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
    9. String data = HttpUtil.sendGet(url);
    10. return (JSONObject)JSONObject.parse(data);
    11. }

    5、在微信公众平台配置网页授权域名

     至此,即可通过上述步骤获取到用户的openid了。

     

  • 相关阅读:
    《Document-level Relation Extraction as Semantic Segmentation》论文阅读笔记
    共议新时代的文化自信与守正创新,第十四届文化中国讲坛举办
    微信小程序navigateTo进入页面后返回原来的页面需要携带数据回来
    mySQL—基础SQL语句
    有懂miracl库的人吗?看看过程呗
    RocketMQ部署
    SpringBoot
    2023吉首大学计算机考研信息汇总
    组合模式
    免改造密码方案入选工信部“首届全国商用密码应用优秀案例”
  • 原文地址:https://blog.csdn.net/weixin_44431073/article/details/126358690