|
|
@@ -0,0 +1,110 @@ |
|
|
|
package svc |
|
|
|
|
|
|
|
import ( |
|
|
|
"applet/app/db" |
|
|
|
"applet/app/e" |
|
|
|
"applet/app/enum" |
|
|
|
"applet/app/md" |
|
|
|
"applet/app/utils" |
|
|
|
"applet/app/utils/cache" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/tidwall/gjson" |
|
|
|
) |
|
|
|
|
|
|
|
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) |
|
|
|
menuStr := gjson.Get(menu, "selfmenu_info.button").String() |
|
|
|
json.Unmarshal([]byte(menuStr), &offcialMenuList) |
|
|
|
for _, v := range offcialMenuList { |
|
|
|
var tmp = md.WechatButton{ |
|
|
|
Name: v.Name, |
|
|
|
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)) |
|
|
|
} |
|
|
|
res := map[string]interface{}{ |
|
|
|
"button": menuList, |
|
|
|
} |
|
|
|
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 |
|
|
|
} |
|
|
|
menu, err := utils.SetWechatSelfMenu(token, args) |
|
|
|
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 GetWechatToken() (string, error) { |
|
|
|
sysCfgDb := db.SysCfgDb{} |
|
|
|
sysCfgDb.Set() |
|
|
|
res := sysCfgDb.SysCfgFindWithDb(enum.WxOfficialAccountAppId, enum.WxOfficialAccountAppSecret) |
|
|
|
key := "wechat_token" |
|
|
|
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 |
|
|
|
|
|
|
|
} |