Skip to content

Commit

Permalink
修改utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gzxgogh committed Jun 6, 2023
1 parent 027d673 commit 00dc858
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 113 deletions.
2 changes: 1 addition & 1 deletion model/result.go → models/result.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package models

type Result struct {
Status int `json:"status" bson:"status"`
Expand Down
28 changes: 28 additions & 0 deletions utils/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"encoding/json"
"strings"
)

func FromJSON(j string, o interface{}) *interface{} {
err := json.Unmarshal([]byte(j), &o)
if err != nil {
return nil
} else {
return &o
}
}

func ToJSON(o interface{}) string {
j, err := json.Marshal(o)
if err != nil {
return "{}"
} else {
js := string(j)
js = strings.Replace(js, "\\u003c", "<", -1)
js = strings.Replace(js, "\\u003e", ">", -1)
js = strings.Replace(js, "\\u0026", "&", -1)
return js
}
}
43 changes: 43 additions & 0 deletions utils/param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package utils

import (
"github.com/gin-gonic/gin"
"strings"
)

func GinParamMap(c *gin.Context) map[string]string {
params := make(map[string]string)
if c.Request.Method == "GET" {
for k, v := range c.Request.URL.Query() {
params[k] = v[0]
}
return params
} else if c.Request.Method == "POST" {
if strings.Contains(c.ContentType(), "x-www-form-urlencoded") {
c.Request.ParseForm()
for k, v := range c.Request.PostForm {
params[k] = v[0]
}
for k, v := range c.Request.URL.Query() {
params[k] = v[0]
}
} else if strings.Contains(c.ContentType(), "multipart/form-data") {
c.Request.ParseMultipartForm(100 * 1024 * 1024)
for k, v := range c.Request.MultipartForm.Value {
params[k] = v[0]
}
for k, v := range c.Request.URL.Query() {
params[k] = v[0]
}
}
}
return params
}

func GinHeaders(c *gin.Context) map[string]string {
headers := make(map[string]string)
for k, v := range c.Request.Header {
headers[k] = v[0]
}
return headers
}
11 changes: 11 additions & 0 deletions utils/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import (
"math/rand"
"time"
)

func GetRandomWithAll(min, max int) int64 {
rand.Seed(time.Now().UnixNano())
return int64(rand.Intn(max-min+1) + min)
}
50 changes: 50 additions & 0 deletions utils/timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package utils

import (
"database/sql/driver"
"fmt"
"strconv"
"time"
)

type Timestamp time.Time

// MarshalJSON implements json.Marshaler.
func (t Timestamp) MarshalJSON() ([]byte, error) {
//entity your serializing here
stamp := fmt.Sprintf("%d", time.Time(t).Unix())
return []byte(stamp), nil
}

func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
var ts int64
ts, err = strconv.ParseInt(string(data), 10, 64)
if err != nil {
return err
}
theTime := time.Unix(ts, 0)
*t = Timestamp(theTime)
return nil
}

func (t Timestamp) Value() (driver.Value, error) {
return time.Time(t), nil
}

func (t *Timestamp) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = Timestamp(value)
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}

func (t Timestamp) GetTime() time.Time {
return time.Time(t)
}

// GetUnixTimeSql 获取unix时间戳sql
func GetUnixTimeSql(unixTime int64) string {
return fmt.Sprintf("FROM_UNIXTIME(%d)", unixTime)
}
112 changes: 0 additions & 112 deletions utils/uitls.go

This file was deleted.

0 comments on commit 00dc858

Please sign in to comment.