wxRobot.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  5. "encoding/json"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "rpc/config"
  8. "time"
  9. )
  10. type WXroBot struct {
  11. *xweb.Action
  12. addTaskHandler xweb.Mapper `xweb:"/add/task"` //新增任务
  13. updateTaskHandler xweb.Mapper `xweb:"/update/task"` //修改任务
  14. }
  15. type TaskRequest struct {
  16. TaskID int64 `json:"taskId,omitempty"`
  17. TaskName string `json:"taskName"`
  18. SendGroupID string `json:"sendGroupId"`
  19. SendGroupName string `json:"sendGroupName"`
  20. TaskType int64 `json:"taskType"`
  21. SendTimeType int64 `json:"sendTimeType"`
  22. SendTime string `json:"sendTime"`
  23. Content []map[string]interface{} `json:"content"`
  24. }
  25. func (a *WXroBot) AddTaskHandler() {
  26. var req TaskRequest
  27. err := json.Unmarshal(a.Body(), &req)
  28. taskId := int64(0)
  29. msg := ""
  30. userid := common.ObjToString(a.GetSession("userId"))
  31. status := int64(0)
  32. content := gconv.Maps(req.Content)
  33. if err == nil && len(content) > 0 {
  34. taskType := req.TaskType
  35. sendTimeType := req.SendTimeType
  36. taskId = config.WxRobot.Insert("task", map[string]interface{}{
  37. "task_name": req.TaskName,
  38. "send_group_id": req.SendGroupID,
  39. "task_type": req.TaskType,
  40. "send_time_type": req.SendTimeType,
  41. "send_time": req.SendTime,
  42. "task_status": 0,
  43. "create_time": time.Now().Format(time.DateTime),
  44. "update_time": time.Now().Format(time.DateTime),
  45. "create_person_id": userid,
  46. "send_group_name": req.SendGroupName,
  47. })
  48. if taskId <= 0 {
  49. msg = "保存失败"
  50. } else {
  51. for _, v := range content {
  52. text := gconv.String(v["text"])
  53. fileType := gconv.Int64(v["type"])
  54. config.WxRobot.Insert("task_send_content", map[string]interface{}{
  55. "task_id": taskId,
  56. "content": text,
  57. "content_type": fileType,
  58. "create_time": time.Now().Format(time.DateTime),
  59. "update_time": time.Now().Format(time.DateTime),
  60. "status": 0,
  61. })
  62. }
  63. status = 1
  64. if taskType == 0 && sendTimeType == 0 {
  65. go func() {
  66. allUserMap := InitUser()
  67. ScheduledTasks(allUserMap, taskId)
  68. }()
  69. }
  70. }
  71. } else {
  72. msg = "参数格式不对"
  73. }
  74. a.ServeJson(config.Result{Data: config.M{"status": status}, Error_msg: msg})
  75. }
  76. func (a *WXroBot) UpdateTaskHandler() {
  77. var req TaskRequest
  78. err := json.Unmarshal(a.Body(), &req)
  79. msg := ""
  80. status := int64(0)
  81. content := gconv.Maps(req.Content)
  82. if err == nil && len(content) > 0 {
  83. taskId := req.TaskID
  84. taskType := req.TaskType
  85. sendTimeType := req.SendTimeType
  86. ok := config.WxRobot.Update("task", map[string]interface{}{
  87. "id": taskId,
  88. }, map[string]interface{}{
  89. "task_name": req.TaskName,
  90. "send_group_id": req.SendGroupID,
  91. "task_type": req.TaskType,
  92. "send_time_type": req.SendTimeType,
  93. "send_time": req.SendTime,
  94. "update_time": time.Now().Format(time.DateTime),
  95. "send_group_name": req.SendGroupName,
  96. })
  97. if ok {
  98. config.WxRobot.Update("task_send_content", map[string]interface{}{
  99. "task_id": taskId,
  100. }, map[string]interface{}{
  101. "status": 1,
  102. })
  103. for _, v := range content {
  104. text := gconv.String(v["text"])
  105. fileType := gconv.Int64(v["type"])
  106. config.WxRobot.Insert("task_send_content", map[string]interface{}{
  107. "task_id": taskId,
  108. "content": text,
  109. "content_type": fileType,
  110. "create_time": time.Now().Format(time.DateTime),
  111. "update_time": time.Now().Format(time.DateTime),
  112. "status": 0,
  113. })
  114. }
  115. status = 1
  116. if taskType == 0 && sendTimeType == 0 {
  117. go func() {
  118. allUserMap := InitUser()
  119. ScheduledTasks(allUserMap, taskId)
  120. }()
  121. }
  122. } else {
  123. msg = "修改失败"
  124. }
  125. } else {
  126. msg = "参数格式不对"
  127. }
  128. a.ServeJson(config.Result{Data: config.M{"status": status}, Error_msg: msg})
  129. }