-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
133 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.