Browse Source

feat: app接口扩展
1. app账户密码名称, 登出接口制作
2. 修改设备名称接口制作

kindring 1 year ago
parent
commit
a7cdc81a69

+ 17 - 0
src/main/java/com/genersoft/iot/vmp/service/IAccountService.java

@@ -26,12 +26,20 @@ public interface IAccountService {
 
     /**
      * 登录账号
+     *
      * @param account
      * @param password
      * @return
      */
     UserAccount login(String account, String password);
 
+    boolean updateName(String accountId, String name);
+
+    boolean updatePassword(String accountId, String newPasswd);
+
+    // 根据 用户id 与密码 获取用户信息
+    UserAccount getByAccountIdAndPassword(String accountId, String password);
+
     /**
      * 绑定设备
      *
@@ -81,4 +89,13 @@ public interface IAccountService {
      * @return Device
      */
     Device getAccountDevice(String accountId, String deviceId);
+
+    /**
+     * 更新设备名称
+     *
+     * @param deviceId
+     * @param deviceName
+     * @return
+     */
+    boolean updateDeviceName(String deviceId, String deviceName);
 }

+ 31 - 0
src/main/java/com/genersoft/iot/vmp/service/impl/AccountServiceImpl.java

@@ -53,6 +53,7 @@ public class AccountServiceImpl implements IAccountService {
 
     /**
      * 登录
+     *
      * @param account
      * @param password
      * @return
@@ -61,6 +62,32 @@ public class AccountServiceImpl implements IAccountService {
         return accountMapper.login(account, password);
     }
 
+    /**
+     * 修改用户名
+     *
+     * @param accountId 用户账号ID
+     * @param name      新用户名
+     * @return 如果修改成功,则返回true;否则返回false
+     */
+    public boolean updateName(String accountId, String name) {
+        return accountMapper.updateName(accountId, name) > 0;
+    }
+
+    /**
+     * 修改密码
+     *
+     * @param accountId 用户账号ID
+     * @param newPasswd 新密码
+     * @return 修改成功返回true,否则返回false
+     */
+    public boolean updatePassword(String accountId, String newPasswd) {
+        return accountMapper.updatePassword(accountId, newPasswd) > 0;
+    }
+
+    public UserAccount getByAccountIdAndPassword(String accountId, String password) {
+        return accountMapper.getByAccountIdAndPassword(accountId, password);
+    }
+
     public int bindDevice(String id, String devId, String devCode) {
         return accountMapper.bindDevice(id, devId, devCode);
     }
@@ -95,5 +122,9 @@ public class AccountServiceImpl implements IAccountService {
         return accountMapper.getAccountDevice(accountId, deviceId);
     }
 
+    public boolean updateDeviceName(String deviceId, String deviceName) {
+        return accountMapper.updateDeviceName(deviceId, deviceName) > 0;
+    }
+
 
 }

+ 13 - 0
src/main/java/com/genersoft/iot/vmp/storager/dao/AccountMapper.java

@@ -43,6 +43,9 @@ public interface AccountMapper {
     @Select("SELECT * FROM `device_bind` as b LEFT JOIN `device` as d ON b.devId = d.id where b.userId = #{userId} AND b.devId = #{devId}")
     Device getAccountDevice(String userId, String devId);
 
+    @Update("UPDATE device SET name = #{deviceName} WHERE id = #{devId}")
+    int updateDeviceName(String devId, String deviceName);
+
     @Select("SELECT * FROM account WHERE id = #{id}")
     UserAccount selectById(String id);
 
@@ -52,5 +55,15 @@ public interface AccountMapper {
     @Select("SELECT * FROM account WHERE account = #{account} AND password = #{password}")
     UserAccount login(String account, String password);
 
+    @Select("SELECT * FROM account WHERE id = #{accountId} AND password = #{password}")
+    UserAccount getByAccountIdAndPassword(String accountId, String password);
+
+    // 修改密码
+    @Update("UPDATE account SET password = #{newPasswd} WHERE id=#{id} limit 1")
+    int updatePassword(String id, String newPasswd);
+
 
+    // 修改用户名
+    @Update("UPDATE account SET name = #{newName} WHERE id=#{id} limit 1")
+    int updateName(String id, String newName);
 }

+ 64 - 3
src/main/java/com/genersoft/iot/vmp/vmanager/account/AccountController.java

@@ -9,6 +9,7 @@ import com.genersoft.iot.vmp.service.IDeviceService;
 import com.genersoft.iot.vmp.storager.dao.dto.UserAccount;
 import com.genersoft.iot.vmp.utils.StpUserUtil;
 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
+import com.genersoft.iot.vmp.vmanager.bean.UserLoginResult;
 import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
@@ -70,8 +71,8 @@ public class AccountController {
     @Operation(summary = "登录")
     @Parameter(name = "account", description = "登陆账号", required = true)
     @Parameter(name = "password", description = "密码", required = true)
-    public WVPResult<SaTokenInfo> login(@RequestParam String account,
-                                        @RequestParam String password) {
+    public WVPResult<UserLoginResult> login(@RequestParam String account,
+                                            @RequestParam String password) {
         logger.info("[登录账号] account {}", account);
         UserAccount userAccount = accountService.login(account, password);
         if (userAccount == null) {
@@ -80,8 +81,68 @@ public class AccountController {
                     "账号或者密码错误");
         }
         StpUserUtil.login(userAccount.getId());
+        logger.info("[登录账号] 用户: {} 登录成功", account);
+        // 登录成功后返回 token 信息, 以及用户名, 头像等信息
+        UserLoginResult userLoginResult = new UserLoginResult(
+                StpUserUtil.getTokenInfo(),
+                userAccount.getName(),
+                userAccount.getId()
+        );
+        return WVPResult.success(userLoginResult);
+    }
+
+    // 注销
+    @GetMapping("/logout")
+    @Operation(summary = "登出账户")
+    public WVPResult<String> logout() {
+        StpUserUtil.logout();
+        return WVPResult.success("ok");
+    }
+
+    @GetMapping("/rename")
+    @Operation(summary = "重命名")
+    @Parameter(name = "name", description = "新名称", required = true)
+    public WVPResult<UserLoginResult> rename(@RequestParam String name) {
+        logger.info("[重命名] name {}", name);
+        String accountId = StpUserUtil.getLoginId().toString();
+        boolean flag = accountService.updateName(accountId, name);
+        // 登录成功后返回 token 信息, 以及用户名, 头像等信息
+        if (!flag) {
+            return WVPResult.fail(
+                    ErrorCode.ERROR100,
+                    "重命名失败");
+        }
+        UserLoginResult userLoginResult = new UserLoginResult(
+                StpUserUtil.getTokenInfo(),
+                name,
+                Integer.parseInt(accountId)
+        );
+        return WVPResult.success(userLoginResult);
+    }
 
-        return WVPResult.success(StpUserUtil.getTokenInfo());
+    // 修改密码
+    @GetMapping("/repasswd")
+    @Operation(summary = "修改密码")
+    @Parameter(name = "oldPasswd", description = "旧密码", required = true)
+    @Parameter(name = "newPasswd", description = "新密码", required = true)
+    public WVPResult<String> rePasswd(@Parameter String oldPasswd, @Parameter String newPasswd) {
+        logger.info("[修改密码] oldPasswd {}", oldPasswd);
+        String accountId = StpUserUtil.getLoginId().toString();
+        // 检查密码是否正确
+        UserAccount userAccount = accountService.getByAccountIdAndPassword(accountId, oldPasswd);
+        if (userAccount == null) {
+            return WVPResult.fail(
+                    ErrorCode.ERROR404,
+                    "请确认密码是否正确");
+        }
+        boolean flag = accountService.updatePassword(accountId, newPasswd);
+        // 登录成功后返回 token 信息, 以及用户名, 头像等信息
+        if (!flag) {
+            return WVPResult.fail(
+                    ErrorCode.ERROR100,
+                    "修改密码失败");
+        }
+        return WVPResult.success("ok");
     }
 
     @GetMapping("/load/bind")

+ 19 - 0
src/main/java/com/genersoft/iot/vmp/vmanager/account/AccountDeviceControl.java

@@ -83,6 +83,25 @@ public class AccountDeviceControl {
         return WVPResult.success("ok");
     }
 
+    // 修改设备名称
+    @PostMapping("/updateName")
+    @Operation(summary = "修改设备名称")
+    @Parameter(name = "deviceId", description = "设备id", required = true)
+    @Parameter(name = "deviceName", description = "设备名称", required = true)
+    public WVPResult<String> updateDeviceName(
+            @RequestParam String deviceId,
+            @RequestParam String deviceName) {
+        logger.info("[web api] /device/updateName 修改设备名称");
+        String userId = StpUserUtil.getLoginId().toString();
+        // 判断当前用户是否有该设备
+        Device device = accountService.getAccountDevice(userId, deviceId);
+        if (device == null) {
+            return WVPResult.fail(ErrorCode.ERROR403, "当前用户无权限访问此设备");
+        }
+        accountService.updateDeviceName(deviceId, deviceName);
+        return WVPResult.success("ok");
+    }
+
     // 获取当前登录的用户设备
     @GetMapping("/search")
     @Operation(summary = "获取设备列表")

+ 141 - 0
src/main/java/com/genersoft/iot/vmp/vmanager/bean/UserLoginResult.java

@@ -0,0 +1,141 @@
+package com.genersoft.iot.vmp.vmanager.bean;
+
+import cn.dev33.satoken.stp.SaTokenInfo;
+
+// 保留 SaTokenInfo 的所有字段,并且添加自定义字段
+public class UserLoginResult {
+    public String tokenName;
+    public String tokenValue;
+    public Boolean isLogin;
+    public Object loginId;
+    public String loginType;
+    public long tokenTimeout;
+    public long sessionTimeout;
+    public long tokenSessionTimeout;
+    public long tokenActiveTimeout;
+    public String loginDevice;
+    public String tag;
+    // 自定义字段
+    public String userName;
+    public String userId;
+
+    public UserLoginResult(SaTokenInfo saTokenInfo, String userName, int userId) {
+        // 复制所有字段
+        this.tokenName = saTokenInfo.tokenName;
+        this.tokenValue = saTokenInfo.tokenValue;
+        this.isLogin = saTokenInfo.isLogin;
+        this.loginId = saTokenInfo.loginId;
+        this.loginType = saTokenInfo.loginType;
+        this.tokenTimeout = saTokenInfo.tokenTimeout;
+        this.sessionTimeout = saTokenInfo.sessionTimeout;
+        this.tokenSessionTimeout = saTokenInfo.tokenSessionTimeout;
+        this.tokenActiveTimeout = saTokenInfo.tokenActiveTimeout;
+        this.loginDevice = saTokenInfo.loginDevice;
+        this.userName = userName;
+        this.userId = String.valueOf(userId);
+    }
+
+    public String getTokenName() {
+        return tokenName;
+    }
+
+    public void setTokenName(String tokenName) {
+        this.tokenName = tokenName;
+    }
+
+    public String getTokenValue() {
+        return tokenValue;
+    }
+
+    public void setTokenValue(String tokenValue) {
+        this.tokenValue = tokenValue;
+    }
+
+    public Boolean getLogin() {
+        return isLogin;
+    }
+
+    public void setLogin(Boolean login) {
+        isLogin = login;
+    }
+
+    public Object getLoginId() {
+        return loginId;
+    }
+
+    public void setLoginId(Object loginId) {
+        this.loginId = loginId;
+    }
+
+    public String getLoginType() {
+        return loginType;
+    }
+
+    public void setLoginType(String loginType) {
+        this.loginType = loginType;
+    }
+
+    public long getTokenTimeout() {
+        return tokenTimeout;
+    }
+
+    public void setTokenTimeout(long tokenTimeout) {
+        this.tokenTimeout = tokenTimeout;
+    }
+
+    public long getSessionTimeout() {
+        return sessionTimeout;
+    }
+
+    public void setSessionTimeout(long sessionTimeout) {
+        this.sessionTimeout = sessionTimeout;
+    }
+
+    public long getTokenSessionTimeout() {
+        return tokenSessionTimeout;
+    }
+
+    public void setTokenSessionTimeout(long tokenSessionTimeout) {
+        this.tokenSessionTimeout = tokenSessionTimeout;
+    }
+
+    public long getTokenActiveTimeout() {
+        return tokenActiveTimeout;
+    }
+
+    public void setTokenActiveTimeout(long tokenActiveTimeout) {
+        this.tokenActiveTimeout = tokenActiveTimeout;
+    }
+
+    public String getLoginDevice() {
+        return loginDevice;
+    }
+
+    public void setLoginDevice(String loginDevice) {
+        this.loginDevice = loginDevice;
+    }
+
+    public String getTag() {
+        return tag;
+    }
+
+    public void setTag(String tag) {
+        this.tag = tag;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getUserId() {
+        return userId;
+    }
+
+    public void setUserId(String userId) {
+        this.userId = userId;
+    }
+}