310 lines
6.6 KiB
Go
310 lines
6.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"github.com/shopspring/decimal"
|
|
"platform/web/auth"
|
|
trade2 "platform/web/domains/trade"
|
|
m "platform/web/models"
|
|
q "platform/web/queries"
|
|
s "platform/web/services"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// region /update
|
|
|
|
type UpdateUserReq struct {
|
|
Username string `json:"username" validate:"omitempty,min=3,max=20"`
|
|
Email string `json:"email" validate:"omitempty,email"`
|
|
ContactQQ string `json:"contact_qq" validate:"omitempty,qq"`
|
|
ContactWechat string `json:"contact_wechat" validate:"omitempty,wechat"`
|
|
}
|
|
|
|
func UpdateUser(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
authCtx, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(UpdateUserReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新用户信息
|
|
_, err = q.User.
|
|
Where(q.User.ID.Eq(authCtx.Payload.Id)).
|
|
Updates(m.User{
|
|
Username: &req.Username,
|
|
Email: &req.Email,
|
|
ContactQQ: &req.ContactQQ,
|
|
ContactWechat: &req.ContactWechat,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 返回结果
|
|
return c.SendStatus(fiber.StatusNoContent)
|
|
}
|
|
|
|
// endregion
|
|
|
|
// region /update/account
|
|
|
|
type UpdateAccountReq struct {
|
|
Username string `json:"username" validate:"omitempty,min=3,max=20"`
|
|
Password string `json:"password" validate:"omitempty,min=6,max=20"`
|
|
}
|
|
|
|
func UpdateAccount(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
authCtx, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(UpdateAccountReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新用户信息
|
|
_, err = q.User.
|
|
Where(q.User.ID.Eq(authCtx.Payload.Id)).
|
|
Updates(m.User{
|
|
Username: &req.Username,
|
|
Password: &req.Password,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 返回结果
|
|
return c.SendStatus(fiber.StatusNoContent)
|
|
}
|
|
|
|
// endregion
|
|
|
|
// region /update/password
|
|
|
|
type UpdatePasswordReq struct {
|
|
Phone string `json:"phone"`
|
|
Code string `json:"code"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
func UpdatePassword(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
authCtx, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(UpdatePasswordReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 验证手机令牌
|
|
if req.Phone == "" || req.Code == "" {
|
|
return fiber.NewError(fiber.StatusBadRequest, "手机号码和验证码不能为空")
|
|
}
|
|
err = s.Verifier.VerifySms(c.Context(), req.Phone, req.Code)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新密码
|
|
newHash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = q.User.
|
|
Where(q.User.ID.Eq(authCtx.Payload.Id)).
|
|
UpdateColumn(q.User.Password, newHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 返回结果
|
|
return c.SendStatus(fiber.StatusNoContent)
|
|
}
|
|
|
|
// endregion
|
|
|
|
// region /recharge
|
|
|
|
type RechargePrepareReq struct {
|
|
Amount string `json:"amount" validate:"required,numeric"`
|
|
}
|
|
|
|
type RechargePrepareResp struct {
|
|
TradeNo string `json:"trade_no"`
|
|
PayURL string `json:"pay_url"`
|
|
}
|
|
|
|
type RechargeConfirmReq struct {
|
|
TradeNo string `json:"trade_no" validate:"required"`
|
|
}
|
|
|
|
type RechargeConfirmResp struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// RechargePrepareAlipay 通过支付宝充值
|
|
func RechargePrepareAlipay(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
authContext, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(RechargePrepareReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 保存交易信息
|
|
var now = time.Now()
|
|
amount, err := decimal.NewFromString(req.Amount)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var result *s.TransactionPrepareResult
|
|
err = q.Q.Transaction(func(tx *q.Query) error {
|
|
result, err = s.Transaction.PrepareTransaction(tx, authContext.Payload.Id, now, &s.TransactionPrepareData{
|
|
Subject: "账户充值 - " + amount.StringFixed(2) + "元",
|
|
Amount: amount,
|
|
ExpireAt: time.Now().Add(30 * time.Minute),
|
|
Type: trade2.TypeRecharge,
|
|
Method: trade2.MethodAlipay,
|
|
})
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 返回结果
|
|
return c.JSON(RechargePrepareResp{
|
|
TradeNo: result.TradeNo,
|
|
PayURL: result.PayURL,
|
|
})
|
|
}
|
|
|
|
func RechargeConfirmAlipay(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
_, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(RechargeConfirmReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 验证支付结果
|
|
result, err := s.Transaction.VerifyTransaction(&s.TransactionVerifyData{
|
|
TradeNo: req.TradeNo,
|
|
Method: trade2.MethodAlipay,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新数据库
|
|
err = s.User.RechargeConfirm(req.TradeNo, result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(fiber.Map{"status": "success"})
|
|
}
|
|
|
|
func RechargePrepareWechat(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
authContext, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(RechargePrepareReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 保存交易信息
|
|
var now = time.Now()
|
|
amount, err := decimal.NewFromString(req.Amount)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var result *s.TransactionPrepareResult
|
|
err = q.Q.Transaction(func(tx *q.Query) error {
|
|
result, err = s.Transaction.PrepareTransaction(tx, authContext.Payload.Id, now, &s.TransactionPrepareData{
|
|
Subject: "账户充值 - " + amount.StringFixed(2) + "元",
|
|
Amount: amount,
|
|
ExpireAt: now.Add(30 * time.Minute),
|
|
Type: trade2.TypeRecharge,
|
|
Method: trade2.MethodWeChat,
|
|
})
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 返回结果
|
|
return c.JSON(RechargePrepareResp{
|
|
TradeNo: result.TradeNo,
|
|
PayURL: result.PayURL,
|
|
})
|
|
}
|
|
|
|
func RechargeConfirmWechat(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
_, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(struct {
|
|
TradeNo string `json:"trade_no" validate:"required"`
|
|
})
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 验证支付结果
|
|
result, err := s.Transaction.VerifyTransaction(&s.TransactionVerifyData{
|
|
TradeNo: req.TradeNo,
|
|
Method: trade2.MethodWeChat,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新数据库
|
|
err = s.User.RechargeConfirm(req.TradeNo, result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(fiber.Map{"status": "success"})
|
|
}
|
|
|
|
// endregion
|