AppUserController.java 19 KB

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