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

svc_wechat.go 4.2 KiB

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