一物一码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

150 lines
3.9 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/enum"
  7. "applet/app/md"
  8. "applet/app/utils"
  9. "applet/app/utils/cache"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "github.com/jinzhu/copier"
  15. "github.com/tidwall/gjson"
  16. "time"
  17. )
  18. func GetMenu(c *gin.Context) {
  19. sysCfgDb := db.SysCfgDb{}
  20. sysCfgDb.Set()
  21. one, _ := sysCfgDb.SysCfgGetOne("wechat_menu")
  22. var menuList = make([]md.WechatButton, 0)
  23. if one != nil && one.Val != "" {
  24. json.Unmarshal([]byte(one.Val), &menuList)
  25. } else {
  26. token, err := GetWechatToken()
  27. if err != nil {
  28. e.OutErr(c, 400, err.Error())
  29. return
  30. }
  31. var offcialMenuList = make([]md.OffcialWechatButton, 0)
  32. menu, err := utils.GetWechatSelfMenu(token)
  33. fmt.Println(menu)
  34. menuStr := gjson.Get(menu, "selfmenu_info.button").String()
  35. json.Unmarshal([]byte(menuStr), &offcialMenuList)
  36. for _, v := range offcialMenuList {
  37. var tmp = md.WechatButton{
  38. Name: v.Name,
  39. Type: v.Type,
  40. Url: v.Url,
  41. Pagepath: v.Pagepath,
  42. Appid: v.Appid,
  43. SubButton: []md.WechatSubButton{},
  44. }
  45. for _, v1 := range v.SubButton.List {
  46. var tmpSub = md.WechatSubButton{
  47. Type: v1.Type,
  48. Name: v1.Name,
  49. Url: "",
  50. Pagepath: "",
  51. Appid: "",
  52. }
  53. if v1.Type == "miniprogram" {
  54. tmpSub.Pagepath = v1.Pagepath
  55. tmpSub.Appid = v1.Appid
  56. tmpSub.Url = v1.Url
  57. }
  58. if v1.Type == "view" {
  59. tmpSub.Url = v1.Url
  60. }
  61. tmp.SubButton = append(tmp.SubButton, tmpSub)
  62. }
  63. menuList = append(menuList, tmp)
  64. }
  65. sysCfgDb.SysCfgUpdate("wechat_menu", utils.SerializeStr(menuList))
  66. }
  67. replyContentSet := []map[string]string{
  68. {"msgType": "text", "name": "文本"},
  69. {"msgType": "image", "name": "图片"},
  70. }
  71. res := map[string]interface{}{
  72. "button": menuList,
  73. "replyContentSet": replyContentSet,
  74. }
  75. e.OutSuc(c, res, nil)
  76. }
  77. func SetMenu(c *gin.Context) {
  78. var args md.WechatReq
  79. if err := c.ShouldBindJSON(&args); err != nil {
  80. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  81. return
  82. }
  83. sysCfgDb := db.SysCfgDb{}
  84. sysCfgDb.Set()
  85. sysCfgDb.SysCfgUpdate("wechat_menu", utils.SerializeStr(args.Button))
  86. token, err := GetWechatToken()
  87. if err != nil {
  88. e.OutErr(c, 400, err.Error())
  89. return
  90. }
  91. var param md.WechatParam
  92. copier.Copy(&param, &args)
  93. for k, v := range param.Button {
  94. if v.Type == "click" {
  95. param.Button[k].Key = commSetModule(utils.SerializeStr(args.Button[k].ReplyContent))
  96. }
  97. for k1, v1 := range v.SubButton {
  98. if v1.Type == "click" {
  99. param.Button[k].SubButton[k1].Key = commSetModule(utils.SerializeStr(args.Button[k].SubButton[k1].ReplyContent))
  100. }
  101. }
  102. }
  103. menu, err := utils.SetWechatSelfMenu(token, param)
  104. if err != nil {
  105. e.OutErr(c, 400, err.Error())
  106. return
  107. }
  108. if gjson.Get(menu, "errcode").Int() != 0 {
  109. e.OutErr(c, 400, e.NewErr(400, gjson.Get(menu, "errmsg").String()))
  110. return
  111. }
  112. e.OutSuc(c, "success", nil)
  113. return
  114. }
  115. func commSetModule(modData string) string {
  116. mod := &model.SysModule{
  117. ModName: "official_account_custom_reply",
  118. SkipIdentifier: "pub.flutter.official_account_custom_reply",
  119. Title: "公众号菜单点击事件回复消息",
  120. Data: modData,
  121. CreateAt: time.Now(),
  122. }
  123. db.Db.Insert(mod)
  124. return "official_account_custom_reply" + "_" + utils.IntToStr(mod.ModId)
  125. }
  126. func GetWechatToken() (string, error) {
  127. sysCfgDb := db.SysCfgDb{}
  128. sysCfgDb.Set()
  129. res := sysCfgDb.SysCfgFindWithDb(enum.WxOfficialAccountAppId, enum.WxOfficialAccountAppSecret)
  130. key := "wechat_token"
  131. redisStr, err := cache.GetString(key)
  132. token := redisStr
  133. if redisStr == "" || err != nil {
  134. tokenStr, _ := utils.GetWechatToken(res[enum.WxOfficialAccountAppId], res[enum.WxOfficialAccountAppSecret])
  135. token = gjson.Get(tokenStr, "access_token").String()
  136. if token == "" {
  137. return "", errors.New(gjson.Get(tokenStr, "errmsg").String())
  138. }
  139. expiresIn := gjson.Get(tokenStr, "expires_in").Int()
  140. cache.SetEx(key, token, int(expiresIn))
  141. }
  142. return token, nil
  143. }