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

com.sinsz.wxpn.open.menu.impl.MenuServiceImpl Maven / Gradle / Ivy

package com.sinsz.wxpn.open.menu.impl;

import com.fcibook.quick.http.QuickHttp;
import com.fcibook.quick.http.ResponseBody;
import com.sinsz.wxpn.exception.WxpnException;
import com.sinsz.wxpn.open.BaseWxpn;
import com.sinsz.wxpn.open.menu.MenuService;
import com.sinsz.wxpn.open.menu.support.MenuInfo;
import com.sinsz.wxpn.open.support.FormaterUtils;
import com.sinsz.wxpn.properties.WxpnProperties;
import com.sinsz.wxpn.support.Constant;
import org.apache.commons.lang3.StringUtils;
import org.nutz.http.Header;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义菜单实现
 * @author chenjianbo
 * @date 2018-11-15
 */
public class MenuServiceImpl extends BaseWxpn implements MenuService {

    public MenuServiceImpl(WxpnProperties prop) {
        super(prop);
    }

    @Override
    public String createCustomMenu(String token, List menus) {
        if (StringUtils.isEmpty(token)) {
            throw new WxpnException("用户授权Token不能为空");
        }
        if (menus == null || menus.isEmpty() || menus.size() > 3) {
            throw new WxpnException("创建菜单参数不能为空或一级菜单超过最多限制(限制3个)");
        }
        Map map = new HashMap<>(0);
        map.put("button", menus);
        try {
            Response response = Http.post3(Constant.CREATE_MENU + token, Json.toJson(map, JsonFormat.tidy()), Header.create().set("Content-Type", "application/json"), Constant.TIME_OUT, Constant.TIME_OUT);
            if (response.isOK()) {
                return FormaterUtils.defaultFormat(response.getContent("UTF-8"));
            } else {
                throw new WxpnException(Json.toJson(response, JsonFormat.tidy()));
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
            throw new WxpnException("接口请求异常.");
        }
    }

    @Override
    public String fetchMenuInfo(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new WxpnException("用户授权Token不能为空");
        }
        Map map = new HashMap<>(0);
        map.put("access_token", token);
        try {
            ResponseBody response = new QuickHttp().addParames(map).url(Constant.GET_MENUS).get().setConnectionTimeout(Constant.TIME_OUT).body();
            return FormaterUtils.defaultFormat(response.text());
        } catch (Exception e) {
            e.printStackTrace(System.out);
            throw new WxpnException("接口请求异常.");
        }
    }

    @Override
    public String delMenuInfo(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new WxpnException("用户授权Token不能为空");
        }
        Map map = new HashMap<>(0);
        map.put("access_token", token);
        try {
            ResponseBody response = new QuickHttp().addParames(map).url(Constant.DEL_MENUS).get().setConnectionTimeout(Constant.TIME_OUT).body();
            return FormaterUtils.defaultFormat(response.text());
        } catch (Exception e) {
            e.printStackTrace(System.out);
            throw new WxpnException("接口请求异常.");
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy