reply_controller.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package mpwechat
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/models"
  6. "SCRM/service/wechat_service"
  7. "encoding/json"
  8. "reflect"
  9. "SCRM/utils"
  10. "time"
  11. "github.com/astaxie/beego"
  12. )
  13. func MpReplyCtlRegistRouters() {
  14. beego.Router("/api/mpwechat/reply/subscribe", &MpReplyAPIController{}, "Get:GetSubscribe")
  15. beego.Router("/api/mpwechat/reply/subscribe", &MpReplyAPIController{}, "Put:SaveSubscribe")
  16. beego.Router("/api/mpwechat/reply/kewords", &MpReplyAPIController{}, "Get:GetKeyWordReplys")
  17. beego.Router("/api/mpwechat/reply/keword", &MpReplyAPIController{}, "Post:CreateKeyWordReply")
  18. beego.Router("/api/mpwechat/reply/keword", &MpReplyAPIController{}, "Put:EditKeyWordReply")
  19. beego.Router("/api/mpwechat/replys", &MpReplyAPIController{}, "Delete:DeleteReplys")
  20. }
  21. type MpReplyAPIController struct {
  22. base_ctl.BaseAuthAPIController
  23. }
  24. func (c *MpReplyAPIController) GetSubscribe() {
  25. adminUserInfo := c.GetAdminUserInfo()
  26. message, err := wechat_service.GetOrgSubscribeReplyMessages(adminUserInfo.CurrentOrgId)
  27. if err != nil {
  28. c.ServeFailJsonSend(enums.ErrorCodeDataException, "读取关注回复数据失败:("+err.Error()+")")
  29. return
  30. }
  31. returnData := make(map[string]interface{}, 0)
  32. returnData["message"] = message
  33. c.ServeSuccessJSON(returnData)
  34. return
  35. }
  36. func (c *MpReplyAPIController) SaveSubscribe() {
  37. adminUserInfo := c.GetAdminUserInfo()
  38. timeNow := time.Now().Unix()
  39. dataBody := make(map[string]interface{}, 0)
  40. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  41. if err != nil {
  42. utils.ErrorLog(err.Error())
  43. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
  44. return
  45. }
  46. var saveMessage models.AuthorizationMessageManagements
  47. message, err := wechat_service.GetOrgSubscribeReplyMessages(adminUserInfo.CurrentOrgId)
  48. if err != nil {
  49. c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存失败:("+err.Error()+")")
  50. return
  51. }
  52. if message != nil {
  53. saveMessage = *message
  54. } else {
  55. saveMessage.CreatedTime = timeNow
  56. saveMessage.MessageMsgType = "event"
  57. saveMessage.MessageMsgEvent = "subscribe"
  58. saveMessage.MessageStatus = 1
  59. saveMessage.UserOrgId = adminUserInfo.CurrentOrgId
  60. }
  61. if dataBody["message_content"] == nil || reflect.TypeOf(dataBody["message_content"]).String() != "string" {
  62. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_content")
  63. return
  64. }
  65. messageContent, _ := dataBody["message_content"].(string)
  66. saveMessage.MessageContent = messageContent
  67. saveMessage.UpdatedTime = timeNow
  68. err = wechat_service.SaveMessage(&saveMessage)
  69. if err != nil {
  70. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存失败:("+err.Error()+")")
  71. return
  72. }
  73. returnData := make(map[string]interface{}, 0)
  74. returnData["message"] = saveMessage
  75. c.ServeSuccessJSON(returnData)
  76. return
  77. }
  78. func (c *MpReplyAPIController) GetKeyWordReplys() {
  79. page, _ := c.GetInt64("page", 1)
  80. limit, _ := c.GetInt64("limit", 10)
  81. searchKey := c.GetString("search", "")
  82. if page <= 0 {
  83. page = 1
  84. }
  85. if limit <= 0 {
  86. limit = 10
  87. }
  88. adminUserInfo := c.GetAdminUserInfo()
  89. messages, total, err := wechat_service.GetPageKeyWordMessages(adminUserInfo.CurrentOrgId, page, limit, searchKey)
  90. if err != nil {
  91. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "拉取失败:("+err.Error()+")")
  92. return
  93. }
  94. returnData := make(map[string]interface{}, 0)
  95. returnData["messages"] = messages
  96. returnData["total"] = total
  97. c.ServeSuccessJSON(returnData)
  98. return
  99. }
  100. func (c *MpReplyAPIController) CreateKeyWordReply() {
  101. adminUserInfo := c.GetAdminUserInfo()
  102. timeNow := time.Now().Unix()
  103. dataBody := make(map[string]interface{}, 0)
  104. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  105. if err != nil {
  106. utils.ErrorLog(err.Error())
  107. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
  108. return
  109. }
  110. var message models.AuthorizationMessageManagements
  111. if dataBody["message_regular_name"] == nil || reflect.TypeOf(dataBody["message_regular_name"]).String() != "string" {
  112. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_regular_name")
  113. return
  114. }
  115. messageRegularName, _ := dataBody["message_regular_name"].(string)
  116. if len(messageRegularName) == 0 {
  117. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "规则名称不能为空")
  118. return
  119. }
  120. message.MessageRegularName = messageRegularName
  121. if dataBody["message_key_type"] == nil || reflect.TypeOf(dataBody["message_key_type"]).String() != "float64" {
  122. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_type")
  123. return
  124. }
  125. messageKeyType := int64(dataBody["message_key_type"].(float64))
  126. if messageKeyType != 1 && messageKeyType != 2 {
  127. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "模式选择不正确")
  128. return
  129. }
  130. message.MessageKeyType = messageKeyType
  131. if dataBody["message_key_name"] == nil || reflect.TypeOf(dataBody["message_key_name"]).String() != "string" {
  132. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_name")
  133. return
  134. }
  135. messageKeyName, _ := dataBody["message_key_name"].(string)
  136. if len(messageKeyName) == 0 {
  137. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "关键字不能为空")
  138. return
  139. }
  140. message.MessageKeyName = messageKeyName
  141. if dataBody["message_content"] == nil || reflect.TypeOf(dataBody["message_content"]).String() != "string" {
  142. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_content")
  143. return
  144. }
  145. messageContent, _ := dataBody["message_content"].(string)
  146. if len(messageKeyName) == 0 {
  147. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "回复内容不能为空")
  148. return
  149. }
  150. message.MessageContent = messageContent
  151. message.MessageMsgType = "text"
  152. message.UpdatedTime = timeNow
  153. message.CreatedTime = timeNow
  154. message.MessageStatus = 1
  155. message.UserOrgId = adminUserInfo.CurrentOrgId
  156. err = wechat_service.SaveMessage(&message)
  157. if err != nil {
  158. c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加回复失败:("+err.Error()+")")
  159. return
  160. }
  161. returnData := make(map[string]interface{}, 0)
  162. returnData["message"] = message
  163. c.ServeSuccessJSON(returnData)
  164. return
  165. }
  166. func (c *MpReplyAPIController) EditKeyWordReply() {
  167. id, _ := c.GetInt64("id", 0)
  168. if id <= 0 {
  169. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id")
  170. return
  171. }
  172. adminUserInfo := c.GetAdminUserInfo()
  173. timeNow := time.Now().Unix()
  174. dataBody := make(map[string]interface{}, 0)
  175. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  176. if err != nil {
  177. utils.ErrorLog(err.Error())
  178. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
  179. return
  180. }
  181. message, err := wechat_service.GetMessageByID(adminUserInfo.CurrentOrgId, id)
  182. if err != nil {
  183. c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存失败:("+err.Error()+")")
  184. return
  185. }
  186. if message == nil {
  187. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存失败:(回复不存在)")
  188. return
  189. }
  190. if dataBody["message_regular_name"] == nil || reflect.TypeOf(dataBody["message_regular_name"]).String() != "string" {
  191. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_regular_name")
  192. return
  193. }
  194. messageRegularName, _ := dataBody["message_regular_name"].(string)
  195. if len(messageRegularName) == 0 {
  196. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "规则名称不能为空")
  197. return
  198. }
  199. message.MessageRegularName = messageRegularName
  200. if dataBody["message_key_type"] == nil || reflect.TypeOf(dataBody["message_key_type"]).String() != "float64" {
  201. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_type")
  202. return
  203. }
  204. messageKeyType := int64(dataBody["message_key_type"].(float64))
  205. if messageKeyType != 1 && messageKeyType != 2 {
  206. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "模式选择不正确")
  207. return
  208. }
  209. message.MessageKeyType = messageKeyType
  210. if dataBody["message_key_name"] == nil || reflect.TypeOf(dataBody["message_key_name"]).String() != "string" {
  211. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_name")
  212. return
  213. }
  214. messageKeyName, _ := dataBody["message_key_name"].(string)
  215. if len(messageKeyName) == 0 {
  216. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "关键字不能为空")
  217. return
  218. }
  219. message.MessageKeyName = messageKeyName
  220. if dataBody["message_content"] == nil || reflect.TypeOf(dataBody["message_content"]).String() != "string" {
  221. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_content")
  222. return
  223. }
  224. messageContent, _ := dataBody["message_content"].(string)
  225. if len(messageKeyName) == 0 {
  226. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "回复内容不能为空")
  227. return
  228. }
  229. message.MessageContent = messageContent
  230. message.UpdatedTime = timeNow
  231. err = wechat_service.SaveMessage(message)
  232. if err != nil {
  233. c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "编辑回复失败:("+err.Error()+")")
  234. return
  235. }
  236. returnData := make(map[string]interface{}, 0)
  237. returnData["message"] = message
  238. c.ServeSuccessJSON(returnData)
  239. return
  240. }
  241. func (c *MpReplyAPIController) DeleteReplys() {
  242. adminUserInfo := c.GetAdminUserInfo()
  243. dataBody := make(map[string]interface{}, 0)
  244. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  245. if err != nil {
  246. utils.ErrorLog(err.Error())
  247. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
  248. return
  249. }
  250. idsInters := dataBody["ids"].([]interface{})
  251. if len(idsInters) == 0 {
  252. if err != nil {
  253. c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除失败:(没有选择回复)")
  254. return
  255. }
  256. }
  257. ids := make([]int64, 0)
  258. for _, idsInter := range idsInters {
  259. id := int64(idsInter.(float64))
  260. ids = append(ids, id)
  261. }
  262. err = wechat_service.DeleteMessages(adminUserInfo.CurrentOrgId, ids)
  263. if err != nil {
  264. c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除失败:("+err.Error()+")")
  265. return
  266. }
  267. returnData := make(map[string]interface{}, 0)
  268. returnData["msg"] = "ok"
  269. c.ServeSuccessJSON(returnData)
  270. return
  271. }