You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.9 KiB
Go

package h5
import (
"github.com/gin-gonic/gin"
"gold-shop/errors"
"gold-shop/request"
"gold-shop/service"
"gold-shop/utils/result"
"gold-shop/utils/weixin"
"net/http"
"strconv"
)
func Index(c *gin.Context) (result.Data, error) {
return result.Data{"hello": "elf"}, nil
}
func Error(c *gin.Context) (result.Data, error) {
return nil, errors.NewBusinessError("测试", "1122")
}
func Register(c *gin.Context) (result.Data, error) {
mobile := c.PostForm("mobile")
password := c.PostForm("password")
err := service.UserService.Register(mobile, password)
return nil, err
}
func Login(c *gin.Context) (result.Data, error) {
mobile := c.PostForm("mobile")
password := c.PostForm("password")
token, err := service.UserService.Login(mobile, password)
return result.Data{"token": token}, err
}
func GetUserInfo(c *gin.Context) (result.Data, error) {
user, _ := c.Get("user")
data := result.Data{"user": user}
return data, nil
}
func GetUserData(c *gin.Context) (result.Data, error) {
data := service.UserService.GetUserData(user(c))
return data, nil
}
func ModifyPassword(c *gin.Context) (result.Data, error) {
oldPassword := c.PostForm("oldPassword")
password := c.PostForm("password")
err := service.UserService.ModifyPassword(password, oldPassword, user(c))
return nil, err
}
func GetProductInfo(c *gin.Context) (result.Data, error) {
productId := c.Query("productId")
id, _ := strconv.Atoi(productId)
product, err := service.ProductService.GetProductInfo(id)
if err != nil {
return nil, err
}
data := result.Data{"product": product}
return data, nil
}
func GetPayments(c *gin.Context) (result.Data, error) {
var paymentQueryRequest request.PaymentQueryRequest
err := c.ShouldBindQuery(&paymentQueryRequest)
if err != nil {
return nil, errors.NewBusinessError("参数错误")
}
payments, total := service.PaymentService.GetUserPayments(user(c).ID, paymentQueryRequest.Page, paymentQueryRequest.PageSize)
data := result.Data{
"payments": payments,
"total": total,
}
return data, nil
}
func Payment(c *gin.Context) (result.Data, error) {
var paymentRequest request.PaymentRequest
err := c.ShouldBind(&paymentRequest)
if err != nil {
return nil, errors.NewBusinessError("参数错误")
}
return service.PaymentService.Payment(user(c).ID, paymentRequest)
}
func GetOrders(c *gin.Context) (result.Data, error) {
var orderQueryRequest request.OrderQueryRequest
err := c.ShouldBindQuery(&orderQueryRequest)
if err != nil {
return nil, errors.NewBusinessError("参数错误")
}
orders, total := service.OrderService.GetUserOrders(user(c).ID, orderQueryRequest.Page, orderQueryRequest.PageSize)
data := result.Data{
"orders": orders,
"total": total,
}
return data, nil
}
func Order(c *gin.Context) (result.Data, error) {
var orderRequest request.OrderRequest
err := c.ShouldBind(&orderRequest)
if err != nil {
return nil, errors.NewBusinessError("参数错误")
}
order, err := service.OrderService.Order(orderRequest, user(c))
if err != nil {
return nil, err
}
data := result.Data{"orderNo": order.OrderNo}
return data, nil
}
func GetWxAuthUrl(c *gin.Context) (result.Data, error) {
authUrl := weixin.WxApi.GetAuthUrl("https://www.baidu.com")
data := result.Data{"url": authUrl}
return data, nil
}
func GetWxOpenId(c *gin.Context) (result.Data, error) {
code := c.Query("code")
res, err := weixin.WxApi.GetAccessToken(code)
if err != nil {
return nil, err
}
data := result.Data{"openid": res.Openid}
return data, nil
}
func SaveAlipayAccount(c *gin.Context) (result.Data, error) {
alipayAccount := c.PostForm("alipayAccount")
err := service.UserService.SaveAlipayAccount(alipayAccount, user(c))
return nil, err
}
func TransferPayNotify(c *gin.Context) {
var req request.TransferPayNotifyRequest
err := c.ShouldBindQuery(&req)
if err != nil {
c.String(http.StatusOK, "fail")
return
}
service.PaymentService.AfterTransferPayNotify(req)
c.String(http.StatusOK, "success")
return
}