美文网首页
java链接易联云打印机

java链接易联云打印机

作者: 小佳佳K3 | 来源:发表于2020-07-24 17:35 被阅读0次

所需jar文件

网盘链接:链接:https://pan.baidu.com/s/1bcPlA5hiimLW74fOssRdjA
提取码:d82y

api相关配置

/**
 * Api相关配置
 */

public class ApiConst {

    /**
     * 主站域名 1
     */
    public static final String MAIN_HOST_DN_ONE = "open-api.10ss.net";

    /**
     * 主站url
     */
    public static final String MAIN_HOST_URL = "https://" + MAIN_HOST_DN_ONE;

    /**
     * 获取token  and  refresh Token
     */
    public static final String GET_TOKEN = "/oauth/oauth";

    /**
     * 急速授权
     */
    public static final String SPEED_AUTHORIZE = "/oauth/scancodemodel";


    /**
     * api 打印
     */
    public static final String API_PRINT = "/print/index";

    /**
     * api 添加终端授权
     */
    public static final String API_ADD_PRINTER = "/printer/addprinter";

    /**
     * api 删除终端授权
     */
    public static final String API_DELET_PRINTER = "/printer/deleteprinter";

    /**
     * api 添加应用菜单
     */
    public static final String API_ADD_PRINT_MENU = "/printmenu/addprintmenu";

    /**
     * api 关机重启接口
     */
    public static final String API_SHUTDOWN_RESTART = "/printer/shutdownrestart";

    /**
     * api 声音调节接口
     */
    public static final String API_SET_SOUND = "/printer/setsound";

    /**
     * api 获取机型打印宽度接口
     */
    public static final String API_PRINT_INFO = "/printer/printinfo";

    /**
     * api 获取机型软硬件版本接口
     */
    public static final String API_GET_VIERSION = "/printer/getversion";

    /**
     * api 取消所有未打印订单
     */
    public static final String API_CANCEL_ALL = "/printer/cancelall";

    /**
     * api 取消单条未打印订单
     */
    public static final String API_CANCEL_ONE = "/printer/cancelone";

    /**
     * api 设置logo接口
     */
    public static final String API_SET_ICON = "/printer/seticon";

    /**
     * api 取消logo接口
     */
    public static final String API_DELET_ICON = "/printer/deleteicon";

    /**
     * api 接单拒单设置接口
     */
    public static final String API_GET_ORDER = "/printer/getorder";

    /**
     * api 打印方式接口
     */
    public static final String API_BTN_PRINT = "/printer/btnprint";


}

调用api接口

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 调用,ssm可用
 * @author admin
 *
 */
public class HttpUtil {
    static String proxyHost = "127.0.0.1";
    static int proxyPort = 8087;

    /**
     * 编码
     *
     * @param source
     * @return
     */
    public static String urlEncode(String source, String encode) {
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source, encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "0";
        }
        return result;
    }

    public static String urlEncodeGBK(String source) {
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "0";
        }
        return result;
    }

    /**
     * 发起http请求获取返回结果
     *
     * @param req_url 请求地址
     * @return
     */
    public static String httpRequest(String req_url) {
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(req_url);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();

            httpUrlConn.setDoOutput(false);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);

            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();

            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
        return buffer.toString();
    }

    /**
     * 发送http请求取得返回的输入流
     *
     * @param requestUrl 请求地址
     * @return InputStream
     */
    public static InputStream httpRequestIO(String requestUrl) {
        InputStream inputStream = null;
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();
            // 获得返回的输入流
            inputStream = httpUrlConn.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }


    /**
     * 向指定URL发送GET方法的请求
     *
     * @param url   发送请求的URL
     * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url     发送请求的 URL
     * @param map     请求参数,请求参数应该是 map 的形式。
     * @param isproxy 是否使用代理模式
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, Map<String, String> map, boolean isproxy) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null;

            // 打开和URL之间的连接
            if (isproxy) {//是否使用代理模式
                @SuppressWarnings("static-access")
                Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                conn = (HttpURLConnection) realUrl.openConnection(proxy);
            } else {
                conn = (HttpURLConnection) realUrl.openConnection();
            }

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");    // POST方法
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            conn.connect();
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            // 发送请求参数
            out.write(getUrlParamsFromMap(map));
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * description:将map转换成url参数格式: name1=value1&name2=value2
     *
     * @param map
     * @return
     */
    public static String getUrlParamsFromMap(Map<String, String> map) {
        try {
            if (null != map) {
                StringBuilder stringBuilder = new StringBuilder();
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    stringBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
                            .append("=")
                            .append(URLEncoder.encode(entry.getValue(), "UTF-8"))
                            .append("&");
                }
                String content = stringBuilder.toString();
                if (content.endsWith("&")) {
                    content = content.substring(0, content.length() - 1);
                }
                return content;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("map数据异常!" + e);
        }
        return "";
    }
}

所有接口的调用

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.UUID;
/**
 * 所有接口的调用
 */
public class LAVApi {

    /**
     * 获取token 开放应用服务模式所需参数
     *
     * @param client_id  易联云颁发给开发者的应用ID 非空值
     * @param grant_type 授与方式(固定为 “authorization_code”)
     * @param sign       签名 详见API文档列表-接口签名
     * @param code       详见商户授权-获取code
     * @param scope      授权权限,传all
     * @param timestamp  当前服务器时间戳(10位)
     * @param id         UUID4 详见API文档列表-uuid4
     * @return
     */
    public static String getToken(String client_id, String grant_type, String sign, String code, String scope, String timestamp, String id) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("grant_type", grant_type);
        hashMap.put("sign", sign);
        hashMap.put("code", code);
        hashMap.put("scope", scope);
        hashMap.put("timestamp", timestamp);
        hashMap.put("id", id);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.GET_TOKEN, hashMap, false);
    }

    /**
     * 获取token  自有应用服务模式所需参数
     *
     * @param client_id  平台id 非空值
     * @param grant_type 授与方式(固定为’client_credentials’)
     * @param sign       签名 详见API文档列表-接口签名
     * @param scope      授权权限,传all
     * @param timestamp  当前服务器时间戳(10位)
     * @param id         UUID4 详见API文档列表-uuid4
     * @return
     */
    public static String getToken(String client_id, String grant_type, String sign, String scope, String timestamp, String id) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("grant_type", grant_type);
        hashMap.put("sign", sign);
        hashMap.put("scope", scope);
        hashMap.put("timestamp", timestamp);
        hashMap.put("id", id);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.GET_TOKEN, hashMap, false);
    }

    /**
     * 刷新access_token
     *
     * @param client_id     易联云颁发给开发者的应用ID 非空值
     * @param grant_type    授与方式(固定为 “refresh_token”)
     * @param scope         授权权限,传all
     * @param sign          签名 详见API文档列表-接口签名
     * @param refresh_token 更新access_token所需
     * @param id            UUID4 详见API文档列表-uuid4
     * @param timestamp     当前服务器时间戳(10位)
     * @return
     */
    public static String refreshToken(String client_id, String grant_type, String scope, String sign, String refresh_token, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("grant_type", grant_type);
        hashMap.put("scope", scope);
        hashMap.put("sign", sign);
        hashMap.put("refresh_token", refresh_token);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.GET_TOKEN, hashMap, false);
    }

    /**
     * 极速授权
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param machine_code 易联云打印机终端号
     * @param qr_key       特殊密钥(有效期为300秒)
     * @param scope        授权权限,传all
     * @param sign         签名 详见API文档列表
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String speedAu(String client_id, String machine_code, String qr_key, String scope, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("machine_code", machine_code);
        hashMap.put("qr_key", qr_key);
        hashMap.put("scope", scope);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.SPEED_AUTHORIZE, hashMap, false);
    }

    /**
     * 打印
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param content      打印内容(需要urlencode)
     * @param origin_id    商户系统内部订单号,要求32个字符内,只能是数字、大小写字母 ,且在同一个client_id下唯一。详见商户订单号
     * @param sign         签名 详见API文档列表
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String print(String client_id, String access_token, String machine_code, String content, String origin_id, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("content", content);
        hashMap.put("origin_id", origin_id);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_PRINT, hashMap, false);
    }

    /**
     * 添加终端授权 开放应用服务模式不需要此接口 ,自有应用服务模式所需参数
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param machine_code 易联云打印机终端号
     * @param msign        易联云终端密钥(如何快速获取终端号和终端秘钥)
     * @param access_token 授权的token 必要参数
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String addPrinter(String client_id, String machine_code, String msign, String access_token, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("machine_code", machine_code);
        hashMap.put("msign", msign);
        hashMap.put("access_token", access_token);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_ADD_PRINTER, hashMap, false);
    }

    /**
     * 删除终端授权 开放应用服务模式、自有应用服务模式所需参数
     * ps 一旦删除,意味着开发者将失去此台打印机的接口权限,请谨慎操作
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String deletePrinter(String client_id, String access_token, String machine_code, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_DELET_PRINTER, hashMap, false);
    }

    /**
     * 添加应用菜单
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param content      json格式的应用菜单(其中url和菜单名称需要urlencode)
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String addPrintMenu(String client_id, String access_token, String machine_code, String content, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("content", content);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_ADD_PRINT_MENU, hashMap, false);
    }

    /**
     * 关机重启接口
     *
     * @param client_id     易联云颁发给开发者的应用ID 非空值
     * @param access_token  授权的token 必要参数
     * @param machine_code  易联云打印机终端号
     * @param response_type 重启:restart,关闭:shutdown
     * @param sign          签名 详见API文档列表-接口签名
     * @param id            UUID4 详见API文档列表-uuid4
     * @param timestamp     当前服务器时间戳(10位)
     * @return
     */
    public static String shutDownRestart(String client_id, String access_token, String machine_code, String response_type, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("response_type", response_type);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_SHUTDOWN_RESTART, hashMap, false);
    }

    /**
     * 声音调节接口
     *
     * @param client_id     易联云颁发给开发者的应用ID 非空值
     * @param access_token  授权的token 必要参数
     * @param machine_code  易联云打印机终端号
     * @param response_type 蜂鸣器:buzzer,喇叭:horn
     * @param voice         [1,2,3] 3种音量设置
     * @param sign          签名 详见API文档列表-接口签名
     * @param id            UUID4 详见API文档列表-uuid4
     * @param timestamp     当前服务器时间戳(10位)
     * @return
     */
    public static String setSound(String client_id, String access_token, String machine_code, String response_type, String voice, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("response_type", response_type);
        hashMap.put("voice", voice);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_SET_SOUND, hashMap, false);
    }

    /**
     * 获取机型打印宽度接口
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String getPrintInfo(String client_id, String access_token, String machine_code, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_PRINT_INFO, hashMap, false);
    }

    /**
     * 获取机型软硬件版本接口
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String getVersion(String client_id, String access_token, String machine_code, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_GET_VIERSION, hashMap, false);
    }

    /**
     * 取消所有未打印订单
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String cancelAll(String client_id, String access_token, String machine_code, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_CANCEL_ALL, hashMap, false);
    }

    /**
     * 取消单条未打印订单
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param order_id     通过打印接口返回的订单号 详见API文档列表-打印接口
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String cancelOne(String client_id, String access_token, String machine_code, String order_id, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("order_id", order_id);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_CANCEL_ONE, hashMap, false);
    }

    /**
     * 设置logo接口
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param img_url      图片地址,图片宽度最大为350px,文件大小不能超过40Kb
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String setIcon(String client_id, String access_token, String machine_code, String img_url, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("img_url", img_url);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_SET_ICON, hashMap, false);
    }

    /**
     * 取消logo接口
     *
     * @param client_id    易联云颁发给开发者的应用ID 非空值
     * @param access_token 授权的token 必要参数
     * @param machine_code 易联云打印机终端号
     * @param sign         签名 详见API文档列表-接口签名
     * @param id           UUID4 详见API文档列表-uuid4
     * @param timestamp    当前服务器时间戳(10位)
     * @return
     */
    public static String deleteIcon(String client_id, String access_token, String machine_code, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_DELET_ICON, hashMap, false);
    }

    /**
     * 接单拒单设置接口
     *
     * @param client_id     易联云颁发给开发者的应用ID 非空值
     * @param access_token  授权的token 必要参数
     * @param machine_code  易联云打印机终端号
     * @param response_type 开启:open,关闭:close
     * @param sign          签名 详见API文档列表-接口签名
     * @param id            UUID4 详见API文档列表-uuid4
     * @param timestamp     当前服务器时间戳(10位)
     * @return
     */
    public static String getOrder(String client_id, String access_token, String machine_code, String response_type, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("response_type", response_type);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_GET_ORDER, hashMap, false);
    }

    /**
     * 打印方式接口
     *
     * @param client_id     易联云颁发给开发者的应用ID 非空值
     * @param access_token  授权的token 必要参数
     * @param machine_code  易联云打印机终端号
     * @param response_type 开启:btnopen,关闭:btnclose; 按键打印
     * @param sign          签名 详见API文档列表-接口签名
     * @param id            UUID4 详见API文档列表-uuid4
     * @param timestamp     当前服务器时间戳(10位)
     * @return
     */
    public static String btnPrint(String client_id, String access_token, String machine_code, String response_type, String sign, String id, String timestamp) {
        HashMap hashMap = new HashMap();
        hashMap.put("client_id", client_id);
        hashMap.put("access_token", access_token);
        hashMap.put("machine_code", machine_code);
        hashMap.put("response_type", response_type);
        hashMap.put("sign", sign);
        hashMap.put("id", id);
        hashMap.put("timestamp", timestamp);
        return HttpUtil.sendPost(ApiConst.MAIN_HOST_URL + ApiConst.API_BTN_PRINT, hashMap, false);
    }

    public static String getSin(String timestamp) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(Methods.CLIENT_ID);
            stringBuilder.append(timestamp);
            stringBuilder.append(Methods.CLIENT_SECRET);
            return getMd5(stringBuilder.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getuuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * @param str
     * @return
     * @Description: 32位小写MD5
     */
    public static String getMd5(String str) {
        String reStr = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(str.getBytes());
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bytes) {
                int bt = b & 0xff;
                if (bt < 16) {
                    stringBuffer.append(0);
                }
                stringBuffer.append(Integer.toHexString(bt));
            }
            reStr = stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return reStr;
    }
}

易联云接口工具类

import org.json.JSONException;
import org.json.JSONObject;
/**
 * 易联云接口工具类
 */
public class Methods {
    /**
     * 易联云颁发给开发者的应用ID 非空值
     */
    public static String CLIENT_ID;

    /**
     * 易联云颁发给开发者的应用secret 非空值
     */
    public static String CLIENT_SECRET;

    /**
     * token
     */
    public static String token;

    /**
     * 刷新token需要的 refreshtoken
     */
    public static String refresh_token;

    /**
     * code
     */
    public static String CODE;

    private Methods() {
    }

    private static class SingleMethods {
        private static final Methods COCOS_MANGER = new Methods();
    }

    public static final Methods getInstance() {
        return SingleMethods.COCOS_MANGER;
    }

    /**
     * 开放式初始化
     *
     * @param client_id
     * @param client_secret
     * @param code
     */
    public void init(String client_id, String client_secret, String code) {
        CLIENT_ID = client_id;
        CLIENT_SECRET = client_secret;
        CODE = code;
    }

    /**
     * 自有初始化
     *
     * @param client_id
     * @param client_secret
     */
    public void init(String client_id, String client_secret) {
        CLIENT_ID = client_id;
        CLIENT_SECRET = client_secret;
    }

    /**
     * 开放应用
     */
    public String getToken() {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String result = LAVApi.getToken(CLIENT_ID,
                "authorization_code",
                LAVApi.getSin(timestamp),
                CODE,
                "all",
                timestamp,
                LAVApi.getuuid());
        try {
            JSONObject json = new JSONObject(result);
            JSONObject body = json.getJSONObject("body");
            token = body.getString("access_token");
            refresh_token = body.getString("refresh_token");
        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("getToken出现Json异常!" + e);
        }
        return result;
    }

    /**
     * 自有应用服务
     */
    public String getFreedomToken() {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String result = LAVApi.getToken(CLIENT_ID,
                "client_credentials",
                LAVApi.getSin(timestamp),
                "all",
                timestamp,
                LAVApi.getuuid());
        try {
            JSONObject json = new JSONObject(result);
            JSONObject body = json.getJSONObject("body");
            token = body.getString("access_token");
            refresh_token = body.getString("refresh_token");
        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("getFreedomToken出现Json异常!" + e);
        }
        return result;
    }

    /**
     * 刷新token
     */
    public String refreshToken() {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String result = LAVApi.refreshToken(CLIENT_ID,
                "refresh_token",
                "all",
                LAVApi.getSin(timestamp),
                refresh_token,
                LAVApi.getuuid(),
                timestamp);
        try {
            JSONObject json = new JSONObject(result);
            JSONObject body = json.getJSONObject("body");
            token = body.getString("access_token");
            refresh_token = body.getString("refresh_token");
        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("refreshToken出现Json异常!" + e);
        }
        return result;
    }

    /**
     * 添加终端授权 开放应用服务模式不需要此接口 ,自有应用服务模式所需参数
     */
    public String addPrinter(String machine_code, String msign) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.addPrinter(CLIENT_ID,
                machine_code,
                msign,
                token,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 极速授权
     */
    public String speedAu(String machine_code, String qr_key) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.speedAu(CLIENT_ID,
                machine_code,
                qr_key,
                "all",
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 打印
     */
    public String print(String machine_code, String content, String origin_id) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.print(CLIENT_ID,
                token,
                machine_code,
                content,
                origin_id,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 删除终端授权
     */
    public String deletePrinter(String machine_code) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.deletePrinter(CLIENT_ID,
                token,
                machine_code,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 添加应用菜单
     */
    public String addPrintMenu(String machine_code, String content) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.addPrintMenu(CLIENT_ID,
                token,
                machine_code,
                content,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 关机重启接口
     */
    public String shutDownRestart(String machine_code, String response_type) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.shutDownRestart(CLIENT_ID,
                token,
                machine_code,
                response_type,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 声音调节
     */
    public String setSound(String machine_code, String response_type, String voice) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.setSound(CLIENT_ID,
                token,
                machine_code,
                response_type,
                voice,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 获取机型打印宽度接口
     */
    public String getPrintInfo(String machine_code) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.getPrintInfo(CLIENT_ID,
                token,
                machine_code,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 获取机型软硬件版本接口
     */
    public String getVersion(String machine_code) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.getVersion(CLIENT_ID,
                token,
                machine_code,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 取消所有未打印订单
     */
    public String cancelAll(String machine_code) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.cancelAll(CLIENT_ID,
                token,
                machine_code,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 取消单条未打印订单
     */
    public String cancelOne(String machine_code, String order_id) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.cancelOne(CLIENT_ID,
                token,
                machine_code,
                order_id,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 设置logo
     */
    public String setIcon(String machine_code, String img_url) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.setIcon(CLIENT_ID,
                token,
                machine_code,
                img_url,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 删除logo
     */
    public String deleteIcon(String machine_code) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.deleteIcon(CLIENT_ID,
                token,
                machine_code,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 打印方式
     */
    public String btnPrint(String machine_code, String response_type) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.btnPrint(CLIENT_ID,
                token,
                machine_code,
                response_type,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

    /**
     * 接单拒单设置接口
     */
    public String getOrder(String machine_code, String response_type) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return LAVApi.getOrder(CLIENT_ID,
                token,
                machine_code,
                response_type,
                LAVApi.getSin(timestamp),
                LAVApi.getuuid(),
                timestamp);
    }

}

自定义商品对象,可根据自己的需求进行设计

/**
 * 菜品对象
 * @author admin
 * 
 */
public class Test {
    // 菜品名称
    private String name;
    // 价格
    private double money;
    // 数量
    private Integer num;
    //菜品分类
    private String fenlei;
    public Test() {
        super();
    }
    public Test(String name, double money, Integer num,String fenlei) {
        super();
        this.name = name;
        this.money = money;
        this.num = num;
        this.fenlei=fenlei;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    public Integer getNum() {
        return num;
    }
    public void setNum(Integer num) {
        this.num = num;
    }
    public String getFenlei() {
        return fenlei;
    }
    public void setFenlei(String fenlei) {
        this.fenlei = fenlei;
    }
}

打印模板,这里设计一个小票模板

import java.util.ArrayList;
import java.util.List;
/**
 * 小票模板
 * 
 * @author admin
 * 
 */
public class Prient{
    // 菜品集合--传入一个商品集合
    public static List<Test> testList = new ArrayList<Test>();
    public static double ZMoney=0;
    public static double YMoney=20;
    public static double SMoney=500;
    // 设置小票打印
    public static String print(){
        //字符串拼接
        StringBuilder sb=new StringBuilder();
        sb.append("<center>点菜清单\r\n</center>");
        sb.append("------------------------------------\r\n");
        sb.append("点餐员:测试打印\r\n");
        sb.append("电话:13408086368\r\n");
        sb.append("用餐时间:2015-04-09 13:01-13:30\r\n");
        sb.append("用餐地址:打印测试\r\n");
        sb.append("------------------------------------\r\n");
        sb.append("<table>");
            sb.append("<tr>");
                sb.append("<td>");
                sb.append("菜品");
                sb.append("</td>");
                sb.append("<td>");
                sb.append("单价");
                sb.append("</td>");
                sb.append("<td>");
                sb.append("小计");
                sb.append("</td>");
            sb.append("</tr>");
            for (Test test : testList) {
                ZMoney=ZMoney+(test.getMoney()*test.getNum());
            sb.append("<tr>");
                sb.append("<td>"+test.getName()+"</td>");
                sb.append("<td>"+test.getMoney()+"</td>");
                sb.append("<td>"+test.getMoney()*test.getNum()+"</td>");
            sb.append("</tr>");
            }
        sb.append("</table>");
        sb.append("------------------------------------\r\n");
        sb.append("合计:¥"+ZMoney+"\r\n");
        sb.append("优惠金额:¥"+YMoney+"\r\n");
        sb.append("应收:¥"+(ZMoney-YMoney)+"\r\n");
        sb.append("实收:¥"+SMoney+"\r\n");
        sb.append("找零:¥"+(SMoney-YMoney)+"\r\n");
        sb.append("收银员:打印测试\r\n");
        sb.append("<center>谢谢惠顾,欢迎下次光临!</center>");
        return sb.toString();
    }
    public static List<Test> getTestList() {
        return testList;
    }
    public static void setTestList(List<Test> testList) {
        Prient.testList = testList;
    }
}

控制器,这里可以自己定义,用于测试

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import test.Methods;
/**
 * 控制器
 * @author admin
 *
 */
public class TestSrvlet extends HttpServlet {
    /**
     * Constructor of the object.
     */
    public TestSrvlet() {
        super();
    }
    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //测试数据
        List<Test> testList = new ArrayList<Test>();
        Test t1 = new Test("麻辣牛肉", 23.00, 1,"1");
        Test t2 = new Test("麻辣牛肉", 23.00, 2,"2");
        Test t3 = new Test("精品千层肚", 24.00, 3,"3");
        Test t4 = new Test("麻辣牛肉", 23.00, 2,"1");
        Test t5 = new Test("极品鲜毛肚", 26.00, 2,"1");
        Test t6 = new Test("极品鲜毛肚", 26.00, 1,"2");
        Test t7 = new Test("极品鲜毛肚", 26.00, 3,"2");
        Test t8 = new Test("极品鲜毛肚", 26.00, 1,"1");
        Test t9 = new Test("极品鲜毛肚", 26.00, 2,"3");
        testList.add(t1);
        testList.add(t2);
        testList.add(t3);
        testList.add(t4);
        testList.add(t5);
        testList.add(t6);
        testList.add(t7);
        testList.add(t8);
        testList.add(t9);
        Prient.setTestList(testList);
        
        //关键代码,自己的程序发送请求
        //初始化控制器类
        Methods m=Methods.getInstance();
        //初始化终端信息
        m.init("1038835098", "1595cb28ea30e98908e6334e735f4b8a");
        //获取token
        m.getFreedomToken();
        //刷新token
        m.refreshToken();
        //添加授权
        m.addPrinter("4004557406", "zqvfw2v5p3fn");
        //打印
        //终端编号     打印内容    订单号
        //生成6位随机数
        Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
        String url=m.print("4004557406", Prient.print(), "Z"+System.currentTimeMillis()+random6.toString());
        response.sendRedirect(url);
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }
}

相关文章

网友评论

      本文标题:java链接易联云打印机

      本文链接:https://www.haomeiwen.com/subject/yrlslktx.html