Selaa lähdekoodia

Merge branch 'master' of http://git.ycjcjy.com/default/wechat-conf

胡轶钦 6 vuotta sitten
vanhempi
commit
e94f3719ee
3 muutettua tiedostoa jossa 29 lisäystä ja 5 poistoa
  1. 23
    5
      controllers/component.go
  2. 2
    0
      controllers/wechatimg/wechatimt.go
  3. 4
    0
      service/wechat/wechat.go

+ 23
- 5
controllers/component.go Näytä tiedosto

@@ -1,9 +1,13 @@
1
+// 微信全网接入测试
2
+// https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318611&token=&lang=zh_CN
3
+
1 4
 package controllers
2 5
 
3 6
 import (
4 7
 	"bytes"
5 8
 	"encoding/base64"
6 9
 	"encoding/json"
10
+	"errors"
7 11
 	"net/http"
8 12
 	"strings"
9 13
 	"wechat-conf/models"
@@ -40,6 +44,10 @@ const (
40 44
 	WX_OFFICE_TEST_USERNAME = "gh_3c884a361561"
41 45
 )
42 46
 
47
+const (
48
+	RESPONSE_CUSTOM_MESSAGE = "response-custom-message"
49
+)
50
+
43 51
 var nilData = []byte("success")
44 52
 
45 53
 // Constructor 初始化 Controller
@@ -220,6 +228,14 @@ func (c *WechatController) WxReceive() {
220 228
 
221 229
 	// 获取返回内容
222 230
 	replyMessage, err := c.getReplayMessage(receiveData, wechat)
231
+	if err != nil && err.Error() == RESPONSE_CUSTOM_MESSAGE {
232
+		// 微信测试用例会跑到这边
233
+		// 模拟粉丝发送消息, 并立即调用客户接口, 反馈固定的字串
234
+		go wechat.SendCustomText(receiveData["FromUserName"], wechat.GetAuthCode()+"_from_api")
235
+
236
+		c.ResponseRaw(nil)
237
+	}
238
+
223 239
 	if err != nil || replyMessage == nil || len(replyMessage) == 0 {
224 240
 		c.ResponseRaw(nilData)
225 241
 	}
@@ -294,7 +310,6 @@ func (c *WechatController) validReceiveData(receiveData []byte, aesKey string, w
294 310
 // encryptReceiveMessage 加密需要发送的信息
295 311
 func (c *WechatController) encryptMessage(message []byte, aesKey, token, appID string, wechat *component.WxClient) ([]byte, error) {
296 312
 	cipher, err := core.NewCipher(token, aesKey, appID)
297
-	// cipher, err := core.NewCipher("pamtest", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG", "wxb11529c136998cb6")
298 313
 	if err != nil {
299 314
 		utils.LogError("NewCipher 失败: " + err.Error())
300 315
 		return nil, err
@@ -337,6 +352,7 @@ func (c *WechatController) getReplayMessage(receviceData map[string]string, wech
337 352
 				// 发送 query_auth_code , 返回空, 并立即调用客服接口
338 353
 			} else if strings.Index(content, "QUERY_AUTH_CODE") > -1 {
339 354
 				replay = nil
355
+				return nil, errors.New(RESPONSE_CUSTOM_MESSAGE)
340 356
 			}
341 357
 			break
342 358
 		}
@@ -369,10 +385,12 @@ func (c *WechatController) getReplayMessage(receviceData map[string]string, wech
369 385
 					return
370 386
 				}
371 387
 
372
-				replay = &model.TaAutoReply{
373
-					MessageType:      models.MESSAGE_TYPE_PARAGRAPH,
374
-					MessageParagraph: replyText,
375
-					IsUse:            models.AUTOREPLY_IS_USE_ON,
388
+				if replyText != "" {
389
+					replay = &model.TaAutoReply{
390
+						MessageType:      models.MESSAGE_TYPE_PARAGRAPH,
391
+						MessageParagraph: replyText,
392
+						IsUse:            models.AUTOREPLY_IS_USE_ON,
393
+					}
376 394
 				}
377 395
 			}
378 396
 			break

+ 2
- 0
controllers/wechatimg/wechatimt.go Näytä tiedosto

@@ -143,6 +143,8 @@ func (c *WechatImgController) AddNewImg() {
143 143
 	defer file.Close()
144 144
 
145 145
 	imgurl := "./upload/" + head.Filename
146
+	defer os.Remove(imgurl)
147
+
146 148
 	//创建文件
147 149
 	fW, err := os.Create(imgurl)
148 150
 	if err != nil {

+ 4
- 0
service/wechat/wechat.go Näytä tiedosto

@@ -104,5 +104,9 @@ func (s *WechatServ) GetMenuReplyText(key string) (string, error) {
104 104
 		return "", err
105 105
 	}
106 106
 
107
+	if res == nil {
108
+		return "", nil
109
+	}
110
+
107 111
 	return res.Value, nil
108 112
 }