123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- package mpwechat
-
- import (
- base_ctl "SCRM/controllers"
- "SCRM/enums"
- "SCRM/models"
- "SCRM/service/wechat_service"
- "encoding/json"
- "reflect"
-
- "SCRM/utils"
- "time"
-
- "github.com/astaxie/beego"
- )
-
- func MpReplyCtlRegistRouters() {
- beego.Router("/api/mpwechat/reply/subscribe", &MpReplyAPIController{}, "Get:GetSubscribe")
- beego.Router("/api/mpwechat/reply/subscribe", &MpReplyAPIController{}, "Put:SaveSubscribe")
- beego.Router("/api/mpwechat/reply/kewords", &MpReplyAPIController{}, "Get:GetKeyWordReplys")
- beego.Router("/api/mpwechat/reply/keword", &MpReplyAPIController{}, "Post:CreateKeyWordReply")
- beego.Router("/api/mpwechat/reply/keword", &MpReplyAPIController{}, "Put:EditKeyWordReply")
- beego.Router("/api/mpwechat/replys", &MpReplyAPIController{}, "Delete:DeleteReplys")
- }
-
- type MpReplyAPIController struct {
- base_ctl.BaseAuthAPIController
- }
-
- func (c *MpReplyAPIController) GetSubscribe() {
-
- adminUserInfo := c.GetAdminUserInfo()
-
- message, err := wechat_service.GetOrgSubscribeReplyMessages(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "读取关注回复数据失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["message"] = message
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpReplyAPIController) SaveSubscribe() {
- adminUserInfo := c.GetAdminUserInfo()
- timeNow := time.Now().Unix()
-
- dataBody := make(map[string]interface{}, 0)
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
- return
- }
-
- var saveMessage models.AuthorizationMessageManagements
- message, err := wechat_service.GetOrgSubscribeReplyMessages(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存失败:("+err.Error()+")")
- return
- }
- if message != nil {
- saveMessage = *message
- } else {
- saveMessage.CreatedTime = timeNow
- saveMessage.MessageMsgType = "event"
- saveMessage.MessageMsgEvent = "subscribe"
- saveMessage.MessageStatus = 1
- saveMessage.UserOrgId = adminUserInfo.CurrentOrgId
- }
-
- if dataBody["message_content"] == nil || reflect.TypeOf(dataBody["message_content"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_content")
- return
- }
- messageContent, _ := dataBody["message_content"].(string)
- saveMessage.MessageContent = messageContent
- saveMessage.UpdatedTime = timeNow
-
- err = wechat_service.SaveMessage(&saveMessage)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["message"] = saveMessage
- c.ServeSuccessJSON(returnData)
- return
-
- }
-
- func (c *MpReplyAPIController) GetKeyWordReplys() {
- page, _ := c.GetInt64("page", 1)
- limit, _ := c.GetInt64("limit", 10)
- searchKey := c.GetString("search", "")
- if page <= 0 {
- page = 1
- }
- if limit <= 0 {
- limit = 10
- }
-
- adminUserInfo := c.GetAdminUserInfo()
- messages, total, err := wechat_service.GetPageKeyWordMessages(adminUserInfo.CurrentOrgId, page, limit, searchKey)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "拉取失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["messages"] = messages
- returnData["total"] = total
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpReplyAPIController) CreateKeyWordReply() {
- adminUserInfo := c.GetAdminUserInfo()
- timeNow := time.Now().Unix()
-
- dataBody := make(map[string]interface{}, 0)
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
- return
- }
-
- var message models.AuthorizationMessageManagements
-
- if dataBody["message_regular_name"] == nil || reflect.TypeOf(dataBody["message_regular_name"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_regular_name")
- return
- }
- messageRegularName, _ := dataBody["message_regular_name"].(string)
- if len(messageRegularName) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "规则名称不能为空")
- return
- }
- message.MessageRegularName = messageRegularName
-
- if dataBody["message_key_type"] == nil || reflect.TypeOf(dataBody["message_key_type"]).String() != "float64" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_type")
- return
- }
- messageKeyType := int64(dataBody["message_key_type"].(float64))
- if messageKeyType != 1 && messageKeyType != 2 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "模式选择不正确")
- return
- }
- message.MessageKeyType = messageKeyType
-
- if dataBody["message_key_name"] == nil || reflect.TypeOf(dataBody["message_key_name"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_name")
- return
- }
- messageKeyName, _ := dataBody["message_key_name"].(string)
- if len(messageKeyName) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "关键字不能为空")
- return
- }
- message.MessageKeyName = messageKeyName
-
- if dataBody["message_content"] == nil || reflect.TypeOf(dataBody["message_content"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_content")
- return
- }
- messageContent, _ := dataBody["message_content"].(string)
- if len(messageKeyName) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "回复内容不能为空")
- return
- }
- message.MessageContent = messageContent
-
- message.MessageMsgType = "text"
- message.UpdatedTime = timeNow
- message.CreatedTime = timeNow
- message.MessageStatus = 1
- message.UserOrgId = adminUserInfo.CurrentOrgId
-
- err = wechat_service.SaveMessage(&message)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加回复失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["message"] = message
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpReplyAPIController) EditKeyWordReply() {
- id, _ := c.GetInt64("id", 0)
- if id <= 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id")
- return
- }
-
- adminUserInfo := c.GetAdminUserInfo()
- timeNow := time.Now().Unix()
-
- dataBody := make(map[string]interface{}, 0)
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
- return
- }
-
- message, err := wechat_service.GetMessageByID(adminUserInfo.CurrentOrgId, id)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存失败:("+err.Error()+")")
- return
- }
- if message == nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存失败:(回复不存在)")
- return
- }
-
- if dataBody["message_regular_name"] == nil || reflect.TypeOf(dataBody["message_regular_name"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_regular_name")
- return
- }
- messageRegularName, _ := dataBody["message_regular_name"].(string)
- if len(messageRegularName) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "规则名称不能为空")
- return
- }
- message.MessageRegularName = messageRegularName
-
- if dataBody["message_key_type"] == nil || reflect.TypeOf(dataBody["message_key_type"]).String() != "float64" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_type")
- return
- }
- messageKeyType := int64(dataBody["message_key_type"].(float64))
- if messageKeyType != 1 && messageKeyType != 2 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "模式选择不正确")
- return
- }
- message.MessageKeyType = messageKeyType
-
- if dataBody["message_key_name"] == nil || reflect.TypeOf(dataBody["message_key_name"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_key_name")
- return
- }
- messageKeyName, _ := dataBody["message_key_name"].(string)
- if len(messageKeyName) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "关键字不能为空")
- return
- }
- message.MessageKeyName = messageKeyName
-
- if dataBody["message_content"] == nil || reflect.TypeOf(dataBody["message_content"]).String() != "string" {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:message_content")
- return
- }
- messageContent, _ := dataBody["message_content"].(string)
- if len(messageKeyName) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "回复内容不能为空")
- return
- }
- message.MessageContent = messageContent
-
- message.UpdatedTime = timeNow
-
- err = wechat_service.SaveMessage(message)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "编辑回复失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["message"] = message
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpReplyAPIController) DeleteReplys() {
- adminUserInfo := c.GetAdminUserInfo()
-
- dataBody := make(map[string]interface{}, 0)
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
- return
- }
-
- idsInters := dataBody["ids"].([]interface{})
- if len(idsInters) == 0 {
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除失败:(没有选择回复)")
- return
- }
- }
-
- ids := make([]int64, 0)
- for _, idsInter := range idsInters {
- id := int64(idsInter.(float64))
- ids = append(ids, id)
- }
-
- err = wechat_service.DeleteMessages(adminUserInfo.CurrentOrgId, ids)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["msg"] = "ok"
-
- c.ServeSuccessJSON(returnData)
- return
- }
|