zjxpcyc 6 лет назад
Родитель
Сommit
b41326d14a
1 измененных файлов: 17 добавлений и 1 удалений
  1. 17
    1
      controllers/component.go

+ 17
- 1
controllers/component.go Просмотреть файл

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