UbiLpWxController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.zy.bms.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.zy.bms.common.ServerResponse;
  4. import com.zy.bms.common.enums.ResponseCode;
  5. import com.zy.bms.entity.DeviceBase;
  6. import com.zy.bms.entity.User;
  7. import com.zy.bms.entity.UserPrivilege;
  8. import com.zy.bms.entity.lp.LpInfoState;
  9. import com.zy.bms.pojo.io.LpInfoRecordIO;
  10. import com.zy.bms.service.*;
  11. import com.zy.bms.service.lp.ILpInfoRecordService;
  12. import com.zy.bms.service.lp.ILpInfoStateService;
  13. import com.zy.bms.utils.AesUtil;
  14. import com.zy.bms.utils.RabbitMQApi;
  15. import com.zy.bms.utils.RandomCode;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.annotation.Resource;
  21. import java.util.*;
  22. /**
  23. * 普适型,路牌 微信小程序接口
  24. *
  25. * @author yang xiao kun
  26. * create on 2021/8/24
  27. */
  28. @RestController
  29. @RequestMapping("/bms/api/wx_app")
  30. public class UbiLpWxController extends BaseController {
  31. @Resource
  32. private IUserService userService;
  33. @Resource
  34. private IUserPrivilegeService userPrivilegeService;
  35. @Resource
  36. private IGroupService groupService;
  37. @Resource
  38. private IDeviceBaseService deviceBaseService;
  39. @Resource
  40. private IPostMqttMsgService postMqttMsgService;
  41. @Resource
  42. private ILpInfoStateService lpInfoStateService;
  43. @Resource
  44. private ILpInfoRecordService lpInfoRecordService;
  45. /**
  46. * 微信登录
  47. *
  48. * @return 用户 加密userId
  49. */
  50. @PostMapping("login.do")
  51. public ServerResponse login(String code) {
  52. String openId = userService.getWxAppId(code);
  53. if (openId == null) return ServerResponse.error();
  54. User user = userService.getOne(new QueryWrapper<User>().eq("open_id", openId));
  55. //保存新用户
  56. if (user == null) {
  57. user = new User();
  58. user.setOpenId(openId);
  59. user.setRandomCode(RandomCode.UUID_8());
  60. userService.save(user);
  61. }
  62. Map<String, String> res = new HashMap<>();
  63. res.put("userId", AesUtil.encrypt(user.getId().toString()));
  64. res.put("uuid", user.getRandomCode());
  65. res.put("admin", user.getAdmin().toString());
  66. return ServerResponse.success(res);
  67. }
  68. /**
  69. * 查看当前在线设备 rabbitMQ
  70. */
  71. @GetMapping("connections.do")
  72. public ServerResponse connections() {
  73. User user = userService.getOne(new QueryWrapper<User>().eq("user_id", userId()));
  74. if (user == null || user.getAdmin() != 1) {
  75. return ServerResponse.custom(ResponseCode.AUTHOR);
  76. }
  77. return ServerResponse.success(RabbitMQApi.connections());
  78. }
  79. /**
  80. * 查看当前注册的用户 rabbitMQ
  81. */
  82. @GetMapping("users.do")
  83. public ServerResponse users() {
  84. User user = userService.getOne(new QueryWrapper<User>().eq("user_id", userId()));
  85. if (user == null || user.getAdmin() != 1) {
  86. return ServerResponse.custom(ResponseCode.AUTHOR);
  87. }
  88. return ServerResponse.success(RabbitMQApi.users());
  89. }
  90. /**
  91. * 查看用户拥有权限的设备组列表
  92. */
  93. @GetMapping("getGroups.do")
  94. public ServerResponse getGroupsByUserId() {
  95. return ServerResponse.success(groupService.getByUserId(userId()));
  96. }
  97. /**
  98. * 校验设备码是否存在
  99. */
  100. @GetMapping("checkOpenNum.do")
  101. public ServerResponse checkOpenNum(String openNum) {
  102. return ServerResponse.success(deviceBaseService.checkOpenNum(openNum));
  103. }
  104. /**
  105. * 通过设备组ID查询设备列表
  106. */
  107. @GetMapping("getDevicesByGroupId.do")
  108. public ServerResponse getDevicesByGroupId(String groupId) {
  109. Set<String> groupIds = new HashSet<>(userPrivilegeService.getGroupIdsByUserId(userId()));
  110. if (!groupIds.contains(groupId)) return ServerResponse.warning("无权限");
  111. return ServerResponse.success(deviceBaseService.listByGroupIdWx(groupId));
  112. }
  113. /**
  114. * 分组查询设备列表
  115. */
  116. @GetMapping("getDevicesByUserId.do")
  117. public ServerResponse getDevicesByUserId() {
  118. List<String> groupIds = userPrivilegeService.getGroupIdsByUserId(userId());
  119. if (groupIds.isEmpty()) return ServerResponse.success(null);
  120. return ServerResponse.success(deviceBaseService.listByGroupIdsWx(groupIds));
  121. }
  122. /**
  123. * 扫码查询
  124. */
  125. @GetMapping("scan.do")
  126. public ServerResponse scan(String openNum) {
  127. DeviceBase device = deviceBaseService.getOne(new QueryWrapper<DeviceBase>().eq("open_num", openNum));
  128. if (device == null) return ServerResponse.warning("设备码错误!");
  129. //如果该用户没有该设备组权限,则加上权限
  130. UserPrivilege userPrivilege = userPrivilegeService.getOne(new QueryWrapper<UserPrivilege>()
  131. .eq("group_id", device.getGroupId()).eq("user_id", userId()));
  132. if (userPrivilege == null) {
  133. UserPrivilege entity = new UserPrivilege();
  134. entity.setUserId(userId());
  135. entity.setGroupId(device.getGroupId());
  136. userPrivilegeService.save(entity);
  137. }
  138. return ServerResponse.success(device.getType());
  139. }
  140. /**
  141. * 获取设备详情信息
  142. */
  143. @GetMapping("getDeviceDetail.do")
  144. public ServerResponse getDeviceDetail(String openNum) {
  145. return ServerResponse.success(deviceBaseService.getDetailWx(openNum));
  146. }
  147. /**
  148. * 更新唤醒时间和阈值
  149. */
  150. @PostMapping("updateLp.do")
  151. public ServerResponse updateLp(LpInfoState entity) {
  152. return ServerResponse.success(lpInfoStateService.updateWakeAndThresh(entity));
  153. }
  154. /**
  155. * 蜂鸣器
  156. */
  157. @GetMapping("buzzing.do")
  158. public ServerResponse buzzing(String openNum) {
  159. postMqttMsgService.requestBuzzingUbi(openNum);
  160. return ServerResponse.success();
  161. }
  162. /**
  163. * 立即请求获取最新心跳包
  164. */
  165. @GetMapping("heartbeat.do")
  166. public ServerResponse heartbeat(String openNum) {
  167. postMqttMsgService.requestHeartbeatUbi(openNum);
  168. return ServerResponse.success();
  169. }
  170. /**
  171. * 查询路牌设备历史记录
  172. */
  173. @GetMapping("getLpHistoryData.do")
  174. public ServerResponse getLpHistoryData(LpInfoRecordIO io) {
  175. return ServerResponse.success(lpInfoRecordService.getHistoryList(io));
  176. }
  177. }