浏览代码

add send wx message

zjxpcyc 6 年前
父节点
当前提交
8e80b453bc

+ 5
- 0
conf/wechat.conf 查看文件

@@ -1 +1,6 @@
1 1
 messageTplID = EB1Bto8JmW-vC-A0NTe8NtxgMFm6s86l3g2Bc-1OP6U
2
+
3
+# 中奖ID
4
+luckdrawMessageTplID = 8t7WQTrx-D566ohRD6RgdMkiu8DUiAxG_Jajg33YHVI
5
+luckdrawNoticeURL = http://dev.ycjcjy.com/game/luckdraw/#/lotteryList?tab=1
6
+flashBuyNoticeURL = http://dev.ycjcjy.com/game/luckdraw/#/lotteryList?tab=2

+ 6
- 0
controllers/config.go 查看文件

@@ -41,4 +41,10 @@ func (c *BaseController) initConfig() {
41 41
 		c.Configer[SMSConf] = smsConf
42 42
 	}
43 43
 	utils.ResetSMSEngine(smsConf)
44
+
45
+	// 微信
46
+	wechatConf, _ := utils.GetConfiger("conf/wechat.conf")
47
+	if wechatConf != nil {
48
+		c.Configer[WeChatConf] = wechatConf
49
+	}
44 50
 }

+ 4
- 0
controllers/flashbuy/flashbuy.go 查看文件

@@ -137,6 +137,10 @@ func (c *FlashBuyController) Verify() {
137 137
 // FlashBuy 抢购
138 138
 func (c *FlashBuyController) FlashBuy() {
139 139
 	id := c.GetString(":id")
140
+
141
+	c.Context.Set("clienturl", c.Configer[controllers.WeChatConf].String("flashBuyNoticeURL"))
142
+	c.Context.Set("tplid", c.Configer[controllers.WeChatConf].String("luckdrawMessageTplID"))
143
+
140 144
 	err := c.dao.FlashBuy(id)
141 145
 	if err != nil {
142 146
 		c.ResponseError(err)

+ 3
- 0
controllers/luckdraw/luckdraw.go 查看文件

@@ -191,6 +191,9 @@ func (c *LuckDrawController) LuckDraw() {
191 191
 
192 192
 	user := userRaw.(model.TaCustomer)
193 193
 
194
+	c.Context.Set("clienturl", c.Configer[controllers.AppConf].String("luckdrawNoticeURL"))
195
+	c.Context.Set("tplid", c.Configer[controllers.WeChatConf].String("luckdrawMessageTplID"))
196
+
194 197
 	prize, detail, err := c.serv.LuckDraw(id, &user)
195 198
 	if err != nil {
196 199
 		c.ResponseError(err)

+ 36
- 1
service/flashbuy/flashbuy.go 查看文件

@@ -2,6 +2,7 @@ package flashbuy
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"fmt"
5 6
 	"spaceofcheng/services/models"
6 7
 	"spaceofcheng/services/models/course"
7 8
 	"spaceofcheng/services/models/flashbuy"
@@ -258,7 +259,7 @@ func (s *FlashBuyServ) FlashBuy(id string) error {
258 259
 	custQRCode := model.TaCustomerCourseQrcode{
259 260
 		CustomerCourseId: recored.CustomerFlashBuyId,
260 261
 		CustomerQrcode:   utils.GenerateQRCode(),
261
-		CreateDate:       time.Now(),
262
+		CreateDate:       time.Now().Local(),
262 263
 		Status:           models.STATUS_NORMAL,
263 264
 		QrcodeType:       models.QRCODE_TYPE_FLASH,
264 265
 	}
@@ -268,6 +269,40 @@ func (s *FlashBuyServ) FlashBuy(id string) error {
268 269
 		return errors.New("写入中奖二维码失败")
269 270
 	}
270 271
 
272
+	// 推送微信消息
273
+	userMap := s.ctx.Get("userMap").(model.TaUserMapping)
274
+	userCases := s.ctx.Get("cases").([]model.SysUserCase)
275
+	clienturl := s.ctx.Get("clienturl").(string)
276
+	tplID := s.ctx.Get("tplid").(string)
277
+
278
+	var caseName string
279
+	if userCases != nil && len(userCases) > 0 {
280
+		for _, cs := range userCases {
281
+			if cs.CaseId == recored.CaseId {
282
+				caseName = cs.CaseName
283
+				break
284
+			}
285
+		}
286
+	}
287
+
288
+	remarkTpl := "请在%s日前, 前往%s兑换"
289
+
290
+	message := utils.Message{
291
+		To: utils.ClientID{ID: userMap.Openid},
292
+		Data: map[string]interface{}{
293
+			"orgid": recored.OrgId,
294
+			"tplid": tplID,
295
+			"link":  clienturl,
296
+			"data": map[string]string{
297
+				"first":    "抢券成功提醒",
298
+				"keyword1": flashbuy.FlashBuyName,
299
+				"keyword2": time.Now().Local().Format("2006-01-02"),
300
+				"remark":   fmt.Sprintf(remarkTpl, recored.ValidateEnd.Format("2006-01-02"), caseName),
301
+			},
302
+		},
303
+	}
304
+	go utils.SendWechat(message)
305
+
271 306
 	return nil
272 307
 }
273 308
 

+ 35
- 0
service/luckdraw/luckdraw.go 查看文件

@@ -2,6 +2,7 @@ package luckdraw
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"fmt"
5 6
 	"spaceofcheng/services/models"
6 7
 	"spaceofcheng/services/models/course"
7 8
 	"spaceofcheng/services/models/luckdraw"
@@ -328,6 +329,40 @@ func (s *LuckdrawServ) LuckDraw(id string, user *model.TaCustomer) (*model.TaLuc
328 329
 		return nil, nil, errors.New("写入中奖二维码失败")
329 330
 	}
330 331
 
332
+	// 推送微信消息
333
+	userMap := s.ctx.Get("userMap").(model.TaUserMapping)
334
+	userCases := s.ctx.Get("cases").([]model.SysUserCase)
335
+	clienturl := s.ctx.Get("clienturl").(string)
336
+	tplID := s.ctx.Get("tplid").(string)
337
+
338
+	var caseName string
339
+	if userCases != nil && len(userCases) > 0 {
340
+		for _, cs := range userCases {
341
+			if cs.CaseId == recored.CaseId {
342
+				caseName = cs.CaseName
343
+				break
344
+			}
345
+		}
346
+	}
347
+
348
+	remarkTpl := "请在%s日前, 前往%s兑换"
349
+
350
+	message := utils.Message{
351
+		To: utils.ClientID{ID: userMap.Openid},
352
+		Data: map[string]interface{}{
353
+			"orgid": recored.OrgId,
354
+			"tplid": tplID,
355
+			"link":  clienturl,
356
+			"data": map[string]string{
357
+				"first":    "抢券成功提醒",
358
+				"keyword1": prize.PrizeName,
359
+				"keyword2": time.Now().Local().Format("2006-01-02"),
360
+				"remark":   fmt.Sprintf(remarkTpl, recored.VerificationEnd.Format("2006-01-02"), caseName),
361
+			},
362
+		},
363
+	}
364
+	go utils.SendWechat(message)
365
+
331 366
 	return prize, prizeDetail, nil
332 367
 }
333 368
 

+ 23
- 2
utils/message.go 查看文件

@@ -2,6 +2,7 @@ package utils
2 2
 
3 3
 import (
4 4
 	"github.com/zjxpcyc/websocket"
5
+	"github.com/zjxpcyc/wechat/wx"
5 6
 )
6 7
 
7 8
 // ClientID 消息对象
@@ -29,17 +30,37 @@ func SendWs(message Message) {
29 30
 		Data: message.Data,
30 31
 	}
31 32
 
32
-	LogError("发送 Websocket 消息: ", msg)
33
-
34 33
 	websocket.Send(msg)
35 34
 }
36 35
 
36
+func SendWechat(message Message) {
37
+	dt := message.Data.(map[string]interface{})
38
+	org := dt["orgid"].(string)
39
+	tplID := dt["tplid"].(string)
40
+	tplLink := dt["link"].(string)
41
+	tplData := dt["data"].(map[string]string)
42
+
43
+	var data map[string]wx.TplMessageData
44
+	if tplData != nil {
45
+		data = make(map[string]wx.TplMessageData)
46
+		for k, v := range tplData {
47
+			data[k] = wx.TplMessageData{Value: v}
48
+		}
49
+	}
50
+
51
+	err := WxClientFor(org).SendTplMessage(message.To.ID, tplID, tplLink, data)
52
+	if err != nil {
53
+		LogError("推送微信消息失败: ", err)
54
+	}
55
+}
56
+
37 57
 // SendMessage 发送消息
38 58
 func SendMessage(message Message, msgType string) {
39 59
 	switch msgType {
40 60
 	case WsMessage:
41 61
 		SendWs(message)
42 62
 	case WechatMessage:
63
+
43 64
 	case SMSMessage:
44 65
 	default:
45 66
 	}