Skip to content

Commit 5e944d8

Browse files
committed
Add file
1 parent afe76b8 commit 5e944d8

19 files changed

+1610
-0
lines changed

array.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package utils
2+
3+
import (
4+
"github.com/thoas/go-funk"
5+
"reflect"
6+
)
7+
8+
func GetItemString(arr []string, index int) (out string, ok bool) {
9+
if len(arr) > index {
10+
out = arr[index]
11+
ok = true
12+
}
13+
return
14+
}
15+
16+
//
17+
func ArrJoin(arrL interface{}, arrR interface{}, keyL, keyR string, joinFunc interface{}) interface{} {
18+
rmap := ArrayToHashmap(arrR, keyR)
19+
funcVal := reflect.ValueOf(joinFunc)
20+
funk.ForEach(arrL, func(vl interface{}) {
21+
keyV := funk.Get(vl, keyL)
22+
if vr, ok := rmap.Get(keyV); ok {
23+
funcVal.Call([]reflect.Value{reflect.ValueOf(vl), reflect.ValueOf(vr)})
24+
}
25+
})
26+
return arrL
27+
}

config.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package utils
2+
3+
import (
4+
"io/ioutil"
5+
"gopkg.in/yaml.v2"
6+
"fmt"
7+
tconfig "github.com/RichardKnop/machinery/v1/config"
8+
)
9+
10+
//main config
11+
type MConfig struct {
12+
Debug bool
13+
Addr string //服务地址
14+
Host string
15+
Mysql string
16+
DefaultPassword string `yaml:"default_password"` //默认密码
17+
MediaPath string
18+
WebApps string
19+
MaxMediaSize string `yaml:"max_media_size"`
20+
CookieExpires int64 `yaml:"cookie_expires"`
21+
SiteCreator struct {
22+
Mysql string
23+
WpDir string
24+
}
25+
WxConfig WxConfig
26+
Redis RedisConf
27+
Task tconfig.Config
28+
29+
Cms struct {
30+
}
31+
32+
LibreOfficePath string `yaml:"libreoffice_path"`
33+
TestAdminSession string
34+
TestStudentSession string
35+
TestTeacherSession string
36+
}
37+
38+
type WxConfig struct {
39+
AppId string
40+
MchId string
41+
ApiKey string
42+
NotifyUrl string
43+
}
44+
45+
type RedisConf struct {
46+
Addr string
47+
Password string
48+
Database string
49+
UniqueIdKey string
50+
}
51+
52+
func LoadConfig(confPath string) (MConfig, error) {
53+
conf := MConfig{
54+
Debug: false,
55+
}
56+
if confPath == "" {
57+
58+
} else {
59+
confd, _ := ioutil.ReadFile(confPath)
60+
err := yaml.Unmarshal(confd, &conf)
61+
if err != nil {
62+
return conf, err
63+
} else {
64+
fmt.Println("MConfig loaded")
65+
}
66+
}
67+
return conf, nil
68+
}

consts.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
const (
4+
XSESSION_KEY = "xsession"
5+
)
6+
7+
var (
8+
DEBUG = true
9+
)

convert.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package utils
2+
3+
import "github.com/thoas/go-funk"
4+
5+
func ToInterfaceArrayString(t []string) []interface{} {
6+
s := make([]interface{}, len(t))
7+
for i, v := range t {
8+
s[i] = v
9+
}
10+
return s
11+
}
12+
13+
func ToInterfaceArray(m interface{}) []interface{} {
14+
return funk.Map(m, func(x interface{}) interface{} {
15+
return x
16+
}).([]interface{})
17+
}

crypto.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
import (
4+
"crypto/md5"
5+
"fmt"
6+
)
7+
8+
func Md5Sum(s string) string {
9+
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
10+
}

errors.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package utils
2+
3+
import (
4+
"github.com/go-errors/errors"
5+
sll "github.com/emirpasic/gods/lists/singlylinkedlist"
6+
)
7+
8+
var (
9+
ERR_REQUIRE_LOGIN = errors.New("require login")
10+
ERR_REQUIRE_ADMIN = errors.New("require admin")
11+
ERR_NO_ACCOUNT = errors.New("no such account")
12+
ERR_PARAMS = errors.New("params error")
13+
ERR_NO_PERMISSION = errors.New("no permission")
14+
ERR_CANNOT_MODIFY = errors.New("cannot modify")
15+
)
16+
17+
func Nothing(...interface{}) {
18+
19+
}
20+
21+
// 获取第一个错误
22+
func FirstError(es ...error) error {
23+
for _, e := range es {
24+
if e != nil {
25+
return e
26+
}
27+
}
28+
return nil
29+
}
30+
31+
type ErrorList struct {
32+
List *sll.List
33+
}
34+
35+
func NewErrorList() *ErrorList {
36+
l := &ErrorList{}
37+
l.List = sll.New()
38+
return l
39+
}
40+
41+
//只添加非nil的error
42+
func (l *ErrorList) AppendE(errs ...error) {
43+
for _, v := range errs {
44+
if v != nil {
45+
//l.List.Add(errors.Wrap(v, 1))
46+
l.List.Add(v)
47+
}
48+
}
49+
}
50+
51+
func (l *ErrorList) FirstError() error {
52+
_, err := l.List.Find(func(index int, value interface{}) bool {
53+
return value != nil
54+
})
55+
if err == nil {
56+
return nil
57+
}
58+
return err.(error)
59+
}
60+
61+
func (l *ErrorList) Panic() {
62+
if err := l.FirstError(); err != nil {
63+
panic(err)
64+
}
65+
}

handler.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package utils
2+
3+
import (
4+
"github.com/kataras/iris"
5+
"github.com/kataras/iris/context"
6+
"encoding/json"
7+
"github.com/go-playground/form"
8+
"github.com/json-iterator/go"
9+
)
10+
11+
type ErrJson struct {
12+
Err string `json:"err_msg"`
13+
}
14+
15+
func AfterHandler(ictx context.Context, o interface{}, err error) {
16+
if err != nil {
17+
ictx.StatusCode(iris.StatusBadRequest)
18+
ictx.JSON(ErrJson{Err: err.Error()})
19+
} else {
20+
var buf []byte
21+
offj, isffj := o.(json.Marshaler)
22+
if isffj {
23+
buf, err = offj.MarshalJSON()
24+
} else {
25+
buf, err = jsoniter.Marshal(o)
26+
}
27+
28+
if err != nil {
29+
ictx.StatusCode(iris.StatusBadRequest)
30+
ictx.JSON(ErrJson{Err: err.Error()})
31+
} else {
32+
ictx.StatusCode(iris.StatusOK)
33+
ictx.ContentType("application/json")
34+
ictx.Write(buf)
35+
}
36+
}
37+
//ictx.Next()
38+
}
39+
40+
//params必须是ptr
41+
func ParseForm(ictx context.Context, params interface{}) (err error) {
42+
dec := form.NewDecoder()
43+
err = dec.Decode(params, ictx.FormValues())
44+
if err != nil {
45+
ictx.Application().Logger().Warnf("Error when reading form: " + err.Error())
46+
err = ERR_PARAMS
47+
}
48+
return
49+
}

math.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
import (
4+
"math"
5+
)
6+
7+
func MinInt(a, b int) int {
8+
return int(math.Min(float64(a), float64(b)))
9+
}

0 commit comments

Comments
 (0)