SceneApiController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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.DateExtUtil;
  11. import com.fdkankan.common.util.DateUtil;
  12. import com.fdkankan.common.util.FileUtils;
  13. import com.fdkankan.common.util.SecurityUtil;
  14. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  15. import com.fdkankan.redis.constant.RedisKey;
  16. import com.fdkankan.redis.constant.RedisLockKey;
  17. import com.fdkankan.redis.util.RedisLockUtil;
  18. import com.fdkankan.ucenter.common.BaseController;
  19. import com.fdkankan.ucenter.common.Result;
  20. import com.fdkankan.ucenter.common.constants.ConstantFilePath;
  21. import com.fdkankan.ucenter.constant.LoginConstant;
  22. import com.fdkankan.ucenter.entity.Camera;
  23. import com.fdkankan.ucenter.entity.CameraDetail;
  24. import com.fdkankan.ucenter.entity.ScenePlus;
  25. import com.fdkankan.ucenter.entity.ScenePro;
  26. import com.fdkankan.ucenter.entity.SceneProEdit;
  27. import com.fdkankan.ucenter.entity.User;
  28. import com.fdkankan.ucenter.service.ICameraDetailService;
  29. import com.fdkankan.ucenter.service.ICameraService;
  30. import com.fdkankan.ucenter.service.IScene3dNumService;
  31. import com.fdkankan.ucenter.service.ISceneCooperationService;
  32. import com.fdkankan.ucenter.service.IScenePlusExtService;
  33. import com.fdkankan.ucenter.service.IScenePlusService;
  34. import com.fdkankan.ucenter.service.ISceneProEditService;
  35. import com.fdkankan.ucenter.service.ISceneProService;
  36. import com.fdkankan.ucenter.service.ISceneStatisticsService;
  37. import com.fdkankan.ucenter.service.IUserService;
  38. import com.fdkankan.ucenter.service.impl.LoginService;
  39. import com.fdkankan.ucenter.vo.request.LoginParam;
  40. import com.fdkankan.ucenter.vo.request.RequestSceneStatistics;
  41. import com.fdkankan.ucenter.vo.response.LoginVo;
  42. import com.fdkankan.ucenter.vo.response.SceneInfoVo;
  43. import com.fdkankan.ucenter.vo.response.UserVo;
  44. import java.io.File;
  45. import java.net.URLEncoder;
  46. import java.util.Arrays;
  47. import java.util.HashMap;
  48. import javax.servlet.http.HttpServletRequest;
  49. import javax.servlet.http.HttpServletResponse;
  50. import lombok.extern.slf4j.Slf4j;
  51. import org.apache.commons.lang3.StringUtils;
  52. import org.springframework.beans.BeanUtils;
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.beans.factory.annotation.Value;
  55. import org.springframework.web.bind.annotation.GetMapping;
  56. import org.springframework.web.bind.annotation.PostMapping;
  57. import org.springframework.web.bind.annotation.RequestBody;
  58. import org.springframework.web.bind.annotation.RequestMapping;
  59. import org.springframework.web.bind.annotation.RequestParam;
  60. import org.springframework.web.bind.annotation.RestController;
  61. @Slf4j
  62. @RestController
  63. @RequestMapping("/scene")
  64. public class SceneApiController extends BaseController {
  65. @Autowired
  66. ISceneProService sceneProService;
  67. @Autowired
  68. IScenePlusService scenePlusService;
  69. @Autowired
  70. IScenePlusExtService scenePlusExtService;
  71. @Autowired
  72. IUserService userService;
  73. @Autowired
  74. LoginService loginService;
  75. @Autowired
  76. ICameraService cameraService;
  77. @Autowired
  78. ICameraDetailService cameraDetailService;
  79. @Autowired
  80. ISceneCooperationService cooperationService;
  81. @Autowired
  82. private IScene3dNumService scene3dNumService;
  83. @Autowired
  84. private ISceneStatisticsService sceneStatisticsService;
  85. @Autowired
  86. private RedisLockUtil redisLockUtil;
  87. @Autowired
  88. private ISceneProEditService sceneProEditService;
  89. @Autowired
  90. private FYunFileServiceInterface fYunFileService;
  91. @Value("${main.url}")
  92. private String mainUrl;
  93. @Value("${scene.url}")
  94. private String sceneUrl;
  95. @Value("${scene.pro.url}")
  96. private String sceneProUrl;
  97. @Value("${scene.pro.new.url}")
  98. private String sceneProNewUrl;
  99. @Value("${scene.v4.url}")
  100. private String sceneProV4Url;
  101. /**
  102. * 获取指定场景信息
  103. * @param num 场景码
  104. */
  105. @RequestMapping("/getInfo")
  106. public Result getInfo(@RequestParam(required = false) String num){
  107. return Result.success( sceneProService.getInfo(num) );
  108. }
  109. //跳转到编辑页面
  110. @RequestMapping("/goEditScenePage")
  111. public void goEditScenePage(HttpServletRequest request, HttpServletResponse response) throws Exception{
  112. String phoneNum = request.getParameter("phoneNum");
  113. String password = request.getParameter("password");
  114. String sceneNum = request.getParameter("sceneNum");
  115. String childName = request.getParameter("childName");
  116. String lang = request.getParameter("lang");
  117. String vlog = request.getParameter("vlog");
  118. log.info("goEditScenePage---param--phoneNum:{},password:{},sceneNum:{},childName:{},lang:{},vlog:{}",
  119. phoneNum,password,sceneNum,childName,lang,vlog);
  120. ScenePro scene = sceneProService.getByNum(sceneNum);
  121. ScenePlus scenePlus = null;
  122. if(scene == null){
  123. scenePlus = scenePlusService.getByNum(sceneNum);
  124. }
  125. if(scene == null && scenePlus == null) {
  126. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  127. }
  128. Long userId = scene == null ? scenePlus.getUserId() : scene.getUserId();
  129. String webSite = scene == null ? scenePlusExtService.getByPlusId(scenePlus.getId()).getWebSite() : scene.getWebSite();
  130. if(StringUtils.isNotEmpty(childName)){
  131. Camera cameraEntity = cameraService.getByChildName(childName);
  132. if(cameraEntity != null){
  133. CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
  134. if(cameraDetailEntity != null && cameraDetailEntity.getCooperationUser() != null){
  135. throw new BusinessException(SceneConstant.FAILURE_CODE_5014, SceneConstant.FAILURE_MSG_5014);
  136. }
  137. UserVo user = new UserVo();
  138. if(userId == null){
  139. user.setUserName(childName);
  140. user.setCameraLogin(1);
  141. user.setCameraId(cameraEntity.getId());
  142. }else {
  143. User dbUser = userService.getById(userId);
  144. BeanUtils.copyProperties(dbUser,user);
  145. }
  146. sendResponse(user,webSite, response,sceneNum,lang,vlog);
  147. }
  148. }
  149. if(StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(password)
  150. || StringUtils.isEmpty(sceneNum)){
  151. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  152. }
  153. if(userId == null) {
  154. throw new BusinessException(SceneConstant.FAILURE_CODE_5013, SceneConstant.FAILURE_MSG_5013);
  155. }
  156. User user = userService.getByUserName(phoneNum);
  157. if(user == null){
  158. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  159. }
  160. UserVo ssoUser = new UserVo();
  161. BeanUtils.copyProperties(user,ssoUser);
  162. if(!ssoUser.getId().equals(userId)) {
  163. HashMap<String, User> cooMap = cooperationService.getByNumList(Arrays.asList(sceneNum));
  164. User cooUser = cooMap.get(sceneNum);
  165. if(cooUser == null ){
  166. throw new BusinessException(SceneConstant.FAILURE_CODE_5014, SceneConstant.FAILURE_MSG_5014);
  167. }
  168. if(cooUser.getId().longValue() != ssoUser.getId().longValue()){
  169. throw new BusinessException(SceneConstant.FAILURE_CODE_5014, SceneConstant.FAILURE_MSG_5014);
  170. }
  171. }
  172. if(!SecurityUtil.MD5(password).equals(ssoUser.getPassword())) {
  173. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  174. }
  175. sendResponse(ssoUser,webSite, response,sceneNum,lang,vlog);
  176. }
  177. private void sendResponse(UserVo user, String webSite,
  178. HttpServletResponse response, String sceneNum, String lang, String vlog) {
  179. String token = loginService.redisLogin(user.getUserName(),JSONObject.toJSONString(user));
  180. //登录成功
  181. try {
  182. String redirect = null;
  183. if(webSite.contains(sceneProUrl)){
  184. redirect = mainUrl + sceneProUrl.replace("show", "edit").replace("PC", "Mobile") +
  185. sceneNum + "&t=" +System.currentTimeMillis() + "&token=" + token + "&app" +
  186. (lang == null ? "" : "&lang=" + lang) + (vlog == null ? "" : "&vlog=" + vlog);
  187. }
  188. if(webSite.contains(sceneProV4Url)){
  189. redirect = mainUrl + sceneProV4Url.replace("s", "e") +
  190. sceneNum + "&t=" +System.currentTimeMillis() + "&token=" + token + "&app" +
  191. (lang == null ? "" : "&lang=" + lang) + (vlog == null ? "" : "&vlog=" + vlog);
  192. }
  193. if(webSite.contains(sceneProNewUrl)) {
  194. redirect = mainUrl + sceneProNewUrl.replace("s", "e") +
  195. sceneNum + "&t=" + System.currentTimeMillis() + "&token=" + token + "&app" +
  196. (lang == null ? "" : "&lang=" + lang) + (vlog == null ? "" : "&vlog=" + vlog);
  197. }
  198. log.info("goEditScenePage---response--website:{},redirect:{}", webSite,redirect);
  199. response.sendRedirect(redirect);
  200. }catch (Exception e){
  201. e.printStackTrace();
  202. }
  203. }
  204. //app调用 跳转到官网消费页面
  205. @RequestMapping("/goConsumptionPage")
  206. public void goConsumptionPage(HttpServletRequest request, HttpServletResponse response) throws Exception{
  207. String phoneNum = request.getParameter("phoneNum");
  208. String password = request.getParameter("password");
  209. String lang = request.getParameter("lang");
  210. password = URLEncoder.encode(password,"UTF-8");
  211. log.info("phoneNum:{},password:{},lang:{}",phoneNum,password,lang);
  212. if(StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(password)){
  213. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  214. }
  215. LoginParam param = new LoginParam();
  216. param.setPhoneNum(phoneNum);
  217. param.setPassword(password);
  218. LoginVo loginVo = loginService.loginClear(param);
  219. //登录成功
  220. response.sendRedirect(mainUrl + "mobile.html?token="+ loginVo.getToken() + "&app" + (lang == null ? "" : "&lang=" + lang)+"#/consumption/0" );
  221. }
  222. /**
  223. * 查找123看房未使用过的场景吗
  224. * */
  225. @PostMapping("/finSkSceneNum")
  226. public Result finSkSceneNum(){
  227. String num = scene3dNumService.generateSceneNum(null);
  228. return Result.success(num);
  229. }
  230. /**
  231. * 查找八目未使用过的场景吗*
  232. * */
  233. @PostMapping("/finSceneNum")
  234. public Result finSceneNum(){
  235. String num = scene3dNumService.generateSceneNum(null);
  236. return Result.success(num);
  237. }
  238. /**
  239. * 根据时间场景码等获取百度统量
  240. * @param param
  241. * @return
  242. */
  243. @PostMapping(value = "/statistics/findByTime")
  244. public Result findByTime(@RequestBody RequestSceneStatistics param){
  245. if(org.springframework.util.StringUtils.isEmpty(param.getSceneNum()) || param.getType() == null ||
  246. org.springframework.util.StringUtils
  247. .isEmpty(param.getStartDate()) || org.springframework.util.StringUtils.isEmpty(param.getEndDate())){
  248. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  249. }
  250. return Result.success(sceneStatisticsService.findByTime(
  251. DateUtil.string2Date(param.getStartDate(), DateUtil.YYYYMMDD_DATA_FORMAT),
  252. DateUtil.string2Date(param.getEndDate(), DateUtil.YYYYMMDD_DATA_FORMAT),
  253. param.getSceneNum(), param.getType()));
  254. }
  255. /**
  256. * 下载思为的html页面
  257. */
  258. @GetMapping(value = "/downloadSiWeiHtml")
  259. public Result downloadSiWeiHtml(HttpServletRequest request) throws Exception{
  260. String token = getToken();
  261. if(StringUtils.isEmpty(token)){
  262. throw new BusinessException(ErrorCode.FAILURE_CODE_3004);
  263. }
  264. User user = userService.getByToken(token);
  265. if(user == null){
  266. throw new BusinessException(ErrorCode.FAILURE_CODE_3004);
  267. }
  268. String sceneNum = request.getParameter("sceneNum");
  269. if(StringUtils.isEmpty(sceneNum)){
  270. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  271. }
  272. //获取getInfo的数据
  273. ScenePro sceneProEntity = sceneProService.getByNum(sceneNum);
  274. if(sceneProEntity == null){
  275. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  276. }
  277. if(sceneProEntity.getPayStatus() != 1){
  278. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  279. }
  280. if(sceneProEntity.getStatus() != 1 && sceneProEntity.getStatus() != -2){
  281. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  282. }
  283. if(user.getId().longValue() != sceneProEntity.getUserId().longValue()){
  284. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  285. }
  286. boolean lock = redisLockUtil.lock(RedisLockKey.LOCK_DOWNLOAD_SIWEI_HTML, RedisKey.EXPIRE_TIME_2_HOUR);
  287. if(!lock){
  288. throw new BusinessException(ErrorCode.FAILURE_CODE_5024, SceneConstant.FAILURE_MSG_5024);
  289. }
  290. try{
  291. SceneInfoVo responseScene = new SceneInfoVo();
  292. SceneProEdit editEntity = sceneProEditService.getByProId(sceneProEntity.getId());
  293. FileUtil.copyContent(new File(ConstantFilePath.BASE_PATH + "/local/localHose"), new File(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum), true);
  294. //14表示matterPro场景,需要调用命令获取切图,并且修改sceneScheme为13
  295. if(sceneProEntity.getSceneSource() == 14){
  296. sceneProEntity.setSceneScheme(13);
  297. RuntimeUtil.execForStr("node /opt/4dkankan_scene/index.js -s " + sceneProEntity.getNum() + " -d " + ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum);
  298. }
  299. BeanUtils.copyProperties(editEntity, responseScene);
  300. BeanUtils.copyProperties(sceneProEntity, responseScene);
  301. if(StrUtil.isNotEmpty(editEntity.getEntry())){
  302. responseScene.setEntry(JSONObject.parseObject(editEntity.getEntry()).toJSONString());
  303. }
  304. if(StrUtil.isNotEmpty(sceneProEntity.getGps())){
  305. responseScene.setGps(JSONObject.parseObject(sceneProEntity.getGps()).toJSONString());
  306. }
  307. responseScene.setCreateTime(sceneProEntity.getCreateTime());
  308. responseScene.setCreateDate(DateExtUtil.parse(sceneProEntity.getCreateTime(), DateExtUtil.dateStyle).getTime());
  309. if(StrUtil.isEmpty(editEntity.getSceneKey())){
  310. responseScene.setIsPublic(0);
  311. }else {
  312. responseScene.setIsPublic(1);
  313. }
  314. //不返回场景访问密码
  315. responseScene.setSceneKey("");
  316. //替换index.html里面的场景码
  317. String indexPath = ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + File.separator + "embed.html";
  318. String index = FileUtils.readFile(indexPath);
  319. FileUtils.deleteFile(indexPath);
  320. FileUtils.writeFile(indexPath, index.replace("__ProjectNum__", sceneNum));
  321. //下载oss资源
  322. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/data", "data/data" + sceneNum);
  323. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/images", "images/images" + sceneNum);
  324. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/video", "video/video" + sceneNum);
  325. fYunFileService.downloadFileByCommand(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/voice", "voice/voice" + sceneNum);
  326. FileUtils.writeFile(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + "/data/data" + sceneNum + "/getInfo.json",
  327. JSONObject.toJSONString(responseScene));
  328. ZipUtil.zip(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum, ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum + ".zip");
  329. FileUtils.deleteDirectory(ConstantFilePath.BASE_PATH + "/local/localHose" + sceneNum);
  330. return Result.success((Object)"local/localHose" + sceneNum + ".zip");
  331. }catch (Exception e){
  332. log.error("思维下载失败,num=" + sceneNum, e);
  333. return Result.failure(LoginConstant.ERROR_MSG);
  334. }finally {
  335. redisLockUtil.unlockLua(RedisLockKey.LOCK_DOWNLOAD_SIWEI_HTML);
  336. }
  337. }
  338. }