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 }