package com.tzy.s.wx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tzy.s.wx.dto.MessageTemplate;
import com.tzy.s.wx.dto.Result;
import com.tzy.s.wx.util.HttpClient;
import org.apache.commons.lang.StringUtils;

import java.text.MessageFormat;

public class SendTemplateMessage {

    private static String send_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}";

    public static Result sendMessage(String accesstoken,JSONObject message){
        String url = MessageFormat.format(send_url,accesstoken);
        String param = JSONObject.toJSONString(message);
        String ret = HttpClient.httpsRequest(url, "POST", param);
        if(StringUtils.isEmpty(ret)) return Result.error();
        try{
            JSONObject retJson = JSONObject.parseObject(ret);
            return JSONObject.toJavaObject(retJson, MessageTemplate.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(-1, "请求异常，请联系开发人员");
        }
    }

}
