123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package logic
- import (
- "app.yhyue.com/moapp/MessageCenter/entity"
- "app.yhyue.com/moapp/MessageCenter/rpc/internal/common"
- "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
- "app.yhyue.com/moapp/MessageCenter/rpc/internal/svc"
- "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
- qutil "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/go-xweb/log"
- "app.yhyue.com/moapp/jybase/mongodb"
- "context"
- "encoding/json"
- "fmt"
- "net/rpc"
- "strings"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type AppLetterPushLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewAppLetterPushLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppLetterPushLogic {
- return &AppLetterPushLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // 发送剑鱼微信模版消息
- func (l *AppLetterPushLogic) AppLetterPush(in *message.WxTmplMsgRequest) (*message.SendMsgResponse, error) {
- var userArr []string
- userArr = strings.Split(in.PositionIds, ",")
- pushConfig, err := common.GetWxTmplConfig(in.MessageClass)
- if err != nil {
- return &message.SendMsgResponse{
- Total: 0,
- Message: err.Error(),
- }, nil
- }
- for _, uId := range userArr {
- if uId == "" {
- continue
- }
- query := make(map[string]interface{})
- uInfo := entity.Mysql.SelectBySql("SELECT user_id FROM base_service.base_position WHERE id = ? ", uId)
- if uInfo != nil && len(*uInfo) > 0 {
- if baseUserId := qutil.Int64All((*uInfo)[0]["user_id"]); baseUserId != 0 {
- query["base_user_id"] = baseUserId
- }
- }
- userData := make(map[string]interface{})
- if len(query) > 0 {
- rData, _ := entity.MQFW.FindOneByField("user", query, fmt.Sprintf(`{"s_appversion":1,"s_nmae":1,"s_nmae":1,"s_jpushid":1,"s_opushid":1,"s_m_openid":1,"o_pushset.%s.i_apppush":1}`, pushConfig.Switch))
- if rData != nil && len(*rData) > 0 {
- userData = *rData
- }
- }
- AppPushMsg(userData, pushConfig.Switch, in.Url, in.Title)
- }
- return &message.SendMsgResponse{
- Total: 1,
- }, nil
- }
- func AppPushMsg(userInfo map[string]interface{}, stm, appPushUrl, title string) {
- userId := mongodb.BsonIdToSId(userInfo["_id"])
- if pushSetMap := qutil.ObjToMap(userInfo["o_pushset"]); pushSetMap != nil && len(*pushSetMap) > 0 {
- if pushKeyMap := qutil.ObjToMap((*pushSetMap)[stm]); pushKeyMap != nil && len(*pushKeyMap) > 0 {
- if qutil.Int64All((*pushKeyMap)["i_apppush"]) == 1 {
- //用户信息
- var otherPushId, jgPushId, phoneType, name, appVersion = "", "", "", "", ""
- otherPushId = qutil.ObjToString(userInfo["s_opushid"])
- jgPushId = qutil.ObjToString(userInfo["s_jpushid"])
- phoneType = qutil.ObjToString(userInfo["s_appponetype"])
- name = qutil.ObjToString(userInfo["s_name"])
- appVersion = qutil.ObjToString(userInfo["s_appversion"])
- dt := map[string]interface{}{
- "receiveUserId": userId,
- "receiveName": name,
- "title": "剑鱼标讯",
- "content": fmt.Sprintf("您收到一条来自%s私信", title),
- "msgType": "messagecenter",
- "link": appPushUrl,
- "appid": "10000",
- "menuName": "message",
- }
- //推送消息
- if appVersion > "3.0.3" {
- go AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl)
- }
- return
- }
- }
- }
- }
- func AppGrpcPush(pushData map[string]interface{}, otherPushId, jgPushId, phoneType, appPushUrl string) {
- menuName := "message"
- if value, ok := pushData["menuName"]; ok {
- menuName = qutil.ObjToString(value)
- }
- var repl string
- client, err := rpc.DialHTTP("tcp", config.ConfigJson.PushGrpcServer)
- if err != nil {
- log.Println(err.Error())
- return
- }
- defer client.Close()
- push := map[string]interface{}{
- "title": pushData["title"], //标题
- "descript": pushData["content"], //副标题
- "otherPushId": otherPushId, //mongodb库user表中s_opushid
- "jgPushId": jgPushId, //mongodb库user表中s_jpushid
- "userId": pushData["receiveUserId"], //mongodb库user表中_id转string
- "phoneType": phoneType, //mongodb库user表中s_appponetype
- "type": "messagecenter", //消息类型,消息中心推送的消息使用messagecenter
- "url": appPushUrl, //点了消息以后,跳转的链接地址,不需要带域名
- "menuName": menuName, //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview 消息中心 message
- "redDot": "", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的
- }
- b, _ := json.Marshal(push)
- err = client.Call("Rpc.Push", b, &repl)
- if err != nil {
- log.Println(err.Error())
- return
- }
- if repl == "y" {
- log.Println("推送成功!", pushData["receiveUserId"])
- } else {
- log.Println("推送失败!", pushData["receiveUserId"])
- }
- }
|