Cyan Blog Cyan Blog
首页
  • Java (opens new window)
  • JUC (opens new window)
  • JVM (opens new window)
  • Redis

    • Redis安装 (opens new window)
    • Redis基础 (opens new window)
    • Redis实战 (opens new window)
    • Redis集群安装 (opens new window)
    • Redis分布式缓存 (opens new window)
    • Redis多级缓存 (opens new window)
    • Redis原理 (opens new window)
  • 管理工具

    • Maven (opens new window)
    • Git (opens new window)
  • SSM

    • Spring (opens new window)
    • SpringBoot (opens new window)
    • Mybatis (opens new window)
    • MybatisPlus (opens new window)
  • 微服务

    • Docker (opens new window)
    • RabbitMQ (opens new window)
    • SpringCloud (opens new window)
    • Dubbo (opens new window)
    • MongoDB (opens new window)
    • Zookeeper (opens new window)
  • Java面试题 (opens new window)
  • JUC面试题 (opens new window)
  • JVM面试题 (opens new window)
  • Linux面试题 (opens new window)
  • SQL面试题 (opens new window)
  • Maven面试题 (opens new window)
  • Redis面试题 (opens new window)
  • SSM面试题 (opens new window)
  • SpringCloud面试题 (opens new window)
  • Linux (opens new window)
  • C++ (opens new window)
  • 数据库

    • MySQL (opens new window)
    • NoSQL (opens new window)
  • 软件测试

    • 软件测试 (opens new window)
  • 加密解密 (opens new window)
  • bilibili字幕提取 (opens new window)
  • 道理 (opens new window)
  • 关于博主

    • Github (opens new window)
    • CSDN (opens new window)
  • 关于本站

    • 如何搭建博客网站 (opens new window)
首页
  • Java (opens new window)
  • JUC (opens new window)
  • JVM (opens new window)
  • Redis

    • Redis安装 (opens new window)
    • Redis基础 (opens new window)
    • Redis实战 (opens new window)
    • Redis集群安装 (opens new window)
    • Redis分布式缓存 (opens new window)
    • Redis多级缓存 (opens new window)
    • Redis原理 (opens new window)
  • 管理工具

    • Maven (opens new window)
    • Git (opens new window)
  • SSM

    • Spring (opens new window)
    • SpringBoot (opens new window)
    • Mybatis (opens new window)
    • MybatisPlus (opens new window)
  • 微服务

    • Docker (opens new window)
    • RabbitMQ (opens new window)
    • SpringCloud (opens new window)
    • Dubbo (opens new window)
    • MongoDB (opens new window)
    • Zookeeper (opens new window)
  • Java面试题 (opens new window)
  • JUC面试题 (opens new window)
  • JVM面试题 (opens new window)
  • Linux面试题 (opens new window)
  • SQL面试题 (opens new window)
  • Maven面试题 (opens new window)
  • Redis面试题 (opens new window)
  • SSM面试题 (opens new window)
  • SpringCloud面试题 (opens new window)
  • Linux (opens new window)
  • C++ (opens new window)
  • 数据库

    • MySQL (opens new window)
    • NoSQL (opens new window)
  • 软件测试

    • 软件测试 (opens new window)
  • 加密解密 (opens new window)
  • bilibili字幕提取 (opens new window)
  • 道理 (opens new window)
  • 关于博主

    • Github (opens new window)
    • CSDN (opens new window)
  • 关于本站

    • 如何搭建博客网站 (opens new window)
  • MicroServices

  • SpringBoot

    • big-event
    • 苍穹外卖

      • 苍穹外卖
      • Day02
      • Day03
      • Day04
      • Day05
        • 课程内容
        • 1. Redis入门
        • 2. Redis数据类型
        • 3. Redis常用命令
        • 4.在Java中操作Redis
        • 5. 店铺营业状态设置
      • Day06
      • Day07
      • Day08
      • Day09
      • Day10
      • Day11
      • Day12
      • 项目优化
    • Spring注解
    • 数据库设计文档
  • SSM

  • Spring生态
  • SpringBoot
  • 苍穹外卖
2025-04-14
0
0
目录

Day05

# 课程内容

  • Redis入门
  • Redis数据类型
  • Redis常用命令
  • 在Java中操作Redis
  • 店铺营业状态设置

功能实现:营业状态设置

效果图:

image-20221130223019209

选择营业中,客户可在小程序端下单:

image-20221130223113561

选择打烊中,客户无法在小程序端下单:

image-20221130223205032

# 1. Redis入门

# 2. Redis数据类型

# 3. Redis常用命令

# 4.在Java中操作Redis

redis基础

# 5. 店铺营业状态设置

# 5.1 需求分析和设计

# 5.1.1 产品原型

进到苍穹外卖后台,显示餐厅的营业状态,营业状态分为营业中和打烊中,若当前餐厅处于营业状态,自动接收任何订单,客户可在小程序进行下单操作;若当前餐厅处于打烊状态,不接受任何订单,客户便无法在小程序进行下单操作。

image-20221130212134915

点击营业状态按钮时,弹出更改营业状态

image-20221130213550300

选择营业,设置餐厅为营业中状态

选择打烊,设置餐厅为打烊中状态

状态说明:

image-20221130213947179

# 5.1.2 接口设计

根据上述原型图设计接口,共包含3个接口。

接口设计:

  • 设置营业状态
  • 管理端查询营业状态
  • 用户端查询营业状态

**注:**从技术层面分析,其实管理端和用户端查询营业状态时,可通过一个接口去实现即可。因为营业状态是一致的。但是,本项目约定:

  • 管理端发出的请求,统一使用/admin作为前缀。
  • 用户端发出的请求,统一使用/user作为前缀。

因为访问路径不一致,故分为两个接口实现。

1). 设置营业状态

image-20221130215725802

2). 管理端营业状态

image-20221130215814021

3). 用户端营业状态

image-20221130215836785

# 5.1.3 营业状态存储方式

虽然,可以通过一张表来存储营业状态数据,但整个表中只有一个字段,所以意义不大。

营业状态数据存储方式:基于Redis的字符串来进行存储

image-20221130220037713

**约定:**1表示营业 0表示打烊

# 5.2 代码开发

# 5.2.1 设置营业状态

在sky-server模块中,创建ShopController.java

根据接口定义创建ShopController的setStatus设置营业状态方法:

package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置店铺的营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺的营业状态")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(KEY,status);
        return Result.success();
    }
}

# 5.2.2 管理端查询营业状态

根据接口定义创建ShopController的getStatus查询营业状态方法:

        /**
         * 获取店铺的营业状态
         * @return
         */
        @GetMapping("/status")
        @ApiOperation("获取店铺的营业状态")
        public Result<Integer> getStatus(){
            Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
            log.info("获取到店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
            return Result.success(status);
        }

# 5.2.3 用户端查询营业状态

创建com.sky.controller.user包,在该包下创建ShopController.java

根据接口定义创建ShopController的getStatus查询营业状态方法:

package com.sky.controller.user;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("userShopController")
@RequestMapping("/user/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取店铺的营业状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("获取店铺的营业状态")
    public Result<Integer> getStatus(){
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
        log.info("获取到店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
        return Result.success(status);
    }
}

# 5.3 功能测试

# 5.3.1 接口文档测试

**启动服务:**访问http://localhost:8080/doc.html,打开店铺相关接口

**注意:**使用admin用户登录重新获取token,防止token失效。

设置营业状态:

image-20221201175251351

点击发送

image-20221201175428279

查看Idea控制台日志

image-20221201175511626

查看Redis中数据

image-20221201175634896

管理端查询营业状态:

image-20221201175807978

用户端查询营业状态:

image-20221201175917397

# 5.3.2 接口分组展示

在上述接口文档测试中,管理端和用户端的接口放在一起,不方便区分。

image-20221201181927458

接下来,我们要实现管理端和用户端接口进行区分。

在WebMvcConfiguration.java中,分别扫描"com.sky.controller.admin"和"com.sky.controller.user"这两个包。

	@Bean
    public Docket docket1(){
        log.info("准备生成接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理端接口")
                .apiInfo(apiInfo)
                .select()
                //指定生成接口需要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin"))
                .paths(PathSelectors.any())
                .build();

        return docket;
    }

    @Bean
    public Docket docket2(){
        log.info("准备生成接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .apiInfo(apiInfo)
                .select()
                //指定生成接口需要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller.user"))
                .paths(PathSelectors.any())
                .build();

        return docket;
    }

重启服务器,再次访问接口文档,可进行选择用户端接口或者管理端接口

image-20221201182658244

# 5.3.3 前后端联调测试

启动nginx,访问 http://localhost

进入后台,状态为营业中

image-20221201180353062

点击营业状态设置,修改状态为打烊中

image-20221201180529842

再次查看状态,状态已为打烊中

image-20221201180639063

# 5.4 代码提交

#

上次更新: 2025/5/7 00:24:40
Day04
Day06

← Day04 Day06→

最近更新
01
项目优化
05-06
02
项目优化
05-06
03
延迟消息
05-05
更多文章>
Theme by Vdoing | Copyright © 2025-2025 Cyan Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式