订单写入数据库

This commit is contained in:
2026-02-09 20:43:55 +08:00
parent 2bdbe0f041
commit 357b505275
6 changed files with 165 additions and 48 deletions
+16
View File
@@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/hex"
"regexp"
"strings"
"time"
)
@@ -23,6 +24,15 @@ func GetCurrentTimeString(format ...string) string {
return time.Now().Format(layout)
}
func StringToTimePtr(str string) (*time.Time, error) {
layout := "2006-01-02 15:04"
t, err := time.Parse(layout, str)
if err != nil {
return nil, err
}
return &t, nil
}
func RandStr32() string {
// 生成 32 字节 (256 位) 随机数据
b := make([]byte, 32)
@@ -90,3 +100,9 @@ func IsEmailValid(email string) bool {
regex := regexp.MustCompile(pattern)
return regex.MatchString(email)
}
// 判断字符串是否包含标点符号
func IsContainsSpecialChar(str string) bool {
specialChars := "!@#$%^&*()-+={}[]|\\:;\"'<>,.?/"
return strings.ContainsAny(str, specialChars)
}
+26 -12
View File
@@ -71,7 +71,7 @@ type TabUserInfo_ struct {
type TabCookie_ struct {
ID uint `gorm:"primaryKey;autoIncrement"`
UserID uint `gorm:"size:16;not null"`
UserID uint `gorm:"not null"`
Name string `gorm:"size:255;not null;index"`
Value string `gorm:"size:255;not null;index"`
ExpiresAt time.Time `gorm:"type:datetime;index"`
@@ -91,23 +91,33 @@ type APIRequestLog_ struct {
}
type TabPurchaseOrder struct {
ID uint `gorm:"primarykey" json:"id"`
Title string `gorm:"size:200;comment:标题" json:"title"`
Remark string `gorm:"type:text;comment:备注" json:"remark"`
Photos datatypes.JSON `gorm:"type:json;comment:照片哈希数组" json:"photos"`
Link string `gorm:"size:1000;comment:链接" json:"link"`
PartName string `gorm:"size:200;not null;comment:物品名称" json:"part_name"`
Styles datatypes.JSON `gorm:"type:json;comment:样式数组" json:"styles"`
Costs datatypes.JSON `gorm:"type:json;comment:费用明细数组" json:"costs"`
UpdateTime *time.Time `gorm:"type:datetime;autoUpdateTime;comment:更新时间" json:"update_time"`
TrackingNumber string `gorm:"size:100;uniqueIndex;comment:快递单号" json:"tracking_number"`
OrderStatus int8 `gorm:"default:1;comment:订单状态" json:"order_status"`
ID uint `gorm:"primarykey"`
UserID uint `gorm:"not null"`
Title string `gorm:"size:200;comment:标题"`
Remark string `gorm:"type:text;comment:备注"`
Photos datatypes.JSON `gorm:"type:json;comment:照片哈希数组"`
Link string `gorm:"size:1000;comment:链接"`
PartName string `gorm:"size:200;not null;comment:物品名称"`
Styles string `gorm:"type:text;comment:样式数组"`
//Costs datatypes.JSON `gorm:"type:json;comment:费用明细数组"`
UpdateTime *time.Time `gorm:"type:datetime;autoUpdateTime;comment:更新时间"`
TrackingNumber string `gorm:"size:100;Index;comment:快递单号"`
OrderStatus string `gorm:"default:1;comment:订单状态"`
CreatedAt *time.Time `gorm:"type:datetime;autoCreateTime"`
UpdatedAt *time.Time `gorm:"type:datetime;autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
type TabPurchaseCosts struct {
ID uint `gorm:"primarykey"`
OrderID uint `gorm:"not null"`
UserID uint `gorm:"not null"`
Price int `gorm:"not null"`
Quantity int `gorm:"not null"`
CreatedAt *time.Time `gorm:"type:datetime;autoCreateTime"`
}
func DatabaseInit() error {
var err error
fmt.Println("database_init")
@@ -149,5 +159,9 @@ func DatabaseInit() error {
DB.AutoMigrate(&APIRequestLog_{})
DB.AutoMigrate(&TabPurchaseOrder{})
DB.AutoMigrate(&TabPurchaseCosts{})
return nil
}
+86 -18
View File
@@ -1,30 +1,32 @@
package routers
import (
"fmt"
"encoding/json"
"ops/models"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"gorm.io/datatypes"
)
type CostItem struct {
Cost float64 `json:"cost" binding:"required,min=0"` // 必须,非负
CostT float64 `json:"cost_t" binding:"required,min=0"` // 必须,非负
CurrencyType string `json:"currency_type" binding:"required"` // 必须
Int int `json:"int" binding:"required"` // 必须
Type string `json:"type" binding:"required"` // 必须
Cost int `json:"cost"` // 必须,非负
CostT int `json:"costt"` // 必须,非负
CurrencyType string `json:"currencytype"` // 必须
Int int `json:"int"` // 必须
Type string `json:"type"` // 必须
}
type From_purchase_addorder struct {
Costs []CostItem `json:"costs"` //
Link string `json:"link"` // 可选
OrderStatus string `json:"order_status" binding:"required"` //
PartName string `json:"part_name"` // 可选
Photos []string `json:"photos"` // 可选
Remark string `json:"remark"` // 可选
Styles string `json:"styles"` // 可选
Title string `json:"title" binding:"required"` // 必须
TrackingNumber string `json:"tracking_number"` // 可选
UpdateTime string `json:"update_time"` // 可选
Costs []CostItem `json:"costs"` //
Link string `json:"link"` // 可选
OrderStatus string `json:"order_status"` //
PartName string `json:"partname"` // 可选
Photos []string `json:"photos"` // 可选
Remark string `json:"remark"` // 可选
Styles string `json:"styles"` // 可选
Title string `json:"title"` // 必须
TrackingNumber string `json:"tracking_number"` // 可选
UpdateTime string `json:"update_time"` // 可选
}
func ApiPurchase(r *gin.RouterGroup) {
@@ -35,12 +37,78 @@ func ApiPurchase(r *gin.RouterGroup) {
//需要处理提交的数据,接口有固定的数据格式,不允许乱搞
//fmt.Println(isAuth)
fmt.Println(user)
//fmt.Println(user)
//DebugPrintJson(data)
var jsondata From_purchase_addorder
if err := mapstructure.Decode(data, &jsondata); err == nil {
fmt.Println("转换后数据:\n", jsondata)
//fmt.Println("转换后数据:\n", jsondata)
//数据比较混乱 在这里校验
//判断标题不为空
is_data_ok := true
if jsondata.Title == "" {
is_data_ok = false
}
//判断数量与价格是否为负数
for i := 0; i < len(jsondata.Costs); i++ {
if jsondata.Costs[i].Cost <= 0 {
is_data_ok = false
}
if jsondata.Costs[i].Int <= 0 {
is_data_ok = false
}
}
//判断图片是否为哈希值
for i := 0; i < len(jsondata.Photos); i++ {
//判断字符串是否包含标点符号
if models.IsContainsSpecialChar(jsondata.Photos[i]) {
is_data_ok = false
}
}
//判断时间字符串是否合法
uptime, e := models.StringToTimePtr(jsondata.UpdateTime)
if e != nil {
is_data_ok = false
}
if is_data_ok {
//校验通过
//载入数据库
photos, _ := json.Marshal(jsondata.Photos) //把图片数组转换成字符串
new_data := models.TabPurchaseOrder{
UserID: user.ID,
Title: jsondata.Title,
Remark: jsondata.Remark,
Photos: datatypes.JSON(photos),
Link: jsondata.Link,
PartName: jsondata.PartName,
Styles: jsondata.Styles,
UpdateTime: uptime,
TrackingNumber: jsondata.TrackingNumber,
OrderStatus: jsondata.OrderStatus,
}
models.DB.Create(&new_data)
for i := 0; i < len(jsondata.Costs); i++ {
new_cost_data := models.TabPurchaseCosts{
Price: jsondata.Costs[i].Cost,
Quantity: jsondata.Costs[i].Int,
UserID: user.ID,
OrderID: new_data.ID,
}
models.DB.Create(&new_cost_data)
}
} else {
ReturnJson(ctx, "jsonErr", nil)
}
} else {
ReturnJson(ctx, "jsonErr", nil)