package com.hfkj.controller; import com.alibaba.fastjson.JSONObject; import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; import com.hfkj.common.security.UserCenter; import com.hfkj.common.utils.DateUtil; import com.hfkj.common.utils.ResponseMsgUtil; import com.hfkj.entity.BsOrder; import com.hfkj.entity.BsUser; import com.hfkj.model.*; import com.hfkj.service.elm.ElmService; import com.hfkj.service.goods.GoodsDataService; import com.hfkj.service.order.BsOrderService; import com.hfkj.service.pdd.PddService; import com.hfkj.service.taobao.TaoBaoService; import com.hfkj.service.user.BsUserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @Controller @RequestMapping(value="/goods") @Api(value="商品") public class GoodsController { private static Logger log = LoggerFactory.getLogger(GoodsController.class); @Resource private GoodsDataService goodsDataService; @Autowired private UserCenter userCenter; @Resource private BsUserService bsUserService; @RequestMapping(value="/goodsList",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "查询商品列表") public ResponseData goodsList(@RequestParam(value = "title" , required = false) String title, @RequestParam(value = "type" , required = true) Integer type, @RequestParam(value = "pageNo" , required = true) Long pageNo, @RequestParam(value = "pageSize" , required = true) Long pageSize ) { try { List goodsList; // 用户session UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); // 1:淘宝 2:拼多多 if (type == 1) { JSONObject jsonObject = TaoBaoService.material(title , pageNo , pageSize ); if (!jsonObject.getBoolean("success")) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败!"); } goodsList = goodsDataService.goodsModelTaoBaoList(jsonObject); } else if (type == 2) { Map mapUser = new JSONObject(); mapUser.put("uid", userSession.getUser().getId()); JSONObject object = PddService.authority(mapUser.toString()); boolean generateAuthorityUrl = object.getJSONObject("authorityQueryResponse").getInteger("bind") == 0; if (generateAuthorityUrl && title != null) { goodsList = new ArrayList<>(); JSONObject jsonObject = PddService.promotion(mapUser.toString()); JSONObject promotion = jsonObject.getJSONObject("rpPromotionUrlGenerateResponse").getJSONArray("urlList").getJSONObject(0); GoodsModel goodsModel = new GoodsModel(); goodsModel.setPddUrl(promotion); goodsList.add(goodsModel); } else { if (pageSize < 10) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR , "pageSize的取值范围是10-100!"); } JSONObject jsonObject = PddService.syncInvoke(title , pageNo , pageSize , mapUser.toString()); goodsList = goodsDataService.goodsModelPddList(jsonObject); } } else { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR , "未知类型!"); } return ResponseMsgUtil.success(goodsList); } catch (Exception e) { log.error("error!",e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value="/createCommand",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "淘口令生成") public ResponseData createCommand(@RequestParam(value = "url" , required = false) String url ) { try { JSONObject jsonObject = TaoBaoService.createCommand("https:" + url); if (jsonObject.getBoolean("success")) { return ResponseMsgUtil.success(jsonObject.getJSONObject("data")); } else { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败!"); } } catch (Exception e) { log.error("error!",e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value="/publisher",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "淘宝客-公用-私域用户备案") public ResponseData publisher(@RequestParam(value = "code" , required = false) String code ) { try { // 用户session UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); JSONObject tokenJson = TaoBaoService.getToken(code); String token = tokenJson.getString("token_result"); JSONObject tokenResult = JSONObject.parseObject(token); // 用户备案 JSONObject jsonObject = TaoBaoService.publisher(tokenResult.getString("access_token") , "5FM9A9"); String body = jsonObject.getString("body"); JSONObject result = JSONObject.parseObject(body); // 渠道备案 JSONObject relationObject = TaoBaoService.publisher(tokenResult.getString("access_token") , "JIIIVF"); String relation = relationObject.getString("body"); JSONObject relationBody = JSONObject.parseObject(relation); BsUser user = bsUserService.getUser(userSession.getUser().getId()); user.setSpecialId(result.getJSONObject("data").getString("special_id")); user.setRelationId(relationBody.getJSONObject("data").getString("relation_id")); bsUserService.updateInfo(user); return ResponseMsgUtil.success("绑定成功"); } catch (Exception e) { log.error("error!",e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value="/getPublisherInfo",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "淘宝客-公用-私域用户备案信息查询") public ResponseData getPublisherInfo(@RequestParam(value = "code" , required = false) String code ) { try { // 用户session UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); JSONObject tokenJson = TaoBaoService.getToken(code); String token = tokenJson.getString("token_result"); JSONObject tokenResult = JSONObject.parseObject(token); BsUser user = bsUserService.getUser(userSession.getUser().getId()); // 用户备案 JSONObject jsonObject = TaoBaoService.getPublisherInfo(tokenResult.getString("access_token") , Long.valueOf(user.getRelationId()), user.getSpecialId()); return ResponseMsgUtil.success(jsonObject); } catch (Exception e) { log.error("error!",e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value="/getOrderList",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "查询订单") public ResponseData getOrderList(@RequestParam(value = "code" , required = false) String code ) { try { return ResponseMsgUtil.success(ElmService.elmOrderList("2024-10-20 09:34:00" , "2024-11-04 12:34:00").getJSONArray("result")); } catch (Exception e) { log.error("error!",e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value="/generalLink",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "淘宝客-公用-私域用户备案信息查询") public ResponseData generalLink( @RequestParam(value = "itemId" , required = false) String itemId, @RequestParam(value = "url" , required = false) String url ) { try { // 用户session UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); // 用户备案 JSONObject jsonObject = TaoBaoService.generalLink(itemId, url, userSession.getUser().getRelationId()); return ResponseMsgUtil.success(jsonObject); } catch (Exception e) { log.error("error!",e); return ResponseMsgUtil.exception(e); } } }