|
@@ -0,0 +1,114 @@
|
|
|
|
+package com.genersoft.iot.vmp.vmanager.user;
|
|
|
|
+
|
|
|
|
+import com.genersoft.iot.vmp.conf.security.SecurityUtils;
|
|
|
|
+import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
|
|
|
|
+import com.genersoft.iot.vmp.gb28181.bean.Device;
|
|
|
|
+import com.genersoft.iot.vmp.service.IAccountService;
|
|
|
|
+import com.genersoft.iot.vmp.service.IDeviceService;
|
|
|
|
+import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
|
|
|
+import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
|
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
+import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
+
|
|
|
|
+@Tag(name = "普通用户管理")
|
|
|
|
+@CrossOrigin(origins = "*")
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("/account")
|
|
|
|
+public class AccountController {
|
|
|
|
+ private Logger logger = LoggerFactory.getLogger(AccountController.class);
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IAccountService accountService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IDeviceService deviceService;
|
|
|
|
+
|
|
|
|
+ @PostMapping("/register")
|
|
|
|
+ @Operation(summary = "注册用户")
|
|
|
|
+ @Parameter(name = "name", description = "用户名", required = true)
|
|
|
|
+ @Parameter(name = "account", description = "登陆账号", required = true)
|
|
|
|
+ @Parameter(name = "password", description = "密码", required = true)
|
|
|
|
+ public WVPResult<String> register(String name, String account, String password) {
|
|
|
|
+ logger.info("register account: " + account + " password: " + password);
|
|
|
|
+ WVPResult wvpResult = new WVPResult();
|
|
|
|
+ if(accountService.checkAccount(account) != null)
|
|
|
|
+ {
|
|
|
|
+ wvpResult.setCode(ErrorCode.ERROR100.getCode());
|
|
|
|
+ wvpResult.setMsg("账号已存在");
|
|
|
|
+ return wvpResult;
|
|
|
|
+ }
|
|
|
|
+ accountService.registerAccount(name, account, password);
|
|
|
|
+ wvpResult.setCode(ErrorCode.SUCCESS.getCode());
|
|
|
|
+ wvpResult.setMsg("注册成功");
|
|
|
|
+ return wvpResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @PostMapping("/login")
|
|
|
|
+ @Operation(summary = "登录")
|
|
|
|
+ @Parameter(name = "account", description = "登陆账号", required = true)
|
|
|
|
+ @Parameter(name = "password", description = "密码", required = true)
|
|
|
|
+ public WVPResult<String> login(String account, String password)
|
|
|
|
+ {
|
|
|
|
+ logger.info("[登录账号] account {}", account);
|
|
|
|
+ if (accountService.login(account, password) == null)
|
|
|
|
+ {
|
|
|
|
+ return WVPResult.fail(
|
|
|
|
+ ErrorCode.ERROR404,
|
|
|
|
+ "账号或者密码错误");
|
|
|
|
+ }
|
|
|
|
+// SecurityUtils.login(username, password, authenticationManager);
|
|
|
|
+ return WVPResult.success(null, "ok");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @GetMapping("/bind/device")
|
|
|
|
+ @Operation(summary = "绑定设备")
|
|
|
|
+ @Parameter(name = "devCode", description = "设备码", required = true)
|
|
|
|
+ public WVPResult<String> bindDevice( String devCode)
|
|
|
|
+ {
|
|
|
|
+ logger.info("[绑定设备] bind account ");
|
|
|
|
+ WVPResult wvpResult = new WVPResult();
|
|
|
|
+ LoginUser loginUser = SecurityUtils.getUserInfo();
|
|
|
|
+ if(loginUser == null){
|
|
|
|
+ logger.warn("[绑定设备] 用户未登录");
|
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR401);
|
|
|
|
+ }
|
|
|
|
+ Device device = deviceService.getBindDevice(devCode);
|
|
|
|
+ if(device == null){
|
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR404, "无法找到对应设备");
|
|
|
|
+ }
|
|
|
|
+ // 判断设备是否已经绑定
|
|
|
|
+ if (!accountService.checkIsAllowBind(devCode)){
|
|
|
|
+ return WVPResult.fail(
|
|
|
|
+ ErrorCode.ERROR403,
|
|
|
|
+ "设备已经绑定, 无法重新绑定");
|
|
|
|
+ }
|
|
|
|
+ accountService.bindDevice(
|
|
|
|
+ loginUser.getId(),
|
|
|
|
+ device.getId(),
|
|
|
|
+ devCode );
|
|
|
|
+
|
|
|
|
+ return wvpResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @GetMapping("/unbind/device")
|
|
|
|
+ @Operation(summary = "解绑设备")
|
|
|
|
+ @Parameter(name = "deviceId", description = "设备id", required = true)
|
|
|
|
+ public WVPResult<String> unbindDevice( String deviceId)
|
|
|
|
+ {
|
|
|
|
+ LoginUser loginUser = SecurityUtils.getUserInfo();
|
|
|
|
+ if(loginUser == null){
|
|
|
|
+ logger.warn("[绑定设备] 用户未登录");
|
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR401);
|
|
|
|
+ }
|
|
|
|
+ accountService.unBindDevice(loginUser.getId(), Integer.parseInt(deviceId));
|
|
|
|
+ return WVPResult.success("ok");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|