数据模型使用指针类型字段以避免空值自动更新的问题

This commit is contained in:
2025-05-26 10:57:39 +08:00
parent 1e7b5777a2
commit c08d625975
43 changed files with 457 additions and 397 deletions

View File

@@ -3,6 +3,7 @@ package services
import (
"context"
"errors"
"platform/pkg/u"
auth2 "platform/web/auth"
client2 "platform/web/domains/client"
"platform/web/globals/orm"
@@ -108,14 +109,14 @@ func (s *authService) OauthPassword(ctx context.Context, _ *m.Client, data *Gran
if user == nil {
user = &m.User{
Phone: data.Username,
Username: data.Username,
Username: u.P(data.Username),
}
}
// 更新用户的登录时间
user.LastLogin = orm.LocalDateTime(time.Now())
user.LastLoginHost = ip
user.LastLoginAgent = agent
user.LastLogin = u.P(orm.LocalDateTime(time.Now()))
user.LastLoginHost = u.P(ip)
user.LastLoginAgent = u.P(agent)
if err := tx.User.Omit(q.User.AdminID).Save(user); err != nil {
return err
}
@@ -127,11 +128,15 @@ func (s *authService) OauthPassword(ctx context.Context, _ *m.Client, data *Gran
}
// 保存到会话
var name = ""
if user.Name != nil {
name = *user.Name
}
authCtx := auth2.Context{
Payload: auth2.Payload{
Id: user.ID,
Type: auth2.PayloadUser,
Name: user.Name,
Name: name,
Avatar: user.Avatar,
},
}

View File

@@ -163,8 +163,11 @@ func removeShortChannelExternal(proxies []*m.Proxy, channels []*m.Channel) error
if !ok {
return core.NewBizErr("代理不存在")
}
if proxy.Secret == nil {
return core.NewBizErr("代理未配置密钥")
}
var secret = strings.Split(proxy.Secret, ":")
var secret = strings.Split(*proxy.Secret, ":")
gateway := g.NewGateway(
proxy.Host,
secret[0],
@@ -322,27 +325,53 @@ func findResource(q *q.Query, resourceId int32, userId int32, count int, now tim
Active: resource.Active,
Type: resource2.Type(resource.Type),
}
switch resource2.Type(resource.Type) {
case resource2.TypeShort:
var sub = resource.Short
var dailyLast = time.Time{}
if sub.DailyLast != nil {
dailyLast = time.Time(*sub.DailyLast)
}
var expire = time.Time{}
if sub.Expire != nil {
expire = time.Time(*sub.Expire)
}
var quota int32
if sub.Quota != nil {
quota = *sub.Quota
}
info.Mode = resource2.Mode(sub.Type)
info.Live = sub.Live
info.DailyLimit = sub.DailyLimit
info.DailyUsed = sub.DailyUsed
info.DailyLast = time.Time(sub.DailyLast)
info.Quota = sub.Quota
info.DailyLast = dailyLast
info.Expire = expire
info.Quota = quota
info.Used = sub.Used
info.Expire = time.Time(sub.Expire)
case resource2.TypeLong:
var sub = resource.Long
var dailyLast = time.Time{}
if sub.DailyLast != nil {
dailyLast = time.Time(*sub.DailyLast)
}
var expire = time.Time{}
if sub.Expire != nil {
expire = time.Time(*sub.Expire)
}
var quota int32
if sub.Quota != nil {
quota = *sub.Quota
}
info.Mode = resource2.Mode(sub.Type)
info.Live = sub.Live
info.DailyLimit = sub.DailyLimit
info.DailyUsed = sub.DailyUsed
info.DailyLast = time.Time(sub.DailyLast)
info.Quota = sub.Quota
info.DailyLast = dailyLast
info.Expire = expire
info.Quota = quota
info.Used = sub.Used
info.Expire = time.Time(sub.Expire)
}
// 检查套餐状态
@@ -544,22 +573,22 @@ func assignShortChannels(q *q.Query, userId int32, resourceId int32, count int,
ResourceID: resourceId,
ProxyHost: proxy.Host,
ProxyPort: int32(port),
Protocol: int32(config.Protocol),
Protocol: u.P(int32(config.Protocol)),
Expiration: orm.LocalDateTime(config.Expiration),
}
if config.AuthIp {
portConf.Whitelist = &config.Whitelists
newChannel.AuthIP = true
newChannel.Whitelists = strings.Join(config.Whitelists, ",")
newChannel.Whitelists = u.P(strings.Join(config.Whitelists, ","))
}
if config.AuthPass {
username, password := genPassPair()
portConf.Userpass = u.P(fmt.Sprintf("%s:%s", username, password))
newChannel.AuthPass = true
newChannel.Username = username
newChannel.Password = password
newChannel.Username = &username
newChannel.Password = &password
}
portConfigs = append(portConfigs, portConf)
@@ -572,8 +601,10 @@ func assignShortChannels(q *q.Query, userId int32, resourceId int32, count int,
// 提交端口配置
if env.DebugExternalChange {
var step = time.Now()
var secret = strings.Split(proxy.Secret, ":")
if proxy.Secret == nil {
return nil, core.NewBizErr("代理未配置密钥")
}
var secret = strings.Split(*proxy.Secret, ":")
gateway := g.NewGateway(
proxy.Host,
secret[0],
@@ -648,11 +679,14 @@ func assignLongChannels(q *q.Query, userId int32, resourceId int32, count int, c
var proxies = make(map[int32]*m.Proxy)
var reqs = make(map[int32][]*g.ProxyPermitConfig)
for _, edge := range edges {
if _, ok := proxies[edge.ProxyID]; !ok {
proxies[edge.ProxyID] = &m.Proxy{
ID: edge.ProxyID,
if edge.ProxyID == nil || edge.ProxyPort == nil {
return nil, core.NewBizErr("节点配置不完整,缺少代理信息")
}
if _, ok := proxies[*edge.ProxyID]; !ok {
proxies[*edge.ProxyID] = &m.Proxy{
ID: *edge.ProxyID,
Host: edge.ProxyHost,
Secret: edge.ProxySecret,
Secret: &edge.ProxySecret,
}
}
@@ -668,28 +702,28 @@ func assignLongChannels(q *q.Query, userId int32, resourceId int32, count int, c
for range acc {
var channel = &m.Channel{
UserID: userId,
ProxyID: edge.ProxyID,
EdgeID: edge.ID,
ProxyID: *edge.ProxyID,
EdgeID: &edge.ID,
ResourceID: resourceId,
Protocol: int32(config.Protocol),
Protocol: u.P(int32(config.Protocol)),
AuthIP: config.AuthIp,
AuthPass: config.AuthPass,
Expiration: orm.LocalDateTime(config.Expiration),
ProxyHost: edge.ProxyHost,
ProxyPort: edge.ProxyPort,
ProxyPort: *edge.ProxyPort,
}
if config.AuthIp {
channel.Whitelists = strings.Join(config.Whitelists, ",")
channel.Whitelists = u.P(strings.Join(config.Whitelists, ","))
}
if config.AuthPass {
username, password := genPassPair()
channel.Username = username
channel.Password = password
channel.Username = &username
channel.Password = &password
}
channels = append(channels, channel)
req := &g.ProxyPermitConfig{
Id: channel.EdgeID,
Id: *channel.EdgeID,
Expire: time.Time(channel.Expiration),
}
@@ -698,11 +732,11 @@ func assignLongChannels(q *q.Query, userId int32, resourceId int32, count int, c
}
if channel.AuthPass {
req.Username = &channel.Username
req.Password = &channel.Password
req.Username = channel.Username
req.Password = channel.Password
}
reqs[edge.ProxyID] = append(reqs[edge.ProxyID], req)
reqs[*edge.ProxyID] = append(reqs[*edge.ProxyID], req)
}
}
@@ -711,7 +745,7 @@ func assignLongChannels(q *q.Query, userId int32, resourceId int32, count int, c
var step = time.Now()
for id, reqs := range reqs {
proxy := proxies[id]
err := g.Proxy.Permit(proxy.Host, proxy.Secret, reqs)
err := g.Proxy.Permit(proxy.Host, *proxy.Secret, reqs)
if err != nil {
return nil, core.NewBizErr("提交端口配置失败", err)
}

View File

@@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"platform/pkg/u"
bill2 "platform/web/domains/bill"
resource2 "platform/web/domains/resource"
trade2 "platform/web/domains/trade"
@@ -56,9 +57,9 @@ func (s *resourceService) CreateResource(uid int32, now time.Time, ser *CreateRe
// 生成账单
bill := m.Bill{
UserID: uid,
ResourceID: resource.ID,
ResourceID: &resource.ID,
BillNo: ID.GenReadable("bil"),
Info: "购买套餐 - " + name,
Info: u.P("购买套餐 - " + name),
Type: int32(bill2.TypeConsume),
Amount: amount,
}
@@ -185,7 +186,7 @@ func (s *resourceService) CompleteResource(tradeNo string, now time.Time, opResu
Select(q.Bill.ResourceID).
Updates(&m.Bill{
ID: cache.BillId,
ResourceID: resource.ID,
ResourceID: &resource.ID,
})
if err != nil {
return err
@@ -248,7 +249,7 @@ func createResource(q *q.Query, uid int32, now time.Time, data CreateResourceDat
// 套餐基本信息
var resource = m.Resource{
UserID: uid,
ResourceNo: ID.GenReadable("res"),
ResourceNo: u.P(ID.GenReadable("res")),
Active: true,
}
@@ -261,8 +262,8 @@ func createResource(q *q.Query, uid int32, now time.Time, data CreateResourceDat
resource.Short = &m.ResourceShort{
Type: data.Mode,
Live: data.Live,
Quota: data.Quota,
Expire: orm.LocalDateTime(now.Add(duration)),
Quota: &data.Quota,
Expire: u.P(orm.LocalDateTime(now.Add(duration))),
DailyLimit: data.DailyLimit,
}
@@ -273,8 +274,8 @@ func createResource(q *q.Query, uid int32, now time.Time, data CreateResourceDat
resource.Long = &m.ResourceLong{
Type: data.Mode,
Live: data.Live,
Quota: data.Quota,
Expire: orm.LocalDateTime(now.Add(duration)),
Quota: &data.Quota,
Expire: u.P(orm.LocalDateTime(now.Add(duration))),
DailyLimit: data.DailyLimit,
}
default:

View File

@@ -56,7 +56,7 @@ func (s *transactionService) PrepareTransaction(q *q.Query, uid int32, now time.
return nil, err
}
var expireAt = time.Time(coupon.ExpireAt)
var expireAt = time.Time(u.Z(coupon.ExpireAt))
if !expireAt.IsZero() && expireAt.Before(now) {
_, err = q.Coupon.
Where(q.Coupon.ID.Eq(coupon.ID)).
@@ -71,26 +71,26 @@ func (s *transactionService) PrepareTransaction(q *q.Query, uid int32, now time.
return nil, errors.New("订单金额未达到使用优惠券的条件")
}
switch {
// 该优惠券不属于当前用户
default:
return nil, errors.New("优惠券不属于当前用户")
// 公开优惠券
case coupon.UserID == 0:
amount = amount.Sub(coupon.Amount)
// 指定用户的优惠券
case coupon.UserID == uid:
amount = amount.Sub(coupon.Amount)
if time.Time(coupon.ExpireAt).IsZero() {
_, err = q.Coupon.
Where(q.Coupon.ID.Eq(coupon.ID)).
Update(q.Coupon.Status, int32(coupon2.StatusUsed))
if err != nil {
return nil, err
if coupon.UserID != nil {
switch *coupon.UserID {
// 指定用户的优惠券
case uid:
amount = amount.Sub(coupon.Amount)
if expireAt.IsZero() {
_, err = q.Coupon.
Where(q.Coupon.ID.Eq(coupon.ID)).
Update(q.Coupon.Status, int32(coupon2.StatusUsed))
if err != nil {
return nil, err
}
}
// 该优惠券不属于当前用户
default:
return nil, errors.New("优惠券不属于当前用户")
}
} else {
// 公开优惠券
amount = amount.Sub(coupon.Amount)
}
}
@@ -161,7 +161,7 @@ func (s *transactionService) PrepareTransaction(q *q.Query, uid int32, now time.
Method: int32(method),
Type: int32(tType),
Amount: amount,
PayURL: payUrl,
PayURL: &payUrl,
}
err = q.Trade.Create(&trade)
if err != nil {
@@ -172,8 +172,8 @@ func (s *transactionService) PrepareTransaction(q *q.Query, uid int32, now time.
var bill = m.Bill{
BillNo: ID.GenReadable("bil"),
UserID: uid,
TradeID: trade.ID,
Info: subject,
TradeID: &trade.ID,
Info: &subject,
Type: int32(billType),
Amount: amount,
}
@@ -284,10 +284,10 @@ func (s *transactionService) CompleteTransaction(q *q.Query, data *TransactionCo
// 如果是未支付,则更新支付状态
case trade2.StatusPending:
trade.Status = int32(trade2.StatusSuccess)
trade.OuterNo = transId
trade.OuterNo = &transId
trade.Payment = payment
trade.PaidAt = orm.LocalDateTime(paidAt)
trade.PayURL = ""
trade.PaidAt = u.P(orm.LocalDateTime(paidAt))
trade.PayURL = u.P("")
_, err = q.Trade.Updates(trade)
if err != nil {
return nil, err
@@ -340,8 +340,8 @@ func (s *transactionService) FinishTransaction(q *q.Query, tradeNo string, now t
Select(q.Trade.Status, q.Trade.CancelAt, q.Trade.PayURL).
Updates(m.Trade{
Status: int32(trade2.StatusCanceled),
CancelAt: orm.LocalDateTime(now),
PayURL: "",
CancelAt: u.P(orm.LocalDateTime(now)),
PayURL: u.P(""),
})
if err != nil {
return err