order.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package course
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "spaceofcheng/services/models/model"
  7. "spaceofcheng/services/utils"
  8. )
  9. // 下单
  10. func (c *CourseController) PostOrder() {
  11. // 订单主信息
  12. info := c.GetString("info")
  13. if info == "" {
  14. c.ResponseError(errors.New("无有效订单信息"))
  15. }
  16. var orderInfo model.TaCourseOrders
  17. // var orderCoupon []model.TaCourseOrdersCoupon
  18. if err := json.Unmarshal([]byte(info), &orderInfo); err != nil {
  19. utils.LogError("下单转换JSON失败: " + err.Error())
  20. c.ResponseError(errors.New("下单数据格式不正确"))
  21. }
  22. // if err := json.Unmarshal([]byte(coupons), &orderCoupon); err != nil {
  23. // utils.LogError("下单优惠转换JSON失败: " + err.Error())
  24. // c.ResponseError(errors.New("优惠数据格式不正确"))
  25. // }
  26. cust := c.Context.Get("customer").(model.TaCustomer)
  27. if cust.Phone == "" {
  28. c.ResponseError(
  29. errors.New("用户未登录或绑定"),
  30. http.StatusNotAcceptable,
  31. )
  32. }
  33. newOrders, err := c.serv.PreOrders(&orderInfo)
  34. if err != nil {
  35. c.ResponseError(err)
  36. }
  37. c.ResponseJSON(newOrders)
  38. }
  39. // ConfirmOrders 确认订单
  40. func (c *CourseController) ConfirmOrders() {
  41. // 订单优惠券
  42. customercouponid := c.GetString("customercouponid")
  43. // 订单id
  44. ordersid := c.GetString(":ordersid")
  45. err := c.serv.ConfirmOrders(ordersid, customercouponid)
  46. if err != nil {
  47. c.ResponseError(err)
  48. }
  49. c.ResponseJSON("下单成功!")
  50. }