|
@@ -1,112 +1,177 @@
|
|
|
-package com.ruoyi.web.controller.system;
|
|
|
-
|
|
|
-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.LoginByTelephoneBody;
|
|
|
-import com.ruoyi.common.utils.SecurityUtils;
|
|
|
-import com.ruoyi.framework.web.service.SysLoginService;
|
|
|
-import com.ruoyi.framework.web.service.SysPermissionService;
|
|
|
-import com.ruoyi.system.service.ISysMenuService;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-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
|
|
|
-public class SysLoginController {
|
|
|
- @Autowired
|
|
|
- private SysLoginService loginService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ISysMenuService menuService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private SysPermissionService permissionService;
|
|
|
-
|
|
|
- /**
|
|
|
- * 登录方法
|
|
|
- *
|
|
|
- * @param loginBody 登录信息
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- @PostMapping("/login")
|
|
|
- public AjaxResult login(@RequestBody LoginBody loginBody) {
|
|
|
- AjaxResult ajax = AjaxResult.success();
|
|
|
- // 生成令牌
|
|
|
- String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
|
|
- loginBody.getUuid(), false);
|
|
|
- ajax.put(Constants.TOKEN, token);
|
|
|
- return ajax;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 登录方法
|
|
|
- *
|
|
|
- * @param loginBody 登录信息
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- @PostMapping("/loginCommunity")
|
|
|
- public AjaxResult loginCommunity(@RequestBody LoginBody loginBody) {
|
|
|
- AjaxResult ajax = AjaxResult.success();
|
|
|
- // 生成令牌
|
|
|
- String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
|
|
- loginBody.getUuid(), true);
|
|
|
- ajax.put(Constants.TOKEN, token);
|
|
|
- return ajax;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户信息
|
|
|
- *
|
|
|
- * @return 用户信息
|
|
|
- */
|
|
|
- @GetMapping("getInfo")
|
|
|
- public AjaxResult getInfo() {
|
|
|
- SysUser user = SecurityUtils.getLoginUser().getUser();
|
|
|
- // 角色集合
|
|
|
- Set<String> roles = permissionService.getRolePermission(user);
|
|
|
- // 权限集合
|
|
|
- Set<String> permissions = permissionService.getMenuPermission(user);
|
|
|
- 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));
|
|
|
- }
|
|
|
-
|
|
|
- @PostMapping("/phoneLogin")
|
|
|
- public AjaxResult phoneLogin(@RequestBody LoginByTelephoneBody loginBody){
|
|
|
-
|
|
|
- AjaxResult ajax = AjaxResult.success();
|
|
|
- // 生成令牌
|
|
|
- String token = loginService.loginByTelephone(loginBody.getUsername(), loginBody.getCode(),
|
|
|
- loginBody.getUuid());
|
|
|
- ajax.put(Constants.TOKEN, token);
|
|
|
- return ajax;
|
|
|
-
|
|
|
- }
|
|
|
-}
|
|
|
+package com.ruoyi.web.controller.system;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.ruoyi.common.config.WxAppConfig;
|
|
|
+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.LoginByTelephoneBody;
|
|
|
+import com.ruoyi.common.core.domain.model.WxLoginBody;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.common.utils.sign.Base64;
|
|
|
+import com.ruoyi.framework.web.service.SysLoginService;
|
|
|
+import com.ruoyi.framework.web.service.SysPermissionService;
|
|
|
+import com.ruoyi.system.service.ISysMenuService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.lang.reflect.Array;
|
|
|
+import java.security.spec.AlgorithmParameterSpec;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 登录验证
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+public class SysLoginController {
|
|
|
+ @Autowired
|
|
|
+ private SysLoginService loginService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysMenuService menuService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysPermissionService permissionService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录方法
|
|
|
+ *
|
|
|
+ * @param loginBody 登录信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @PostMapping("/login")
|
|
|
+ public AjaxResult login(@RequestBody LoginBody loginBody) {
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ // 生成令牌
|
|
|
+ String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
|
|
+ loginBody.getUuid(), false);
|
|
|
+ ajax.put(Constants.TOKEN, token);
|
|
|
+ return ajax;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WxAppConfig wxAppConfig;
|
|
|
+
|
|
|
+ @RequestMapping("/wxLogin")
|
|
|
+ public AjaxResult wxLogin(@RequestBody WxLoginBody wxLoginBody) {
|
|
|
+ String code = wxLoginBody.getCode();
|
|
|
+
|
|
|
+ //向微信服务器发送请求获取用户信息
|
|
|
+ String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxAppConfig.getAppId() + "&secret=" + wxAppConfig.getAppSecret() + "&code=" + code + "&grant_type=authorization_code";
|
|
|
+
|
|
|
+ final RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String res = restTemplate.getForObject(url, String.class);
|
|
|
+ System.out.println("res:" + res);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ // 判断JSON对象中 是否存在 错误代码
|
|
|
+ if (jsonObject.containsKey("errcode")) {
|
|
|
+ // 返回错误代码
|
|
|
+ return AjaxResult.error(jsonObject.getString("errcode"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取 unionid
|
|
|
+ String unionid = jsonObject.getString("unionid");
|
|
|
+ // 获取 openId
|
|
|
+ String openid = jsonObject.getString("openid");
|
|
|
+ // 获取 accessToken
|
|
|
+ String accessToken = jsonObject.getString("access_token");
|
|
|
+
|
|
|
+
|
|
|
+ //向微信服务器发送请求获取用户信息
|
|
|
+ url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid==" + openid + "&grant_type=authorization_code";
|
|
|
+
|
|
|
+ res = restTemplate.getForObject(url, String.class);
|
|
|
+ System.out.println("res:" + res);
|
|
|
+ jsonObject = JSONObject.parseObject(res);
|
|
|
+ // 判断JSON对象中 是否存在 错误代码
|
|
|
+ if (jsonObject.containsKey("errcode")) {
|
|
|
+ // 返回错误代码
|
|
|
+ return AjaxResult.error(jsonObject.getString("errcode"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取 province
|
|
|
+ //String province= jsonObject.getString("province");
|
|
|
+ // 获取 nickname
|
|
|
+ String nickname = jsonObject.getString("nickname");
|
|
|
+ // 获取 headimgurl
|
|
|
+ String headimgurl = jsonObject.getString("headimgurl");
|
|
|
+
|
|
|
+ String[] result = {openid,nickname,headimgurl};
|
|
|
+
|
|
|
+ String token = loginService.wxLogin(result);
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ ajax.put(Constants.TOKEN, token);
|
|
|
+ return ajax;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录方法
|
|
|
+ *
|
|
|
+ * @param loginBody 登录信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @PostMapping("/loginCommunity")
|
|
|
+ public AjaxResult loginCommunity(@RequestBody LoginBody loginBody) {
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ // 生成令牌
|
|
|
+ String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
|
|
+ loginBody.getUuid(), true);
|
|
|
+ ajax.put(Constants.TOKEN, token);
|
|
|
+ return ajax;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户信息
|
|
|
+ *
|
|
|
+ * @return 用户信息
|
|
|
+ */
|
|
|
+ @GetMapping("getInfo")
|
|
|
+ public AjaxResult getInfo() {
|
|
|
+ SysUser user = SecurityUtils.getLoginUser().getUser();
|
|
|
+ // 角色集合
|
|
|
+ Set<String> roles = permissionService.getRolePermission(user);
|
|
|
+ // 权限集合
|
|
|
+ Set<String> permissions = permissionService.getMenuPermission(user);
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/phoneLogin")
|
|
|
+ public AjaxResult phoneLogin(@RequestBody LoginByTelephoneBody loginBody){
|
|
|
+
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ // 生成令牌
|
|
|
+ String token = loginService.loginByTelephone(loginBody.getUsername(), loginBody.getCode(),
|
|
|
+ loginBody.getUuid());
|
|
|
+ ajax.put(Constants.TOKEN, token);
|
|
|
+ return ajax;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|