SceneApiController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package com.fdkankan.ucenter.controller.app;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.RuntimeUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.core.util.ZipUtil;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.fdkankan.common.constant.ErrorCode;
  8. import com.fdkankan.common.constant.SceneConstant;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.common.util.*;
  11. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  12. import com.fdkankan.redis.constant.RedisKey;
  13. import com.fdkankan.redis.constant.RedisLockKey;
  14. import com.fdkankan.redis.util.RedisLockUtil;
  15. import com.fdkankan.ucenter.annotation.CheckInnerApiPermit;
  16. import com.fdkankan.ucenter.common.BaseController;
  17. import com.fdkankan.ucenter.common.Result;
  18. import com.fdkankan.ucenter.common.constants.ConstantFilePath;
  19. import com.fdkankan.ucenter.common.constants.NacosProperty;
  20. import com.fdkankan.ucenter.constant.CameraConstant;
  21. import com.fdkankan.ucenter.constant.LoginConstant;
  22. import com.fdkankan.ucenter.entity.*;
  23. import com.fdkankan.ucenter.service.*;
  24. import com.fdkankan.ucenter.service.impl.LoginService;
  25. import com.fdkankan.ucenter.service.impl.SceneApiService;
  26. import com.fdkankan.ucenter.util.DateUserUtil;
  27. import com.fdkankan.ucenter.vo.request.LoginParam;
  28. import com.fdkankan.ucenter.vo.request.RequestSceneStatistics;
  29. import com.fdkankan.ucenter.vo.response.LoginVo;
  30. import com.fdkankan.ucenter.vo.response.SceneInfoVo;
  31. import com.fdkankan.ucenter.vo.response.UserVo;
  32. import java.io.File;
  33. import java.io.UnsupportedEncodingException;
  34. import java.net.URLEncoder;
  35. import java.util.*;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. import lombok.extern.slf4j.Slf4j;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.joda.time.DateTime;
  41. import org.springframework.beans.BeanUtils;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.beans.factory.annotation.Value;
  44. import org.springframework.util.ObjectUtils;
  45. import org.springframework.web.bind.annotation.*;
  46. @Slf4j
  47. @RestController
  48. @RequestMapping("/ucenter/scene")
  49. public class SceneApiController extends BaseController {
  50. @Autowired
  51. ISceneProService sceneProService;
  52. @Autowired
  53. IScenePlusService scenePlusService;
  54. @Autowired
  55. IScenePlusExtService scenePlusExtService;
  56. @Autowired
  57. IUserService userService;
  58. @Autowired
  59. LoginService loginService;
  60. @Autowired
  61. ICameraService cameraService;
  62. @Autowired
  63. ICameraDetailService cameraDetailService;
  64. @Autowired
  65. ISceneCooperationService cooperationService;
  66. @Autowired
  67. private IScene3dNumService scene3dNumService;
  68. @Autowired
  69. private ISceneStatisticsService sceneStatisticsService;
  70. @Autowired
  71. private RedisLockUtil redisLockUtil;
  72. @Autowired
  73. private ISceneProEditService sceneProEditService;
  74. @Autowired
  75. private FYunFileServiceInterface fYunFileService;
  76. @Autowired
  77. SceneApiService sceneApiService;
  78. @Autowired
  79. private IInnerService innerService;
  80. @Value("${scene.pro.url}")
  81. private String sceneProUrl;
  82. @Value("${scene.pro.new.url}")
  83. private String sceneProNewUrl;
  84. @Value("${scene.v4.url}")
  85. private String sceneProV4Url;
  86. /**
  87. * 获取指定场景信息
  88. * @param num 场景码
  89. */
  90. @RequestMapping("/getInfo")
  91. public Result getInfo(@RequestParam(required = false) String num){
  92. return Result.success( sceneProService.getInfo(num) );
  93. }
  94. //app 使用跳转到编辑页面
  95. @RequestMapping("/goEditScenePage")
  96. public void goEditScenePage(HttpServletRequest request, HttpServletResponse response) throws Exception{
  97. String phoneNum = request.getParameter("phoneNum");
  98. String password = request.getParameter("password");
  99. String sceneNum = request.getParameter("sceneNum");
  100. String childName = request.getParameter("childName");
  101. String lang = request.getParameter("lang");
  102. String vlog = request.getParameter("vlog");
  103. log.info("goEditScenePage---param--phoneNum:{},password:{},sceneNum:{},childName:{},lang:{},vlog:{}",
  104. phoneNum,password,sceneNum,childName,lang,vlog);
  105. ScenePro scene = sceneProService.getByNum(sceneNum);
  106. ScenePlus scenePlus = null;
  107. if(scene == null){
  108. scenePlus = scenePlusService.getByNum(sceneNum);
  109. }
  110. if(scene == null && scenePlus == null) {
  111. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  112. }
  113. Long userId = scene == null ? scenePlus.getUserId() : scene.getUserId();
  114. String webSite = scene == null ? scenePlusExtService.getByPlusId(scenePlus.getId()).getWebSite() : scene.getWebSite();
  115. if(StringUtils.isNotEmpty(childName)){
  116. Camera cameraEntity = cameraService.getByChildName(childName);
  117. if(cameraEntity != null){
  118. CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
  119. if(cameraDetailEntity != null && cameraDetailEntity.getCooperationUser() != null){
  120. throw new BusinessException(SceneConstant.FAILURE_CODE_5014, SceneConstant.FAILURE_MSG_5014);
  121. }
  122. UserVo user = new UserVo();
  123. if(userId == null){
  124. user.setUserName(childName);
  125. user.setCameraLogin(1);
  126. user.setCameraId(cameraEntity.getId());
  127. }else {
  128. User dbUser = userService.getById(userId);
  129. BeanUtils.copyProperties(dbUser,user);
  130. }
  131. sendResponse(user,webSite, request,response,sceneNum,lang,vlog);
  132. }
  133. }
  134. if(StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(password)
  135. || StringUtils.isEmpty(sceneNum)){
  136. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  137. }
  138. if(userId == null) {
  139. throw new BusinessException(SceneConstant.FAILURE_CODE_5013, SceneConstant.FAILURE_MSG_5013);
  140. }
  141. User user = userService.getByUserName(phoneNum);
  142. if(user == null){
  143. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  144. }
  145. UserVo ssoUser = new UserVo();
  146. BeanUtils.copyProperties(user,ssoUser);
  147. if(!ssoUser.getId().equals(userId)) {
  148. HashMap<String, User> cooMap = cooperationService.getByNumList(Arrays.asList(sceneNum));
  149. User cooUser = cooMap.get(sceneNum);
  150. if(cooUser == null ){
  151. throw new BusinessException(SceneConstant.FAILURE_CODE_5014, SceneConstant.FAILURE_MSG_5014);
  152. }
  153. if(cooUser.getId().longValue() != ssoUser.getId().longValue()){
  154. throw new BusinessException(SceneConstant.FAILURE_CODE_5014, SceneConstant.FAILURE_MSG_5014);
  155. }
  156. }
  157. if(!SecurityUtil.MD5(password).equals(ssoUser.getPassword())) {
  158. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  159. }
  160. sendResponse(ssoUser,webSite, request,response,sceneNum,lang,vlog);
  161. }
  162. private void sendResponse(UserVo user, String webSite, HttpServletRequest request,
  163. HttpServletResponse response, String sceneNum, String lang, String vlog) {
  164. String token = loginService.redisLogin(user.getUserName(),JSONObject.toJSONString(user),"user");
  165. //登录成功
  166. String mainUrl = NacosProperty.getMainUrl();
  167. if(StringUtils.isBlank(mainUrl)){
  168. mainUrl = "http://"+request.getRemoteHost() +"/";
  169. }
  170. try {
  171. String redirect = null;
  172. if(webSite.contains(sceneProUrl)){
  173. redirect = mainUrl + sceneProUrl.replace("show", "edit").replace("PC", "Mobile") +
  174. sceneNum + "&t=" +System.currentTimeMillis() + "&token=" + token + "&app" +
  175. (lang == null ? "" : "&lang=" + lang) + (vlog == null ? "" : "&vlog=" + vlog);
  176. }
  177. if(webSite.contains(sceneProV4Url)){
  178. redirect = mainUrl + sceneProV4Url.replace("s", "e") +
  179. sceneNum + "&t=" +System.currentTimeMillis() + "&token=" + token + "&app" +
  180. (lang == null ? "" : "&lang=" + lang) + (vlog == null ? "" : "&vlog=" + vlog);
  181. }
  182. if(webSite.contains(sceneProNewUrl)) {
  183. redirect = mainUrl + sceneProNewUrl.replace("s", "e") +
  184. sceneNum + "&t=" + System.currentTimeMillis() + "&token=" + token + "&app" +
  185. (lang == null ? "" : "&lang=" + lang) + (vlog == null ? "" : "&vlog=" + vlog);
  186. }
  187. log.info("goEditScenePage---response--website:{},redirect:{}", webSite,redirect);
  188. response.sendRedirect(redirect);
  189. }catch (Exception e){
  190. e.printStackTrace();
  191. }
  192. }
  193. //app调用 跳转到官网消费页面
  194. @RequestMapping("/goConsumptionPage")
  195. public void goConsumptionPage(HttpServletRequest request, HttpServletResponse response) throws Exception{
  196. String phoneNum = request.getParameter("phoneNum");
  197. String password = request.getParameter("password");
  198. String lang = request.getParameter("lang");
  199. password = URLEncoder.encode(password,"UTF-8");
  200. log.info("phoneNum:{},password:{},lang:{}",phoneNum,password,lang);
  201. if(StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(password)){
  202. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  203. }
  204. LoginParam param = new LoginParam();
  205. param.setPhoneNum(phoneNum);
  206. param.setPassword(password);
  207. LoginVo loginVo = loginService.loginClear(param);
  208. //登录成功
  209. String mainUrl = NacosProperty.getMainUrl();
  210. if(StringUtils.isBlank(mainUrl)){
  211. mainUrl = "http://"+request.getRemoteHost() +"/";
  212. }
  213. response.sendRedirect(mainUrl+ "mobile.html?token="+ loginVo.getToken() + "&app" + (lang == null ? "" : "&lang=" + lang)+"#/consumption/0" );
  214. }
  215. /**
  216. * 查找123看房未使用过的场景吗
  217. * */
  218. @PostMapping("/finSkSceneNum")
  219. public Result finSkSceneNum(){
  220. String num = scene3dNumService.generateSceneNum(null);
  221. return Result.success(num);
  222. }
  223. /**
  224. * 查找八目未使用过的场景吗*
  225. * */
  226. @PostMapping("/finSceneNum")
  227. public Result finSceneNum(){
  228. String num = scene3dNumService.generateSceneNum(null);
  229. return Result.success(num);
  230. }
  231. /**
  232. * 根据时间场景码等获取百度统量
  233. * @param param
  234. * @return
  235. */
  236. @PostMapping(value = "/statistics/findByTime")
  237. public Result findByTime(@RequestBody RequestSceneStatistics param){
  238. if(org.springframework.util.StringUtils.isEmpty(param.getSceneNum()) || param.getType() == null ||
  239. org.springframework.util.StringUtils
  240. .isEmpty(param.getStartDate()) || org.springframework.util.StringUtils.isEmpty(param.getEndDate())){
  241. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  242. }
  243. return Result.success(sceneStatisticsService.findByTime(
  244. DateUtil.string2Date(param.getStartDate(), DateUtil.YYYYMMDD_DATA_FORMAT),
  245. DateUtil.string2Date(param.getEndDate(), DateUtil.YYYYMMDD_DATA_FORMAT),
  246. param.getSceneNum(), param.getType()));
  247. }
  248. /**
  249. * 下载思为的html页面
  250. */
  251. @GetMapping(value = "/downloadSiWeiHtml")
  252. public Result downloadSiWeiHtml(HttpServletRequest request) throws Exception{
  253. String token = getToken();
  254. if(StringUtils.isEmpty(token)){
  255. throw new BusinessException(ErrorCode.FAILURE_CODE_3004);
  256. }
  257. User user = userService.getByToken(token);
  258. if(user == null){
  259. throw new BusinessException(ErrorCode.FAILURE_CODE_3004);
  260. }
  261. String sceneNum = request.getParameter("sceneNum");
  262. if(StringUtils.isEmpty(sceneNum)){
  263. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  264. }
  265. //获取getInfo的数据
  266. ScenePro sceneProEntity = sceneProService.getByNum(sceneNum);
  267. if(sceneProEntity == null){
  268. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  269. }
  270. if(sceneProEntity.getPayStatus() != 1){
  271. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  272. }
  273. if(sceneProEntity.getStatus() != 1 && sceneProEntity.getStatus() != -2){
  274. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  275. }
  276. if(user.getId().longValue() != sceneProEntity.getUserId().longValue()){
  277. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  278. }
  279. boolean lock = redisLockUtil.lock(RedisLockKey.LOCK_DOWNLOAD_SIWEI_HTML, RedisKey.EXPIRE_TIME_2_HOUR);
  280. if(!lock){
  281. throw new BusinessException(ErrorCode.FAILURE_CODE_5024, SceneConstant.FAILURE_MSG_5024);
  282. }
  283. try{
  284. SceneInfoVo responseScene = new SceneInfoVo();
  285. SceneProEdit editEntity = sceneProEditService.getByProId(sceneProEntity.getId());
  286. FileUtil.copyContent(new File(ConstantFilePath.BASE_PATH + "/local/localHose"), new File(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum), true);
  287. //14表示matterPro场景,需要调用命令获取切图,并且修改sceneScheme为13
  288. if(sceneProEntity.getSceneSource() == 14){
  289. sceneProEntity.setSceneScheme(13);
  290. RuntimeUtil.execForStr("node /opt/4dkankan_scene/index.js -s " + sceneProEntity.getNum() + " -d " + ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum);
  291. }
  292. BeanUtils.copyProperties(editEntity, responseScene);
  293. BeanUtils.copyProperties(sceneProEntity, responseScene);
  294. if(StrUtil.isNotEmpty(editEntity.getEntry())){
  295. responseScene.setEntry(JSONObject.parseObject(editEntity.getEntry()).toJSONString());
  296. }
  297. if(StrUtil.isNotEmpty(sceneProEntity.getGps())){
  298. responseScene.setGps(JSONObject.parseObject(sceneProEntity.getGps()).toJSONString());
  299. }
  300. responseScene.setCreateTime(sceneProEntity.getCreateTime());
  301. responseScene.setCreateDate(DateExtUtil.parse(sceneProEntity.getCreateTime(), DateExtUtil.dateStyle).getTime());
  302. if(StrUtil.isEmpty(editEntity.getSceneKey())){
  303. responseScene.setIsPublic(0);
  304. }else {
  305. responseScene.setIsPublic(1);
  306. }
  307. //不返回场景访问密码
  308. responseScene.setSceneKey("");
  309. //替换index.html里面的场景码
  310. String indexPath = ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + File.separator + "embed.html";
  311. String index = FileUtils.readFile(indexPath);
  312. FileUtils.deleteFile(indexPath);
  313. FileUtils.writeFile(indexPath, index.replace("__ProjectNum__", sceneNum));
  314. //下载oss资源
  315. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/data", "data/data" + sceneNum);
  316. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/images", "images/images" + sceneNum);
  317. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/video", "video/video" + sceneNum);
  318. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/voice", "voice/voice" + sceneNum);
  319. FileUtils.writeFile(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/data/data" + sceneNum + "/getInfo.json",
  320. JSONObject.toJSONString(responseScene));
  321. ZipUtil.zip(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum, ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + ".zip");
  322. FileUtils.deleteDirectory(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum);
  323. return Result.success((Object)"local/localHose" + sceneNum + ".zip");
  324. }catch (Exception e){
  325. log.error("思维下载失败,num=" + sceneNum, e);
  326. return Result.failure(LoginConstant.ERROR_MSG);
  327. }finally {
  328. redisLockUtil.unlockLua(RedisLockKey.LOCK_DOWNLOAD_SIWEI_HTML);
  329. }
  330. }
  331. /**
  332. * 内部使用查询场景名称和场景码
  333. */
  334. @RequestMapping(value = "/getScenesBySnCode", method = RequestMethod.GET)
  335. public Result getScenesBySnCode(String snCode) throws Exception{
  336. return sceneApiService.getScenesBySnCode(snCode,getToken());
  337. }
  338. /**
  339. * 思为获取场景列表
  340. */
  341. @RequestMapping(value = "/siweiSceneList", method = RequestMethod.GET)
  342. public Result siweiSceneList(HttpServletRequest request) throws Exception{
  343. return Result.success(sceneApiService.siweiSceneList(request,getToken()));
  344. }
  345. /**
  346. * 场景浏览
  347. * @return
  348. */
  349. @RequestMapping(value = "/loadAllScene", method = RequestMethod.POST)
  350. public Result loadAllScene(@RequestBody JSONObject param) throws Exception {
  351. return Result.success(sceneApiService.loadAllScene(param));
  352. }
  353. /**
  354. * 增加场景下载次数
  355. */
  356. @RequestMapping(value = "/addDownloadNum", method = RequestMethod.GET)
  357. public Result addDownloadNum(HttpServletRequest request) throws Exception{
  358. sceneApiService.addDownloadNum(request.getParameter("sceneNum"));
  359. return Result.success();
  360. }
  361. /**
  362. * 获取编辑页面的资源下载路径
  363. */
  364. @RequestMapping(value = "/getEditDataUrl")
  365. public Result getEditDataUrl(HttpServletRequest request) throws Exception{
  366. List<String> result = sceneApiService.getEditDataUrl(request.getParameter("num"));
  367. return Result.success(result);
  368. }
  369. /**
  370. * 查询场景资源路径
  371. */
  372. @GetMapping("/querySceneDataSource")
  373. public String querySceneDataSource(String num){
  374. return innerService.querySceneDataSource(num);
  375. }
  376. /**
  377. *
  378. * 获取场景描述等信息
  379. */
  380. @RequestMapping(value = "/getSceneDetail", method = RequestMethod.GET)
  381. public Result getSceneDetail(String sceneNum) throws Exception{
  382. String token = getToken();
  383. if(StringUtils.isEmpty(token)){
  384. throw new BusinessException(3004, "无token参数");
  385. }
  386. User user = userService.getByToken(token);
  387. if(user == null){
  388. throw new BusinessException(3004, "token参数不正确");
  389. }
  390. if(StringUtils.isEmpty(sceneNum)){
  391. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  392. }
  393. ScenePro pro = sceneProService.getByNum(sceneNum);
  394. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  395. if(pro == null && plus == null){
  396. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  397. }
  398. Long cameraId = pro == null ? plus.getCameraId() : pro.getCameraId();
  399. String snCode = null;
  400. if(!ObjectUtils.isEmpty(cameraId)){
  401. Camera cameraEntity = cameraService.getById(cameraId);
  402. if(ObjectUtils.isEmpty(cameraEntity)){
  403. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  404. }
  405. snCode = cameraEntity.getSnCode();
  406. }
  407. Map<String, Object> map = new HashMap<>();
  408. String webSite = null;
  409. String thumb = null;
  410. String sceneName = null;
  411. String sceneDec = null;
  412. String createDate = null;
  413. if(pro != null){
  414. webSite = pro.getWebSite();
  415. thumb = pro.getThumb();
  416. sceneName = pro.getSceneName();
  417. sceneDec = pro.getSceneDec();
  418. createDate = pro.getCreateTime();
  419. }
  420. if(plus != null){
  421. sceneName = plus.getTitle();
  422. createDate = plus.getCreateTime();
  423. sceneDec = plus.getDescription();
  424. ScenePlusExt plusExt = scenePlusExtService.getByPlusId(plus.getId());
  425. if(plusExt != null){
  426. webSite = plusExt.getWebSite();
  427. thumb = plusExt.getThumb();
  428. }
  429. }
  430. map.put("webSite", webSite);
  431. map.put("thumb", thumb);
  432. map.put("num", sceneNum);
  433. map.put("sceneName", sceneName);
  434. map.put("sceneDec", sceneDec);
  435. map.put("snCode", snCode);
  436. map.put("createDate", DateUserUtil.getDate(createDate).getTime());
  437. map.put("createTime", createDate);
  438. return Result.success(map);
  439. }
  440. }