123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- package mpwechat
-
- import (
- base_ctl "SCRM/controllers"
- "SCRM/enums"
- "SCRM/models"
- "SCRM/service/wechat_service"
- "time"
-
- "encoding/json"
-
- "fmt"
- "github.com/astaxie/beego"
- )
-
- func MpMenusCtlRegistRouters() {
- beego.Router("/api/mpwechat/menus", &MpMenusAPIController{}, "Get:GetMenus")
- beego.Router("/api/mpwechat/savemenus", &MpMenusAPIController{}, "Put:SaveMenus")
- beego.Router("/api/mpwechat/deletemenus", &MpMenusAPIController{}, "Post:DeleteMenu")
- }
-
- type MpMenusAPIController struct {
- base_ctl.BaseAuthAPIController
- }
-
- func (c *MpMenusAPIController) GetMenus() {
-
- adminUserInfo := c.GetAdminUserInfo()
-
- buttons, err := wechat_service.GetOrgMenusWithMsgs(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "读取菜单配置信息失败:("+err.Error()+")")
- return
- }
- if buttons != nil && buttons.ButtonStatus != 1 {
- buttons = nil
- }
-
- mediaIDs := make([]string, 0)
- var menus wechat_service.SelfMenuInfo
- if buttons != nil {
- err = json.Unmarshal([]byte(buttons.ButtonFileds), &menus)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "解析菜单配置信息失败:("+err.Error()+")")
- return
- }
-
- msgMap := make(map[string]string, 0)
- for _, msg := range buttons.Messages {
- msgMap[msg.MessageKeyName] = msg.MessageContent
- }
-
- for index, menu := range menus.Button {
- if menu.Type == "media_id" && len(menu.MediaId) > 0 {
- mediaIDs = append(mediaIDs, menu.MediaId)
- }
- if _, exist := msgMap[menu.Key]; exist {
- menus.Button[index].Message = msgMap[menu.Key]
- }
- for sindex, sub := range menus.Button[index].SubButton {
- if sub.Type == "media_id" && len(sub.MediaId) > 0 {
- mediaIDs = append(mediaIDs, sub.MediaId)
- }
-
- if _, exist := msgMap[sub.Key]; exist {
- menus.Button[index].SubButton[sindex].Message = msgMap[sub.Key]
- }
- }
- }
- if len(mediaIDs) > 0 {
- medias, err := wechat_service.FindMeterialsByMediaIDs(adminUserInfo.CurrentOrgId, "image", mediaIDs)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取菜单信息失败:("+err.Error()+")")
- return
- }
- if len(medias) > 0 {
- mediaMap := make(map[string]string, 0)
- for _, media := range medias {
- mediaMap[media.MediaId] = media.MediaUrl
- }
- for index, menu := range menus.Button {
- if menu.Type == "media_id" && len(menu.MediaId) > 0 {
- if _, exist := mediaMap[menu.MediaId]; exist {
- menus.Button[index].MediaUrl = mediaMap[menu.MediaId]
- }
- }
- for sindex, sub := range menus.Button[index].SubButton {
- if sub.Type == "media_id" && len(sub.MediaId) > 0 {
- if _, exist := mediaMap[sub.MediaId]; exist {
- menus.Button[index].SubButton[sindex].MediaUrl = mediaMap[sub.MediaId]
- }
- }
- }
- }
- }
-
- }
-
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["buttons"] = buttons
- returnData["menus"] = menus
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpMenusAPIController) SaveMenus() {
- adminUserInfo := c.GetAdminUserInfo()
- authorization, err := wechat_service.GetAuthorizationByOrgID(adminUserInfo.CurrentOrgId)
- fmt.Println("authorization是什么", authorization)
- fmt.Println("错误", err)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存菜单失败:("+err.Error()+")")
- return
- }
- if authorization == nil || authorization.AuthorizerStatus != 1 {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单失败:(未授权)")
- return
- }
-
- // 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
- // 授权方认证类型,-1代表未认证,0代表微信认证,1代表新浪微博认证,2代表腾讯微博认证,3代表已资质认证通过但还未通过名称认证,4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
- if (authorization.AuthorizerServiceTypeInfo == 0 || authorization.AuthorizerServiceTypeInfo == 1) && authorization.AuthorizerVerifyTypeInfo == -1 {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单失败:(未认证的订阅号没有自定义菜单的接口权限)")
- return
- }
- var buttons wechat_service.SelfMenuInfo
-
- err = json.Unmarshal(c.Ctx.Input.RequestBody, &buttons)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "解析菜单失败:("+err.Error()+")")
- return
- }
-
- clickMap := make(map[string]string, 0)
- for pindex, button := range buttons.Button {
- if button.Type == "click" && len(button.Key) > 0 && len(button.SubButton) == 0 {
- clickMap[button.Key] = button.Message
- }
- if button.Type != "click" {
- buttons.Button[pindex].Key = ""
- }
- if len(button.SubButton) > 0 {
- for sindex, child := range button.SubButton {
- if child.Type == "click" && len(child.Key) > 0 {
- clickMap[child.Key] = child.Message
- }
- buttons.Button[pindex].SubButton[sindex].Message = ""
-
- if child.Type != "click" {
- buttons.Button[pindex].SubButton[sindex].Key = ""
- }
- }
- }
- buttons.Button[pindex].Message = ""
- }
- messages, err := wechat_service.GetOrgAllClickMessages(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存菜单失败:("+err.Error()+")")
- return
- }
- oldMsgs := make(map[string]models.AuthorizationMessageManagements)
- for _, message := range messages {
- if _, exist := clickMap[message.MessageKeyName]; exist {
- msg := *message
- oldMsgs[message.MessageKeyName] = msg
- }
- }
-
- buttonFileds, err := json.Marshal(buttons)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "解析菜单失败:("+err.Error()+")")
- return
- }
-
- timeNow := time.Now().Unix()
- var saveButton wechat_service.Buttons
- saveButton.UserOrgID = adminUserInfo.CurrentOrgId
- saveButton.UpdatedTime = timeNow
- saveButton.ButtonStatus = 1
- saveButton.SendStatus = 0
- saveButton.ButtonFileds = string(buttonFileds)
-
- button, err := wechat_service.GetMenusByOrgID(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存菜单失败:("+err.Error()+")")
- return
- }
- if button != nil {
- saveButton.ID = button.ID
- saveButton.CreatedTime = button.CreatedTime
- }
-
- saveButton.Messages = make([]*models.AuthorizationMessageManagements, 0)
- for keyName, Content := range clickMap {
- var msg models.AuthorizationMessageManagements
- msg.MessageMsgType = "event"
- msg.MessageMsgEvent = "click"
- msg.MessageContent = Content
- msg.UpdatedTime = timeNow
- msg.MessageStatus = 1
- msg.MessageKeyName = keyName
- msg.UserOrgId = adminUserInfo.CurrentOrgId
-
- if _, exist := oldMsgs[keyName]; exist {
- msg.ID = oldMsgs[keyName].ID
- msg.CreatedTime = oldMsgs[keyName].CreatedTime
- } else {
- msg.CreatedTime = timeNow
- }
- saveButton.Messages = append(saveButton.Messages, &msg)
- }
-
- err = wechat_service.SaveButtons(&saveButton)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单失败:("+err.Error()+")")
- return
- }
- err = wechat_service.SendMpWechatMenus(authorization.AuthorizerAccessToken, buttonFileds)
- fmt.Println("公众号错误是什么", err)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单成功,但发布菜单到公众号失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["buttons"] = buttons
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpMenusAPIController) DeleteMenu() {
-
- adminUserInfo := c.GetAdminUserInfo()
- authorization, err := wechat_service.GetAuthorizationByOrgID(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
- return
- }
- if authorization == nil || authorization.AuthorizerStatus != 1 {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "删除菜单失败:(未授权)")
- return
- }
-
- // 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
- // 授权方认证类型,-1代表未认证,0代表微信认证,1代表新浪微博认证,2代表腾讯微博认证,3代表已资质认证通过但还未通过名称认证,4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
- if (authorization.AuthorizerServiceTypeInfo == 0 || authorization.AuthorizerServiceTypeInfo == 1) && authorization.AuthorizerVerifyTypeInfo == -1 {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "删除菜单失败:(未认证的订阅号没有自定义菜单相关接口的权限)")
- return
- }
-
- button, err := wechat_service.GetMenusByOrgID(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
- return
- }
- if button == nil || button.ButtonStatus != 1 {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:(未设置菜单)")
- return
- }
-
- err = wechat_service.DeleteMpWechatMenus(authorization.AuthorizerAccessToken)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
- return
- }
-
- err = wechat_service.DeleteButton(adminUserInfo.CurrentOrgId, button.ID)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["msg"] = "ok"
- c.ServeSuccessJSON(returnData)
- return
-
- }
|