wangfei 6 years ago
parent
commit
85653a11f4
4 changed files with 134 additions and 9 deletions
  1. 21
    8
      controllers/component.go
  2. 44
    0
      models/wechat/wechat.go
  3. 67
    0
      service/wechat/wechat.go
  4. 2
    1
      utils/wechat.go

+ 21
- 8
controllers/component.go View File

@@ -2,11 +2,13 @@ package controllers
2 2
 
3 3
 import (
4 4
 	"encoding/base64"
5
+	"encoding/json"
5 6
 	"io/ioutil"
6 7
 	"net/http"
7 8
 	"wechat-conf/models"
8 9
 	"wechat-conf/models/model"
9 10
 	"wechat-conf/service/autoreply"
11
+	"wechat-conf/service/wechat"
10 12
 	"wechat-conf/utils"
11 13
 
12 14
 	"github.com/kinisky564477/wechat/component"
@@ -17,7 +19,8 @@ import (
17 19
 // WechatController 用户
18 20
 type WechatController struct {
19 21
 	BaseController
20
-	serv *autoreply.AutoreplyServ
22
+	serv       *autoreply.AutoreplyServ
23
+	wechatServ *wechat.WechatServ
21 24
 }
22 25
 
23 26
 // Constructor 初始化 Controller
@@ -25,6 +28,7 @@ type WechatController struct {
25 28
 // @Description 初始化 Controller, 系统自动调用
26 29
 func (c *WechatController) Constructor() {
27 30
 	c.serv = autoreply.NewAutoreplyServ(c.Context)
31
+	c.wechatServ = wechat.NewWechatServ(c.Context)
28 32
 }
29 33
 
30 34
 const (
@@ -40,12 +44,7 @@ func (c *WechatController) ComponentPush() {
40 44
 	defer r.Body.Close()
41 45
 	con, _ := ioutil.ReadAll(r.Body)
42 46
 
43
-	// timestamp := c.GetString("timestamp")
44
-	// nonce := c.GetString("nonce")
45
-
46
-	// token := "testtoken"
47 47
 	EncodingAESKey := "key"
48
-	// appid := "appid"
49 48
 
50 49
 	AESKey, err := base64.StdEncoding.DecodeString(EncodingAESKey + "=")
51 50
 	if err != nil {
@@ -56,15 +55,18 @@ func (c *WechatController) ComponentPush() {
56 55
 	val, err := xp.Parse(string(con))
57 56
 	if err != nil {
58 57
 		utils.LogError("xml解析失败:", err)
58
+		c.ResponseRaw([]byte(""))
59 59
 	}
60 60
 
61 61
 	msgbyte, err := utils.AesDecrypt([]byte(val["Encrypt"]), AESKey)
62 62
 	if err != nil {
63 63
 		utils.LogError("解密失败:", err)
64
+		c.ResponseRaw([]byte(""))
64 65
 	}
65 66
 	msg, err := xp.Parse(string(msgbyte))
66 67
 	if err != nil {
67 68
 		utils.LogError("msgxml解析失败:", err)
69
+		c.ResponseRaw([]byte(""))
68 70
 	}
69 71
 	switch msg["InfoType"] {
70 72
 	case INFOTYPE_TICKET:
@@ -77,9 +79,20 @@ func (c *WechatController) ComponentPush() {
77 79
 			"appid":              msg["AuthorizerAppid"],
78 80
 			"authorization_code": msg["AuthorizationCode"],
79 81
 		}
80
-
81
-		wxclient := utils.WechatInit(cert)
82
+		wxclient := utils.WechatInit(cert, c.wechatServ.UpdateToken)
82 83
 		utils.AppendWxClient(wxclient)
84
+		// 插入数据库
85
+		mjson, _ := json.Marshal(msg)
86
+		var conf = model.SysWechatConf{
87
+			Appid:             msg["AuthorizerAppid"],
88
+			AuthorizationCode: msg["AuthorizationCode"],
89
+			AuthorizationInfo: string(mjson),
90
+		}
91
+		err := c.wechatServ.SaveWechatConf(conf)
92
+		if err != nil {
93
+			utils.LogError("保存微信授权信息失败:", err)
94
+			c.ResponseRaw([]byte(""))
95
+		}
83 96
 		c.ResponseRaw([]byte("success"))
84 97
 		break
85 98
 	case INFOTYPE_UPDATEAUTHORIZED:

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

@@ -0,0 +1,44 @@
1
+package wechat
2
+
3
+import (
4
+	"jcjy/demo/models"
5
+	"wechat-conf/models/model"
6
+	"wechat-conf/utils"
7
+
8
+	"github.com/go-xorm/xorm"
9
+)
10
+
11
+// WechatDAO 当前数据库操作对象
12
+type WechatDAO struct {
13
+	ctx *utils.Context
14
+	db  *xorm.Session
15
+}
16
+
17
+// NewWechatDAO New Inst
18
+func NewWechatDAO(ctx *utils.Context) *WechatDAO {
19
+	return &WechatDAO{
20
+		ctx: ctx,
21
+		db:  ctx.DB,
22
+	}
23
+}
24
+
25
+// GetWechatConfByAppID 根据appid获取配置信息
26
+func (m *WechatDAO) GetWechatConfByAppID(appid string) (*model.SysWechatConf, error) {
27
+	var conf model.SysWechatConf
28
+	_, err := m.db.Where("appid=?", appid).And("status>?", models.STATUS_DEL).Get(&conf)
29
+	return &conf, err
30
+}
31
+
32
+// SaveWechatConf 保存微信配置信息
33
+func (m *WechatDAO) SaveWechatConf(conf *model.SysWechatConf) error {
34
+	conf.ConfId = utils.GetGUID()
35
+	conf.Status = models.STATUS_READY
36
+	_, err := m.db.Insert(conf)
37
+	return err
38
+}
39
+
40
+// UpdateWechatConf 更新微信配置信息
41
+func (m *WechatDAO) UpdateWechatConf(conf model.SysWechatConf, cols []string) error {
42
+	_, err := m.db.Cols(cols...).Where("conf_id = ?", conf.ConfId).Update(conf)
43
+	return err
44
+}

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

@@ -0,0 +1,67 @@
1
+package wechat
2
+
3
+import (
4
+	"wechat-conf/models/model"
5
+	"wechat-conf/models/wechat"
6
+	"wechat-conf/utils"
7
+)
8
+
9
+// WechatServ 用户
10
+type WechatServ struct {
11
+	ctx *utils.Context
12
+	dao *wechat.WechatDAO
13
+}
14
+
15
+// NewWechatServ 初始化
16
+func NewWechatServ(ctx *utils.Context) *WechatServ {
17
+	return &WechatServ{
18
+		ctx: ctx,
19
+		dao: wechat.NewWechatDAO(ctx),
20
+	}
21
+}
22
+
23
+// SaveWechatConf 保存微信配置
24
+func (s *WechatServ) SaveWechatConf(conf model.SysWechatConf) error {
25
+	wechatConf, err := s.dao.GetWechatConfByAppID(conf.Appid)
26
+	if err != nil {
27
+		utils.LogError("根据appid获取微信配置失败:", err)
28
+		return err
29
+	}
30
+	if wechatConf != nil && wechatConf.ConfId != "" {
31
+		// 修改
32
+		conf.ConfId = wechatConf.ConfId
33
+		var cols = []string{
34
+			"refresh_token",
35
+			"token",
36
+		}
37
+		if conf.AuthorizationInfo != "" {
38
+			cols = append(cols, "authorization_info")
39
+		}
40
+		err := s.dao.UpdateWechatConf(conf, cols)
41
+		if err != nil {
42
+			utils.LogError("修改微信配置失败:", err)
43
+			return err
44
+		}
45
+	} else {
46
+		// 新增
47
+		err := s.dao.SaveWechatConf(&conf)
48
+		if err != nil {
49
+			utils.LogError("新增微信配置失败:", err)
50
+			return err
51
+		}
52
+	}
53
+	return nil
54
+}
55
+
56
+// UpdateToken 更新微信token
57
+func (s *WechatServ) UpdateToken(token map[string]string) {
58
+	var conf = model.SysWechatConf{
59
+		Token:        token["token"],
60
+		RefreshToken: token["refreshToken"],
61
+		Appid:        token["appid"],
62
+	}
63
+	err := s.SaveWechatConf(conf)
64
+	if err != nil {
65
+		utils.LogError("更新微信token失败:", err)
66
+	}
67
+}

+ 2
- 1
utils/wechat.go View File

@@ -31,13 +31,14 @@ func RefreshComponentTicket(ticket string) {
31 31
 }
32 32
 
33 33
 // WechatInit 微信初始化
34
-func WechatInit(cert map[string]string) *component.WxClient {
34
+func WechatInit(cert map[string]string, reflashToken func(map[string]string)) *component.WxClient {
35 35
 	var wechatClient *component.WxClient
36 36
 
37 37
 	wechatClient = component.NewWxClient(
38 38
 		cert,
39 39
 		Component.GetToken,
40 40
 		Component.GetCertificate,
41
+		reflashToken,
41 42
 	)
42 43
 
43 44
 	return wechatClient