HwMeetingController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.sooka.sponest.dataexchange.hwMeeting.controller;
  2. import com.dahuatech.hutool.json.JSONArray;
  3. import com.dahuatech.hutool.json.JSONObject;
  4. import com.dahuatech.hutool.json.JSONTokener;
  5. import com.dahuatech.hutool.json.JSONUtil;
  6. import com.ruoyi.common.core.web.domain.AjaxResult;
  7. import com.ruoyi.common.redis.service.RedisService;
  8. import com.sooka.sponest.dataexchange.hwMeeting.service.HwMeetingService;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.PathVariable;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import javax.annotation.Resource;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * 华为云会议控制器
  19. */
  20. @Controller
  21. @RequestMapping("/hwMeeting")
  22. public class HwMeetingController {
  23. @Resource
  24. private HwMeetingService hwMeetingService;
  25. @Resource
  26. private RedisService redisService;
  27. /**
  28. * 创建或加入会议
  29. */
  30. @GetMapping("/joinConferences/{eventId}/{subject}/{nickName}")
  31. @ResponseBody
  32. public AjaxResult hasConferences(@PathVariable(name = "eventId") String eventId,
  33. @PathVariable(name = "subject") String subject,
  34. @PathVariable(name = "nickName") String nickName) {
  35. Map<String,Object> map = new HashMap<>();
  36. //查询在线会议
  37. JSONObject hasConferences = JSONUtil.parseObj(hwMeetingService.hasConferences());
  38. //有在线会议 返回来宾入会凭据
  39. if (hasConferences.getInt("count") > 0) {
  40. //取会议信息
  41. JSONObject conferencesInfo = hasConferences.getJSONArray("data").getJSONObject(0);
  42. //取密码数组 passwordEntry
  43. JSONArray array = JSONUtil.parseArray(conferencesInfo.get("passwordEntry"));
  44. for (Object o : array) {
  45. JSONObject password = JSONUtil.parseObj(o);
  46. JSONObject generalInfo = new JSONObject();
  47. if (password.getStr("conferenceRole").equals("general")) {
  48. generalInfo.put("conferenceID", conferencesInfo.getStr("conferenceID"));
  49. generalInfo.put("password", password.getStr("password"));
  50. map.put("info",generalInfo);
  51. }
  52. }
  53. }else{
  54. //无在线会议 创建会议并返回主持人入会凭据
  55. this.createConferences(eventId,subject,map);
  56. }
  57. map.put("nickName",nickName);
  58. map.put("nonce",hwMeetingService.getNonce());
  59. return AjaxResult.success(map);
  60. }
  61. /**
  62. * 创建会议
  63. */
  64. public boolean createConferences(String eventId,String subject,Map map) {
  65. String result = hwMeetingService.conferences(subject);
  66. if (isSuccess(result)) {
  67. //将结果转换为json数组,当处理成功时返回的格式为数组
  68. Object ob = JSONUtil.parseArray(result).get(0);
  69. //ob转换为JSONObject 取主持人入会信息返回
  70. JSONObject object = JSONUtil.parseObj(ob);
  71. //取密码数组 passwordEntry
  72. JSONArray array = JSONUtil.parseArray(object.get("passwordEntry"));
  73. for (Object o : array) {
  74. JSONObject password = JSONUtil.parseObj(o);
  75. JSONObject chairInfo = new JSONObject();
  76. //返回主持人入会凭据
  77. if (password.getStr("conferenceRole").equals("chair")) {
  78. chairInfo.put("conferenceID", object.getStr("conferenceID"));
  79. chairInfo.put("password", password.getStr("password"));
  80. map.put("info",chairInfo);
  81. }
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. /**
  88. * 获取加入会议URI
  89. */
  90. @ResponseBody
  91. @GetMapping("/guestJoinUri/{eventId}")
  92. public AjaxResult guestJoinUri(@PathVariable(name = "eventId") String eventId) {
  93. //查询在线会议
  94. JSONObject hasConferences = JSONUtil.parseObj(hwMeetingService.hasConferences());
  95. if(hasConferences != null){
  96. //取会议信息
  97. JSONObject conferencesInfo = hasConferences.getJSONArray("data").getJSONObject(0);
  98. //取入会链接
  99. return AjaxResult.success(conferencesInfo.get("guestJoinUri"));
  100. }
  101. return AjaxResult.error("未查询到数据");
  102. }
  103. /**
  104. * 判断返回数据是JSONArray还是JSONObject,
  105. * 如果是Array 则表示成功
  106. * 反之失败
  107. * */
  108. private boolean isSuccess(String result) {
  109. Object object = new JSONTokener(result).nextValue();
  110. if (object instanceof JSONObject) {
  111. JSONObject jsonObject = (JSONObject) object;
  112. String error_code = jsonObject.get("error_code").toString();
  113. System.err.println(error_code);
  114. return false;
  115. }
  116. return true;
  117. }
  118. }