report_push.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/wenzhenxi/gorsa"
  7. "hongze/hongze_yb/global"
  8. models "hongze/hongze_yb/models/tables/report_send_ths_detail"
  9. "hongze/hongze_yb/models/tables/voice_broadcast"
  10. "hongze/hongze_yb/services/alarm_msg"
  11. "hongze/hongze_yb/services/wechat"
  12. "hongze/hongze_yb/utils"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. "strings"
  17. "time"
  18. )
  19. //func init() {
  20. // report, _ := models.GetReportById(572)
  21. // SendReportToThs(report)
  22. //}
  23. var permissionMap map[string]string = map[string]string{
  24. "化里化外日评": "原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱",
  25. "股债日评": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  26. "贵金属复盘": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  27. "每日经济数据备忘录": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  28. "宏观商品复盘": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  29. "知白守黑日评": "钢材,铁矿,双焦(焦煤、焦炭)",
  30. "有声有色日度闲篇": "有色(铜、铝),有色(锌、铅),镍+不锈钢",
  31. "EIA原油库存点评": "原油",
  32. "苯乙烯数据点评": "苯乙烯",
  33. "API原油库存点评": "原油",
  34. "铁矿航运数据点评": "铁矿",
  35. "中观需求点评": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  36. "聚酯数据点评": "PTA,MEG",
  37. "钢材周度数据点评": "钢材",
  38. "寻根知本": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  39. "国际宏观": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  40. "能化百家谈": "原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱",
  41. "有色百家谈": "有色(铜、铝),有色(锌、铅),镍+不锈钢",
  42. "黑色百家谈": "钢材,铁矿,双焦(焦煤、焦炭)",
  43. }
  44. // permissionLabelMap 品种与同花顺标签的映射关系
  45. var permissionLabelMap = map[string]string{
  46. "宏观经济": "宏观",
  47. "利率债": "利率债",
  48. "原油": "原油",
  49. "PTA": "PTA",
  50. "MEG": "MEG",
  51. "织造终端": "织造终端",
  52. "甲醇": "甲醇",
  53. "聚烯烃": "聚烯烃",
  54. "沥青": "沥青",
  55. "纯苯+苯乙烯": "纯苯+苯乙烯",
  56. "玻璃纯碱": "玻璃纯碱",
  57. "钢材": "钢材",
  58. "铁矿": "铁矿",
  59. "双焦(焦煤、焦炭)": "双焦(焦煤、焦炭)",
  60. "有色(锌)": "有色(锌)",
  61. "有色(铜、铝)": "有色(铜、铝)",
  62. "镍+不锈钢": "镍+不锈钢",
  63. "PVC": "PVC",
  64. "聚酯": "聚酯",
  65. "钴锂": "钴锂",
  66. "策略": "策略",
  67. "苯乙烯": "纯苯+苯乙烯",
  68. "锌": "有色(锌)",
  69. "双焦": "双焦(焦煤、焦炭)",
  70. "铜/铝": "有色(铜、铝)",
  71. "镍/不锈钢": "镍+不锈钢",
  72. "成品油": "成品油",
  73. "油品": "成品油",
  74. "纺服": "织造终端",
  75. }
  76. // TshResult 同花顺返回信息
  77. type TshResult struct {
  78. ErrorCode int `json:"error" description:"错误状态码"`
  79. Message string `json:"message" description:"提示信息"`
  80. }
  81. var (
  82. THS_SendUrl string //同花顺地址url
  83. THS_PubKey string //同花顺公钥
  84. )
  85. func init() {
  86. THS_PubKey = `-----BEGIN PUBLIC KEY-----
  87. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqugglfCboOEfWtHlGBOW
  88. 40a4Y3xOs0MPBwjTOzHgcaWzx5XCc20VftGVXkWlpjs8u4dza/Bp1SV7SJ5Y7U95
  89. jgUOP8Js9Qgp6UVqBJDJf3i1KpjHzlk3ma8zxAYUAdieEUE+SKSxSY+BD9A6lpf5
  90. n+igXLmzR5GeVGFeLzoMhB1+pXgGhW30ao9wPwuRF7DBl+FKa/ACi7iXLiwXVgqT
  91. FFi29TKeerEENu3EpMXvPml7tNUiVmVW6d83hlascfbAlkShwuHLSGpLqK7brtg6
  92. jRS9hreKFKb0BUQ4TB26e7IDCstbMRvUp4+OGezexzic5NYPQ8uLo5OTaS7f7PrW
  93. ZwIDAQAB
  94. -----END PUBLIC KEY-----`
  95. if global.CONFIG.Serve.RunMode == "release" {
  96. //同花顺正式地址
  97. THS_SendUrl = `https://board.10jqka.com.cn/gateway/ps/syncNews`
  98. } else {
  99. //同花顺测试地址
  100. THS_SendUrl = `https://mtest.10jqka.com.cn/gateway/ps/syncNews`
  101. }
  102. }
  103. // SendThs 发送消息到同花顺
  104. func SendThs(title, labelStr, abstract, jumpBaseUrl, logoUrl, dataType string) (err error) {
  105. return nil
  106. defer func() {
  107. if err != nil {
  108. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  109. go alarm_msg.SendAlarmMsg("发送消息至同花顺失败 ErrMsg:"+err.Error(), 3)
  110. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送报告至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  111. }
  112. }()
  113. pubKey := THS_PubKey
  114. sendUrl := THS_SendUrl
  115. //标题字符长度截取,最多50位字符
  116. title = utils.SubStr(title, 50)
  117. global.LOG.Info(fmt.Sprintf("title:%s", title))
  118. title, err = gorsa.PublicEncrypt(title, pubKey)
  119. if err != nil {
  120. return
  121. }
  122. //简介字符长度截取,最多50位字符
  123. abstract = utils.SubStr(abstract, 50)
  124. global.LOG.Info(fmt.Sprintf("abstract:%s", abstract))
  125. abstract, err = gorsa.PublicEncrypt(abstract, pubKey)
  126. if err != nil {
  127. return
  128. }
  129. // 关联后的标签数据
  130. newLabelList := make([]string, 0)
  131. labelList := strings.Split(labelStr, ",")
  132. for _, v := range labelList {
  133. tmpLabel, ok := permissionLabelMap[v] //判断是否在关联标签里面
  134. if !ok { //如果不在关联标签里面,那么就把原始的值赋值给
  135. tmpLabel = v
  136. }
  137. newLabelList = append(newLabelList, tmpLabel)
  138. }
  139. labelStr = strings.Join(newLabelList, ",")
  140. global.LOG.Info(fmt.Sprintf("labelStr:%s", labelStr))
  141. label, err := gorsa.PublicEncrypt(labelStr, pubKey)
  142. if err != nil {
  143. return
  144. }
  145. // 跳转地址
  146. jumpUrl, err := gorsa.PublicEncrypt(jumpBaseUrl, pubKey)
  147. if err != nil {
  148. return
  149. }
  150. picUrl, err := gorsa.PublicEncrypt(logoUrl, pubKey)
  151. if err != nil {
  152. return
  153. }
  154. dataTypeEncript, err := gorsa.PublicEncrypt(dataType, pubKey)
  155. if err != nil {
  156. return
  157. }
  158. //开始发送
  159. client := http.Client{}
  160. form := url.Values{}
  161. form.Add("title", title)
  162. form.Add("description", abstract)
  163. form.Add("label", label)
  164. form.Add("url", jumpUrl)
  165. form.Add("icon", picUrl)
  166. form.Add("dataType", dataTypeEncript)
  167. global.LOG.Info(fmt.Sprintf("SendThs parms:%s", form.Encode()))
  168. resp, err := client.PostForm(sendUrl, form)
  169. if err != nil {
  170. return
  171. }
  172. defer resp.Body.Close()
  173. body, _ := ioutil.ReadAll(resp.Body)
  174. //fmt.Println(string(body))
  175. global.LOG.Info(fmt.Sprintf("ThsResult parms:%s", string(body)))
  176. //同花顺接口返回数据
  177. var tshResult TshResult
  178. err = json.Unmarshal(body, &tshResult)
  179. if err != nil {
  180. err = errors.New(fmt.Sprint("同花顺接口返回数据转换成结构体异常,Err:", err))
  181. return
  182. }
  183. if tshResult.ErrorCode != 1 {
  184. err = errors.New(fmt.Sprint("发送数据到同花顺接口异常,result:", string(body)))
  185. return
  186. }
  187. return
  188. }
  189. // SendVoiceBroadcastToThs 发送语音播报到同花顺
  190. func SendVoiceBroadcastToThs(voice *voice_broadcast.VoiceBroadcast) (err error) {
  191. defer func() {
  192. if err != nil {
  193. go alarm_msg.SendAlarmMsg("发送语音播报至同花顺失败 ErrMsg:"+err.Error(), 3)
  194. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendReportMiniToThs发送报告至同花顺失败, ReportId:" + strconv.Itoa(report.Id) + ", ErrMsg:" + err.Error(), utils.EmailSendToUsers)
  195. }
  196. }()
  197. permissionName := "宏观" //写死宏观,默认所有群都推
  198. //小程序跳转地址
  199. jumpBaseUrl := wechat.WxYbAppId + `/pages-voice/voiceDetail?voiceId=`
  200. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  201. sendDetail, err := models.GetVoiceSendThsDetailById(voice.BroadcastId, "语音播报")
  202. if err != nil && err != utils.ErrNoRow {
  203. return
  204. } else if err == nil && sendDetail != nil {
  205. if sendDetail.Status >= 0 {
  206. fmt.Println("重复发送")
  207. return
  208. }
  209. }
  210. pushTime := time.Now() //预发送时间
  211. //isPrePush := false //是否预发布
  212. //addTime := time.Minute * 40 //短时间内多次发布报告,每篇报告的间隔时间(目前暂定40分钟,只有日度点评的报告需要限制)
  213. ////获取距离现在最近的一条发送成功失败记录
  214. //latelySendDetail, err := models.GetLatelyReportSendThsDetail()
  215. ////如果存在最近一条发送记录,那么去校验时间
  216. //if (err == nil && latelySendDetail != nil) || err == utils.ErrNoRow {
  217. // pushTime = latelySendDetail.PushTime.Add(addTime)
  218. // //如果最近一条的发送记录 的 (发送时间 + 每篇报告的间隔时间) 晚于 当前时间
  219. // if pushTime.After(time.Now()) {
  220. // isPrePush = true
  221. // } else {
  222. // pushTime = time.Now()
  223. // }
  224. //}
  225. //
  226. //if isPrePush { //预发布,只添加预发布记录,不立马发送报告,等待定时任务推送消息给同花顺
  227. // newSendDetail := &models.ReportSendThsDetail{
  228. // ReportId: voice.BroadcastId,
  229. // ReportType: "语音播报",
  230. // Status: 2,
  231. // PushTime: pushTime,
  232. // CreateTime: time.Now(),
  233. // }
  234. // _, tmpErr := models.AddReportSendThsDetail(newSendDetail)
  235. // if tmpErr != nil {
  236. // err = tmpErr
  237. // return
  238. // }
  239. //} else {
  240. // newSendDetail := &models.ReportSendThsDetail{
  241. // ReportId: voice.BroadcastId,
  242. // ReportType: "语音播报",
  243. // Status: 0,
  244. // PushTime: pushTime,
  245. // CreateTime: time.Now(),
  246. // }
  247. // sendDetailId, tmpErr := models.AddReportSendThsDetail(newSendDetail)
  248. // if tmpErr != nil {
  249. // err = tmpErr
  250. // return
  251. // }
  252. // //及时发送
  253. // dataType := "2" //内容类型:1文字 2小程序
  254. // err = SendThs(voice.BroadcastName, permissionName, voice.BroadcastName, fmt.Sprint(jumpBaseUrl, voice.BroadcastId), logoUrl, dataType)
  255. // if err != nil {
  256. // _ = models.ModifyReportSendThsDetailStatus(int(sendDetailId), -1, err.Error())
  257. // return
  258. // }
  259. // _ = models.ModifyReportSendThsDetailStatus(int(sendDetailId), 1, "")
  260. //}
  261. newSendDetail := &models.ReportSendThsDetail{
  262. ReportId: voice.BroadcastId,
  263. ReportType: "语音播报",
  264. Status: 0,
  265. PushTime: pushTime,
  266. CreateTime: time.Now(),
  267. Title: voice.BroadcastName,
  268. LabelStr: permissionName,
  269. RecordStatus: 0,
  270. MsgType: 2,
  271. Content: voice.BroadcastName,
  272. JumpUrl: fmt.Sprint(jumpBaseUrl, voice.BroadcastId),
  273. Pic: logoUrl,
  274. }
  275. sendDetailId, tmpErr := models.AddReportSendThsDetail(newSendDetail)
  276. if tmpErr != nil {
  277. err = tmpErr
  278. return
  279. }
  280. //及时发送
  281. dataType := "2" //内容类型:1文字 2小程序
  282. err = SendThs(voice.BroadcastName, permissionName, voice.BroadcastName, fmt.Sprint(jumpBaseUrl, voice.BroadcastId), logoUrl, dataType)
  283. if err != nil {
  284. _ = models.ModifyReportSendThsDetailStatus(int(sendDetailId), -1, err.Error())
  285. return
  286. }
  287. _ = models.ModifyReportSendThsDetailStatus(int(sendDetailId), 1, "")
  288. return
  289. }