0b4a1b4594
- Updated OSSStorage validation errors to use internationalized messages. - Changed error messages in provider.go for unsupported file storage types. - Refactored tag_service.go to replace hardcoded error messages with internationalized versions. - Updated ticket_service.go to use internationalized error messages for various validation checks. - Refactored ticket_tag_service.go to use internationalized error messages for tag validation. - Changed ticket_view_service.go to use internationalized error messages for view validation. - Updated tool_catalog_service.go to use internationalized error messages for tool code validation. - Refactored user_service.go to replace error messages with internationalized versions. - Updated ws_service.go to use internationalized error messages for WebSocket handling. - Refactored wxwork_kf_inbound_service.go to use internationalized error messages for message handling. - Updated wxwork_kf_outbound_service.go to use internationalized error messages for outbound message handling. - Refactored wxwork_login_service.go to use internationalized error messages for login handling. - Updated login.go in wxwork package to use internationalized error messages for login state and ticket validation.
104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"agent-desk/internal/models"
|
|
"agent-desk/internal/pkg/dto"
|
|
"agent-desk/internal/pkg/errorsx"
|
|
"crypto/rand"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
func NormalizeNullableString(value *string) *string {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
v := strings.TrimSpace(*value)
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
return &v
|
|
}
|
|
|
|
func BuildAuditFields(operator *dto.AuthPrincipal) models.AuditFields {
|
|
now := time.Now()
|
|
fields := models.AuditFields{
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if operator != nil {
|
|
fields.CreateUserID = operator.UserID
|
|
fields.CreateUserName = operator.Username
|
|
fields.UpdateUserID = operator.UserID
|
|
fields.UpdateUserName = operator.Username
|
|
}
|
|
return fields
|
|
}
|
|
|
|
func FormatTimePtr(t *time.Time) string {
|
|
if t == nil || t.IsZero() {
|
|
return ""
|
|
}
|
|
return t.Format(time.DateTime)
|
|
}
|
|
|
|
func FormatTime(t time.Time) string {
|
|
if t.IsZero() {
|
|
return ""
|
|
}
|
|
return t.Format(time.DateTime)
|
|
}
|
|
|
|
func GenerateRandomPassword(length int) (string, error) {
|
|
if length <= 0 {
|
|
return "", errorsx.InvalidParamI18n("error.e0175")
|
|
}
|
|
|
|
const charset = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"
|
|
buf := make([]byte, length)
|
|
random := make([]byte, length)
|
|
if _, err := rand.Read(random); err != nil {
|
|
return "", err
|
|
}
|
|
for i := range buf {
|
|
buf[i] = charset[int(random[i])%len(charset)]
|
|
}
|
|
return string(buf), nil
|
|
}
|
|
|
|
func JoinInt64s(values []int64) string {
|
|
if len(values) == 0 {
|
|
return ""
|
|
}
|
|
parts := make([]string, 0, len(values))
|
|
for _, value := range values {
|
|
parts = append(parts, cast.ToString(value))
|
|
}
|
|
return strings.Join(parts, ",")
|
|
}
|
|
|
|
func SplitInt64s(raw string) []int64 {
|
|
parts := strings.Split(strings.TrimSpace(raw), ",")
|
|
ret := make([]int64, 0, len(parts))
|
|
seen := make(map[int64]struct{})
|
|
for _, part := range parts {
|
|
value := strings.TrimSpace(part)
|
|
if value == "" {
|
|
continue
|
|
}
|
|
id, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil || id <= 0 {
|
|
continue
|
|
}
|
|
if _, exists := seen[id]; exists {
|
|
continue
|
|
}
|
|
seen[id] = struct{}{}
|
|
ret = append(ret, id)
|
|
}
|
|
return ret
|
|
}
|