wangfei 6 years ago
parent
commit
b2d4ffe269
7 changed files with 66 additions and 25 deletions
  1. 1
    1
      bootstrap/bootstrap.go
  2. 0
    4
      conf/wechat.conf
  3. 10
    5
      controllers/component.go
  4. 24
    0
      models/wechat.go
  5. 15
    0
      models/wechat/wechat.go
  6. 15
    0
      service/wechat/wechat.go
  7. 1
    15
      utils/wechat.go

+ 1
- 1
bootstrap/bootstrap.go View File

@@ -15,7 +15,7 @@ func SystemInit() {
15 15
 	utils.LogInit()
16 16
 
17 17
 	// 第三方平台
18
-	utils.ComponentInit()
18
+	models.ComponentInit()
19 19
 
20 20
 	// 微信系统
21 21
 	models.InitWechat()

+ 0
- 4
conf/wechat.conf View File

@@ -1,4 +0,0 @@
1
-[wechat]
2
-appid = wx9fd33312e78e8d02
3
-aeskey = 41b2994de43d4e3b9dc0f54ee8c5c1bb0050496367e
4
-secret = 69ee34668cd2635138b831f9ecb1fb4f

+ 10
- 5
controllers/component.go View File

@@ -11,7 +11,6 @@ import (
11 11
 	"wechat-conf/service/wechat"
12 12
 	"wechat-conf/utils"
13 13
 
14
-	"github.com/astaxie/beego/config"
15 14
 	"github.com/kinisky564477/wechat/component"
16 15
 
17 16
 	"github.com/zjxpcyc/wechat/core"
@@ -44,16 +43,17 @@ func (c *WechatController) ComponentPush() {
44 43
 	r := c.Ctx.Request
45 44
 	defer r.Body.Close()
46 45
 	con, _ := ioutil.ReadAll(r.Body)
47
-	conf, err := config.NewConfig("ini", utils.GetAppRoot()+"/conf/wechat.conf")
48
-	if err != nil {
46
+	conf, err := c.wechatServ.GetComponentInfo()
47
+	if err != nil || conf == nil || conf.Appid == "" {
49 48
 		utils.LogError("读取微信配置文件失败")
49
+		c.ResponseRaw([]byte(""))
50 50
 	}
51
-
52
-	EncodingAESKey := conf.String("wechat::aeskey")
51
+	EncodingAESKey := conf.Aeskey
53 52
 
54 53
 	AESKey, err := base64.StdEncoding.DecodeString(EncodingAESKey + "=")
55 54
 	if err != nil {
56 55
 		utils.LogError("DecodeString失败:", err)
56
+		c.ResponseRaw([]byte(""))
57 57
 	}
58 58
 	xp := &core.XMLParse{}
59 59
 	// 解析xml
@@ -77,6 +77,11 @@ func (c *WechatController) ComponentPush() {
77 77
 	switch msg["InfoType"] {
78 78
 	case INFOTYPE_TICKET:
79 79
 		// 更新ticket
80
+		conf.Ticket = msg["ComponentVerifyTicket"]
81
+		err := c.wechatServ.UpdateComponentTicket(conf)
82
+		if err != nil {
83
+			utils.LogError("修改ticket失败:", err)
84
+		}
80 85
 		utils.RefreshComponentTicket(msg["ComponentVerifyTicket"])
81 86
 		break
82 87
 	case INFOTYPE_AUTHORIZED:

+ 24
- 0
models/wechat.go View File

@@ -5,6 +5,30 @@ import (
5 5
 	"wechat-conf/utils"
6 6
 )
7 7
 
8
+// ComponentInit 初始化第三方
9
+func ComponentInit() {
10
+	var conf = model.SysComponentConf{}
11
+
12
+	_, err := DBEngine.Get(&conf)
13
+	if err != nil {
14
+		utils.LogError("初始化第三方失败:", err)
15
+		return
16
+	}
17
+	utils.LogError("获取第三方信息:", conf)
18
+	if conf.Appid == "" {
19
+		utils.LogError("初始化第三方数据失败,数据为空")
20
+		return
21
+	}
22
+	var cert = map[string]string{
23
+		"appid":                 conf.Appid,
24
+		"aeskey":                conf.Aeskey,
25
+		"secret":                conf.Secret,
26
+		"componentVerifyTicket": conf.Ticket,
27
+	}
28
+
29
+	utils.ComponentInit(cert)
30
+}
31
+
8 32
 // InitWechat 初始化微信
9 33
 func InitWechat() {
10 34
 	var wxconfs []model.SysWechatConf

+ 15
- 0
models/wechat/wechat.go View File

@@ -75,3 +75,18 @@ func (m *WechatDAO) SaveWechatConf(conf model.SysWechatConf) error {
75 75
 	}
76 76
 	return nil
77 77
 }
78
+
79
+// GetComponentInfo 获取第三方conf
80
+func (m *WechatDAO) GetComponentInfo() (*model.SysComponentConf, error) {
81
+	var conf = model.SysComponentConf{}
82
+	_, err := m.db.Get(&conf)
83
+	return &conf, err
84
+}
85
+
86
+// UpdateComponentTicket 更新第三方ticket
87
+func (m *WechatDAO) UpdateComponentTicket(conf *model.SysComponentConf) error {
88
+	_, err := m.db.Cols([]string{
89
+		"ticket",
90
+	}...).Where("appid = ?", conf.Appid).Update(conf)
91
+	return err
92
+}

+ 15
- 0
service/wechat/wechat.go View File

@@ -31,3 +31,18 @@ func (s *WechatServ) SaveWechatConf(conf model.SysWechatConf) error {
31 31
 func (s *WechatServ) UpdateToken(token map[string]string) {
32 32
 	models.UpdateToken(token)
33 33
 }
34
+
35
+// GetComponentInfo 获取第三方信息
36
+func (s *WechatServ) GetComponentInfo() (*model.SysComponentConf, error) {
37
+	conf, err := s.dao.GetComponentInfo()
38
+	if err != nil {
39
+		return nil, err
40
+	}
41
+	return conf, nil
42
+}
43
+
44
+// UpdateComponentTicket 更新微信ticket
45
+func (s *WechatServ) UpdateComponentTicket(conf *model.SysComponentConf) error {
46
+	err := s.dao.UpdateComponentTicket(conf)
47
+	return err
48
+}

+ 1
- 15
utils/wechat.go View File

@@ -1,7 +1,6 @@
1 1
 package utils
2 2
 
3 3
 import (
4
-	"github.com/astaxie/beego/config"
5 4
 	"github.com/kinisky564477/wechat/component"
6 5
 )
7 6
 
@@ -9,20 +8,7 @@ import (
9 8
 var Component *component.ComponentClient
10 9
 
11 10
 // ComponentInit 第三方初始化
12
-func ComponentInit() {
13
-	// 初始化第三方
14
-
15
-	// 读取配置文件
16
-	conf, err := config.NewConfig("ini", GetAppRoot()+"/conf/wechat.conf")
17
-	if err != nil {
18
-		LogError("读取微信配置文件失败")
19
-	}
20
-	var cert = map[string]string{
21
-		"appid":  conf.String("wechat::appid"),
22
-		"aeskey": conf.String("wechat::aeskey"),
23
-		"secret": conf.String("wechat::secret"),
24
-	}
25
-	LogError("微信配置信息:", cert)
11
+func ComponentInit(cert map[string]string) {
26 12
 	Component = component.NewComponentClient(cert)
27 13
 	LogError(Component)
28 14
 }