pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
SwaggerConfig
package com.leyou.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost:8089")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.leyou.order.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("乐优商城订单系统")
.description("乐优商城订单系统接口文档")
.version("1.0")
.build();
}
}
OrderController
package com.leyou.order.controller;
import com.leyou.common.pojo.PageResult;
import com.leyou.order.pojo.Order;
import com.leyou.order.service.OrderService;
import com.leyou.utils.PayHelper;
import com.leyou.utils.PayState;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("order")
@Api("订单服务接口")
public class OrderController {
@Autowired
private OrderService orderService;
@Autowired
private PayHelper payHelper;
/**
* 创建订单
*
* @param order 订单对象
* @return 订单编号
*/
@PostMapping
@ApiOperation(value = "创建订单接口,返回订单编号", notes = "创建订单")
@ApiImplicitParam(name = "order", required = true, value = "订单的json对象,包含订单条目和物流信息")
public ResponseEntity<Long> createOrder(@RequestBody @Valid Order order) {
Long id = this.orderService.createOrder(order);
return new ResponseEntity<>(id, HttpStatus.CREATED);
}
/**
* 根据订单编号查询订单
*
* @param id
* @return
*/
@GetMapping("{id}")
@ApiOperation(value = "根据订单编号查询订单,返回订单对象", notes = "查询订单")
@ApiImplicitParam(name = "id", required = true, value = "订单的编号")
public ResponseEntity<Order> queryOrderById(@PathVariable("id") Long id) {
Order order = this.orderService.queryById(id);
if (order == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return ResponseEntity.ok(order);
}
/**
* 分页查询当前用户订单
*
* @param status 订单状态
* @return 分页订单数据
*/
@GetMapping("list")
@ApiOperation(value = "分页查询当前用户订单,并且可以根据订单状态过滤", notes = "分页查询当前用户订单")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "当前页", defaultValue = "1", type = "Integer"),
@ApiImplicitParam(name = "rows", value = "每页大小", defaultValue = "5", type = "Integer"),
@ApiImplicitParam(name = "status", value = "订单状态:1未付款,2已付款未发货,3已发货未确认,4已确认未评价,5交易关闭,6交易成功,已评价", type = "Integer"),
})
@ApiResponses({
@ApiResponse(code = 200, message = "订单的分页结果"),
@ApiResponse(code = 404, message = "没有查询到结果"),
@ApiResponse(code = 500, message = "查询失败"),
})
public ResponseEntity<PageResult<Order>> queryUserOrderList(
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "rows", defaultValue = "5") Integer rows,
@RequestParam(value = "status", required = false) Integer status) {
PageResult<Order> result = this.orderService.queryUserOrderList(page, rows, status);
if (result == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return ResponseEntity.ok(result);
}
/**
* 更新订单状态
*
* @param id
* @param status
* @return
*/
@PutMapping("{id}/{status}")
@ApiOperation(value = "更新订单状态", notes = "更新订单状态")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "订单编号", type = "Long"),
@ApiImplicitParam(name = "status", value = "订单状态:1未付款,2已付款未发货,3已发货未确认,4已确认未评价,5交易关闭,6交易成功,已评价", type = "Integer"),
})
@ApiResponses({
@ApiResponse(code = 204, message = "true:修改状态成功;false:修改状态失败"),
@ApiResponse(code = 400, message = "请求参数有误"),
@ApiResponse(code = 500, message = "查询失败")
})
public ResponseEntity<Boolean> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
Boolean boo = this.orderService.updateStatus(id, status);
if (boo == null) {
// 返回400
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
// 返回204
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
/**
* 生成付款链接
*
* @param orderId
* @return
*/
@GetMapping("url/{id}")
@ApiOperation(value = "生成微信扫码支付付款链接", notes = "生成付款链接")
@ApiImplicitParam(name = "id", value = "订单编号", type = "Long")
@ApiResponses({
@ApiResponse(code = 200, message = "根据订单编号生成的微信支付地址"),
@ApiResponse(code = 404, message = "生成链接失败"),
@ApiResponse(code = 500, message = "服务器异常"),
})
public ResponseEntity<String> generateUrl(@PathVariable("id") Long orderId) {
// 生成付款链接
String url = this.payHelper.createPayUrl(orderId);
if (StringUtils.isBlank(url)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return ResponseEntity.ok(url);
}
/**
* 查询付款状态
*
* @param orderId
* @return 0, 状态查询失败 1,支付成功 2,支付失败
*/
@GetMapping("state/{id}")
@ApiOperation(value = "查询扫码支付付款状态", notes = "查询付款状态")
@ApiImplicitParam(name = "id", value = "订单编号", type = "Long")
@ApiResponses({
@ApiResponse(code = 200, message = "0, 未查询到支付信息 1,支付成功 2,支付失败"),
@ApiResponse(code = 500, message = "服务器异常"),
})
public ResponseEntity<Integer> queryPayState(@PathVariable("id") Long orderId) {
PayState payState = this.payHelper.queryOrder(orderId);
return ResponseEntity.ok(payState.getValue());
}
}