sendWxTmplMsg.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package common
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/url"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "app.yhyue.com/moapp/MessageCenter/entity"
  12. "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
  13. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  14. "app.yhyue.com/moapp/MessageCenter/util"
  15. "app.yhyue.com/moapp/jybase/common"
  16. dataFormat "app.yhyue.com/moapp/jybase/date"
  17. m "app.yhyue.com/moapp/jybase/mongodb"
  18. "app.yhyue.com/moapp/jybase/redis"
  19. qrpc "app.yhyue.com/moapp/jybase/rpc"
  20. )
  21. type WxTmplPush struct {
  22. MgoId, OpenId, Position, OpushId, JpushId, AppPoneType string //UserId 用户mgoId, OpenId 微信id, Position 职位id
  23. Config *WxTmplConfig
  24. CustomWxTpl *message.CustomWxTpl
  25. }
  26. var AllMsgType func() map[int64]WxTmplConfig
  27. var allMsgValueKeys = []string{"$class", "$title", "$detail", "$date", "$row4", "$note"}
  28. var AppPushMsgType map[int]string
  29. type WxTmplConfig struct {
  30. Name string //信息名称
  31. Switch string //开关
  32. TmplId string //微信模版id
  33. TmplValue string //微信模版
  34. }
  35. const CacheDb = "msgCount"
  36. func MessageType() (func() map[int64]WxTmplConfig, []map[string]interface{}) {
  37. var data []map[string]interface{}
  38. rData1, err := entity.ClickhouseConn.Query(context.Background(), `SELECT group_id,name,switch,img,sequence FROM message_group ORDER BY sequence ASC`)
  39. if err != nil {
  40. log.Println("MessageType SELECT message_group出错:", err)
  41. return nil, nil
  42. }
  43. switchName := map[int64]WxTmplConfig{}
  44. appMsgType := map[int]string{}
  45. for rData1.Next() {
  46. group := entity.MsgGroup{}
  47. err = rData1.ScanStruct(&group)
  48. if err != nil {
  49. log.Println("MessageType 初始化rData1 ScanStruct出错:", err)
  50. return nil, nil
  51. }
  52. groupId := util.IntAll(group.GroupId)
  53. switchs := util.ObjToString(group.Switch)
  54. appMsgType[groupId] = switchs
  55. data = append(data, map[string]interface{}{
  56. "group_id": groupId,
  57. "name": group.Name,
  58. "switch": group.Switch,
  59. "img": group.Img,
  60. "sequence": group.Sequence,
  61. })
  62. }
  63. AppPushMsgType = appMsgType
  64. rData2, err := entity.ClickhouseConn.Query(context.Background(), `SELECT g.switch,c.wxtmpl_Id,c.msg_type,c.msg_name,c.wxtmp_value FROM message_class c INNER JOIN message_group g on c.group_id =g.group_id WHERE c.wxtmpl_Id IS NOT NULL`)
  65. if err != nil {
  66. log.Println("MessageType SELECT message_group,message_class出错:", err)
  67. return nil, nil
  68. }
  69. for rData2.Next() {
  70. gc := entity.GroupClass{}
  71. err = rData2.ScanStruct(&gc)
  72. if err != nil {
  73. log.Println("MessageType 初始化rData2 ScanStruct出错:", err)
  74. return nil, nil
  75. }
  76. if msg_type, settingKey, messageName, tmplId, tmplValue := util.Int64All(gc.Msg_type), util.ObjToString(gc.Switch), util.ObjToString(gc.Msg_name), util.ObjToString(gc.Wxtmpl_Id), util.ObjToString(gc.Wxtmp_value); msg_type > 0 && settingKey != "" && messageName != "" && tmplId != "" && tmplValue != "" {
  77. switchName[msg_type] = WxTmplConfig{
  78. Name: messageName,
  79. Switch: settingKey,
  80. TmplId: tmplId,
  81. TmplValue: tmplValue,
  82. }
  83. }
  84. }
  85. rData3, err := entity.ClickhouseConn.Query(context.Background(), "SELECT msg_type,group_id FROM message_class WHERE msg_type < 999")
  86. if err != nil {
  87. log.Println("MessageType SELECT message_class出错:", err)
  88. return nil, nil
  89. }
  90. mg := map[int]int{}
  91. for rData3.Next() {
  92. group := entity.MsgGroup{}
  93. err = rData3.ScanStruct(&group)
  94. if err != nil {
  95. log.Println("MessageType 初始化rData1 ScanStruct出错:", err)
  96. return nil, nil
  97. }
  98. mg[common.IntAll(group.Msg_type)] = common.IntAll(group.GroupId)
  99. }
  100. MsgGroupIdMap = mg
  101. return func() map[int64]WxTmplConfig {
  102. return switchName
  103. }, data
  104. }
  105. var getSendTotalRedisKey = func(uFlag ...string) string {
  106. if len(uFlag) == 0 { //统计当日信息总发送量
  107. return fmt.Sprintf("messageCenter_SendWxMsgTotal_%s", time.Now().Format(dataFormat.Date_yyyyMMdd))
  108. } //统计单用户今日发送量
  109. return fmt.Sprintf("messageCenter_SendWxMsgTotal_%s_%s", time.Now().Format(dataFormat.Date_yyyyMMdd), uFlag[0])
  110. }
  111. func GetWxTmplConfig(msgType int64) (*WxTmplConfig, error) {
  112. if val, ok := AllMsgType()[msgType]; ok {
  113. return &val, nil
  114. }
  115. return nil, fmt.Errorf("未知消息类型")
  116. }
  117. func (stm *WxTmplPush) SendMsg(link, title, detail, date, row4 string, note string) error {
  118. if stm.Config == nil || stm.Config.TmplId == "" || (stm.MgoId != "" && stm.OpenId != "" && stm.Position != "") || link == "" {
  119. log.Println("err=====", stm.Config, stm.MgoId, stm.OpenId, link, AllMsgType())
  120. return fmt.Errorf("缺少参数 stm.Config:%v stm.MgoId:%v stm.OpenId:%v stm.Position:%v link:%v ", stm.Config, stm.MgoId, stm.OpenId, stm.Position, link)
  121. }
  122. // 校验推送是否开启
  123. if err := stm.getUserOpenIdAndWxPushState(); err != nil {
  124. return err
  125. }
  126. var msg map[string]*qrpc.TmplItem
  127. if stm.CustomWxTpl != nil && stm.CustomWxTpl.TplId != "" {
  128. stm.Config.TmplId = stm.CustomWxTpl.TplId
  129. msg = map[string]*qrpc.TmplItem{}
  130. for k, v := range stm.CustomWxTpl.TmplData {
  131. msg[k] = &qrpc.TmplItem{
  132. Value: v.Value,
  133. Color: v.Color,
  134. }
  135. }
  136. } else {
  137. // 获取消息
  138. msgTemp, err := stm.getMessage(title, detail, date, row4, note)
  139. if err != nil {
  140. return err
  141. }
  142. msg = msgTemp
  143. }
  144. // 校验发送量及频率
  145. err, noteFunc := stm.IncrCount()
  146. if err != nil {
  147. return err
  148. }
  149. // 发送信息
  150. autoLoginHref := fmt.Sprintf("%s/swordfish/SingleLogin?toHref=%s", config.ConfigJson.WxWebdomain, url.QueryEscape(link))
  151. if _, err := stm.Send(autoLoginHref, msg); err != nil {
  152. // 发送失败数量回滚
  153. stm.RollBack()
  154. return err
  155. }
  156. if noteFunc != nil {
  157. noteFunc()
  158. }
  159. return nil
  160. }
  161. var (
  162. regSpecial = regexp.MustCompile(`\\n|\\t|\\'|\\"|\n|\t|\'|\"`)
  163. )
  164. // getMessage 获取消息内容
  165. func (stm *WxTmplPush) getMessage(title, detail, date, row4 string, note string) (map[string]*qrpc.TmplItem, error) {
  166. var formatValue string = stm.Config.TmplValue
  167. for _, key := range allMsgValueKeys {
  168. switch key {
  169. case "$class":
  170. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(stm.Config.Name, ""))
  171. case "$title":
  172. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(title, ""))
  173. case "$detail":
  174. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(detail, ""))
  175. case "$date":
  176. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(date, ""))
  177. case "$row4":
  178. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(row4, ""))
  179. case "$note":
  180. if note == "" {
  181. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(config.ConfigJson.WxTmplConfig.CloseNotice, ""))
  182. } else {
  183. formatValue = strings.ReplaceAll(formatValue, key, regSpecial.ReplaceAllString(note, ""))
  184. }
  185. }
  186. }
  187. bValue := map[string]*qrpc.TmplItem{}
  188. if err := json.Unmarshal([]byte(formatValue), &bValue); err != nil {
  189. return nil, fmt.Errorf("格式化信息内容异常 %s", err.Error())
  190. }
  191. for _, item := range bValue {
  192. val := []rune(item.Value)
  193. if len(val) > 20 {
  194. item.Value = string(val[:17]) + "..."
  195. }
  196. }
  197. return bValue, nil
  198. }
  199. // RollBack 发送失败数量回滚
  200. func (stm *WxTmplPush) RollBack() {
  201. uCache, allCache := getSendTotalRedisKey(stm.OpenId), getSendTotalRedisKey()
  202. redis.Decrby(CacheDb, allCache, 1)
  203. redis.Decrby(CacheDb, uCache, 1)
  204. redis.Del(CacheDb, fmt.Sprintf("%s_sendwait", uCache)) //清除发送间隔
  205. }
  206. func (stm *WxTmplPush) IncrCount() (error, func()) {
  207. uCache, allCache := getSendTotalRedisKey(stm.OpenId), getSendTotalRedisKey() //当日微信模版消息发送总量
  208. //校验发送间隔
  209. if sendWait, _ := redis.Exists(CacheDb, fmt.Sprintf("%s_sendwait", uCache)); sendWait {
  210. return fmt.Errorf("发送模版消息频繁,稍后重试"), nil
  211. }
  212. var total int64
  213. if total = redis.Incr(CacheDb, allCache); total == 1 {
  214. _ = redis.SetExpire(CacheDb, allCache, 60*60*24)
  215. }
  216. if total > config.ConfigJson.WxTmplConfig.Limit.Total {
  217. redis.Decrby(CacheDb, allCache, 1)
  218. return fmt.Errorf("已达发送总量上限"), nil
  219. }
  220. uTotal := redis.Incr(CacheDb, uCache)
  221. if uTotal == 1 {
  222. _ = redis.SetExpire(CacheDb, uCache, 60*60*24)
  223. } //当日用户发送数量
  224. if uTotal > config.ConfigJson.WxTmplConfig.Limit.OneDayLimit {
  225. redis.Decrby(CacheDb, allCache, 1)
  226. redis.Decrby(CacheDb, uCache, 1)
  227. return fmt.Errorf("已达单该用户发送总量上限"), nil
  228. }
  229. //下次发送时间
  230. redis.Put(CacheDb, fmt.Sprintf("%s_sendwait", uCache), 1, config.ConfigJson.WxTmplConfig.Limit.DuringMine*60)
  231. return nil, func() {
  232. for _, num := range config.ConfigJson.WxTmplConfig.Limit.Alert.Nums {
  233. if total == num {
  234. util.SendRetryMail(3, strings.Join(config.ConfigJson.WxTmplConfig.Limit.Alert.ToMail, ","), strings.Join(config.ConfigJson.WxTmplConfig.Limit.Alert.CcMail, ","),
  235. "剑鱼微信模版告警邮件", fmt.Sprintf("今日发送微信模版信息数量已达%d条,总量%d条", total, config.ConfigJson.WxTmplConfig.Limit.Total), entity.GmailAuth)
  236. }
  237. }
  238. }
  239. }
  240. // getUserOpenIdAndWxPushState 查询微信openid微信消息通知状态
  241. // mId mongoUserid、oId 用户openid、pId positionId 用户职位id
  242. func (stm *WxTmplPush) getUserOpenIdAndWxPushState() error {
  243. uData := stm.GetUserPushInfo()
  244. if uData == nil {
  245. return fmt.Errorf("未查询到用户信息")
  246. }
  247. stm.OpenId = common.ObjToString(uData["s_m_openid"])
  248. if stm.OpenId == "" {
  249. return fmt.Errorf("未查询到用户微信信息")
  250. }
  251. log.Println("======", stm.Config.Switch)
  252. if pushSetMap := common.ObjToMap(uData["o_pushset"]); pushSetMap != nil && len(*pushSetMap) > 0 {
  253. if pushKeyMap := common.ObjToMap((*pushSetMap)[stm.Config.Switch]); pushKeyMap != nil && len(*pushKeyMap) > 0 {
  254. switch stm.Config.Switch {
  255. case "o_msg_active", "o_msg_service", "o_msg_jyschool", "o_msg_privateletter", "o_msg_business", "o_msg_pending":
  256. registedate := common.Int64All(uData["l_registedate"])
  257. s_m_openid := common.InterfaceToStr(uData["s_m_openid"])
  258. ShowWx := false
  259. if s_m_openid != "" {
  260. //微信是否关注处理
  261. subscribeList := &[]map[string]interface{}{}
  262. subscribeList, _ = entity.MQFW.Find("jy_subscribe", map[string]interface{}{"s_m_openid": s_m_openid}, `{"l_date":1}`, `{"s_event":1"}`, true, 1, 1)
  263. if subscribeList != nil && len(*subscribeList) > 0 {
  264. s_event := common.InterfaceToStr((*subscribeList)[0]["s_event"])
  265. if s_event == "subscribe" {
  266. ShowWx = true
  267. }
  268. }
  269. }
  270. if (*pushKeyMap)["i_wxpush"] != nil {
  271. if common.Int64All((*pushKeyMap)["i_wxpush"]) == 1 {
  272. return nil
  273. }
  274. } else {
  275. if registedate > config.ConfigJson.Registedate && ShowWx {
  276. return nil
  277. }
  278. }
  279. default:
  280. if common.Int64All((*pushKeyMap)["i_wxpush"]) == 1 {
  281. return nil
  282. }
  283. }
  284. }
  285. }
  286. return fmt.Errorf("未开启推送设置")
  287. }
  288. func (stm *WxTmplPush) GetUserPushInfo() map[string]interface{} {
  289. uData := func() map[string]interface{} {
  290. query := map[string]interface{}{}
  291. if stm.OpenId != "" {
  292. query["s_m_openid"] = stm.OpenId
  293. } else if stm.MgoId != "" {
  294. query["_id"] = m.StringTOBsonId(stm.MgoId)
  295. } else if stm.Position != "" {
  296. uInfo := entity.Mysql.SelectBySql("SELECT user_id FROM base_service.base_position WHERE id = ? ", stm.Position)
  297. if uInfo != nil && len(*uInfo) > 0 {
  298. if baseUserId := common.Int64All((*uInfo)[0]["user_id"]); baseUserId != 0 {
  299. query["base_user_id"] = baseUserId
  300. }
  301. }
  302. }
  303. if len(query) > 0 {
  304. rData, _ := entity.MQFW.FindOneByField("user", query, fmt.Sprintf(`{"l_registedate":1,"s_m_openid":1,"s_opushid": 1, "s_jpushid": 1, "s_appponetype": 1, "s_appversion": 1,"o_pushset.%s":1}`, stm.Config.Switch))
  305. if rData != nil && len(*rData) > 0 {
  306. return *rData
  307. }
  308. }
  309. return nil
  310. }()
  311. return uData
  312. }
  313. // Send 发送微信模版消息
  314. func (stm *WxTmplPush) Send(link string, msg map[string]*qrpc.TmplItem) (pushOk bool, err error) {
  315. return qrpc.WxSendTmplMsg(config.ConfigJson.WxTmplConfig.RpcAddr, &qrpc.WxTmplMsg{
  316. OpenId: stm.OpenId,
  317. TplId: stm.Config.TmplId,
  318. TmplData: msg,
  319. Url: link,
  320. })
  321. }