|
@@ -0,0 +1,159 @@
|
|
|
|
+package main
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "bytes"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "errors"
|
|
|
|
+ "fmt"
|
|
|
|
+ "io/ioutil"
|
|
|
|
+ "log"
|
|
|
|
+ "net/http"
|
|
|
|
+ "qfw/util"
|
|
|
|
+ "qfw/util/redis"
|
|
|
|
+ "time"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+const (
|
|
|
|
+ RedisCode = "wxtoken"
|
|
|
|
+ MenuButtonTypeUrl = "view"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var post_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s"
|
|
|
|
+var Config *config
|
|
|
|
+
|
|
|
|
+type config struct {
|
|
|
|
+ Appid string
|
|
|
|
+ Webdomain string
|
|
|
|
+ Redis string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Custom Menu
|
|
|
|
+type Menu struct {
|
|
|
|
+ Buttons []MenuButton `json:"button,omitempty"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type MenuButton struct {
|
|
|
|
+ Name string `json:"name"`
|
|
|
|
+ Type string `json:"type,omitempty"`
|
|
|
|
+ Key string `json:"key,omitempty"`
|
|
|
|
+ Url string `json:"url,omitempty"`
|
|
|
|
+ MediaId string `json:"media_id,omitempty"`
|
|
|
|
+ SubButtons []MenuButton `json:"sub_button,omitempty"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type response struct {
|
|
|
|
+ ErrorCode int `json:"errcode,omitempty"`
|
|
|
|
+ ErrorMessage string `json:"errmsg,omitempty"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//微信菜单数据
|
|
|
|
+func WXMenu() Menu {
|
|
|
|
+ urlstr := fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s/wx/oauth/%s&response_type=code&scope=snsapi_base&state=1#wechat_redirect", Config.Appid, Config.Webdomain, "%s")
|
|
|
|
+
|
|
|
|
+ menu := &Menu{make([]MenuButton, 3)}
|
|
|
|
+ menu.Buttons[0].Name = "招标搜索1"
|
|
|
|
+ menu.Buttons[0].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[0].Url = fmt.Sprintf(urlstr, "searchinfo")
|
|
|
|
+
|
|
|
|
+ menu.Buttons[1].Name = "招标订阅2"
|
|
|
|
+ menu.Buttons[1].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[1].Url = fmt.Sprintf(urlstr, "rssset")
|
|
|
|
+
|
|
|
|
+ menu.Buttons[2].Name = "发现"
|
|
|
|
+ menu.Buttons[2].SubButtons = make([]MenuButton, 5)
|
|
|
|
+
|
|
|
|
+ menu.Buttons[2].SubButtons[0].Name = "\U0001F4BC百宝箱"
|
|
|
|
+ menu.Buttons[2].SubButtons[0].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[2].SubButtons[0].Url = fmt.Sprintf(urlstr, "treasurebox")
|
|
|
|
+
|
|
|
|
+ menu.Buttons[2].SubButtons[1].Name = string(0x1F4C1) + "大会员"
|
|
|
|
+ menu.Buttons[2].SubButtons[1].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[2].SubButtons[1].Url = fmt.Sprintf(urlstr, "bigMember")
|
|
|
|
+
|
|
|
|
+ menu.Buttons[2].SubButtons[2].Name = "\U0001F4DD结构化数据"
|
|
|
|
+ menu.Buttons[2].SubButtons[2].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[2].SubButtons[2].Url = fmt.Sprintf(urlstr, "structed")
|
|
|
|
+
|
|
|
|
+ menu.Buttons[2].SubButtons[3].Name = string(0x1F465) + "剑鱼标讯社区"
|
|
|
|
+ menu.Buttons[2].SubButtons[3].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[2].SubButtons[3].Url = fmt.Sprintf(urlstr, "community")
|
|
|
|
+
|
|
|
|
+ menu.Buttons[2].SubButtons[4].Name = "\U0001F464我的"
|
|
|
|
+ menu.Buttons[2].SubButtons[4].Type = MenuButtonTypeUrl
|
|
|
|
+ menu.Buttons[2].SubButtons[4].Url = fmt.Sprintf(urlstr, "mymenu")
|
|
|
|
+ return *menu
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+func init() {
|
|
|
|
+ util.ReadConfig(&Config)
|
|
|
|
+ redis.InitRedis(Config.Redis)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+func main() {
|
|
|
|
+ tokenKey := fmt.Sprintf("WxToken_%s", Config.Appid)
|
|
|
|
+ ret, err := redis.GetNewBytes(RedisCode, tokenKey)
|
|
|
|
+ if err == nil {
|
|
|
|
+ if ret != nil {
|
|
|
|
+ var res map[string]interface{}
|
|
|
|
+ err := json.Unmarshal(*ret, &res)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println(Config.Appid, "从redis加载token出错", err)
|
|
|
|
+ } else if res["access_token"] != nil {
|
|
|
|
+ post_url = fmt.Sprintf(post_url, res["access_token"].(string))
|
|
|
|
+ data, err := marshal(WXMenu())
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("序列化菜单内容异常")
|
|
|
|
+ } else {
|
|
|
|
+ _, err := CreateMenu(post_url, data)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("err:", err)
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("create wx menu success")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("redis 读取信息有误...")
|
|
|
|
+ }
|
|
|
|
+ time.Sleep(time.Second)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//创建菜单
|
|
|
|
+func CreateMenu(post_url string, data []byte) ([]byte, error) {
|
|
|
|
+ // log.Println(string(data))
|
|
|
|
+ r, err := http.Post(post_url, "application/json; charset=utf-8", bytes.NewReader(data))
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ defer r.Body.Close()
|
|
|
|
+ reply, err := ioutil.ReadAll(r.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ var result response
|
|
|
|
+ if err := json.Unmarshal(reply, &result); err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ switch result.ErrorCode {
|
|
|
|
+ case 0:
|
|
|
|
+ return reply, nil
|
|
|
|
+ case 42001, 40001:
|
|
|
|
+ log.Println("reget the tocken by ", result.ErrorCode, result.ErrorMessage)
|
|
|
|
+ default:
|
|
|
|
+ return nil, errors.New(fmt.Sprintf("WeiXin send post request reply[%d]: %s", result.ErrorCode, result.ErrorMessage))
|
|
|
|
+ }
|
|
|
|
+ return nil, errors.New("WeiXin post request error")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func marshal(v interface{}) ([]byte, error) {
|
|
|
|
+ data, err := json.Marshal(v)
|
|
|
|
+ if err == nil {
|
|
|
|
+ data = bytes.Replace(data, []byte("\\u003c"), []byte("<"), -1)
|
|
|
|
+ data = bytes.Replace(data, []byte("\\u003e"), []byte(">"), -1)
|
|
|
|
+ data = bytes.Replace(data, []byte("\\u0026"), []byte("&"), -1)
|
|
|
|
+ }
|
|
|
|
+ return data, err
|
|
|
|
+}
|