黄梓健 5 years ago
parent
commit
13241f5706
  1. 24
      controllers/common/im.go
  2. 8
      controllers/pc/shake_red_envelope.go
  3. 55
      libs/im/im.go
  4. 1
      main.go
  5. 36
      services/im/im.go
  6. 2
      services/pay/handle.go
  7. 0
      test/123.gif
  8. 0
      test/annex_test.go
  9. 7
      test/pay_test.go
  10. 17
      utils/utils.go

24
controllers/common/im.go

@ -0,0 +1,24 @@
package common
import (
"hudongzhuanjia/controllers"
"hudongzhuanjia/libs/im"
im_service "hudongzhuanjia/services/im"
"time"
)
type ImTestCtl struct {
controllers.BaseCtl
}
func (t *ImTestCtl) SendNotice() {
activityId := t.MustGetInt64("activity_id")
status := t.MustGetInt("status")
im_service.SendGroupCustomMessage(activityId, im.NoticeStatus(status), map[string]interface{}{
"customer_id": 1,
"shake_red_envelope_rule_id": 2,
"timestamp": time.Now().Unix(),
})
t.SUCCESS("发送成功")
}

8
controllers/pc/shake_red_envelope.go

@ -61,6 +61,7 @@ func (t *ShakeRedEnvelopeCtl) Ready() {
//开始摇红包
func (t *ShakeRedEnvelopeCtl) Start() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
uid := t.MustGetUID()
//根据摇红包规则id查找该条记录并且更给状态
rule := new(models.ShakeRedEnvelopeRule)
@ -81,8 +82,9 @@ func (t *ShakeRedEnvelopeCtl) Start() {
_, err = rule.UpdateStatus(ruleId, define.StatusRunning)
t.CheckErr(err)
go im_service.SendNoticeByActivityId(envelope.ActivityId, im.NoticeShakeRedPackStart,
go im_service.SendGroupCustomMessage(envelope.ActivityId, im.NoticeShakeRedPackStart,
map[string]interface{}{
"customer_id": uid,
"shake_red_envelope_rule_id": rule.Id,
"timestamp": time.Now().Unix(),
})
@ -93,6 +95,7 @@ func (t *ShakeRedEnvelopeCtl) Start() {
//停止摇红包
func (t *ShakeRedEnvelopeCtl) Stop() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
uid := t.MustGetUID()
rule := new(models.ShakeRedEnvelopeRule)
exist, err := models.GetById(rule, ruleId)
@ -108,8 +111,9 @@ func (t *ShakeRedEnvelopeCtl) Stop() {
_, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus)
t.CheckErr(err)
go im_service.SendNoticeByActivityId(envelope.ActivityId, im.NoticeShakeRedPackEnd,
go im_service.SendGroupCustomMessage(envelope.ActivityId, im.NoticeShakeRedPackEnd,
map[string]interface{}{
"customer_id": uid,
"shake_red_envelope_rule_id": rule.Id,
"timestamp": time.Now().Unix(),
})

55
libs/im/im.go

@ -72,6 +72,61 @@ func SendGroupSystemNotification(groupId string, status NoticeStatus, data map[s
return err
}
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
if err != nil {
return err
}
res := CommonResult{}
err = resp.Json(&res)
if err != nil {
return err
}
if res.ErrorCode != 0 {
return errors.New(res.ErrorInfo)
}
return nil
}
// https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
type MsgBody struct {
MsgType string `json:"MsgType"`
MsgContent MsgBodyContent `json:"MsgContent"`
}
type MsgBodyContent struct {
Data string `json:"Data"`
Desc string `json:"Desc"`
Ext string `json:"Ext"`
Sound string `json:"Sound"`
}
type SendGroupMessageParam struct {
GroupId string `json:"GroupId"`
Random int `json:"Random"`
MsgPriority string `json:"MsgPriority,omitempty"`
MsgBody []MsgBody `json:"MsgBody"`
FromAccount string `json:"FromAccount,omitempty"`
OfflinePushInfo interface{} `json:"OfflinePushInfo,omitempty"`
ForbidCallbackControl interface{} `json:"ForbidCallbackControl,omitempty"`
OnlineOnlyFlag int `json:"OnlineOnlyFlag,omitempty"`
}
func SendGroupMessage(param *SendGroupMessageParam) error {
sig, err := GenSig("admin")
if err != nil {
return err
}
random := utils.RandomInt(16)
u := fmt.Sprintf("%s/group_open_http_svc/send_group_msg?sdkappid=%d&identifier=admin&usersig=%s&random=%v&contenttype=json",
TxImHostV4, SdkAppid, sig, random)
bs, err := json.Marshal(param)
if err != nil {
return err
}
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
if err != nil {
return err
}
res := CommonResult{}
err = resp.Json(&res)
if err != nil {

1
main.go

@ -53,6 +53,7 @@ func main() {
// common
core.GetInstanceRouterManage().Registered(new(common.AnnexCtl))
core.GetInstanceRouterManage().Registered(new(common.WeChatOauthCtl))
core.GetInstanceRouterManage().Registered(new(common.ImTestCtl))
//r := osmanthuswine.GetChiRouter()
//r.HandleFunc("/debug/pprof/", pprof.Index)

36
services/im/im.go

@ -1,10 +1,13 @@
package im_service
import (
"encoding/json"
"errors"
"fmt"
"hudongzhuanjia/libs/im"
"hudongzhuanjia/models"
"hudongzhuanjia/utils"
"strconv"
)
func SendNoticeByActivityId(activityId int64, _type im.NoticeStatus, data map[string]interface{}, members ...string) error {
@ -19,3 +22,36 @@ func SendNoticeByActivityId(activityId int64, _type im.NoticeStatus, data map[st
groupId := fmt.Sprintf("%v%d%d", live.LiveRoomId, live.Id, live.ActivityId)
return im.SendGroupSystemNotification(groupId, _type, data, members...)
}
func SendGroupCustomMessage(activityId int64, _type im.NoticeStatus, data map[string]interface{}) error {
live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activityId)
if err != nil {
return err
}
if !exist {
return errors.New("直播信息不存在")
}
bs, err := json.Marshal(data)
if err != nil {
return err
}
groupId := fmt.Sprintf("%v%d%d", live.LiveRoomId, live.Id, live.ActivityId)
return im.SendGroupMessage(&im.SendGroupMessageParam{
GroupId: groupId,
Random: utils.RandomInt(6),
MsgPriority: "High",
MsgBody: []im.MsgBody{
{
MsgType: "TIMCustomElem",
MsgContent: im.MsgBodyContent{
Data: string(bs),
Desc: strconv.Itoa(int(_type)),
Ext: "",
Sound: "",
},
},
},
})
}

2
services/pay/handle.go

@ -81,7 +81,7 @@ func Handle(order *models.UserOrder) error {
return errors.New("直播红包信息异常")
}
return im_service.SendNoticeByActivityId(order.ActivityId, im.NoticeLiveRedPackStart,
return im_service.SendGroupCustomMessage(order.ActivityId, im.NoticeLiveRedPackStart,
map[string]interface{}{
"live_red_envelope_rule_id": info.Id,
"timestamp": time.Now().Unix(),

0
controllers/common/test/123.gif → test/123.gif

Before

Width: 72  |  Height: 72  |  Size: 6.5 KiB

After

Width: 72  |  Height: 72  |  Size: 6.5 KiB

0
controllers/common/test/annex_test.go → test/annex_test.go

7
test/pay_test.go

@ -52,9 +52,12 @@ func TestTimeExpire(t *testing.T) {
}
func TestSendNotice(t *testing.T) {
im_service.SendNoticeByActivityId(114, im.NoticeLiveRedPackStart, map[string]interface{}{
"nihao": "wojiaohuanxiao",
err := im_service.SendGroupCustomMessage(114, im.NoticeShakeRedPackStart, map[string]interface{}{
"customer_id": 1,
"shake_red_envelope_rule_id": 2,
"timestamp": time.Now().Unix(),
})
fmt.Print(err)
}
// 地球半径,单位米

17
utils/utils.go

@ -119,3 +119,20 @@ func RandomStr(length int) string {
}
return string(result)
}
func RandomInt(length int) int {
str := "0123456789"
bytes := []byte(str)
var result []byte
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; {
b := bytes[r.Intn(len(bytes))]
if i == 0 && b == '0' {
continue
}
result = append(result, b)
i++
}
num, _ := strconv.Atoi(string(result))
return num
}
Loading…
Cancel
Save