wangfei 6 år sedan
förälder
incheckning
68dd72ef5e

+ 1
- 1
conf/log.conf Visa fil

@@ -1,4 +1,4 @@
1 1
 [common]
2
-filename="E:\\GoProjects\\src\\spaceofcheng\\services\\log\\common.log"
2
+filename="E:\\GoProject\\src\\wechat-conf\\log\\common.log"
3 3
 # log level "emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"
4 4
 level="debug"

+ 3
- 0
conf/wechat.conf Visa fil

@@ -0,0 +1,3 @@
1
+[component]
2
+appid = test
3
+aeskey = aeskey

+ 31
- 15
controllers/component.go Visa fil

@@ -11,6 +11,8 @@ import (
11 11
 	"wechat-conf/service/wechat"
12 12
 	"wechat-conf/utils"
13 13
 
14
+	"github.com/astaxie/beego"
15
+
14 16
 	"github.com/kinisky564477/wechat/component"
15 17
 
16 18
 	"github.com/zjxpcyc/wechat/core"
@@ -40,40 +42,54 @@ const (
40 42
 
41 43
 // ComponentPush 第三方平台推送
42 44
 func (c *WechatController) ComponentPush() {
45
+	utils.LogError("推送开始")
43 46
 	r := c.Ctx.Request
44 47
 	defer r.Body.Close()
45 48
 	con, _ := ioutil.ReadAll(r.Body)
49
+	beego.Error(string(con))
50
+	// 解析xml
51
+	xp := &core.XMLParse{}
52
+	resMsg, err := xp.Parse(string(con))
53
+	if err != nil {
54
+		utils.LogError("xml解析失败:", err)
55
+		c.ResponseRaw([]byte("success"))
56
+	}
57
+	utils.LogError("解析xml成功:", resMsg)
58
+	encrypt := resMsg["Encrypt"]
59
+	beego.Error(encrypt)
46 60
 	conf, err := c.wechatServ.GetComponentInfo()
47 61
 	if err != nil || conf == nil || conf.Appid == "" {
48 62
 		utils.LogError("读取微信配置文件失败")
49
-		c.ResponseRaw([]byte(""))
63
+		c.ResponseRaw([]byte("success"))
50 64
 	}
65
+	beego.Error(conf.Aeskey)
51 66
 	EncodingAESKey := conf.Aeskey
52 67
 
53 68
 	AESKey, err := base64.StdEncoding.DecodeString(EncodingAESKey + "=")
54 69
 	if err != nil {
55 70
 		utils.LogError("DecodeString失败:", err)
56
-		c.ResponseRaw([]byte(""))
71
+		c.ResponseRaw([]byte("success"))
57 72
 	}
58
-	xp := &core.XMLParse{}
59
-	// 解析xml
60
-	val, err := xp.Parse(string(con))
73
+	beego.Error(AESKey)
74
+	EncryptVal, err := base64.StdEncoding.DecodeString(encrypt)
61 75
 	if err != nil {
62
-		utils.LogError("xml解析失败:", err)
63
-		c.ResponseRaw([]byte(""))
76
+		utils.LogError("密文解析失败:", err)
77
+		c.ResponseRaw([]byte("success"))
64 78
 	}
65
-
66
-	msgbyte, err := utils.AesDecrypt([]byte(val["Encrypt"]), AESKey)
79
+	msgbyte, err := utils.AesDecrypt(EncryptVal, AESKey)
67 80
 	if err != nil {
68 81
 		utils.LogError("解密失败:", err)
69
-		c.ResponseRaw([]byte(""))
82
+		c.ResponseRaw([]byte("success"))
70 83
 	}
84
+	beego.Error("解密数据:", string(msgbyte))
85
+	beego.Error("解密成功")
86
+	// 解析xml
71 87
 	msg, err := xp.Parse(string(msgbyte))
72 88
 	if err != nil {
73
-		utils.LogError("msgxml解析失败:", err)
74
-		c.ResponseRaw([]byte(""))
89
+		utils.LogError("xml解析失败:", err)
90
+		c.ResponseRaw([]byte("success"))
75 91
 	}
76
-	utils.LogError("接收微信推送消息:", msg)
92
+	beego.Error("解析xml成功:", msg)
77 93
 	switch msg["InfoType"] {
78 94
 	case INFOTYPE_TICKET:
79 95
 		// 更新ticket
@@ -102,9 +118,8 @@ func (c *WechatController) ComponentPush() {
102 118
 		err := c.wechatServ.SaveWechatConf(conf)
103 119
 		if err != nil {
104 120
 			utils.LogError("保存微信授权信息失败:", err)
105
-			c.ResponseRaw([]byte(""))
121
+			c.ResponseRaw([]byte("success"))
106 122
 		}
107
-		c.ResponseRaw([]byte("success"))
108 123
 		break
109 124
 	case INFOTYPE_UPDATEAUTHORIZED:
110 125
 		// 授权更新
@@ -113,6 +128,7 @@ func (c *WechatController) ComponentPush() {
113 128
 		// 取消授权
114 129
 		break
115 130
 	}
131
+	c.ResponseRaw([]byte("success"))
116 132
 }
117 133
 
118 134
 // WechatInfo 微信接入

+ 0
- 6
controllers/config.go Visa fil

@@ -34,10 +34,4 @@ func (c *BaseController) initConfig() {
34 34
 	if aliConf != nil {
35 35
 		c.Configer[AliYunConf] = aliConf
36 36
 	}
37
-
38
-	// 微信
39
-	wechatConf, _ := utils.GetConfiger("conf/wechat.conf")
40
-	if wechatConf != nil {
41
-		c.Configer[WeChatConf] = wechatConf
42
-	}
43 37
 }

+ 9
- 5
models/model/sys_component_conf.go Visa fil

@@ -1,9 +1,13 @@
1 1
 package model
2 2
 
3
+import "time"
4
+
3 5
 type SysComponentConf struct {
4
-	Wxid   string `xorm:"VARCHAR(20)"`
5
-	Appid  string `xorm:"VARCHAR(50)"`
6
-	Aeskey string `xorm:"VARCHAR(100)"`
7
-	Secret string `xorm:"VARCHAR(100)"`
8
-	Ticket string `xorm:"VARCHAR(100)"`
6
+	Wxid        string    `xorm:"VARCHAR(20)"`
7
+	Appid       string    `xorm:"VARCHAR(50)"`
8
+	Aeskey      string    `xorm:"VARCHAR(100)"`
9
+	Secret      string    `xorm:"VARCHAR(100)"`
10
+	Ticket      string    `xorm:"VARCHAR(100)"`
11
+	Token       string    `xorm:"VARCHAR(100)"`
12
+	ExpiresDate time.Time `xorm:"DATETIME"`
9 13
 }

+ 1
- 0
models/model/sys_wechat_conf.go Visa fil

@@ -14,4 +14,5 @@ type SysWechatConf struct {
14 14
 	Status            int       `xorm:"SMALLINT(6)"`
15 15
 	AuthorizationInfo string    `xorm:"TEXT"`
16 16
 	BindDate          time.Time `xorm:"DATETIME"`
17
+	ExpiresDate       time.Time `xorm:"DATETIME"`
17 18
 }

+ 42
- 1
models/wechat.go Visa fil

@@ -1,8 +1,11 @@
1 1
 package models
2 2
 
3 3
 import (
4
+	"time"
4 5
 	"wechat-conf/models/model"
5 6
 	"wechat-conf/utils"
7
+
8
+	"github.com/astaxie/beego"
6 9
 )
7 10
 
8 11
 // ComponentInit 初始化第三方
@@ -19,14 +22,52 @@ func ComponentInit() {
19 22
 		utils.LogError("初始化第三方数据失败,数据为空")
20 23
 		return
21 24
 	}
25
+	d := "0"
26
+	if !conf.ExpiresDate.IsZero() {
27
+		now := time.Now()
28
+		d = string(conf.ExpiresDate.Sub(now))
29
+	}
30
+
22 31
 	var cert = map[string]string{
23 32
 		"appid":                 conf.Appid,
24 33
 		"aeskey":                conf.Aeskey,
25 34
 		"secret":                conf.Secret,
26 35
 		"componentVerifyTicket": conf.Ticket,
36
+		"token":                 conf.Token,
37
+		"expires_in":            d,
27 38
 	}
28 39
 
29
-	utils.ComponentInit(cert)
40
+	utils.ComponentInit(cert, UpdateComponentToken)
41
+}
42
+
43
+// UpdateComponentToken 更新第三方token
44
+func UpdateComponentToken(token map[string]interface{}) {
45
+	beego.Error("更新第三方token:", token)
46
+	if token["accessToken"].(string) == "" {
47
+		return
48
+	}
49
+
50
+	var conf = model.SysComponentConf{
51
+		Token: token["accessToken"].(string),
52
+	}
53
+
54
+	expires := token["expire_in"].(int)
55
+	beego.Error(expires)
56
+	if expires > 0 {
57
+		expriesTime := time.Now().Local().Add(time.Duration(expires))
58
+		conf.ExpiresDate = expriesTime
59
+		sql := `update sys_component_conf set token=?,expires_date=?`
60
+		_, err := DBEngine.Exec(sql, conf.Token, conf.ExpiresDate)
61
+		if err != nil {
62
+			utils.LogError("更新第三方token失败:", err)
63
+		}
64
+	} else {
65
+		sql := `update sys_component_conf set token=?`
66
+		_, err := DBEngine.Exec(sql, conf.Token, conf.ExpiresDate)
67
+		if err != nil {
68
+			utils.LogError("更新第三方token失败:", err)
69
+		}
70
+	}
30 71
 }
31 72
 
32 73
 // InitWechat 初始化微信

+ 5
- 0
utils/aes.go Visa fil

@@ -4,6 +4,8 @@ import (
4 4
 	"bytes"
5 5
 	"crypto/aes"
6 6
 	"crypto/cipher"
7
+
8
+	"github.com/astaxie/beego"
7 9
 )
8 10
 
9 11
 // PKCS7Padding 填充序列的每个字节都填paddingSize
@@ -38,11 +40,14 @@ func AesEncrypt(origData, key []byte) ([]byte, error) {
38 40
 func AesDecrypt(crypted, key []byte) ([]byte, error) {
39 41
 	block, err := aes.NewCipher(key)
40 42
 	if err != nil {
43
+		beego.Error(err)
41 44
 		return nil, err
42 45
 	}
43 46
 	blockSize := block.BlockSize()
44 47
 	blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
45 48
 	origData := make([]byte, len(crypted))
49
+	beego.Error(origData)
50
+	beego.Error(crypted)
46 51
 	blockMode.CryptBlocks(origData, crypted)
47 52
 	origData = PKCS7UnPadding(origData)
48 53
 	return origData, nil

+ 2
- 2
utils/wechat.go Visa fil

@@ -8,8 +8,8 @@ import (
8 8
 var Component *component.ComponentClient
9 9
 
10 10
 // ComponentInit 第三方初始化
11
-func ComponentInit(cert map[string]string) {
12
-	Component = component.NewComponentClient(cert)
11
+func ComponentInit(cert map[string]string, updateToken func(map[string]interface{})) {
12
+	Component = component.NewComponentClient(cert, updateToken)
13 13
 	LogError(Component)
14 14
 }
15 15