menu_controller.go 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package mpwechat
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/models"
  6. "SCRM/service/wechat_service"
  7. "time"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/astaxie/beego"
  11. )
  12. func MpMenusCtlRegistRouters() {
  13. beego.Router("/api/mpwechat/menus", &MpMenusAPIController{}, "Get:GetMenus")
  14. beego.Router("/api/mpwechat/savemenus", &MpMenusAPIController{}, "Put:SaveMenus")
  15. beego.Router("/api/mpwechat/deletemenus", &MpMenusAPIController{}, "Post:DeleteMenu")
  16. }
  17. type MpMenusAPIController struct {
  18. base_ctl.BaseAuthAPIController
  19. }
  20. func (c *MpMenusAPIController) GetMenus() {
  21. adminUserInfo := c.GetAdminUserInfo()
  22. buttons, err := wechat_service.GetOrgMenusWithMsgs(adminUserInfo.CurrentOrgId)
  23. if err != nil {
  24. c.ServeFailJsonSend(enums.ErrorCodeDataException, "读取菜单配置信息失败:("+err.Error()+")")
  25. return
  26. }
  27. if buttons != nil && buttons.ButtonStatus != 1 {
  28. buttons = nil
  29. }
  30. mediaIDs := make([]string, 0)
  31. var menus wechat_service.SelfMenuInfo
  32. if buttons != nil {
  33. err = json.Unmarshal([]byte(buttons.ButtonFileds), &menus)
  34. if err != nil {
  35. c.ServeFailJsonSend(enums.ErrorCodeDataException, "解析菜单配置信息失败:("+err.Error()+")")
  36. return
  37. }
  38. msgMap := make(map[string]string, 0)
  39. for _, msg := range buttons.Messages {
  40. msgMap[msg.MessageKeyName] = msg.MessageContent
  41. }
  42. for index, menu := range menus.Button {
  43. if menu.Type == "media_id" && len(menu.MediaId) > 0 {
  44. mediaIDs = append(mediaIDs, menu.MediaId)
  45. }
  46. if _, exist := msgMap[menu.Key]; exist {
  47. menus.Button[index].Message = msgMap[menu.Key]
  48. }
  49. for sindex, sub := range menus.Button[index].SubButton {
  50. if sub.Type == "media_id" && len(sub.MediaId) > 0 {
  51. mediaIDs = append(mediaIDs, sub.MediaId)
  52. }
  53. if _, exist := msgMap[sub.Key]; exist {
  54. menus.Button[index].SubButton[sindex].Message = msgMap[sub.Key]
  55. }
  56. }
  57. }
  58. if len(mediaIDs) > 0 {
  59. medias, err := wechat_service.FindMeterialsByMediaIDs(adminUserInfo.CurrentOrgId, "image", mediaIDs)
  60. if err != nil {
  61. c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取菜单信息失败:("+err.Error()+")")
  62. return
  63. }
  64. if len(medias) > 0 {
  65. mediaMap := make(map[string]string, 0)
  66. for _, media := range medias {
  67. mediaMap[media.MediaId] = media.MediaUrl
  68. }
  69. for index, menu := range menus.Button {
  70. if menu.Type == "media_id" && len(menu.MediaId) > 0 {
  71. if _, exist := mediaMap[menu.MediaId]; exist {
  72. menus.Button[index].MediaUrl = mediaMap[menu.MediaId]
  73. }
  74. }
  75. for sindex, sub := range menus.Button[index].SubButton {
  76. if sub.Type == "media_id" && len(sub.MediaId) > 0 {
  77. if _, exist := mediaMap[sub.MediaId]; exist {
  78. menus.Button[index].SubButton[sindex].MediaUrl = mediaMap[sub.MediaId]
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. returnData := make(map[string]interface{}, 0)
  87. returnData["buttons"] = buttons
  88. returnData["menus"] = menus
  89. c.ServeSuccessJSON(returnData)
  90. return
  91. }
  92. func (c *MpMenusAPIController) SaveMenus() {
  93. adminUserInfo := c.GetAdminUserInfo()
  94. authorization, err := wechat_service.GetAuthorizationByOrgID(adminUserInfo.CurrentOrgId)
  95. fmt.Println("authorization是什么", authorization)
  96. fmt.Println("错误", err)
  97. if err != nil {
  98. c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存菜单失败:("+err.Error()+")")
  99. return
  100. }
  101. if authorization == nil || authorization.AuthorizerStatus != 1 {
  102. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单失败:(未授权)")
  103. return
  104. }
  105. // 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
  106. // 授权方认证类型,-1代表未认证,0代表微信认证,1代表新浪微博认证,2代表腾讯微博认证,3代表已资质认证通过但还未通过名称认证,4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
  107. if (authorization.AuthorizerServiceTypeInfo == 0 || authorization.AuthorizerServiceTypeInfo == 1) && authorization.AuthorizerVerifyTypeInfo == -1 {
  108. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单失败:(未认证的订阅号没有自定义菜单的接口权限)")
  109. return
  110. }
  111. var buttons wechat_service.SelfMenuInfo
  112. err = json.Unmarshal(c.Ctx.Input.RequestBody, &buttons)
  113. if err != nil {
  114. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "解析菜单失败:("+err.Error()+")")
  115. return
  116. }
  117. clickMap := make(map[string]string, 0)
  118. for pindex, button := range buttons.Button {
  119. if button.Type == "click" && len(button.Key) > 0 && len(button.SubButton) == 0 {
  120. clickMap[button.Key] = button.Message
  121. }
  122. if button.Type != "click" {
  123. buttons.Button[pindex].Key = ""
  124. }
  125. if len(button.SubButton) > 0 {
  126. for sindex, child := range button.SubButton {
  127. if child.Type == "click" && len(child.Key) > 0 {
  128. clickMap[child.Key] = child.Message
  129. }
  130. buttons.Button[pindex].SubButton[sindex].Message = ""
  131. if child.Type != "click" {
  132. buttons.Button[pindex].SubButton[sindex].Key = ""
  133. }
  134. }
  135. }
  136. buttons.Button[pindex].Message = ""
  137. }
  138. messages, err := wechat_service.GetOrgAllClickMessages(adminUserInfo.CurrentOrgId)
  139. if err != nil {
  140. c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存菜单失败:("+err.Error()+")")
  141. return
  142. }
  143. oldMsgs := make(map[string]models.AuthorizationMessageManagements)
  144. for _, message := range messages {
  145. if _, exist := clickMap[message.MessageKeyName]; exist {
  146. msg := *message
  147. oldMsgs[message.MessageKeyName] = msg
  148. }
  149. }
  150. buttonFileds, err := json.Marshal(buttons)
  151. if err != nil {
  152. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "解析菜单失败:("+err.Error()+")")
  153. return
  154. }
  155. timeNow := time.Now().Unix()
  156. var saveButton wechat_service.Buttons
  157. saveButton.UserOrgID = adminUserInfo.CurrentOrgId
  158. saveButton.UpdatedTime = timeNow
  159. saveButton.ButtonStatus = 1
  160. saveButton.SendStatus = 0
  161. saveButton.ButtonFileds = string(buttonFileds)
  162. button, err := wechat_service.GetMenusByOrgID(adminUserInfo.CurrentOrgId)
  163. if err != nil {
  164. c.ServeFailJsonSend(enums.ErrorCodeDataException, "保存菜单失败:("+err.Error()+")")
  165. return
  166. }
  167. if button != nil {
  168. saveButton.ID = button.ID
  169. saveButton.CreatedTime = button.CreatedTime
  170. }
  171. saveButton.Messages = make([]*models.AuthorizationMessageManagements, 0)
  172. for keyName, Content := range clickMap {
  173. var msg models.AuthorizationMessageManagements
  174. msg.MessageMsgType = "event"
  175. msg.MessageMsgEvent = "click"
  176. msg.MessageContent = Content
  177. msg.UpdatedTime = timeNow
  178. msg.MessageStatus = 1
  179. msg.MessageKeyName = keyName
  180. msg.UserOrgId = adminUserInfo.CurrentOrgId
  181. if _, exist := oldMsgs[keyName]; exist {
  182. msg.ID = oldMsgs[keyName].ID
  183. msg.CreatedTime = oldMsgs[keyName].CreatedTime
  184. } else {
  185. msg.CreatedTime = timeNow
  186. }
  187. saveButton.Messages = append(saveButton.Messages, &msg)
  188. }
  189. err = wechat_service.SaveButtons(&saveButton)
  190. if err != nil {
  191. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单失败:("+err.Error()+")")
  192. return
  193. }
  194. err = wechat_service.SendMpWechatMenus(authorization.AuthorizerAccessToken, buttonFileds)
  195. fmt.Println("公众号错误是什么", err)
  196. if err != nil {
  197. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "保存菜单成功,但发布菜单到公众号失败:("+err.Error()+")")
  198. return
  199. }
  200. returnData := make(map[string]interface{}, 0)
  201. returnData["buttons"] = buttons
  202. c.ServeSuccessJSON(returnData)
  203. return
  204. }
  205. func (c *MpMenusAPIController) DeleteMenu() {
  206. adminUserInfo := c.GetAdminUserInfo()
  207. authorization, err := wechat_service.GetAuthorizationByOrgID(adminUserInfo.CurrentOrgId)
  208. if err != nil {
  209. c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
  210. return
  211. }
  212. if authorization == nil || authorization.AuthorizerStatus != 1 {
  213. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "删除菜单失败:(未授权)")
  214. return
  215. }
  216. // 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
  217. // 授权方认证类型,-1代表未认证,0代表微信认证,1代表新浪微博认证,2代表腾讯微博认证,3代表已资质认证通过但还未通过名称认证,4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
  218. if (authorization.AuthorizerServiceTypeInfo == 0 || authorization.AuthorizerServiceTypeInfo == 1) && authorization.AuthorizerVerifyTypeInfo == -1 {
  219. c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "删除菜单失败:(未认证的订阅号没有自定义菜单相关接口的权限)")
  220. return
  221. }
  222. button, err := wechat_service.GetMenusByOrgID(adminUserInfo.CurrentOrgId)
  223. if err != nil {
  224. c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
  225. return
  226. }
  227. if button == nil || button.ButtonStatus != 1 {
  228. c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:(未设置菜单)")
  229. return
  230. }
  231. err = wechat_service.DeleteMpWechatMenus(authorization.AuthorizerAccessToken)
  232. if err != nil {
  233. c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
  234. return
  235. }
  236. err = wechat_service.DeleteButton(adminUserInfo.CurrentOrgId, button.ID)
  237. if err != nil {
  238. c.ServeFailJsonSend(enums.ErrorCodeDataException, "删除菜单失败:("+err.Error()+")")
  239. return
  240. }
  241. returnData := make(map[string]interface{}, 0)
  242. returnData["msg"] = "ok"
  243. c.ServeSuccessJSON(returnData)
  244. return
  245. }