package walletctrl import ( "91porn-server/app/appg" "91porn-server/app/service/walletser" "91porn-server/common" "91porn-server/common/stderr" "91porn-server/common/store" "91porn-server/models/v/walletmod" "encoding/json" "errors" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" "net/http" "time" ) func WalletBalance(ctx *gin.Context) { req := struct { Uid uint64 `json:"uid" form:"uid"` }{} err := ctx.ShouldBind(&req) if err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err) return } w, err := walletmod.GetWallet(req.Uid) if err != nil { common.ServeJSON(ctx, stderr.ErrServerUnavailable, err.Error()) return } resp := struct { Balance int64 `json:"balance"` }{} resp.Balance = w.Amount + w.Income ctx.JSON(http.StatusOK, gin.H{ "code": stderr.Success, "msg": stderr.Success.Msg(), "tip": stderr.Success.Tip(), "data": resp, "time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), }) } func WalletDeduct(ctx *gin.Context) { req := struct { Uid uint64 `json:"uid" form:"uid"` OrderId primitive.ObjectID `json:"orderId" form:"orderId"` Source int `json:"source" form:"source"` Amount int64 `json:"amount" form:"amount"` // 扣除金额 Desc string `json:"desc" form:"desc"` }{} err := ctx.ShouldBind(&req) if err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err) return } if req.Uid <= 0 || req.OrderId.IsZero() || req.Amount <= 0 { common.ServeJSON(ctx, stderr.ErrParamError, err) return } err = Check(ctx, req) if err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err) return } resp := struct { Balance int64 `json:"balance"` }{} balance, code := walletser.StoreDeductBalance(req.Uid, req.Amount, req.OrderId, req.Source, req.Desc) resp.Balance = balance ctx.JSON(http.StatusOK, gin.H{ "code": code, "msg": code.Msg(), "tip": code.Tip(), "data": resp, "time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), }) } func Check(ctx *gin.Context, param interface{}) (err error) { b, _ := json.Marshal(param) sign, err := store.Encrypt(b, appg.Conf.Base.StoreEncryptKey) if err != nil { return } headerSign := ctx.Request.Header.Get("sign") if sign != headerSign { return errors.New("校验失败") } return nil } func WalletRefund(ctx *gin.Context) { req := struct { Uid uint64 `json:"uid"` OrderId primitive.ObjectID `json:"orderId"` Source int `json:"source"` Amount int64 `json:"amount"` Desc string `json:"desc"` }{} err := ctx.ShouldBind(&req) if err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err) return } if req.Uid <= 0 || req.OrderId.IsZero() || req.Amount <= 0 { common.ServeJSON(ctx, stderr.ErrParamError, err) return } err = Check(ctx, req) if err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err) return } resp := struct { Balance int64 `json:"balance"` }{} balance, code := walletser.StoreRefund(req.Uid, req.Amount, req.OrderId, req.Source, req.Desc) resp.Balance = balance ctx.JSON(http.StatusOK, gin.H{ "code": code, "msg": code.Msg(), "tip": code.Tip(), "data": resp, "time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), }) }