3.4 加载秒杀商品实现
当前已经完成了秒杀时间段菜单的显示,那么当用户在切换不同的时间段的时候,需要按照用户所选择的时间去显示相对应时间段下的秒杀商品
3.4.1 秒杀服务-查询秒杀商品列表
@RestController
@RequestMapping("/seckillgoods")
public class SecKillGoodsController {
@Autowired
private SecKillGoodsService secKillGoodsService;
@RequestMapping("/list")
public Result<List<SeckillGoods>> list(@RequestParam("time") String time){
List<SeckillGoods> seckillGoodsList = secKillGoodsService.list(time);
return new Result<>(true, StatusCode.OK,"查询成功",seckillGoodsList);
}
}
3.4.1.2 秒杀服务-service&serviceImpl
public interface SecKillGoodsService {
List<SeckillGoods> list(String time);
}
@Service
public class SecKillGoodsServiceImpl implements SecKillGoodsService {
@Autowired
private RedisTemplate redisTemplate;
public static final String SECKILL_GOODS_KEY="seckill_goods_";
@Override
public List<SeckillGoods> list(String time) {
List<SeckillGoods> list = redisTemplate.boundHashOps(SECKILL_GOODS_KEY + time).values();
return list;
}
}
3.4.1.3 查询秒杀商品放行
秒杀微服务的ResourceServerConfig类,对查询方法放行
@Override
public void configure(HttpSecurity http) throws Exception {
//所有请求必须认证通过
http.authorizeRequests()
.antMatchers("/seckillgoods/list/**")
.permitAll()
.anyRequest()
.authenticated(); //其他地址需要认证授权
}
3.4.1.4 杀服务Api- feign接口定义
@FeignClient(name="seckill")
public interface SecKillFeign {
/**
* 查询秒杀商品列表
* @param time
* @return
*/
@RequestMapping("/seckillgoods/list")
public Result<List<SeckillGoods>> list(@RequestParam("time") String time);
}
3.4.2 秒杀渲染服务-查询秒杀商品列表
3.4.2.1 更新changgou_web_seckill的启动类
添加feign接口扫描
@EnableFeignClients(basePackages = "com.changgou.seckill.feign")
3.4.2.2 更新changgou_web_seckill的SecKillGoodsController
注入secKillFeign,并添加获取秒杀商品列表方法实现
/**
* 获取秒杀商品列表
* 默认当前时间
*/
@RequestMapping("/list")
@ResponseBody
public Result<List<SeckillGoods>> list(String time){
Result<List<SeckillGoods>> listResult =secKillFeign.list(DateUtil.formatStr(time));
return listResult;
}
3.4.2.3 更新secKill-index.html。添加按照时间查询方法
searchList:function (time) {
axios.get("/api/wseckillgoods/list?time="+time).then(function (response) {
app.goodslist= response.data.data;
})
},