All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lodsve.wechat.api.menu.WeChatMenuService Maven / Gradle / Ivy

There is a newer version: 2.7.5-RELEASE
Show newest version
/*
 * Copyright (C) 2018  Sun.Hao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package lodsve.wechat.api.menu;

import com.fasterxml.jackson.core.type.TypeReference;
import lodsve.wechat.beans.Menu;
import lodsve.wechat.core.WeChat;
import lodsve.wechat.core.WeChatRequest;
import lodsve.wechat.core.WeChatUrl;
import lodsve.wechat.exception.WeChatException;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 菜单操作.
 *
 * @author sunhao([email protected])
 * @version V1.0, 16/2/23 下午4:17
 */
@Component
public class WeChatMenuService {
    public static final int MENU_LENGTH = 3;

    /**
     * 创建菜单(会删除原来的菜单)
     *
     * @param menus 需要创建的菜单
     */
    public void create(Menu... menus) {
        if (ArrayUtils.isEmpty(menus)) {
            return;
        }

        if (menus.length > MENU_LENGTH) {
            throw new WeChatException(107001, "微信按钮个数限制小于等于3个");
        }
        for (Menu m : menus) {
            List subButtons = m.getSubButtons();
            if (subButtons != null && subButtons.size() > 5) {
                throw new WeChatException(107002, "微信子按钮个数限制小于等于5个");
            }
        }

        WeChatRequest.post(String.format(WeChatUrl.CREATE_MENUS, WeChat.accessToken()), Collections.singletonMap("button", Arrays.asList(menus)), new TypeReference() {
        });
    }

    /**
     * 获取全部菜单
     *
     * @return 全部菜单
     */
    public List getMenus() {
        try {
            Map>>> result = WeChatRequest.get(String.format(WeChatUrl.GET_MENUS, WeChat.accessToken()),
                    new TypeReference>>>>() {
                    });
            List> temp = result.get("menu").get("button");

            List menus = new ArrayList<>(temp.size());
            for (Map t : temp) {
                Menu m = WeChatRequest.evalMap(t, new TypeReference() {
                });
                menus.add(m);
            }

            return menus;
        } catch (Exception e) {
            return Collections.emptyList();
        }
    }

    /**
     * 删除全部菜单
     */
    public void delete() {
        WeChatRequest.get(String.format(WeChatUrl.DELETE_MENUS, WeChat.accessToken()), new TypeReference() {
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy