后台项目环境搭建

后端项目搭建

项目架构

具体总目录

hospital-manage:医院接口模拟端(已开发,直接使用)

yygh-parent:根目录,管理子模块:

  • common:公共模块父节点
    • common-util:工具类模块,所有模块都可以依赖于它
    • rabbit-util:rabbitmq业务封装
    • service-util:service服务的工具包,包含service服务的公共配置类,所有service模块依赖于它
  • server-gateway:服务网关
  • model:实体类模块
  • service:api接口服务父节点
    • service-hosp:医院api接口服务
    • service-cmn:公共api接口服务
    • service-user:用户api接口服务
    • service-order:订单api接口服务
    • service-oss:文件api接口服务
    • service-sms:短信 api接口服务
    • service-task:定时任务服务
    • service-statistics:统计api接口服务
  • service-client:feign服务调用父节点
    • service-cmn-client:公共api接口
    • service-hosp-client:医院api接口
    • service-order-client:订单api接口
  • service-user:用户api接口

项目环境搭建 - 图1

具体依赖

此处具体依赖只是思路,具体依赖见详细文档。

parent打包方式为pom,dependencyManagement作为管理依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <modules>
  6. <module>common</module>
  7. <module>model</module>
  8. <module>service</module>
  9. </modules>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.2.1.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <groupId>com.atguigu</groupId>
  17. <artifactId>yygh-parent</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. <name>yygh-parent</name>
  20. <description>Demo project for Spring Boot</description>
  21. <packaging>pom</packaging>
  22. <properties>
  23. 后面引用作为版本
  24. </properties>
  25. <dependencyManagement>
  26. <!--配置dependencyManagement锁定依赖的版本-->
  27. <dependencies>
  28. ...
  29. </dependencies>
  30. </dependencyManagement>
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-maven-plugin</artifactId>
  36. </plugin>
  37. </plugins>
  38. </build>
  39. </project>

其他二级模块,如service,common(除model)打包方式还是为pom,model为单个实体类,打包还是为jar,都依赖依赖parent模块,引入依赖时可以忽略版本

例如:

  1. <parent>
  2. <artifactId>yygh-parent</artifactId>
  3. <groupId>com.atguigu</groupId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>
  6. <modelVersion>4.0.0</modelVersion>
  7. <artifactId>service</artifactId>
  8. <packaging>pom</packaging>
  9. <properties>
  10. <maven.compiler.source>8</maven.compiler.source>
  11. <maven.compiler.target>8</maven.compiler.target>
  12. </properties>
  13. <dependencies>
  14. ...
  15. </dependencies>

其他三级模块,依赖二级模块即可,打包为jar,需要使用其他模块,直接引入即可

swagger测试

概述

编写和维护接口文档是每个程序员的职责,根据Swagger2可以快速帮助我们编写最新的API接口文档,再也不用担心开会前仍忙于整理各种资料了,间接提升了团队开发的沟通效率。

常用注解(swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息):

  • @Api:修饰整个类,描述Controller的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiModelProperty:用对象接收参数时,描述对象的一个字段
  • @ApiImplicitParam:一个请求参数
  • @ApiImplicitParams:多个请求参数

common中引入依赖

  1. <!--swagger-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. </dependency>

添加配置

项目环境搭建 - 图2

  1. /**
  2. * Swagger2配置信息
  3. */
  4. @Configuration
  5. @EnableSwagger2
  6. public class Swagger2Config {
  7. @Bean
  8. public Docket webApiConfig() {
  9. return new Docket(DocumentationType.SWAGGER_2)
  10. .groupName("webApi")
  11. .apiInfo(webApiInfo())
  12. .select()
  13. //只显示api路径下的页面
  14. .paths(Predicates.and(PathSelectors.regex("/api/.*")))
  15. .build();
  16. }
  17. @Bean
  18. public Docket adminApiConfig() {
  19. return new Docket(DocumentationType.SWAGGER_2)
  20. .groupName("adminApi")
  21. .apiInfo(adminApiInfo())
  22. .select()
  23. //只显示admin路径下的页面
  24. .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
  25. .build();
  26. }
  27. private ApiInfo webApiInfo() {
  28. return new ApiInfoBuilder()
  29. .title("网站-API文档")
  30. .description("本文档描述了网站微服务接口定义")
  31. .version("1.0")
  32. .contact(new Contact("peanut", "http://peanut325.github.io", "1009703142@qq.com"))
  33. .build();
  34. }
  35. private ApiInfo adminApiInfo() {
  36. return new ApiInfoBuilder()
  37. .title("后台管理系统-API文档")
  38. .description("本文档描述了后台管理系统微服务接口定义")
  39. .version("1.0")
  40. .contact(new Contact("peanut", "http://peanut325.github.io", "1009703142@qq.com"))
  41. .build();
  42. }
  43. }

在需要使用的模块中导入此模块依赖,并扫描包

  1. <dependency>
  2. <groupId>com.atguigu</groupId>
  3. <artifactId>service-util</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>
  1. @SpringBootApplication
  2. @ComponentScan(basePackages = "com.atguigu") // 扫描common工程
  3. public class ServiceHospApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ServiceHospApplication.class, args);
  6. }
  7. }

登录页面即可测试

项目环境搭建 - 图3

全局异常处理

spring boot 默认情况下会映射到 /error 进行异常处理,但是提示并不十分友好,下面自定义异常处理,提供友好展示。

项目环境搭建 - 图4

自定义异常类

继承RuntimeException即可

  1. /**
  2. * 自定义全局异常类
  3. *
  4. * @author qy
  5. */
  6. @Data
  7. @ApiModel(value = "自定义全局异常类")
  8. public class YyghException extends RuntimeException {
  9. @ApiModelProperty(value = "异常状态码")
  10. private Integer code;
  11. /**
  12. * 通过状态码和错误消息创建异常对象
  13. * @param message
  14. * @param code
  15. */
  16. public YyghException(String message, Integer code) {
  17. super(message);
  18. this.code = code;
  19. }
  20. /**
  21. * 接收枚举类型对象
  22. * @param resultCodeEnum
  23. */
  24. public YyghException(ResultCodeEnum resultCodeEnum) {
  25. super(resultCodeEnum.getMessage());
  26. this.code = resultCodeEnum.getCode();
  27. }
  28. @Override
  29. public String toString() {
  30. return "YyghException{" +
  31. "code=" + code +
  32. ", message=" + this.getMessage() +
  33. '}';
  34. }
  35. }

添加全局异常处理类

  1. /**
  2. * 统一异常处理
  3. */
  4. @RestControllerAdvice
  5. public class GlobalExceptionHandler {
  6. @ExceptionHandler(YyghException.class)
  7. public Result error(YyghException exception) {
  8. exception.printStackTrace();
  9. return Result.fail();
  10. }
  11. }

返回统一结果集

由于是前后端分离的项目,所以需要统一的返回结果集规范数据

项目环境搭建 - 图5

全局统一结果集类

  1. /**
  2. * 全局统一返回结果类
  3. */
  4. @Data
  5. @ApiModel(value = "全局统一返回结果")
  6. public class Result<T> {
  7. @ApiModelProperty(value = "返回码")
  8. private Integer code;
  9. @ApiModelProperty(value = "返回消息")
  10. private String message;
  11. @ApiModelProperty(value = "返回数据")
  12. private T data;
  13. public Result(){}
  14. protected static <T> Result<T> build(T data) {
  15. Result<T> result = new Result<T>();
  16. if (data != null)
  17. result.setData(data);
  18. return result;
  19. }
  20. public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
  21. Result<T> result = build(body);
  22. result.setCode(resultCodeEnum.getCode());
  23. result.setMessage(resultCodeEnum.getMessage());
  24. return result;
  25. }
  26. public static <T> Result<T> build(Integer code, String message) {
  27. Result<T> result = build(null);
  28. result.setCode(code);
  29. result.setMessage(message);
  30. return result;
  31. }
  32. public static<T> Result<T> ok(){
  33. return Result.ok(null);
  34. }
  35. /**
  36. * 操作成功
  37. * @param data
  38. * @param <T>
  39. * @return
  40. */
  41. public static<T> Result<T> ok(T data){
  42. Result<T> result = build(data);
  43. return build(data, ResultCodeEnum.SUCCESS);
  44. }
  45. public static<T> Result<T> fail(){
  46. return Result.fail(null);
  47. }
  48. /**
  49. * 操作失败
  50. * @param data
  51. * @param <T>
  52. * @return
  53. */
  54. public static<T> Result<T> fail(T data){
  55. Result<T> result = build(data);
  56. return build(data, ResultCodeEnum.FAIL);
  57. }
  58. public Result<T> message(String msg){
  59. this.setMessage(msg);
  60. return this;
  61. }
  62. public Result<T> code(Integer code){
  63. this.setCode(code);
  64. return this;
  65. }
  66. public boolean isOk() {
  67. if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
  68. return true;
  69. }
  70. return false;
  71. }
  72. }

定义状态码(枚举类型)

  1. /**
  2. * 统一返回结果状态信息类
  3. */
  4. @Getter
  5. public enum ResultCodeEnum {
  6. SUCCESS(200,"成功"),
  7. FAIL(201, "失败"),
  8. PARAM_ERROR( 202, "参数不正确"),
  9. SERVICE_ERROR(203, "服务异常"),
  10. DATA_ERROR(204, "数据异常"),
  11. DATA_UPDATE_ERROR(205, "数据版本异常"),
  12. LOGIN_AUTH(208, "未登陆"),
  13. PERMISSION(209, "没有权限"),
  14. CODE_ERROR(210, "验证码错误"),
  15. // LOGIN_MOBLE_ERROR(211, "账号不正确"),
  16. LOGIN_DISABLED_ERROR(212, "改用户已被禁用"),
  17. REGISTER_MOBLE_ERROR(213, "手机号已被使用"),
  18. LOGIN_AURH(214, "需要登录"),
  19. LOGIN_ACL(215, "没有权限"),
  20. URL_ENCODE_ERROR( 216, "URL编码失败"),
  21. ILLEGAL_CALLBACK_REQUEST_ERROR( 217, "非法回调请求"),
  22. FETCH_ACCESSTOKEN_FAILD( 218, "获取accessToken失败"),
  23. FETCH_USERINFO_ERROR( 219, "获取用户信息失败"),
  24. //LOGIN_ERROR( 23005, "登录失败"),
  25. PAY_RUN(220, "支付中"),
  26. CANCEL_ORDER_FAIL(225, "取消订单失败"),
  27. CANCEL_ORDER_NO(225, "不能取消预约"),
  28. HOSCODE_EXIST(230, "医院编号已经存在"),
  29. NUMBER_NO(240, "可预约号不足"),
  30. TIME_NO(250, "当前时间不可以预约"),
  31. SIGN_ERROR(300, "签名错误"),
  32. HOSPITAL_OPEN(310, "医院未开通,暂时不能访问"),
  33. HOSPITAL_LOCK(320, "医院被锁定,暂时不能访问"),
  34. ;
  35. private Integer code;
  36. private String message;
  37. private ResultCodeEnum(Integer code, String message) {
  38. this.code = code;
  39. this.message = message;
  40. }
  41. }

数据库设计

运行资料中的sql即可,会创建4个数据库

项目环境搭建 - 图6

前端项目搭建

由于使用的时vue-admin-template,在谷粒学院有详细配置。

项目架构

├── build // 构建脚本

├── config // 全局配置

├── node_modules // 项目依赖模块

├── src //项目源代码

├── static // 静态资源

└── package.jspon // 项目信息和依赖配置

src

├── api // 各种接口

├── assets // 图片等资源

├── components // 各种公共组件,非公共组件在各自view下维护

├── icons //svg icon

├── router // 路由表

├── store // 存储

├── styles // 各种样式

├── utils // 公共工具,非公共工具,在各自view下维护

├── views // 各种layout

├── App.vue //项目顶层组件

├── main.js //项目入口文件

└── permission.js //认证入口

安装依赖

npm insall

如果node.js版本过高,会安装失败,可以降低版本,也可以使用评论区的解决方法,虽然我不知道什么意思,但确实成功了!!!

项目环境搭建 - 图7

启动项目

npm run dev