AppUserController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. package com.goafanti.app.controller;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import javax.annotation.Resource;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.validation.Valid;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.apache.shiro.crypto.hash.SimpleHash;
  12. import org.springframework.beans.BeanUtils;
  13. import org.springframework.http.HttpMethod;
  14. import org.springframework.validation.BindingResult;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.goafanti.achievement.bo.InputAchievement;
  20. import com.goafanti.achievement.service.AchievementInterestService;
  21. import com.goafanti.achievement.service.AchievementService;
  22. import com.goafanti.activity.service.ActivityService;
  23. import com.goafanti.banners.service.BannersService;
  24. import com.goafanti.common.bo.Result;
  25. import com.goafanti.common.constant.AFTConstants;
  26. import com.goafanti.common.constant.ErrorConstants;
  27. import com.goafanti.common.controller.BaseApiController;
  28. import com.goafanti.common.enums.AchievementFields;
  29. import com.goafanti.common.enums.AttachmentType;
  30. import com.goafanti.common.enums.DemandFields;
  31. import com.goafanti.common.model.Achievement;
  32. import com.goafanti.common.model.Demand;
  33. import com.goafanti.common.model.MessageConsumer;
  34. import com.goafanti.common.model.MessageFromSystem;
  35. import com.goafanti.common.model.MessageProducer;
  36. import com.goafanti.common.model.User;
  37. import com.goafanti.common.service.FieldGlossoryService;
  38. import com.goafanti.common.utils.DateUtils;
  39. import com.goafanti.core.shiro.token.TokenManager;
  40. import com.goafanti.demand.bo.InputDemand;
  41. import com.goafanti.demand.service.DemandService;
  42. import com.goafanti.easemob.EasemobUtils;
  43. import com.goafanti.easemob.bo.EasemobInfo;
  44. import com.goafanti.message.bo.MessageBo;
  45. import com.goafanti.message.bo.MessageListBo;
  46. import com.goafanti.message.service.MessageService;
  47. import com.goafanti.news.service.NewsService;
  48. import com.goafanti.user.service.UserCareerService;
  49. import com.goafanti.user.service.UserIdentityService;
  50. import com.goafanti.user.service.UserInterestService;
  51. import com.goafanti.user.service.UserService;
  52. @RestController
  53. @RequestMapping(path = "/app/user", method = RequestMethod.GET)
  54. public class AppUserController extends BaseApiController {
  55. @Resource
  56. private UserService userServiceImpl;
  57. @Resource
  58. private MessageService messageService;
  59. @Resource
  60. private EasemobUtils easemobUtils;
  61. @Resource
  62. private BannersService bannersService;
  63. @Resource
  64. private ActivityService activityService;
  65. @Resource
  66. private NewsService newsService;
  67. @Resource
  68. private AchievementService achievementService;
  69. @Resource
  70. private DemandService demandService;
  71. @Resource
  72. private UserCareerService userCareerService;
  73. @Resource
  74. private UserInterestService userInterestService;
  75. @Resource
  76. private UserIdentityService userIdentityService;
  77. @Resource
  78. AchievementInterestService achievementInterestService;
  79. @Resource
  80. private FieldGlossoryService fieldGlossoryService;
  81. @RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
  82. public Result uploadAvatar(HttpServletRequest req){
  83. Result res = new Result();
  84. String headPortraitUrl = handleFile(res, "/avatar/", false, req, "");
  85. if(TokenManager.getToken() instanceof User){
  86. User u = new User();
  87. u.setId(TokenManager.getUserId());
  88. u.setHeadPortraitUrl(headPortraitUrl);
  89. userServiceImpl.updateByPrimaryKeySelective(u);
  90. }
  91. res.setData(headPortraitUrl);
  92. return res;
  93. }
  94. @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
  95. public Result updateUser(User u){
  96. Result res = new Result();
  97. if(StringUtils.isBlank(u.getNickname())){
  98. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"用户昵称不能为空"));
  99. return res;
  100. }
  101. u.setId(TokenManager.getUserId());
  102. userServiceImpl.updateByPrimaryKeySelective(u);
  103. return res;
  104. }
  105. @RequestMapping(value = "/logout",method = RequestMethod.GET)
  106. public Result logout(HttpServletRequest request){
  107. Result res = new Result();
  108. TokenManager.logout();
  109. return res;
  110. }
  111. @RequestMapping(value = "/userInfo",method = RequestMethod.GET)
  112. public Result userInfo(HttpServletRequest request){
  113. Result res = new Result();
  114. res.setData(userServiceImpl.selectBaseInfo());
  115. return res;
  116. }
  117. @RequestMapping(value = "/index", method = RequestMethod.GET)
  118. public Result index(){
  119. Result res = new Result();
  120. res.setData(messageService.selectMessageWithGroup());
  121. return res;
  122. }
  123. @RequestMapping(value = "/listMessage", method = RequestMethod.POST)
  124. public Result listMessage(Integer subject,Integer sourceType,Integer pageNo,Integer pageSize){
  125. Result res = new Result();
  126. if(null == subject){
  127. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "必须指定消息类型"));
  128. return res;
  129. }
  130. User u = TokenManager.getUserToken();
  131. if(u != null){
  132. List<MessageBo> boList = messageService.selectSyncMessage(u.getId());
  133. if(boList != null && boList.size()>0){
  134. List<MessageProducer> producers = new ArrayList<MessageProducer>();
  135. List<MessageConsumer> consumers = new ArrayList<MessageConsumer>();
  136. MessageProducer mp = null;
  137. MessageConsumer mc = null;
  138. Date date = new Date();
  139. for(MessageBo bo : boList){
  140. mp = new MessageProducer();
  141. mp.setId(bo.getId());
  142. mp.setSendTime(date);
  143. producers.add(mp);
  144. mc = new MessageConsumer();
  145. mc.setId(UUID.randomUUID().toString());
  146. mc.setConsumerId(TokenManager.getUserId());
  147. mc.setMessageId(bo.getMessageId());
  148. consumers.add(mc);
  149. }
  150. if(producers.size()>0)messageService.updateMessageProducer(producers);
  151. //新增 message_consumer
  152. if(consumers.size()>0)messageService.insertBatchConsumer(consumers);
  153. }
  154. }
  155. res.setData(messageService.listPersonalMessage(subject,sourceType,pageNo,pageSize));
  156. return res;
  157. }
  158. /**
  159. * 同步绑定账号
  160. * @param uuid
  161. * @param registrationId
  162. * @param userName
  163. */
  164. @RequestMapping(value = "/synBindingAccount",method = RequestMethod.POST)
  165. private Result synBindingAccount(String uuid,String registrationId,String easemobName,String easemobPass){
  166. Result res = new Result();
  167. //更新 jpush_easemob_account
  168. messageService.updateJpushEasemobAccount(uuid, registrationId, easemobName,easemobPass);
  169. return res;
  170. }
  171. /**
  172. * 读取消息
  173. * @param messageId
  174. * @throws IllegalAccessException
  175. * @throws InvocationTargetException
  176. */
  177. @RequestMapping(value = "/readMessage",method = RequestMethod.GET)
  178. private Result readMessage(String messageId){
  179. Result res = new Result();
  180. if(StringUtils.isBlank(messageId)){
  181. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号为空"));
  182. return res;
  183. }
  184. //读取消息详情
  185. MessageFromSystem mfs = messageService.selectSystemMessageDetail(messageId);
  186. MessageListBo bo = new MessageListBo();
  187. bo.setCreateTime(mfs.getCreateTime()==null?"":DateUtils.formatDate(mfs.getCreateTime(), AFTConstants.YYYYMMDDHHMMSS));
  188. bo.setResourceType(String.valueOf(mfs.getResourceType()));
  189. bo.setSubject(String.valueOf(mfs.getSubject()));
  190. bo.setMessageId(mfs.getId());
  191. bo.setTitle(mfs.getTitle());
  192. bo.setBody(mfs.getBody());
  193. res.setData(bo);
  194. //更新 message_consumer
  195. messageService.updateMessageConsumer(messageId);
  196. return res;
  197. }
  198. /**
  199. * 查询环信昵称和头像
  200. * @param easemobName
  201. * @return
  202. */
  203. @RequestMapping(value = "/getNicknameAndHeadPortrait",method = RequestMethod.GET)
  204. public Result getNicknameAndHeadPortrait(String easemobName){
  205. Result res = new Result();
  206. User user = userServiceImpl.selectByNumber(easemobName);
  207. JSONObject jsonObject = new JSONObject();
  208. jsonObject.put("nickName", user.getNickname());
  209. jsonObject.put("headPortraitUrl", null == user.getHeadPortraitUrl()?"":user.getHeadPortraitUrl());
  210. res.setData(jsonObject);
  211. return res;
  212. }
  213. /**
  214. * 关注功能
  215. */
  216. @RequestMapping(value = "/interestAdd", method = RequestMethod.GET)
  217. public Result Addinterest( Integer type,String objectId,String interest) {
  218. Result res = new Result();
  219. if (null==type) {
  220. res.getError().add(buildError( "分类错误!", "分类错误"));
  221. return res;
  222. }
  223. String uid=null;
  224. if (StringUtils.isNotBlank(TokenManager.getUserId())) {
  225. uid=TokenManager.getUserId();
  226. }
  227. if (Integer.valueOf(interest)==0) {
  228. res.setData(achievementInterestService.saveInterest( uid, type, objectId,interest));
  229. }else {
  230. res.setData(achievementInterestService.deleteInterest( uid, type, objectId,interest));
  231. }
  232. return res;
  233. }
  234. /**
  235. * 我的成果
  236. */
  237. @RequestMapping(value = "/achievementList", method = RequestMethod.GET)
  238. private Result achievementList(String pageNo, String pageSize) {
  239. Result res = new Result();
  240. Integer pNo = 1;
  241. Integer pSize = 10;
  242. if (StringUtils.isNumeric(pageSize)) {
  243. pSize = Integer.parseInt(pageSize);
  244. }
  245. if (StringUtils.isNumeric(pageNo)) {
  246. pNo = Integer.parseInt(pageNo);
  247. }
  248. res.setData(achievementService.listAppMyAchievement(pNo, pSize));
  249. return res;
  250. }
  251. /**
  252. * 我的需求
  253. */
  254. @RequestMapping(value = "/demandList", method = RequestMethod.GET)
  255. public Result demandList(String pageNo, String pageSize) {
  256. Result res = new Result();
  257. Integer pNo = 1;
  258. Integer pSize = 10;
  259. if (StringUtils.isNumeric(pageSize)) {
  260. pSize = Integer.parseInt(pageSize);
  261. }
  262. if (StringUtils.isNumeric(pageNo)) {
  263. pNo = Integer.parseInt(pageNo);
  264. }
  265. res.setData(demandService.listMyDemand( pNo, pSize));
  266. return res;
  267. }
  268. /**
  269. * 关注列表
  270. */
  271. @RequestMapping(value = "/interestList", method = RequestMethod.GET)
  272. public Result interestList(Integer type,String pageNo, String pageSize) {
  273. Result res = new Result();
  274. Integer pNo = 1;
  275. Integer pSize = 10;
  276. if (null==type||type<0||type>8) {
  277. res.getError().add(buildError( "类型选择错误", "类型选择错误"));
  278. }
  279. if (StringUtils.isNumeric(pageSize)) {
  280. pSize = Integer.parseInt(pageSize);
  281. }
  282. if (StringUtils.isNumeric(pageNo)) {
  283. pNo = Integer.parseInt(pageNo);
  284. }
  285. res.setData(demandService.selectinterest(type, pNo, pSize));
  286. return res;
  287. }
  288. /**
  289. * 专家详情
  290. */
  291. @RequestMapping(value = "/expertsDetail", method = RequestMethod.GET)
  292. public Result expertsDetail(String uid) {
  293. Result res = new Result();
  294. if (StringUtils.isBlank(uid)) {
  295. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"专家必须指定","专家"));
  296. return res;
  297. }
  298. res.setData(userIdentityService.expertsDetail( uid));
  299. return res;
  300. }
  301. /**
  302. * 成果详情
  303. * @param id
  304. * @return
  305. */
  306. @RequestMapping(value = "/achievementDetail", method = RequestMethod.GET)
  307. private Result userDetail(String id ) {
  308. Result res = new Result();
  309. if (StringUtils.isBlank(id)) {
  310. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"成果必须指定","成果"));
  311. return res;
  312. }
  313. res.setData(achievementService.selectAppUserOwnerDetail(id));
  314. return res;
  315. }
  316. /**
  317. * 需求详情
  318. */
  319. @RequestMapping(value = "/demandDetail", method = RequestMethod.GET)
  320. public Result DemandDetail(String id ) {
  321. Result res = new Result();
  322. if (StringUtils.isBlank(id)) {
  323. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"需求必须指定","需求"));
  324. return res;
  325. }
  326. res.setData(demandService.selectDemandDetail( id));
  327. return res;
  328. }
  329. /**
  330. * 新增成果
  331. */
  332. @RequestMapping(value = "/appAddAchievement", method = RequestMethod.POST)
  333. private Result appAddAchievement(@Valid InputAchievement ia, BindingResult bindingResult,
  334. String keyword1,String keyword2,String keyword3) {
  335. Result res = new Result();
  336. List<String> keyword=new ArrayList<>();
  337. if (!StringUtils.isBlank(keyword1)) {
  338. keyword.add(keyword1);
  339. }
  340. if (!StringUtils.isBlank(keyword2)) {
  341. keyword.add(keyword2);
  342. }
  343. if (!StringUtils.isBlank(keyword3)) {
  344. keyword.add(keyword3);
  345. }
  346. String[] keywords=(String[])keyword.toArray(new String[keyword.size()]);
  347. System.out.println(StringUtils.join(keywords,","));
  348. ia.setKeyword(StringUtils.join(keywords,","));
  349. if (bindingResult.hasErrors()) {
  350. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  351. AchievementFields.getFieldDesc(bindingResult.getFieldError().getField())));
  352. return res;
  353. }
  354. res = disposeInputAchievement(res, ia, keywords);
  355. if (!res.getError().isEmpty()) {
  356. return res;
  357. }
  358. Achievement a = new Achievement();
  359. BeanUtils.copyProperties(ia, a);
  360. res.setData(achievementService.saveAppAchievement(a, keywords));
  361. return res;
  362. }
  363. private Result disposeInputAchievement(Result res, InputAchievement ia, String[] keywords) {
  364. if (StringUtils.isBlank(ia.getName())) {
  365. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果名称", "成果名称"));
  366. return res;
  367. }
  368. if (null == ia.getCategory()) {
  369. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到数据类别", "数据类别"));
  370. return res;
  371. }
  372. if (StringUtils.isBlank(ia.getKeyword())) {
  373. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  374. return res;
  375. }
  376. if (null == keywords || keywords.length < 1) {
  377. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  378. return res;
  379. }
  380. if (null == ia.getCooperationMode()) {
  381. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到合作方式", "合作方式"));
  382. return res;
  383. }
  384. for (int i = 0; i < keywords.length; i++) {
  385. if (AFTConstants.KEYWORDLENTH < keywords[i].length()) {
  386. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "关键词长度"));
  387. return res;
  388. }
  389. }
  390. return res;
  391. }
  392. /**
  393. * 新增需求
  394. */
  395. @RequestMapping(value = "/appAddDemand", method = RequestMethod.POST)
  396. public Result appAddDemand(@Valid InputDemand demand, BindingResult bindingResult,
  397. String keyword1,String keyword2,String keyword3,
  398. String validityPeriodFormattedDate) {
  399. Result res = new Result();
  400. /*String[] keywords ;
  401. if (StringUtils.isNotEmpty(keyword3)) {
  402. keywords=new String[]{keyword1,keyword2};
  403. }else if (StringUtils.isNotEmpty(keyword2)) {
  404. keywords=new String[]{keyword1};
  405. }else {
  406. keywords=new String[]{keyword1,keyword2,keyword3};
  407. }
  408. if (null!=keywords) {
  409. demand.setKeyword(StringUtils.join(keywords,","));
  410. }*/
  411. if (bindingResult.hasErrors()) {
  412. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  413. DemandFields.getFieldDesc(bindingResult.getFieldError().getField())));
  414. return res;
  415. }
  416. res = disposeDemand(res, demand, null);
  417. if (!res.getError().isEmpty()) {
  418. return res;
  419. }
  420. Demand d = new Demand();
  421. BeanUtils.copyProperties(demand, d);
  422. if (StringUtils.isNotBlank(demand.getUrgentDays())) {
  423. d.setUrgentDays(demand.getUrgentDays());
  424. }
  425. if (null==demand.getUrgentMoney()) {
  426. d.setUrgentMoney(demand.getUrgentMoney());
  427. }
  428. res.setData(demandService.saveAppUserDemand(d, validityPeriodFormattedDate, null));
  429. return res;
  430. }
  431. private Result disposeDemand(Result res, InputDemand demand, String[] keywords) {
  432. if (StringUtils.isBlank(demand.getName())) {
  433. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求名称", "需求名称"));
  434. return res;
  435. }
  436. /*if (StringUtils.isBlank(demand.getKeyword())) {
  437. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  438. return res;
  439. }
  440. if (null == keywords || keywords.length < 1) {
  441. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  442. return res;
  443. }*/
  444. if (null == demand.getIndustryCategoryA()) {
  445. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到行业类别", "行业类别"));
  446. return res;
  447. }
  448. if (null == demand.getDemandType()) {
  449. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求类型", "需求类型"));
  450. return res;
  451. }
  452. if (StringUtils.isBlank(demand.getProblemDes())) {
  453. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到问题说明", "问题说明"));
  454. return res;
  455. }
  456. /*for (int i = 0; i < keywords.length; i++) {
  457. if (AFTConstants.KEYWORDLENTH < keywords[i].length()) {
  458. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "关键词长度"));
  459. return res;
  460. }
  461. }*/
  462. return res;
  463. }
  464. /**
  465. * 成果、需求图片上传
  466. */
  467. @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
  468. public Result uploadPicture(HttpServletRequest req, String sign) {
  469. Result res = new Result();
  470. AttachmentType attachmentType = AttachmentType.getField(sign);
  471. if (attachmentType == AttachmentType.ACHIEVEMENT_TECHNICAL_PICTURE) {
  472. res.setData(handleFiles(res, "/achievement/", false, req, sign, "achievement"));
  473. } else if (attachmentType==AttachmentType.DEMAND_PICTURE){
  474. res.setData(handleFiles(res, "/demand/", false, req, sign, "demand"));
  475. }else {
  476. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  477. }
  478. return res;
  479. }
  480. /**
  481. * 获取领域
  482. * @param pid
  483. * @param level
  484. * @return
  485. */
  486. @RequestMapping(value = "/getField", method = RequestMethod.POST)
  487. public Result getField(Integer pid, Integer level) {
  488. Result res = new Result();
  489. res.setData(fieldGlossoryService.getField(pid, level));
  490. return res;
  491. }
  492. /**
  493. * 获取环信登录账号,如果未注册则先注册
  494. */
  495. @RequestMapping(value = "/easemob", method = RequestMethod.GET)
  496. public Result getEasemob() {
  497. User u = TokenManager.getUserToken();
  498. if (u != null) {
  499. JSONObject res = easemobUtils.send(new EasemobInfo().uri("/users/" + u.getNumber()).method(HttpMethod.GET));
  500. JSONObject resultObj = new JSONObject();
  501. resultObj.put("easemobPass", new SimpleHash("md5", u.getId(), null, 1).toHex());
  502. resultObj.put("easemobName", String.valueOf(u.getNumber()));
  503. resultObj.put("nickname", StringUtils.isBlank(u.getNickname()) ? "技淘用户" + u.getNumber() : u.getNickname());
  504. if (res == null || StringUtils.equals("service_resource_not_found", (CharSequence) res.get("error"))) {
  505. easemobUtils.sendLater(
  506. new EasemobInfo().uri("/users/").data(resultObj.toJSONString()).method(HttpMethod.POST));
  507. }
  508. return res().data(resultObj);
  509. } else {
  510. return res().error(buildError("user only", "必须是登录会员才能访问。"));
  511. }
  512. }
  513. }