content: package com.ruoyi.web.controller.system;
  
  import cn.binarywang.wx.miniapp.api.WxMaService;
  import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
  import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
  import com.ruoyi.common.config.RuoYiConfig;
  import com.ruoyi.common.constant.Constants;
  import com.ruoyi.common.core.domain.AjaxResult;
  import com.ruoyi.common.core.domain.entity.SysMenu;
  import com.ruoyi.common.core.domain.entity.SysUser;
  import com.ruoyi.common.core.domain.model.LoginBody;
  import com.ruoyi.common.core.domain.model.LoginUser;
  import com.ruoyi.common.core.domain.model.WechatLogin;
  import com.ruoyi.common.sign.RsaUtils;
  import com.ruoyi.common.utils.SecurityUtils;
  import com.ruoyi.framework.web.service.SysLoginService;
  import com.ruoyi.framework.web.service.SysPermissionService;
  import com.ruoyi.framework.web.service.TokenService;
  import com.ruoyi.system.service.ISysConfigService;
  import com.ruoyi.system.service.ISysMenuService;
  import me.chanjar.weixin.common.error.WxErrorException;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.validation.annotation.Validated;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RestController;
  
  import java.util.List;
  import java.util.Set;
  
  /**
   * 登录验证
   *
   * @author ruoyi
   */
  @RestController
  @Validated
  public class SysLoginController {
      @Autowired
      private SysLoginService loginService;
  
      @Autowired
      private ISysMenuService menuService;
  
      @Autowired
      private SysPermissionService permissionService;
  
      @Autowired
      private TokenService tokenService;
  
      @Autowired
      private WxMaService wxMaService;
  
      @Autowired
      private RuoYiConfig ruoYiConfig;
  
      @Autowired
      private ISysConfigService configService;
  
      /**
       * 登录方法
       *
       * @param loginBody 登录信息
       * @return 结果
       */
      @PostMapping("/login")
      public AjaxResult login(@RequestBody LoginBody loginBody) throws Exception {
          if(Boolean.parseBoolean(configService.selectConfigByKey("sys_public_demonstrate")) && !Constants.SUPER_ADMIN.equals(loginBody.getUsername())) {
              if (!ruoYiConfig.getPublicCode().equals(loginBody.getPublicCode())) {
                  return AjaxResult.error("公众号code错误，请关注ruoyi-wvp公众号获取正确的公众号code");
              }
          }
  
          AjaxResult ajax = AjaxResult.success();
          // 生成令牌
          String token = loginService.login(loginBody.getUsername(),
                  RsaUtils.decryptByPrivateKey(loginBody.getPassword()), loginBody.getCode(), loginBody.getUuid());
          ajax.put(Constants.TOKEN, token);
          return ajax;
      }
  
      /**
       * 获取用户信息
       *
       * @return 用户信息
       */
      @GetMapping("getInfo")
      public AjaxResult getInfo() {
          LoginUser loginUser = SecurityUtils.getLoginUser();
          SysUser user = loginUser.getUser();
          // 角色集合
          Set<String> roles = permissionService.getRolePermission(user);
          // 权限集合
          Set<String> permissions = permissionService.getMenuPermission(user);
          if (!loginUser.getPermissions().equals(permissions)) {
              loginUser.setPermissions(permissions);
              tokenService.refreshToken(loginUser);
          }
          AjaxResult ajax = AjaxResult.success();
          ajax.put("user", user);
          ajax.put("roles", roles);
          ajax.put("permissions", permissions);
          return ajax;
      }
  
      /**
       * 获取路由信息
       *
       * @return 路由信息
       */
      @GetMapping("getRouters")
      public AjaxResult getRouters() {
          Long userId = SecurityUtils.getUserId();
          List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
          return AjaxResult.success(menuService.buildMenus(menus));
      }
  
      /**
       * 微信登录
       *
       * @param wechatLogin 微信登录参数
       * @return
       */
      @PostMapping("/wechatLogin")
      public AjaxResult wechatLogin(@RequestBody WechatLogin wechatLogin) throws WxErrorException {
          // 调用微信小程序服务，获取jscode对应的session信息
          WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(wechatLogin.getCode());
          // 调用微信小程序服务，获取用户手机号信息
          WxMaPhoneNumberInfo phoneInfo = wxMaService.getUserService().getPhoneNoInfo(wxMaJscode2SessionResult.getSessionKey(), wechatLogin.getEncryptedData(), wechatLogin.getIv());
          // 获取用户手机号
          String purePhoneNumber = phoneInfo.getPurePhoneNumber();
          // 返回用户手机号
          return AjaxResult.success(purePhoneNumber);
      }
  
  }
  