Kaynağa Gözat

add feat:event

zjxpcyc 6 yıl önce
ebeveyn
işleme
3e3d0e18c5
5 değiştirilmiş dosya ile 124 ekleme ve 23 silme
  1. 8
    8
      controllers/base.go
  2. 15
    15
      models/models.go
  3. 49
    0
      service/events.go
  4. 27
    0
      utils/event.go
  5. 25
    0
      utils/log.go

+ 8
- 8
controllers/base.go Dosyayı Görüntüle

@@ -27,21 +27,21 @@ func (c *BaseController) Prepare() {
27 27
 
28 28
 // ResponseJSON 返回JSON数据
29 29
 func (c *BaseController) ResponseJSON(data interface{}) {
30
-	c.ResponseData(data, "")
30
+	c.ResponseData(data, "", http.StatusOK)
31 31
 }
32 32
 
33 33
 // ResponseError 返回错误
34 34
 func (c *BaseController) ResponseError(err error, code ...int) {
35
-	c.ResponseData(nil, err, code...)
36
-}
37
-
38
-// ResponseData 自定义的JSON返回
39
-func (c *BaseController) ResponseData(data interface{}, msg interface{}, code ...int) {
40
-	status := http.StatusOK
41 35
 	if len(code) > 0 {
42
-		status = code[0]
36
+		c.ResponseData(nil, err, code[0])
43 37
 	}
44 38
 
39
+	c.ResponseData(nil, err, http.StatusBadRequest)
40
+}
41
+
42
+// ResponseData 自定义的JSON返回
43
+func (c *BaseController) ResponseData(data interface{}, msg interface{}, code int) {
44
+	status := code
45 45
 	c.destroyContext(status < http.StatusMultipleChoices)
46 46
 
47 47
 	sendMessage := ""

+ 15
- 15
models/models.go Dosyayı Görüntüle

@@ -1,7 +1,6 @@
1 1
 package models
2 2
 
3 3
 import (
4
-	"github.com/astaxie/beego/config"
5 4
 	_ "github.com/go-sql-driver/mysql"
6 5
 	"github.com/go-xorm/xorm"
7 6
 )
@@ -30,23 +29,24 @@ func NewDBEngine() *xorm.Engine {
30 29
 }
31 30
 
32 31
 func getMySQLDNS() string {
33
-	dbconf, _ := config.NewConfig("ini", "conf/db.conf")
32
+	// dbconf, _ := config.NewConfig("ini", "conf/db.conf")
34 33
 
35
-	conProt := dbconf.DefaultString("con_protocol", "tcp")
36
-	dbAddr := dbconf.DefaultString("db_addr", "localhost")
37
-	dbPort := dbconf.DefaultString("db_port", "3306")
38
-	userName := dbconf.DefaultString("username", "root")
39
-	password := dbconf.String("password")
40
-	database := dbconf.String("database")
41
-	charSet := dbconf.DefaultString("char_set", "utf8")
34
+	// conProt := dbconf.DefaultString("con_protocol", "tcp")
35
+	// dbAddr := dbconf.DefaultString("db_addr", "localhost")
36
+	// dbPort := dbconf.DefaultString("db_port", "3306")
37
+	// userName := dbconf.DefaultString("username", "root")
38
+	// password := dbconf.String("password")
39
+	// database := dbconf.String("database")
40
+	// charSet := dbconf.DefaultString("char_set", "utf8")
42 41
 
43
-	dns := userName
42
+	// dns := userName
44 43
 
45
-	if len(password) > 0 {
46
-		dns += ":" + password
47
-	}
44
+	// if len(password) > 0 {
45
+	// 	dns += ":" + password
46
+	// }
48 47
 
49
-	dns += "@" + conProt + "(" + dbAddr + ":" + dbPort + ")" + "/" + database + "?charset=" + charSet
48
+	// dns += "@" + conProt + "(" + dbAddr + ":" + dbPort + ")" + "/" + database + "?charset=" + charSet
50 49
 
51
-	return dns
50
+	// return dns
51
+	return ""
52 52
 }

+ 49
- 0
service/events.go Dosyayı Görüntüle

@@ -1,4 +1,53 @@
1 1
 package service
2 2
 
3
+import (
4
+	"reflect"
5
+	"spaceofcheng/services/utils"
6
+
7
+	"github.com/zjxpcyc/tinyevent"
8
+)
9
+
10
+// 内置事件列表
11
+const (
12
+	EvtRegiste = "reigste"
13
+)
14
+
3 15
 // EventServ 事件 Serv
4 16
 type EventServ struct{}
17
+
18
+// ListenAllEvent 监听所有事件
19
+func (s *EventServ) ListenAllEvent() {
20
+	// 所有需要执行的事件
21
+	allEvts, err := s.getAllEvents()
22
+	if err != nil {
23
+		utils.LogError(err)
24
+	}
25
+
26
+	// 刷新中控中心
27
+	utils.ResetEvtBus()
28
+
29
+	for evt, mth := range allEvts {
30
+		act := func(e tinyevent.Event) error {
31
+			s.execAction(e, mth)
32
+			return nil
33
+		}
34
+
35
+		utils.ListenEvent(evt, act)
36
+	}
37
+}
38
+
39
+// ExecAction 反射执行动作
40
+func (s *EventServ) execAction(e tinyevent.Event, mth string) {
41
+	this := reflect.ValueOf(s)
42
+	v := this.MethodByName(mth)
43
+	if v.IsValid() {
44
+		v.Call([]reflect.Value{reflect.ValueOf(e)})
45
+	}
46
+}
47
+
48
+func (s *EventServ) getAllEvents() (map[string]string, error) {
49
+	// todo
50
+	return map[string]string{
51
+		"foo": "Foo",
52
+	}, nil
53
+}

+ 27
- 0
utils/event.go Dosyayı Görüntüle

@@ -0,0 +1,27 @@
1
+package utils
2
+
3
+import (
4
+	"github.com/zjxpcyc/tinyevent"
5
+)
6
+
7
+var evtBus = &tinyevent.DefaultBus{}
8
+
9
+// EmitEvent 执行事件
10
+func EmitEvent(evt string, payload []byte) {
11
+	e := tinyevent.Event{
12
+		Name:    evt,
13
+		Payload: payload,
14
+	}
15
+
16
+	evtBus.Emit(e)
17
+}
18
+
19
+// ListenEvent 注册监听
20
+func ListenEvent(evt string, act tinyevent.Action) {
21
+	evtBus.On(evt, act)
22
+}
23
+
24
+// ResetEvtBus 重置一个事件中控中心
25
+func ResetEvtBus() {
26
+	evtBus = &tinyevent.DefaultBus{}
27
+}

+ 25
- 0
utils/log.go Dosyayı Görüntüle

@@ -0,0 +1,25 @@
1
+package utils
2
+
3
+import (
4
+	"errors"
5
+
6
+	"github.com/astaxie/beego"
7
+)
8
+
9
+// LogError 错误日志
10
+func LogError(v ...interface{}) error {
11
+	beego.Error(v...)
12
+
13
+	if len(v) > 0 {
14
+		switch err := v[0].(type) {
15
+		case string:
16
+			return errors.New(err)
17
+		case error:
18
+			return err
19
+		default:
20
+			return errors.New("Unknown error type")
21
+		}
22
+	}
23
+
24
+	return nil
25
+}