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

com.gitlab.summercattle.addons.wechat.officialaccounts.menu.button.MenuButton Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitlab.summercattle.addons.wechat.officialaccounts.menu.button;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.gitlab.summercattle.addons.wechat.officialaccounts.menu.AbstractButton;
import com.gitlab.summercattle.addons.wechat.officialaccounts.menu.ButtonType;
import com.gitlab.summercattle.commons.exception.CommonException;

/**
 * 菜单按钮
 * @author orange
 *
 */
public class MenuButton extends AbstractButton {

	private static final long serialVersionUID = 1L;

	private final int MAX_BUTTONS = 5;

	private AbstractButton[] abstractButtons;

	public MenuButton(String name, AbstractButton[] buttons) {
		super(name);
		this.abstractButtons = buttons;
	}

	@Override
	public ButtonType getType() {
		return ButtonType.Menu;
	}

	@Override
	public void setJson(JSONObject jsonObj) throws CommonException {
		if (null == abstractButtons || abstractButtons.length == 0) {
			throw new CommonException("二级菜单数为空");
		}
		if (abstractButtons.length > MAX_BUTTONS) {
			throw new CommonException("二级菜单数不能超过" + MAX_BUTTONS + "个");
		}
		JSONArray buttonsArray = new JSONArray();
		for (int i = 0; i < abstractButtons.length; i++) {
			buttonsArray.add(abstractButtons[i].toJson());
		}
		jsonObj.put("sub_button", buttonsArray);
	}

	@Override
	public JSONObject toJson() throws CommonException {
		if (StringUtils.isBlank(name)) {
			throw new CommonException("菜单标题为空");
		}
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("name", name);
		setJson(jsonObj);
		return jsonObj;
	}

	public AbstractButton[] getButtons() {
		return abstractButtons;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy