后台服务
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
15 KiB

  1. package com.iformall.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.code.kaptcha.Producer;
  5. import com.iformall.annotation.AuthIgnore;
  6. import com.iformall.common.ErrorCode;
  7. import com.iformall.common.ResultData;
  8. import com.iformall.domain.po.WxCUserBasicInfo;
  9. import com.iformall.domain.po.WxCVideoTable;
  10. import com.iformall.domain.po.sm.PhotoSpeakVideo;
  11. import com.iformall.domain.po.sm.PreviewParam;
  12. import com.iformall.domain.po.sm.VoiceInfo;
  13. import com.iformall.domain.vo.WxCLiveLoginVo;
  14. import com.iformall.enums.EnumVideoStatus;
  15. import com.iformall.exception.MallinkException;
  16. import com.iformall.service.*;
  17. import com.iformall.service.sm.VoiceInfoService;
  18. import com.iformall.sm.AiPreviewParam;
  19. import com.iformall.utils.Constant;
  20. import com.iformall.utils.PasswordHelper;
  21. import com.iformall.utils.RedisCacheUtils;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiImplicitParam;
  24. import io.swagger.annotations.ApiOperation;
  25. import lombok.SneakyThrows;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.beans.factory.annotation.Qualifier;
  32. import org.springframework.data.redis.core.RedisTemplate;
  33. import org.springframework.web.bind.annotation.*;
  34. import springfox.documentation.spring.web.json.Json;
  35. import javax.imageio.ImageIO;
  36. import javax.servlet.ServletOutputStream;
  37. import javax.servlet.http.HttpServletRequest;
  38. import javax.servlet.http.HttpServletResponse;
  39. import java.awt.image.BufferedImage;
  40. import java.io.File;
  41. import java.io.IOException;
  42. import java.io.InputStream;
  43. import java.io.OutputStream;
  44. import java.net.URL;
  45. import java.net.URLEncoder;
  46. import java.nio.charset.StandardCharsets;
  47. import java.util.*;
  48. @RestController
  49. @RequestMapping("/api/live")
  50. @Api(description = "直播相关接口")
  51. public class UserLiveController extends BaseController {
  52. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  53. @Autowired
  54. private Producer producer;
  55. @Autowired
  56. @Qualifier("objectCommonRedisTemplate")
  57. RedisTemplate<String, Object> redisTemplate;
  58. @Autowired
  59. private WxCUserBasicInfoService wxCUserBasicInfoService;
  60. @Autowired
  61. private WxCLiveUserBasicInfoService wxCLiveUserBasicInfoService;
  62. @Autowired
  63. private WxCVideoService wxCVideoService;
  64. @Autowired
  65. private WxCVoiceService wxCVoiceService;
  66. @Autowired
  67. private WxCUserAuthorityService wxCUserAuthorityService;
  68. @Autowired
  69. private VoiceInfoService voiceInfoService;
  70. @AuthIgnore
  71. @PostMapping("/login")
  72. @ApiOperation(value = "用户登录", notes = "{\"username\":\"string\",\"password\":\"string\",\"code\":\"string\",\"status\":\"int\"}")
  73. public Map<String, Object> login(@RequestBody Map<String, String> map, HttpServletResponse response) {
  74. String ipAddr = getIpAddr();
  75. logger.debug("[" + ipAddr + "] WxUserGrantController::login");
  76. Map<String, Object> resultMap = new HashMap<>();
  77. HashMap<String, Object> status = new HashMap<>();
  78. String code = map.get("code");
  79. String phone = map.get("username");
  80. String password = map.get("password");
  81. if (StringUtils.isBlank(phone) || StringUtils.isBlank(password)) {
  82. status.put("code", ErrorCode.SYS_PARAMETER_ERROR.getCode());
  83. status.put("message", "手机号或密码为空");
  84. resultMap.put("status", status);
  85. return resultMap;
  86. }
  87. WxCUserBasicInfo basicInfo = wxCUserBasicInfoService.findInfoByPhone(getTenantInfo(), phone);
  88. if (basicInfo == null) {
  89. status.put("code", ErrorCode.USER_IS_EMPTY);
  90. status.put("message", "用户不存在");
  91. resultMap.put("status", status);
  92. return resultMap;
  93. }
  94. String encryptPassword = new PasswordHelper().encryptPassword(password);
  95. if (!encryptPassword.equals(basicInfo.getPassword())) {
  96. status.put("code", ErrorCode.USER_PASSWD_ERR.getCode());
  97. status.put("message", "手机号或密码错误");
  98. resultMap.put("status", status);
  99. return resultMap;
  100. }
  101. int statu = Integer.parseInt(map.get("status"));
  102. if (statu == 0) {
  103. WxCUserBasicInfo basicLiveInfo = wxCLiveUserBasicInfoService.getById(basicInfo.getId());
  104. if (basicLiveInfo.getCode() != null && !basicLiveInfo.getCode().equals(code)) {
  105. status.put("code", ErrorCode.USER_ALREADY_LOGIN.getCode());
  106. status.put("message", "用户已在其他设备登录");
  107. resultMap.put("status", status);
  108. return resultMap;
  109. }
  110. if (basicLiveInfo.getCode() == null) {
  111. wxCLiveUserBasicInfoService.updateCode(basicInfo.getId(), map.get("code"));
  112. }
  113. }
  114. if (statu == -1) {
  115. wxCLiveUserBasicInfoService.updateCode(basicInfo.getId(), null);
  116. basicInfo.setStatus(-2);
  117. status.put("code", ErrorCode.USER_CANCEL_MCODE.getCode());
  118. status.put("message", "设备已注销");
  119. resultMap.put("status", status);
  120. return resultMap;
  121. }
  122. wxCUserBasicInfoService.handleLoginUser(basicInfo);
  123. WxCUserBasicInfo basicLiveInfo = wxCLiveUserBasicInfoService.getById(basicInfo.getId());
  124. Map<String, Object> data = new HashMap();
  125. data.put("username",basicInfo.getPhone());
  126. data.put("status", 0);
  127. data.put("version", basicLiveInfo.getVersion());
  128. data.put("current_time", new Date(System.currentTimeMillis() / 1000));
  129. data.put("expire_time", basicLiveInfo.getExpireTime().getTime() / 1000);
  130. data.put("token", basicInfo.getToken());
  131. resultMap.put("data", data);
  132. status.put("code", 1000);
  133. status.put("message", "success");
  134. resultMap.put("status", status);
  135. return resultMap;
  136. }
  137. /**
  138. * 视频模板列表
  139. */
  140. @PostMapping("/avatarList")
  141. @ApiOperation(value = "视频模板列表", notes = "{\"username\",\"string\",\"code\",\"string\"}")
  142. public Map<String, Object> avatarList(@RequestBody Map<String, String> params) throws Exception {
  143. String ipaddress = getIpAddr();
  144. logger.debug("[" + ipaddress + "] WxUserGrantController::getAvatarList");
  145. Map<String, Object> resultMap = new HashMap<>();
  146. HashMap<String, Object> status = new HashMap<>();
  147. String code = params.get("code");
  148. Long userId = getMemberId();
  149. //鉴权
  150. WxCUserBasicInfo basicLiveInfo = wxCLiveUserBasicInfoService.getById(userId);
  151. if (basicLiveInfo.getCode() != null && !basicLiveInfo.getCode().equals(code)) {
  152. status.put("code", ErrorCode.USER_ALREADY_LOGIN.getCode());
  153. status.put("message", "用户已在其他设备登录");
  154. resultMap.put("status", status);
  155. return resultMap;
  156. }
  157. Map<String, Object> data = wxCVideoService.getById(userId);
  158. resultMap.put("data", data);
  159. status.put("code", 1000);
  160. status.put("msg", "success");
  161. resultMap.put("status", status);
  162. return resultMap;
  163. }
  164. /**
  165. * 音频模板列表
  166. */
  167. @PostMapping("/audioList")
  168. @ApiOperation(value = "音频模板列表", notes = "{\"username\",\"string\",\"code\",\"string\"}")
  169. public Map<String, Object> audioList(@RequestBody Map<String, String> params) {
  170. String ipaddress = getIpAddr();
  171. logger.debug("[" + ipaddress + "] WxUserGrantController::getAudioList");
  172. Map<String, Object> resultMap = new HashMap<>();
  173. HashMap<String, Object> status = new HashMap<>();
  174. String code = params.get("code");
  175. Long userId = getMemberId();
  176. WxCUserBasicInfo basicLiveInfo = wxCLiveUserBasicInfoService.getById(userId);
  177. if (basicLiveInfo.getCode() != null && !basicLiveInfo.getCode().equals(code)) {
  178. status.put("code", ErrorCode.USER_ALREADY_LOGIN.getCode());
  179. status.put("message", "用户已在其他设备登录");
  180. resultMap.put("status", status);
  181. return resultMap;
  182. }
  183. Map<String, Object> data = wxCVoiceService.getById(userId);
  184. resultMap.put("data", data);
  185. status.put("code", 1000);
  186. status.put("msg", "success");
  187. resultMap.put("status", status);
  188. return resultMap;
  189. }
  190. /**
  191. * 资源权限查询
  192. */
  193. @ApiOperation(value = "资源权限查询", notes = "{\"username\",\"string\",\"code\",\"string\",\"type\",\"int\",\"resource_id\",\"long\"}")
  194. @PostMapping("/author")
  195. public Map<String, Object> getAuthor(@RequestBody Map<String, String> params) {
  196. String ipaddress = getIpAddr();
  197. logger.debug("[" + ipaddress + "] WxUserGrantController::getAuthor");
  198. Map<String, Object> resultMap = new HashMap<>();
  199. HashMap<String, Object> status = new HashMap<>();
  200. String code = params.get("code");
  201. Integer type = Integer.parseInt(params.get("type"));
  202. Long resourceId = Long.valueOf(params.get("resource_id"));
  203. if(type == null){
  204. status.put("code", ErrorCode.SYS_PARAMETER_NOT_NULL.getCode());
  205. status.put("message", "type 为空");
  206. resultMap.put("status", status);
  207. return resultMap;
  208. }
  209. if(resourceId == null){
  210. status.put("code", ErrorCode.SYS_PARAMETER_NOT_NULL.getCode());
  211. status.put("message", "资源ID 为空");
  212. resultMap.put("status", status);
  213. return resultMap;
  214. }
  215. Long userId = getMemberId();
  216. WxCUserBasicInfo basicLiveInfo = wxCLiveUserBasicInfoService.getById(userId);
  217. if (basicLiveInfo.getCode() != null && !basicLiveInfo.getCode().equals(code)) {
  218. status.put("code", ErrorCode.USER_ALREADY_LOGIN.getCode());
  219. status.put("message", "用户已在其他设备登录");
  220. resultMap.put("status", status);
  221. return resultMap;
  222. }
  223. Map<String, Object> data = wxCUserAuthorityService.getAuthor(userId, code, type, resourceId);
  224. if(data != null){
  225. resultMap.put("data", data);
  226. status.put("code", 1000);
  227. status.put("msg", "success");
  228. resultMap.put("status", status);
  229. return resultMap;
  230. }
  231. status.put("code", ErrorCode.SYS_NULLPOINTER_ERROR.getCode());
  232. status.put("msg", "未查询到资源权限");
  233. resultMap.put("status", status);
  234. return resultMap;
  235. }
  236. @ApiOperation(value = "tts", notes = "{\"username\",\"string\",\"gen_txt\",\"string\",\"voice_id\",\"string\",\"voice_style\",\"string\",\"speed\",\"int\"}")
  237. @PostMapping("/audiotts")
  238. public Map<String, Object> voicePreview(@RequestBody Map<String, String> params) {
  239. logger.debug("[" + getIpAddr() + "] UserLiveController::voicePreview");
  240. Map<String, Object> resultMap = new HashMap<>();
  241. HashMap<String, Object> status = new HashMap<>();
  242. String voice_id = params.get("voice_id");
  243. String voiceStyle = params.get("voice_style");
  244. if (StringUtils.isBlank(voice_id)) {
  245. status.put("code", ErrorCode.SYS_PARAMETER_NOT_NULL.getCode());
  246. status.put("msg", "音色ID不能为空");
  247. resultMap.put("status", status);
  248. return resultMap;
  249. }
  250. Long voiceId = null;
  251. try{
  252. voiceId = Long.parseLong(voice_id);
  253. }catch(Exception e){
  254. status.put("code", ErrorCode.SYS_PARAMETER_ERROR.getCode());
  255. status.put("msg", "音色ID参数异常");
  256. resultMap.put("status", status);
  257. return resultMap;
  258. }
  259. String text = params.get("gen_txt");
  260. if (StringUtils.isBlank(text)) {
  261. resultMap.put("code", ErrorCode.SYS_PARAMETER_NOT_NULL.getCode());
  262. resultMap.put("msg", "需要生成的文字不能为空");
  263. return resultMap;
  264. }
  265. String speedStr = params.get("speed");
  266. Integer speed = null;
  267. try{
  268. speed = Integer.parseInt(speedStr);
  269. }catch(Exception e){}
  270. Map<String, Object> data = wxCVoiceService.voicePreview(voiceId,voiceStyle,text,speed);
  271. if(data != null){
  272. resultMap.put("data", data);
  273. status.put("code", 1000);
  274. status.put("msg", "success");
  275. resultMap.put("status", status);
  276. return resultMap;
  277. }
  278. status.put("code", ErrorCode.SYS_PARAMETER_NOT_NULL.getCode());
  279. status.put("msg", "获取tts 异常");
  280. resultMap.put("status", status);
  281. return resultMap;
  282. }
  283. @AuthIgnore
  284. @SneakyThrows
  285. @ApiOperation(value = "模板下载接口", notes = "{\"username\",\"string\",\"gen_txt\",\"string\",\"voice_id\",\"string\",\"voice_style\",\"string\",\"speed\",\"int\"}")
  286. @GetMapping("exportVideo")
  287. public void exportVideo(@RequestBody Map<String, String> params, HttpServletRequest request, HttpServletResponse response) {
  288. logger.debug("[" + getIpAddr() + "] UserLiveController::exportVideo");
  289. response.reset();
  290. File tmpFile = null;
  291. OutputStream outputStream = null;
  292. WxCUserBasicInfo infoByPhone = wxCUserBasicInfoService.findInfoByPhone(getTenantInfo(), params.get("username"));
  293. Long id = infoByPhone.getId();
  294. try {
  295. outputStream = response.getOutputStream();
  296. } catch (IOException e) {
  297. throw new MallinkException(ErrorCode.SYS_SERVER_ERROR);
  298. }
  299. try {
  300. if (id == null) {
  301. throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL);
  302. }
  303. WxCVideoTable videoTable = wxCVideoService.selectOne(id, Long.parseLong(params.get("resource_id")));
  304. //获取响应的输出流
  305. InputStream inputStream = new URL(videoTable.getDemo()).openStream();
  306. response.setHeader("Content-Disposition", "attachment; filename="
  307. + URLEncoder.encode(videoTable.getId() + ".mp4", "UTF-8"));
  308. //解决编码问题
  309. response.setCharacterEncoding("UTF-8");
  310. response.setHeader("Content-Type", "application/octet-stream");
  311. byte[] cache = new byte[1024 * 300];
  312. int flag;
  313. while ((flag = inputStream.read(cache)) != -1) {
  314. outputStream.write(cache, 0, flag);
  315. }
  316. } catch (MallinkException e) {
  317. ResultData resultData = new ResultData(e.getErrorCode(), e.getMessage());
  318. //解决编码问题
  319. response.setCharacterEncoding("UTF-8");
  320. response.setHeader("Content-Type", "application/json");
  321. outputStream.write(JSONObject.toJSONString(resultData).getBytes(StandardCharsets.UTF_8));
  322. } finally {
  323. if (tmpFile != null) {
  324. tmpFile.delete();
  325. }
  326. outputStream.flush();
  327. outputStream.close();
  328. }
  329. }
  330. @AuthIgnore
  331. @ApiOperation("选择声音选择风格")
  332. @GetMapping("/chooseType")
  333. @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
  334. public ResultData chooseType(Long id) {
  335. logger.debug("[" + getIpAddr() + "] MouldPatchController::chooseType");
  336. List<VoiceInfo> voiceInfos = wxCVoiceService.chooseType(id);
  337. return new ResultData(voiceInfos);
  338. }
  339. }