若依VUE前后分离spring boot版微信登陆(免密登录)

SysLoginController

/**
 * 微信openID登陆
 * @param openid
 * @return
 */
@PostMapping("/wxLogin")
public AjaxResult wxLogin(String openid) {
   AjaxResult ajax = AjaxResult.success();
   // 生成令牌
   String token = loginService.wxLogin(openid);
   ajax.put(Constants.TOKEN, token);
   return ajax;
}

 SysLoginService

类名: com.ruoyi.framework.web.service.SysLoginService

    @Autowired
    private UserDetailsService userDetailsService;
    /**
     * 无密码登录
     * @param userName
     * @return
     */
    public String noPwdLogin(String userName){

        LoginUser loginUser
                = (LoginUser)userDetailsService.loadUserByUsername(userName);
        // 记录登陆信息
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_SUCCESS,
                MessageUtils.message("user.login.success")));
        recordLoginInfo(loginUser.getUserId());
        return tokenService.createToken(loginUser);
    }

    /**
    * 微信登陆
    * @param openid
    * @return
    */
    public String wxLogin(String openid) {

        SysUser user = userService.selectUserByIdOpenid(openid);
        Optional.ofNullable(user).orElseThrow(
            ()->new ServiceException("openid为:" + openid + " 的用户不存在"));
        return this.noPwdLogin(user.getUserName());

      }

 

/**
     * 注册用户
     */
    @PostMapping("/register")
    public AjaxResult register(@Validated @RequestBody WxUserInfo wxUser) {

        SysUser user = new SysUser();
        user.setUserName(wxUser.getNickname() + "_" + NanoId.randomNanoId(4));
        user.setOpenid(wxUser.getOpenid());
        user.setNickName(wxUser.getNickname());
        user.setSex(getSexFromWechatSex(wxUser.getSex()));
        user.setAvatar(wxUser.getHeadimgurl());
        user.setCreateBy("admin");

        user.setDeptId(101L);
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,账号已存在");
        }
        return AjaxResult.success(userService.insertUser(user));
    }

 若依VUE前后分离spring boot版微信登陆(免密登录)_第1张图片

 

你可能感兴趣的:(vue.js,spring,boot,微信)