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.

191 lines
4.9 KiB
Go

package h5
import (
"fmt"
"github.com/gin-gonic/gin"
"gold-shop/errors"
"gold-shop/global"
"gold-shop/request"
"gold-shop/service"
"gold-shop/utils/result"
"gold-shop/utils/tlpay"
"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)
fmt.Println(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) {
amount := c.DefaultQuery("amount", "")
redirectUri := global.Config.Payment.WxPay.AuthRedirectUri
if amount != "" {
redirectUri += "?amount=" + amount
}
authUrl := weixin.WxApi.GetAuthUrl(redirectUri)
fmt.Println(authUrl)
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
}
func PaymentNotify(c *gin.Context) {
var req request.PaymentNotifyRequest
err := c.ShouldBindQuery(&req)
if err != nil {
c.String(http.StatusOK, "fail")
return
}
service.PaymentService.AfterPaymentNotify(req)
c.String(http.StatusOK, "success")
return
}
func TLPay(c *gin.Context) {
param := tlpay.PayParam{}
param.PayType = "B2B"
param.OrderID = "12344565667"
param.CusID = "990581007426001"
param.AppID = "00000051"
param.TrxAmt = "0.01"
param.RetUrl = "https://www.baidu.com"
param.NotifyUrl = "https://www.baidu.com"
param.GoodsID = "123122"
param.GoodsInf = "slsdfxxx"
param.RandomStr = "sdfssss"
param.SignType = "RSA"
//c.String(http.StatusOK, "<div>nihao</div>")
c.Writer.Header().Set("content-type", "text/html")
c.Writer.WriteString("<div>nihao</div>")
return
}