瀏覽代碼

Merge branch 'dev' of http://git.ycjcjy.com/SpaceOfCheng/services into dev

wangfei 6 年之前
父節點
當前提交
89bc8c19a9
共有 5 個檔案被更改,包括 124 行新增23 行删除
  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 查看文件

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

+ 15
- 15
models/models.go 查看文件

@@ -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
 )
@@ -35,23 +34,24 @@ func NewDBEngine() *xorm.Engine {
35 34
 }
36 35
 
37 36
 func getMySQLDNS() string {
38
-	dbconf, _ := config.NewConfig("ini", "conf/db.conf")
37
+	// dbconf, _ := config.NewConfig("ini", "conf/db.conf")
39 38
 
40
-	conProt := dbconf.DefaultString("con_protocol", "tcp")
41
-	dbAddr := dbconf.DefaultString("db_addr", "localhost")
42
-	dbPort := dbconf.DefaultString("db_port", "3306")
43
-	userName := dbconf.DefaultString("username", "root")
44
-	password := dbconf.String("password")
45
-	database := dbconf.String("database")
46
-	charSet := dbconf.DefaultString("char_set", "utf8")
39
+	// conProt := dbconf.DefaultString("con_protocol", "tcp")
40
+	// dbAddr := dbconf.DefaultString("db_addr", "localhost")
41
+	// dbPort := dbconf.DefaultString("db_port", "3306")
42
+	// userName := dbconf.DefaultString("username", "root")
43
+	// password := dbconf.String("password")
44
+	// database := dbconf.String("database")
45
+	// charSet := dbconf.DefaultString("char_set", "utf8")
47 46
 
48
-	dns := userName
47
+	// dns := userName
49 48
 
50
-	if len(password) > 0 {
51
-		dns += ":" + password
52
-	}
49
+	// if len(password) > 0 {
50
+	// 	dns += ":" + password
51
+	// }
53 52
 
54
-	dns += "@" + conProt + "(" + dbAddr + ":" + dbPort + ")" + "/" + database + "?charset=" + charSet
53
+	// dns += "@" + conProt + "(" + dbAddr + ":" + dbPort + ")" + "/" + database + "?charset=" + charSet
55 54
 
56
-	return dns
55
+	// return dns
56
+	return ""
57 57
 }

+ 49
- 0
service/events.go 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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
+}