Parcourir la source

add send wx message

zjxpcyc il y a 6 ans
Parent
révision
8e80b453bc

+ 5
- 0
conf/wechat.conf Voir le fichier

1
 messageTplID = EB1Bto8JmW-vC-A0NTe8NtxgMFm6s86l3g2Bc-1OP6U
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 Voir le fichier

41
 		c.Configer[SMSConf] = smsConf
41
 		c.Configer[SMSConf] = smsConf
42
 	}
42
 	}
43
 	utils.ResetSMSEngine(smsConf)
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 Voir le fichier

137
 // FlashBuy 抢购
137
 // FlashBuy 抢购
138
 func (c *FlashBuyController) FlashBuy() {
138
 func (c *FlashBuyController) FlashBuy() {
139
 	id := c.GetString(":id")
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
 	err := c.dao.FlashBuy(id)
144
 	err := c.dao.FlashBuy(id)
141
 	if err != nil {
145
 	if err != nil {
142
 		c.ResponseError(err)
146
 		c.ResponseError(err)

+ 3
- 0
controllers/luckdraw/luckdraw.go Voir le fichier

191
 
191
 
192
 	user := userRaw.(model.TaCustomer)
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
 	prize, detail, err := c.serv.LuckDraw(id, &user)
197
 	prize, detail, err := c.serv.LuckDraw(id, &user)
195
 	if err != nil {
198
 	if err != nil {
196
 		c.ResponseError(err)
199
 		c.ResponseError(err)

+ 36
- 1
service/flashbuy/flashbuy.go Voir le fichier

2
 
2
 
3
 import (
3
 import (
4
 	"errors"
4
 	"errors"
5
+	"fmt"
5
 	"spaceofcheng/services/models"
6
 	"spaceofcheng/services/models"
6
 	"spaceofcheng/services/models/course"
7
 	"spaceofcheng/services/models/course"
7
 	"spaceofcheng/services/models/flashbuy"
8
 	"spaceofcheng/services/models/flashbuy"
258
 	custQRCode := model.TaCustomerCourseQrcode{
259
 	custQRCode := model.TaCustomerCourseQrcode{
259
 		CustomerCourseId: recored.CustomerFlashBuyId,
260
 		CustomerCourseId: recored.CustomerFlashBuyId,
260
 		CustomerQrcode:   utils.GenerateQRCode(),
261
 		CustomerQrcode:   utils.GenerateQRCode(),
261
-		CreateDate:       time.Now(),
262
+		CreateDate:       time.Now().Local(),
262
 		Status:           models.STATUS_NORMAL,
263
 		Status:           models.STATUS_NORMAL,
263
 		QrcodeType:       models.QRCODE_TYPE_FLASH,
264
 		QrcodeType:       models.QRCODE_TYPE_FLASH,
264
 	}
265
 	}
268
 		return errors.New("写入中奖二维码失败")
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
 	return nil
306
 	return nil
272
 }
307
 }
273
 
308
 

+ 35
- 0
service/luckdraw/luckdraw.go Voir le fichier

2
 
2
 
3
 import (
3
 import (
4
 	"errors"
4
 	"errors"
5
+	"fmt"
5
 	"spaceofcheng/services/models"
6
 	"spaceofcheng/services/models"
6
 	"spaceofcheng/services/models/course"
7
 	"spaceofcheng/services/models/course"
7
 	"spaceofcheng/services/models/luckdraw"
8
 	"spaceofcheng/services/models/luckdraw"
328
 		return nil, nil, errors.New("写入中奖二维码失败")
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
 	return prize, prizeDetail, nil
366
 	return prize, prizeDetail, nil
332
 }
367
 }
333
 
368
 

+ 23
- 2
utils/message.go Voir le fichier

2
 
2
 
3
 import (
3
 import (
4
 	"github.com/zjxpcyc/websocket"
4
 	"github.com/zjxpcyc/websocket"
5
+	"github.com/zjxpcyc/wechat/wx"
5
 )
6
 )
6
 
7
 
7
 // ClientID 消息对象
8
 // ClientID 消息对象
29
 		Data: message.Data,
30
 		Data: message.Data,
30
 	}
31
 	}
31
 
32
 
32
-	LogError("发送 Websocket 消息: ", msg)
33
-
34
 	websocket.Send(msg)
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
 // SendMessage 发送消息
57
 // SendMessage 发送消息
38
 func SendMessage(message Message, msgType string) {
58
 func SendMessage(message Message, msgType string) {
39
 	switch msgType {
59
 	switch msgType {
40
 	case WsMessage:
60
 	case WsMessage:
41
 		SendWs(message)
61
 		SendWs(message)
42
 	case WechatMessage:
62
 	case WechatMessage:
63
+
43
 	case SMSMessage:
64
 	case SMSMessage:
44
 	default:
65
 	default:
45
 	}
66
 	}