mina001 2024-01-29 16:54 采纳率: 0%
浏览 1

微信小程序多设备登录后,用户信息无法同时更改

#使用若依框架实现的微信小程序后端,小程序可在多设备登录,且登录后永不过期,问题是在一个设备修改用户信息后比如用户头像,其它设备信息不能自动更新,请问该如何解决


    /**
     * 登录验证
     *
     * @param code 微信小程序登录code
     * @return 结果
     */
    public String openidLogin(String code)
    {
        //获取token和sessiceKey
        String res = HttpUtils.sendGet("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code");
        JSONObject resJSON = JSONObject.parseObject(res);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("openId", resJSON.get("openid"));
        ajax.put("sessionKey", resJSON.get("session_key"));
        //查询oppid是否存在,不存在则插入到数据库中
        if (resJSON.get("openid")!=null){
            SysUser openid = userMapper.selectByOpenid((String) resJSON.get("openid"));
            if (openid==null){
                SysUser sysUser = new SysUser();
                sysUser.setOpenid((String) resJSON.get("openid"));
                sysUser.setUnionid((String) resJSON.get("unionid"));
                sysUser.setCreateTime(new Date());
                sysUser.setUserName("露营人生"+((String) resJSON.get("openid")).substring(((String) resJSON.get("openid")).length()-6));
                sysUser.setNickName("露营人生"+((String) resJSON.get("openid")).substring(((String) resJSON.get("openid")).length()-6));
                sysUser.setDelFlag("0");
                userMapper.insertUser(sysUser);
            }
            //根据openid更新用户IP地址
                //获取请求头中的token信息
                RequestAttributes ra = RequestContextHolder.getRequestAttributes();
                if(Objects.isNull(ra)){
                    return null;
                }
                ServletRequestAttributes sra = (ServletRequestAttributes) ra;
                HttpServletRequest request = sra.getRequest();
                //更新用户IP地址
                String ip = request.getHeader("x-forwarded-for");
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("Proxy-Client-IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("WL-Proxy-Client-IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_CLIENT_IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getRemoteAddr();
                }
                userMapper.updateUserIpAddressTOOpenid((String) resJSON.get("openid"),ip);
        }

        //用户验证
        Authentication authentication = null;
        LoginUser loginUser=null;
        String username = null;
        try
        {
            authentication = authenticationManager.authenticate(new WxAuthenticationToken((String)resJSON.get("openid")));
            loginUser = (LoginUser)authentication.getPrincipal();
            username = loginUser.getUsername();
        }
        catch (Exception e)
        {
            String errMsg = e.getMessage();
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,Constants.LOGIN_FAIL,errMsg));
            throw new RuntimeException(errMsg);
        }

        // 生成token
        String token = tokenService.createToken(loginUser);
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,Constants.LOGIN_SUCCESS,MessageUtils.message("user.login.success")));
        recordLoginInfo(loginUser.getUser().getUserId());
        return token;
    }

  • 写回答

3条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2024-01-29 16:58
    关注

    这个问题首先需要明确的是,两个设备同时登录同一个微信号时,他们访问服务器的身份都是这个微信号。对于服务器来说,他们形象上就是“两个人同时使用同一个账号访问”,不存在“身份不同步”的问题。

    然而,你所提到的“在一个设备修改用户信息后比如用户头像,其它设备信息不能自动更新”这个问题,其实是另外一个问题,即前端(也就是小程序)的数据不同步。遇到这种情况,可以尝试以下两种解决方式:

    1. 主动更新:当用户做出更改用户信息这种会影响其它设备的操作之后,可以调用一次刷新接口,使得这台设备上看到的数据是最新的状态。

    2. 被动更新:在其它设备上,每次进入页面或者从后台切换到前台的时候,都去调用一次获取用户信息的接口,这样可以保证每次用户看到的都是最新的数据。

    最后,如果你们的微信小程序主要会在前台运行,且用户信息更新的动作较少,那么方案2可能更适合你。反之,如果用户信息更新的动作会很频繁,那么方案1可能更适合你。无论选择哪种方案,其核心思想都是一致的:只要用户信息有更改,就去主动获取最新的数据。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月29日

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP