一物一码
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.
 
 
 
 
 
 

111 lines
2.8 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/enum"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "encoding/json"
  10. "errors"
  11. "github.com/gin-gonic/gin"
  12. "github.com/tidwall/gjson"
  13. )
  14. func GetMenu(c *gin.Context) {
  15. sysCfgDb := db.SysCfgDb{}
  16. sysCfgDb.Set()
  17. one, _ := sysCfgDb.SysCfgGetOne("wechat_menu")
  18. var menuList = make([]md.WechatButton, 0)
  19. if one != nil && one.Val != "" {
  20. json.Unmarshal([]byte(one.Val), &menuList)
  21. } else {
  22. token, err := GetWechatToken()
  23. if err != nil {
  24. e.OutErr(c, 400, err.Error())
  25. return
  26. }
  27. var offcialMenuList = make([]md.OffcialWechatButton, 0)
  28. menu, err := utils.GetWechatSelfMenu(token)
  29. menuStr := gjson.Get(menu, "selfmenu_info.button").String()
  30. json.Unmarshal([]byte(menuStr), &offcialMenuList)
  31. for _, v := range offcialMenuList {
  32. var tmp = md.WechatButton{
  33. Name: v.Name,
  34. SubButton: []md.WechatSubButton{},
  35. }
  36. for _, v1 := range v.SubButton.List {
  37. var tmpSub = md.WechatSubButton{
  38. Type: v1.Type,
  39. Name: v1.Name,
  40. Url: "",
  41. Pagepath: "",
  42. Appid: "",
  43. }
  44. if v1.Type == "miniprogram" {
  45. tmpSub.Pagepath = v1.Pagepath
  46. tmpSub.Appid = v1.Appid
  47. tmpSub.Url = v1.Url
  48. }
  49. if v1.Type == "view" {
  50. tmpSub.Url = v1.Url
  51. }
  52. tmp.SubButton = append(tmp.SubButton, tmpSub)
  53. }
  54. menuList = append(menuList, tmp)
  55. }
  56. sysCfgDb.SysCfgUpdate("wechat_menu", utils.SerializeStr(menuList))
  57. }
  58. res := map[string]interface{}{
  59. "button": menuList,
  60. }
  61. e.OutSuc(c, res, nil)
  62. }
  63. func SetMenu(c *gin.Context) {
  64. var args md.WechatReq
  65. if err := c.ShouldBindJSON(&args); err != nil {
  66. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  67. return
  68. }
  69. sysCfgDb := db.SysCfgDb{}
  70. sysCfgDb.Set()
  71. sysCfgDb.SysCfgUpdate("wechat_menu", utils.SerializeStr(args.Button))
  72. token, err := GetWechatToken()
  73. if err != nil {
  74. e.OutErr(c, 400, err.Error())
  75. return
  76. }
  77. menu, err := utils.SetWechatSelfMenu(token, args)
  78. if err != nil {
  79. e.OutErr(c, 400, err.Error())
  80. return
  81. }
  82. if gjson.Get(menu, "errcode").Int() != 0 {
  83. e.OutErr(c, 400, e.NewErr(400, gjson.Get(menu, "errmsg").String()))
  84. return
  85. }
  86. e.OutSuc(c, "success", nil)
  87. return
  88. }
  89. func GetWechatToken() (string, error) {
  90. sysCfgDb := db.SysCfgDb{}
  91. sysCfgDb.Set()
  92. res := sysCfgDb.SysCfgFindWithDb(enum.WxOfficialAccountAppId, enum.WxOfficialAccountAppSecret)
  93. key := "wechat_token"
  94. redisStr, err := cache.GetString(key)
  95. token := redisStr
  96. if redisStr == "" || err != nil {
  97. tokenStr, _ := utils.GetWechatToken(res[enum.WxOfficialAccountAppId], res[enum.WxOfficialAccountAppSecret])
  98. token = gjson.Get(tokenStr, "access_token").String()
  99. if token == "" {
  100. return "", errors.New(gjson.Get(tokenStr, "errmsg").String())
  101. }
  102. expiresIn := gjson.Get(tokenStr, "expires_in").Int()
  103. cache.SetEx(key, token, int(expiresIn))
  104. }
  105. return token, nil
  106. }