456 lines
11 KiB
Go
456 lines
11 KiB
Go
|
package controller
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"github.com/codinl/go-logger"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"golang.org/x/sync/errgroup"
|
||
|
"mh-server/kuaidi"
|
||
|
"mh-server/lib/auth"
|
||
|
"mh-server/lib/status"
|
||
|
"mh-server/lib/utils"
|
||
|
"mh-server/lib/wxpay"
|
||
|
"mh-server/model"
|
||
|
"io/ioutil"
|
||
|
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
//func checkout(response http.ResponseWriter, request *http.Request) {
|
||
|
//func WxMsg(c *gin.Context) {
|
||
|
// // 获取参数
|
||
|
// signature := c.PostForm("signature")
|
||
|
// timestamp := c.PostForm("timestamp")
|
||
|
// nonce := c.PostForm("nonce")
|
||
|
// echostr := c.PostForm("echostr")
|
||
|
//
|
||
|
// //timestamp := request.FormValue("echostr")
|
||
|
// //nonce := request.FormValue("nonce")
|
||
|
// //echostr := request.FormValue("echostr")
|
||
|
//
|
||
|
// //将token、timestamp、nonce三个参数进行字典序排序
|
||
|
// var tempArray = []string{TOKEN, timestamp, nonce}
|
||
|
// sort.Strings(tempArray)
|
||
|
//
|
||
|
// //将三个参数字符串拼接成一个字符串进行sha1加密
|
||
|
// var sha1String string = ""
|
||
|
// for _, v := range tempArray {
|
||
|
// sha1String += v
|
||
|
// }
|
||
|
//
|
||
|
// h := sha1.New()
|
||
|
// h.Write([]byte(sha1String))
|
||
|
// sha1String = hex.EncodeToString(h.Sum([]byte("")))
|
||
|
//
|
||
|
// //获得加密后的字符串可与signature对比
|
||
|
// if sha1String == signature {
|
||
|
// _, err := c.Writer.Write([]byte(echostr))
|
||
|
// if err != nil {
|
||
|
// fmt.Println("响应失败。。。")
|
||
|
// }
|
||
|
// } else {
|
||
|
// fmt.Println("验证失败")
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
func OrderList(c *gin.Context) {
|
||
|
req := struct {
|
||
|
Page int `json:"page"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
}{
|
||
|
Page: 1,
|
||
|
PageSize: 10,
|
||
|
}
|
||
|
if c.ShouldBindJSON(&req) != nil {
|
||
|
logger.Error("ShouldBindJSON err")
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
uc := auth.GetCurrentUser(c)
|
||
|
if uc == nil {
|
||
|
logger.Error("uc is nil")
|
||
|
RespJson(c, status.Unauthorized, nil)
|
||
|
return
|
||
|
}
|
||
|
orderList, totalPage, err := model.GetOrderList(uint64(uc.Uid), req.Page, req.PageSize)
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ret := map[string]interface{}{
|
||
|
"order_list": orderList,
|
||
|
"cur_page": req.Page,
|
||
|
"total_page": totalPage,
|
||
|
}
|
||
|
RespOK(c, ret)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func OrderInfo(c *gin.Context) {
|
||
|
req := struct {
|
||
|
OrderId uint32 `json:"order_id"`
|
||
|
}{}
|
||
|
if c.ShouldBindJSON(&req) != nil {
|
||
|
logger.Error("ShouldBindJSON err")
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
order := model.Order{}
|
||
|
order.ID = req.OrderId
|
||
|
info, err := order.Info()
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
RespOK(c, info)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func OrderExpress(c *gin.Context) {
|
||
|
req := struct {
|
||
|
ExpressCompanyNo string `json:"express_company_no"`
|
||
|
ExpressNo string `json:"express_no"`
|
||
|
}{}
|
||
|
if c.ShouldBindJSON(&req) != nil {
|
||
|
logger.Error("ShouldBindJSON err")
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
fmt.Println("CompanyNo:", req.ExpressCompanyNo)
|
||
|
fmt.Println("Num:", req.ExpressNo)
|
||
|
|
||
|
expressInfo, err := kuaidi.ExpressInfo(req.ExpressCompanyNo, req.ExpressNo)
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
RespOK(c, expressInfo)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func WXPaySuccess(c *gin.Context) {
|
||
|
//fbConfig := model.GetFBConfig()
|
||
|
//if fbConfig.AssistanceValidityTerm == 0 {
|
||
|
// logger.Error(errors.New("get assistance_validity_term err"))
|
||
|
// RespJson(c, status.DBOperateError, nil)
|
||
|
// return
|
||
|
//}
|
||
|
|
||
|
//assistanceValidityTerm := time.Unix(time.Now().Unix()+int64(fbConfig.AssistanceValidityTerm), 0)
|
||
|
//ret := map[string]interface{}{
|
||
|
// "assistance_validity_term": assistanceValidityTerm,
|
||
|
// "postage": fbConfig.Postage,
|
||
|
//}
|
||
|
//RespOK(c, ret)
|
||
|
}
|
||
|
|
||
|
// 创建订单
|
||
|
func OrderCreate(c *gin.Context) {
|
||
|
//type Goods struct {
|
||
|
// Price uint32 `json:"price"`
|
||
|
//}
|
||
|
|
||
|
req := struct {
|
||
|
GameCardId uint64 `json:"game_card_id"`
|
||
|
StoreId uint64 `json:"store_id"`
|
||
|
UserAddressId uint64 `json:"user_address_id"`
|
||
|
Price uint32 `json:"price"`
|
||
|
DeliveryType uint8 `json:"delivery_type"` // 取货方式
|
||
|
ExpressFee uint32 `json:"express_fee"`
|
||
|
}{}
|
||
|
|
||
|
bodyString, err := ioutil.ReadAll(c.Request.Body)
|
||
|
if err != nil {
|
||
|
logger.Error(errors.New("OrderCreate ReadAll req err"))
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//bodyString := strings.Replace(string(body), "\"[", "[", -1)
|
||
|
//bodyString = strings.Replace(string(bodyString), "]\"", "]", -1)
|
||
|
//bodyString = strings.Replace(string(bodyString), "\\", "", -1)
|
||
|
logger.Info("bodyString:", string(bodyString))
|
||
|
|
||
|
err = json.Unmarshal([]byte(bodyString), &req)
|
||
|
if err != nil {
|
||
|
logger.Error(errors.New("OrderCreate Unmarshal req err"))
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
//if err := c.ShouldBindJSON(&req);err != nil {
|
||
|
// logger.Error(err)
|
||
|
// RespJson(c,status.BadRequest,nil)
|
||
|
// return
|
||
|
//}
|
||
|
|
||
|
fmt.Println("商品下单 req:", req)
|
||
|
|
||
|
//uc := &struct {
|
||
|
// Uid uint32 `json:"uid"`
|
||
|
//}{}
|
||
|
uc := auth.GetCurrentUser(c)
|
||
|
if uc == nil {
|
||
|
logger.Error("uc is nil")
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
//uc.Uid = 62389201
|
||
|
user := model.GetUserByUid(uc.Uid)
|
||
|
if user == nil {
|
||
|
logger.Error(errors.New("GetUserByUid err"))
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if user.MemberLevel == 1 {
|
||
|
logger.Error(errors.New("user level 1"))
|
||
|
RespJson(c, status.NotMember, nil)
|
||
|
return
|
||
|
}
|
||
|
unreturnedOrders, err := model.IsHaveUnreturnedOrders(uc.Uid)
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
if unreturnedOrders {
|
||
|
logger.Error("unreturnedOrders")
|
||
|
RespJson(c, status.HaveUnreturnedOrders, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
online, err := model.IsGameCardOnline(uint32(req.GameCardId))
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
if !online {
|
||
|
logger.Error("unreturnedOrders")
|
||
|
RespJson(c, status.GoodsSoldOut, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// TODO 邮费
|
||
|
orderSn := utils.GetSerialNo32HEXString()
|
||
|
order := model.Order{
|
||
|
Uid: uint64(uc.Uid),
|
||
|
GameCardId: req.GameCardId,
|
||
|
StoreId: req.StoreId,
|
||
|
UserAddressId: req.UserAddressId,
|
||
|
DeliveryType: req.DeliveryType,
|
||
|
Count: 1,
|
||
|
PickupCode: model.GetPickupCode(), // TODO 取货码
|
||
|
CardStatus: model.OrderCardStatusUnPick,
|
||
|
PayTime: time.Now(),
|
||
|
OrderSn: orderSn,
|
||
|
PayPrice: req.Price,
|
||
|
Postage: req.ExpressFee,
|
||
|
}
|
||
|
err = order.OrderCreate()
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//tx := model.TransactionBegin()
|
||
|
////TODO 以支付成功作为订单下单成功的依据,不是订单创建 已修改
|
||
|
orderId := fmt.Sprintf("%d", order.ID)
|
||
|
fmt.Println("orderId:", orderId)
|
||
|
if req.Price == 0 {
|
||
|
ret := map[string]interface{}{
|
||
|
"web_pay": "",
|
||
|
"order_id": order.ID,
|
||
|
}
|
||
|
RespOK(c, ret)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
webPay, err := wxpay.WebPay(orderId, req.Price, user.WxOpenID, "N", wxpay.WxPayRentCard)
|
||
|
if err != nil {
|
||
|
logger.Error(errors.New("WebPay err"))
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
ret := map[string]interface{}{
|
||
|
"web_pay": webPay,
|
||
|
"order_id": order.ID,
|
||
|
}
|
||
|
RespOK(c, ret)
|
||
|
}
|
||
|
|
||
|
func OrderAmount(c *gin.Context) {
|
||
|
req := struct {
|
||
|
//StoreId uint32 `json:"store_id" binding:"required"`
|
||
|
UserAddressId uint32 `json:"user_address_id" binding:"required"`
|
||
|
Price uint32 `json:"price" `
|
||
|
}{}
|
||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
logger.Error("ShouldBindJSON err:", err)
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
eg errgroup.Group
|
||
|
userAddress = &model.UserAddress{}
|
||
|
//store = &model.Store{}
|
||
|
)
|
||
|
|
||
|
//eg.Go(func() error {
|
||
|
// store.ID = req.StoreId
|
||
|
// err := store.Info()
|
||
|
// if err != nil {
|
||
|
// logger.Error("err:", err)
|
||
|
// }
|
||
|
// return nil
|
||
|
//})
|
||
|
|
||
|
eg.Go(func() error {
|
||
|
userAddress.ID = req.UserAddressId
|
||
|
err := userAddress.Info()
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
if err := eg.Wait(); err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
//fmt.Println("Successfully fetched all URLs.")
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
expressFee := model.ExpressFeeProvince
|
||
|
if userAddress.Province != "广东省" {
|
||
|
expressFee = model.ExpressFeeOutsideProvince
|
||
|
}
|
||
|
|
||
|
ret := map[string]interface{}{
|
||
|
"total_amount": req.Price + uint32(expressFee),
|
||
|
"express_fee": expressFee,
|
||
|
}
|
||
|
RespOK(c, ret)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//func WXPay(c *gin.Context) {
|
||
|
// req := struct {
|
||
|
// //TotalFee uint32 `json:"total_fee"`
|
||
|
// AddressId uint32 `json:"address_id"`
|
||
|
// OrderSn string `json:"order_sn"`
|
||
|
// }{}
|
||
|
// if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
// logger.Error(err)
|
||
|
// RespJson(c, status.BadRequest, nil)
|
||
|
// return
|
||
|
// }
|
||
|
//
|
||
|
// uc := auth.GetCurrentUser(c)
|
||
|
// if uc == nil {
|
||
|
// RespJson(c, status.Unauthorized, nil)
|
||
|
// return
|
||
|
// }
|
||
|
//
|
||
|
// //user := model.GetUserByUid(uc.Uid)
|
||
|
//
|
||
|
// var order model.Order
|
||
|
// //TODO 支付金额 地址id
|
||
|
// err := model.NewOrderQuerySet(model.DB).OrderSnEq(req.OrderSn).One(&order)
|
||
|
// if err != nil {
|
||
|
// logger.Error(err)
|
||
|
// RespJson(c, status.DBOperateError, nil)
|
||
|
// return
|
||
|
// }
|
||
|
//
|
||
|
// //if err := model.NewFBOrderQuerySet(model.DB).OrderSnEq(req.OrderSn).GetUpdater().
|
||
|
// // SetAddressId(req.AddressId).Update(); err != nil {
|
||
|
// // logger.Error(err)
|
||
|
// // RespJson(c, status.DBOperateError, nil)
|
||
|
// // return
|
||
|
// //}
|
||
|
// //
|
||
|
// //var sextuple *wxpay.Sextuple
|
||
|
// //sextuple, err = wxpay.FBXiaoChengXuPay(order.PayPrice, req.OrderSn, model.FB_WX_PAY_TYPE, user.WxOpenID)
|
||
|
// //if err != nil {
|
||
|
// // logger.Error(err)
|
||
|
// // RespJson(c, status.InternalServerError, nil)
|
||
|
// // return
|
||
|
// //}
|
||
|
//
|
||
|
// //RespOK(c, sextuple)
|
||
|
//}
|
||
|
|
||
|
func ConfirmReceipt(c *gin.Context) {
|
||
|
req := struct {
|
||
|
OrderId uint32 `json:"order_id"`
|
||
|
GameCardId uint32 `json:"game_card_id"`
|
||
|
}{}
|
||
|
if c.ShouldBindJSON(&req) != nil {
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
uc := auth.GetCurrentUser(c)
|
||
|
if uc == nil {
|
||
|
logger.Error("uc is nil")
|
||
|
RespJson(c, status.Unauthorized, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
order := model.Order{Uid: uint64(uc.Uid)}
|
||
|
order.ID = req.OrderId
|
||
|
order.GameCardId = uint64(req.GameCardId)
|
||
|
order.CardStatus = model.OrderCardStatusPlaying
|
||
|
info, err := order.Modify()
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
RespOK(c, info)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func ExpressCompanyList(c *gin.Context) {
|
||
|
company := &model.ExpressCompany{}
|
||
|
list, err := company.List()
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
RespOK(c, list)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func OrderRevert(c *gin.Context) {
|
||
|
order := &model.Order{}
|
||
|
if c.ShouldBindJSON(order) != nil {
|
||
|
logger.Error("ShouldBindJSON err")
|
||
|
RespJson(c, status.BadRequest, nil)
|
||
|
return
|
||
|
}
|
||
|
//order := model.Order{}
|
||
|
//order.ID = req.OrderId
|
||
|
info, err := order.Revert()
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
RespJson(c, status.InternalServerError, nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
RespOK(c, info)
|
||
|
return
|
||
|
}
|