AppUserController.java 19 KB

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