8.订单Controller

前台传过来的数据用orderForm类包装:

@Data
public class OrderForm {
    @NotEmpty
    //买家姓名
    private String name;
    @NotEmpty
    //买家电话
    private String phone;
    @NotEmpty
    //买家地址
    private String address;
    @NotEmpty
    //买家微信
    private String openid;
    @NotEmpty
    //购物车
    private String items;
}

注意:表单验证用到@Valid注解

转换器:orderForm转换成orderDto

public class OrderForm2OrderDto {
    public static OrderDto convert(OrderForm orderForm) {
        Gson gson = new Gson();

        OrderDto orderDto = new OrderDto();
        orderDto.setBuyerName(orderForm.getName());
        orderDto.setBuyerPhone(orderForm.getPhone());
        orderDto.setBuyerAddress(orderForm.getAddress());
        orderDto.setBuyerOpenid(orderForm.getOpenid());

        List<OrderDetail> orderDetailList = new ArrayList<>();

        try {
            //json数据转换成list
            orderDetailList = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
            }.getType());
        } catch (Exception e) {
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        orderDto.setOrderDetailList(orderDetailList);
        return orderDto;
    }
}
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【新建订单】参数错误");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        //orderform转成orderdto
        OrderDto orderDto = OrderForm2OrderDto.convert(orderForm);
        OrderDto createResult = orderService.creat(orderDto);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId());
        return ResultVOUtil.success(map);
    }

    //订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDto>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(openid)) {
            log.error("【订单列表】 微信名不能为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page, size);
        Page<OrderDto> orderDtoPage = orderService.findList(openid, pageRequest);
        return ResultVOUtil.success(orderDtoPage.getContent());
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVO<OrderDto> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId) {
        OrderDto orderDto = orderService.findOne(orderId);
        return ResultVOUtil.success(orderDto);
    }

    //取消订单
    @PostMapping("/cancle")
    public ResultVO cancle(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        OrderDto orderDto = orderService.findOne(orderId);
        orderService.cancel(orderDto);
        return ResultVOUtil.success();
    }
}

 

以下是在 Laravel 8 中使用 Redis 队列处理订单的示例: 首先,在 `.env` 文件中,将队列驱动程序设置为 redis: ``` QUEUE_CONNECTION=redis ``` 然后,创建一个新的队列作业类 `OrderProcessJob`,该类将处理订单处理过程: ```php <?php namespace App\Jobs; use App\Models\Order; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class OrderProcessJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $order; /** * Create a new job instance. * * @param Order $order * @return void */ public function __construct(Order $order) { $this->order = $order; } /** * Execute the job. * * @return void */ public function handle() { // 处理订单逻辑 echo 'Order processed: ' . $this->order->id; } } ``` 在控制器中,您可以将订单添加到队列中: ```php <?php namespace App\Http\Controllers; use App\Jobs\OrderProcessJob; use App\Models\Order; use Illuminate\Http\Request; class OrderController extends Controller { public function process(Order $order) { // 将订单添加到队列中 OrderProcessJob::dispatch($order); return response()->json([ 'message' => 'Order added to queue' ]); } } ``` 最后,您需要运行 Laravel 的队列工作进程以处理队列作业。可以使用以下命令启动工作进程: ``` php artisan queue:work --queue=orders ``` 这将启动一个工作进程,该进程将从名为 `orders` 的队列中获取作业并执行它们。 当您在控制器中调用 `OrderProcessJob::dispatch($order)` 时,Laravel 将向 Redis 队列中添加一个新的作业。队列工作进程将从队列中获取作业并将其传递给 `OrderProcessJob` 类的 `handle()` 方法进行处理。 希望这可以帮助您了解如何在 Laravel 8 中使用 Redis 队列处理订单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值