diff --git a/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdmin.java b/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdmin.java index f1f1731..f1f2be3 100644 --- a/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdmin.java +++ b/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdmin.java @@ -13,4 +13,7 @@ public class MatrixAdmin { private String appIds; private Integer role;//1超级管理员,2管理员,3普通账号 private String channel;//fanmiyou,qiji + private Integer parentAdminId; + private Integer deviceCnt; + private Integer incomeRate; } diff --git a/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdminDevice.java b/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdminDevice.java new file mode 100644 index 0000000..268567e --- /dev/null +++ b/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdminDevice.java @@ -0,0 +1,19 @@ +package awesome.group.game.dao.bean; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; + +import java.sql.Timestamp; + +@Data +public class MatrixAdminDevice { + @TableId(value = "id", type = IdType.AUTO) + private Integer id; + private String deviceId; + private Integer appId; + private Integer adminId; + private Integer status;//0默认,-1下线 + private Timestamp createdAt; + private Timestamp updatedAt; +} diff --git a/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdvRecord.java b/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdvRecord.java index c1d2bf2..523570b 100644 --- a/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdvRecord.java +++ b/game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdvRecord.java @@ -19,6 +19,7 @@ public class MatrixAdvRecord { private String deviceBrand; private String deviceName; private String ip; + private Integer adminId; private Timestamp createdAt; } diff --git a/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdminDeviceMapper.java b/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdminDeviceMapper.java new file mode 100644 index 0000000..137c5b8 --- /dev/null +++ b/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdminDeviceMapper.java @@ -0,0 +1,14 @@ +package awesome.group.game.dao.mapper; + +import awesome.group.game.dao.bean.MatrixAdminDevice; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +public interface MatrixAdminDeviceMapper extends BaseMapper { + @Select("select * from matrix_admin_device where device_id = #{deviceId}") + MatrixAdminDevice selectByDeviceId(String deviceId); + + @Update("update matrix_admin_device set status = #{status} where admin_id=#{adminId} and device_id = #{deviceId}") + int updateStatusByDeviceId(int status, String deviceId, int adminId); +} diff --git a/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAppMapper.java b/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAppMapper.java index ea1ebc1..5daed65 100644 --- a/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAppMapper.java +++ b/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAppMapper.java @@ -4,8 +4,6 @@ import awesome.group.game.dao.bean.MatrixApp; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Select; -import java.util.List; - public interface MatrixAppMapper extends BaseMapper { @Select("select * from matrix_app where code = #{code}") MatrixApp queryByCode(String code); diff --git a/game-service/src/main/java/awesome/group/game/service/AdminDeviceService.java b/game-service/src/main/java/awesome/group/game/service/AdminDeviceService.java new file mode 100644 index 0000000..b05559a --- /dev/null +++ b/game-service/src/main/java/awesome/group/game/service/AdminDeviceService.java @@ -0,0 +1,92 @@ +package awesome.group.game.service; + +import awesome.group.game.dao.bean.MatrixAdmin; +import awesome.group.game.dao.bean.MatrixAdminDevice; +import awesome.group.game.dao.bean.MatrixApp; +import awesome.group.game.dao.mapper.MatrixAdminDeviceMapper; +import awesome.group.game.dao.mapper.MatrixAdminMapper; +import awesome.group.game.dao.mapper.MatrixAppMapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +import java.util.Arrays; +import java.util.List; + +@Service +public class AdminDeviceService { + + public static final int STATUS_DEFAULT = 0; + public static final int STATUS_OFFLINE = -1; + @Autowired + private MatrixAdminDeviceMapper mapper; + + @Autowired + private MatrixAdminMapper adminMapper; + + @Autowired + private MatrixAppMapper appMapper; + + public Integer getAdminId(String deviceId) { + MatrixAdminDevice device = mapper.selectByDeviceId(deviceId); + if (device == null || device.getStatus() == STATUS_OFFLINE) { + return null; + } + return device.getAdminId(); + } + + public List getDeviceList(Integer adminId, String appCode ) { + MatrixApp app = appMapper.queryByCode(appCode); + Assert.isTrue(app != null, "非法请求"); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(MatrixAdminDevice::getAdminId, adminId); + queryWrapper.eq(MatrixAdminDevice::getAppId, app.getId()); + return mapper.selectList(queryWrapper); + } + + public void setStatusOffline(Integer adminId, String deviceId) { + mapper.updateStatusByDeviceId(STATUS_OFFLINE, deviceId, adminId); + } + + public String bind(String deviceId, String appCode, String adminName) { + MatrixAdminDevice device = mapper.selectByDeviceId(deviceId); + if(device != null){ + return "设备已绑定"; + } + MatrixAdmin admin = adminMapper.query(adminName); + if (admin == null || admin.getRole() != AdminService.DEVICE_OWNER) { + return "设备主不存在"; + } + MatrixApp app = appMapper.queryByCode(appCode); + if (app == null) { + return "应用不存在"; + } + if(!StringUtils.hasText(admin.getAppIds()) || + !Arrays.stream(admin.getAppIds().split(",")).map(Integer::parseInt).toList().contains(app.getId())){ + return "无权限绑定该应用"; + } + long cnt = queryBindCnt(admin.getId(), app.getId()); + if (cnt >= admin.getDeviceCnt()) { + return "绑定设备数已达上限"; + } + device = new MatrixAdminDevice(); + device.setDeviceId(deviceId); + device.setAppId(app.getId()); + device.setAdminId(admin.getId()); + device.setStatus(STATUS_DEFAULT); + mapper.insert(device); + return "绑定成功"; + } + + public long queryBindCnt(int adminId, int appId){ + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(MatrixAdminDevice::getAdminId, adminId); + queryWrapper.eq(MatrixAdminDevice::getAppId, appId); + queryWrapper.eq(MatrixAdminDevice::getStatus, STATUS_DEFAULT); + return mapper.selectCount(queryWrapper); + } + + +} diff --git a/game-service/src/main/java/awesome/group/game/service/AdminService.java b/game-service/src/main/java/awesome/group/game/service/AdminService.java index f6c7f5c..a1a8e99 100644 --- a/game-service/src/main/java/awesome/group/game/service/AdminService.java +++ b/game-service/src/main/java/awesome/group/game/service/AdminService.java @@ -79,6 +79,10 @@ public class AdminService { if (app.isEmpty()) { return null; } + MatrixAdmin admin = adminMapper.selectById(adminId); + if (admin.getRole() == DEVICE_OWNER) { + wrapper.eq(MatrixAdvRecord::getAdminId, adminId); + } wrapper.eq(MatrixAdvRecord::getAppId, app.get().getId()); if (param.advType != null) { wrapper.eq(MatrixAdvRecord::getAdvType, param.advType); @@ -132,20 +136,22 @@ public class AdminService { adminMapper.updateAppIds(str, adminId); } - public List adminList(String channel) { + public List adminList(int adminId, String channel) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(MatrixAdmin::getChannel, channel); + query.eq(MatrixAdmin::getParentAdminId, adminId); List res = adminMapper.selectList(query); return res.stream().map(MatrixAdminBo::new).toList(); } - public void saveAdmin(MatrixAdminBo bo, String channel) { + public void saveAdmin(int adminId, MatrixAdminBo bo, String channel) { MatrixAdmin admin = new MatrixAdmin(); BeanUtils.copyProperties(bo, admin); if (!CollectionUtils.isEmpty(bo.getAppIds())) { admin.setAppIds(Joiner.on(",").join(bo.getAppIds())); } + admin.setParentAdminId(adminId); admin.setChannel(channel); if (admin.getId() != null) { if (StringUtils.hasText(admin.getPassword())) { diff --git a/game-service/src/main/java/awesome/group/game/service/MatrixService.java b/game-service/src/main/java/awesome/group/game/service/MatrixService.java index 4e24446..285400a 100644 --- a/game-service/src/main/java/awesome/group/game/service/MatrixService.java +++ b/game-service/src/main/java/awesome/group/game/service/MatrixService.java @@ -20,12 +20,16 @@ public class MatrixService { @Resource private MatrixAppMapper appMapper; + @Resource + private AdminDeviceService adminDeviceService; + public void saveRecord(MatrixAdvRecordEditBo bo, String ip) { Assert.isTrue(StringUtils.hasText(bo.appCode), "appCode不能为空"); MatrixApp app = mapper.queryApp(bo.appCode); if (app == null) { return;//非法请求,直接忽略 } + MatrixAdvRecord record = new MatrixAdvRecord(); record.setDeviceId(bo.deviceId); record.setAppId(app.getId()); @@ -36,6 +40,9 @@ public class MatrixService { record.setDeviceName(bo.deviceName); record.setIp(ip); record.setAdvId(bo.advId); + + Integer adminId = adminDeviceService.getAdminId(bo.deviceId); + record.setAdminId(adminId); mapper.insert(record); } diff --git a/game-service/src/main/java/awesome/group/game/service/bo/MatrixAdminBo.java b/game-service/src/main/java/awesome/group/game/service/bo/MatrixAdminBo.java index 4df4a08..af3d799 100644 --- a/game-service/src/main/java/awesome/group/game/service/bo/MatrixAdminBo.java +++ b/game-service/src/main/java/awesome/group/game/service/bo/MatrixAdminBo.java @@ -15,6 +15,8 @@ public class MatrixAdminBo { private String password; private List appIds; private Integer role;//1超级管理员,2管理员,3普通账号 + private Integer deviceCnt; + private Integer incomeRate; public MatrixAdminBo() { } diff --git a/game-web/src/main/java/awesome/group/game/web/controller/MatrixController.java b/game-web/src/main/java/awesome/group/game/web/controller/MatrixController.java index be956cd..a9f83d6 100644 --- a/game-web/src/main/java/awesome/group/game/web/controller/MatrixController.java +++ b/game-web/src/main/java/awesome/group/game/web/controller/MatrixController.java @@ -1,5 +1,6 @@ package awesome.group.game.web.controller; +import awesome.group.game.service.AdminDeviceService; import awesome.group.game.web.RequestContext; import awesome.group.game.web.aop.RestApi; import awesome.group.game.service.MatrixService; @@ -14,6 +15,9 @@ public class MatrixController { @Resource private MatrixService matrixService; + @Resource + private AdminDeviceService deviceService; + @PostMapping("/saveAdvRecord") @RestApi public R saveAdvRecord(@RequestBody MatrixAdvRecordEditBo bo) { @@ -40,4 +44,10 @@ public class MatrixController { matrixService.addIntoWhiteList(deviceId, appCode); return new R<>(R.CODE_SUCCESS, "ok", null); } + + @PostMapping("/bindDevice") + @RestApi + public R bindDevice(@RequestParam String deviceId, @RequestParam String appCode, @RequestParam String adminName){ + return new R<>(R.CODE_SUCCESS, "ok", deviceService.bind(deviceId, appCode, adminName)); + } } diff --git a/game-web/src/main/java/awesome/group/game/web/filter/LoginFilter.java b/game-web/src/main/java/awesome/group/game/web/filter/LoginFilter.java index 3bf426c..bed3511 100644 --- a/game-web/src/main/java/awesome/group/game/web/filter/LoginFilter.java +++ b/game-web/src/main/java/awesome/group/game/web/filter/LoginFilter.java @@ -18,7 +18,7 @@ import java.util.List; /** * @author nidaren */ -@WebFilter(filterName = "ContextFilter", urlPatterns = {"/api/game/*"}) +@WebFilter(filterName = "ContextFilter", urlPatterns = {"/api/game/auth"}) public class LoginFilter implements Filter { @@ -33,14 +33,9 @@ public class LoginFilter implements Filter { HttpServletRequest httpRequest = (HttpServletRequest) request; String token = httpRequest.getHeader("Authorization"); List openApi = List.of( - "/api/game/user/demo", - "/api/game/app/info", "/api/game/auth/login", "/api/game/auth/loginV2", "/api/game/auth/loginV3", - "/api/game/matrix/saveAdvRecord", - "/api/game/matrix/whiteList", - "/api/game/matrix/addWhiteList", "/api/game/auth/test"); boolean pass = false; Integer userId = null; diff --git a/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java b/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java index dcd3dfb..777357f 100644 --- a/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java +++ b/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java @@ -64,13 +64,13 @@ public class AdminController { @GetMapping("/adminList") @RestApi public R> adminList() { - return new R<>(R.CODE_SUCCESS, "ok", adminService.adminList(RequestContext.getRequestChannel())); + return new R<>(R.CODE_SUCCESS, "ok", adminService.adminList(RequestContext.getAdminID(),RequestContext.getRequestChannel())); } @PostMapping("/saveAdmin") @RestApi public R saveAdmin(@RequestBody MatrixAdminBo matrixAdmin) { - adminService.saveAdmin(matrixAdmin, RequestContext.getRequestChannel()); + adminService.saveAdmin(RequestContext.getAdminID(),matrixAdmin, RequestContext.getRequestChannel()); return new R<>(R.CODE_SUCCESS, "ok", null); } diff --git a/game-web/src/main/java/awesome/group/game/web/rest/matrix/DeviceController.java b/game-web/src/main/java/awesome/group/game/web/rest/matrix/DeviceController.java new file mode 100644 index 0000000..cf0c6e5 --- /dev/null +++ b/game-web/src/main/java/awesome/group/game/web/rest/matrix/DeviceController.java @@ -0,0 +1,34 @@ +package awesome.group.game.web.rest.matrix; + +import awesome.group.game.dao.bean.MatrixAdminDevice; +import awesome.group.game.service.AdminDeviceService; +import awesome.group.game.service.common.response.R; +import awesome.group.game.web.RequestContext; +import awesome.group.game.web.aop.RestApi; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/admin/device") +@Tag(name = "device") +public class DeviceController { + + @Autowired + private AdminDeviceService adminDeviceService; + + @GetMapping("/list") + @RestApi + public R> deviceList(@RequestParam String appCode) { + return new R<>(adminDeviceService.getDeviceList(RequestContext.getAdminID(), appCode)); + } + + @PostMapping("/offline") + @RestApi + public R offline(@RequestParam String deviceId) { + adminDeviceService.setStatusOffline(RequestContext.getAdminID(), deviceId); + return new R<>("ok"); + } +}