GetMallTokenCmd.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.java110.job.cmd.mall;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.core.annotation.Java110Cmd;
  4. import com.java110.core.context.CmdContextUtils;
  5. import com.java110.core.context.ICmdDataFlowContext;
  6. import com.java110.core.event.cmd.Cmd;
  7. import com.java110.core.event.cmd.CmdEvent;
  8. import com.java110.dto.store.StoreDto;
  9. import com.java110.dto.store.StoreUserDto;
  10. import com.java110.dto.user.UserDto;
  11. import com.java110.intf.store.IStoreUserV1InnerServiceSMO;
  12. import com.java110.intf.store.IStoreV1InnerServiceSMO;
  13. import com.java110.intf.user.IUserV1InnerServiceSMO;
  14. import com.java110.job.adapt.hcIot.IotConstant;
  15. import com.java110.job.mall.ISendMall;
  16. import com.java110.utils.cache.MappingCache;
  17. import com.java110.utils.exception.CmdException;
  18. import com.java110.utils.util.Assert;
  19. import com.java110.vo.ResultVo;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import java.text.ParseException;
  22. import java.util.List;
  23. @Java110Cmd(serviceCode = "mall.getMallToken")
  24. public class GetMallTokenCmd extends Cmd {
  25. @Autowired
  26. private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;
  27. @Autowired
  28. private IStoreUserV1InnerServiceSMO storeUserV1InnerServiceSMOImpl;
  29. @Autowired
  30. private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;
  31. public static final String URL_DOMAIN = "URL_DOMAIN"; // 物联网域
  32. public static final String MALL_PC_URL = "MALL_PC_URL"; // 物联网域
  33. @Autowired
  34. private ISendMall sendMallImpl;
  35. @Override
  36. public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
  37. Assert.hasKeyAndValue(reqJson, "targetUrl", "未包含targetUrl");
  38. Assert.hasKeyAndValue(reqJson, "communityId", "未包含communityId");
  39. String mallSwitch = MappingCache.getValue("MALL", "MALL_SWITCH");
  40. if (!"ON".equals(mallSwitch)) {
  41. throw new CmdException("商城系统未部署");
  42. }
  43. }
  44. @Override
  45. public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
  46. String userId = CmdContextUtils.getUserId(context);
  47. UserDto userDto = new UserDto();
  48. userDto.setUserId(userId);
  49. List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
  50. Assert.listOnlyOne(userDtos, "用户未登录");
  51. StoreUserDto storeUserDto = new StoreUserDto();
  52. storeUserDto.setUserId(userId);
  53. List<StoreUserDto> storeUserDtos = storeUserV1InnerServiceSMOImpl.queryStoreUsers(storeUserDto);
  54. Assert.listOnlyOne(storeUserDtos, "未包含商户");
  55. StoreDto storeDto = new StoreDto();
  56. storeDto.setStoreId(storeUserDtos.get(0).getStoreId());
  57. List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);
  58. Assert.listOnlyOne(storeDtos, "商户不存在");
  59. JSONObject staff = new JSONObject();
  60. staff.put("tel", userDtos.get(0).getTel());
  61. staff.put("storeName", storeDtos.get(0).getName());
  62. staff.put("storeId", storeDtos.get(0).getStoreId());
  63. ResultVo resultVo = sendMallImpl.post("/mall/api/token.getSsoToken", staff);
  64. if (resultVo.getCode() != ResultVo.CODE_OK) {
  65. throw new CmdException(resultVo.getMsg());
  66. }
  67. JSONObject paramOut = (JSONObject) resultVo.getData();
  68. String mallUrl = MappingCache.getValue(URL_DOMAIN, MALL_PC_URL);
  69. String targetUrl = mallUrl + reqJson.getString("targetUrl");
  70. String url = mallUrl + paramOut.getString("url") + "&targetUrl=" + targetUrl;
  71. paramOut.put("url", url);
  72. context.setResponseEntity(ResultVo.createResponseEntity(paramOut));
  73. }
  74. }