|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/enum"
- "applet/app/md"
- "applet/app/utils"
- "applet/app/utils/cache"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- "github.com/tidwall/gjson"
- "strings"
- "time"
- )
-
- func GetMenu(c *gin.Context) {
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- one, _ := sysCfgDb.SysCfgGetOne("wechat_menu")
- var menuList = make([]md.WechatButton, 0)
- if one != nil && one.Val != "" {
- json.Unmarshal([]byte(one.Val), &menuList)
- } else {
- token, err := GetWechatToken()
- if err != nil {
- e.OutErr(c, 400, err.Error())
- return
- }
- var offcialMenuList = make([]md.OffcialWechatButton, 0)
- menu, err := utils.GetWechatSelfMenu(token)
- fmt.Println(menu)
- menuStr := gjson.Get(menu, "selfmenu_info.button").String()
- json.Unmarshal([]byte(menuStr), &offcialMenuList)
- for _, v := range offcialMenuList {
- var tmp = md.WechatButton{
- Name: v.Name,
- Type: v.Type,
- Url: v.Url,
- Pagepath: v.Pagepath,
- Appid: v.Appid,
- SubButton: []md.WechatSubButton{},
- }
- for _, v1 := range v.SubButton.List {
- var tmpSub = md.WechatSubButton{
- Type: v1.Type,
- Name: v1.Name,
- Url: "",
- Pagepath: "",
- Appid: "",
- }
- if v1.Type == "miniprogram" {
- tmpSub.Pagepath = v1.Pagepath
- tmpSub.Appid = v1.Appid
- tmpSub.Url = v1.Url
- }
- if v1.Type == "view" {
- tmpSub.Url = v1.Url
- }
- tmp.SubButton = append(tmp.SubButton, tmpSub)
- }
- menuList = append(menuList, tmp)
- }
- sysCfgDb.SysCfgUpdate("wechat_menu", utils.SerializeStr(menuList))
- }
- for k, v := range menuList {
- menuList[k].Url = strings.ReplaceAll(v.Url, "\\u0026", "&")
- for k1, v1 := range v.SubButton {
- menuList[k].SubButton[k1].Url = strings.ReplaceAll(v1.Url, "\\u0026", "&")
- }
- }
- replyContentSet := []map[string]string{
- {"msgType": "text", "name": "文本"},
- {"msgType": "image", "name": "图片"},
- }
- res := map[string]interface{}{
- "button": menuList,
- "replyContentSet": replyContentSet,
- }
- e.OutSuc(c, res, nil)
- }
-
- func SetMenu(c *gin.Context) {
- var args md.WechatReq
- if err := c.ShouldBindJSON(&args); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err)
- return
- }
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- sysCfgDb.SysCfgUpdate("wechat_menu", utils.SerializeStr(args.Button))
- token, err := GetWechatToken()
- if err != nil {
- e.OutErr(c, 400, err.Error())
- return
- }
- var param md.WechatParam
- copier.Copy(¶m, &args)
- for k, v := range param.Button {
- if v.Type == "click" {
- param.Button[k].Key = commSetModule(utils.SerializeStr(args.Button[k].ReplyContent))
- }
- for k1, v1 := range v.SubButton {
- if v1.Type == "click" {
- param.Button[k].SubButton[k1].Key = commSetModule(utils.SerializeStr(args.Button[k].SubButton[k1].ReplyContent))
- }
- }
- }
- fmt.Println(utils.SerializeStr(param))
- menu, err := utils.SetWechatSelfMenu(token, param)
- if err != nil {
- e.OutErr(c, 400, err.Error())
- return
- }
- if gjson.Get(menu, "errcode").Int() != 0 {
- e.OutErr(c, 400, e.NewErr(400, gjson.Get(menu, "errmsg").String()))
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
- func commSetModule(modData string) string {
- mod := &model.SysModule{
- ModName: "official_account_custom_reply",
- SkipIdentifier: "pub.flutter.official_account_custom_reply",
- Title: "公众号菜单点击事件回复消息",
- Data: modData,
- CreateAt: time.Now(),
- }
- insert, err := db.Db.Insert(mod)
- fmt.Println(insert)
- fmt.Println(err)
- return "official_account_custom_reply" + "_" + utils.IntToStr(mod.ModId)
- }
-
- func GetWechatToken() (string, error) {
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- res := sysCfgDb.SysCfgFindWithDb(enum.WxOfficialAccountAppId, enum.WxOfficialAccountAppSecret)
- key := "wechat_token1"
- redisStr, err := cache.GetString(key)
- token := redisStr
- if redisStr == "" || err != nil {
- tokenStr, _ := utils.GetWechatToken(res[enum.WxOfficialAccountAppId], res[enum.WxOfficialAccountAppSecret])
- token = gjson.Get(tokenStr, "access_token").String()
- if token == "" {
- return "", errors.New(gjson.Get(tokenStr, "errmsg").String())
- }
- expiresIn := gjson.Get(tokenStr, "expires_in").Int()
- cache.SetEx(key, token, int(expiresIn))
- }
- return token, nil
-
- }
|