package com.tofly.feesapi.common.wechat;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

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

public class Wxapi {

    /// <summary>
    /// 获取微信access_token
    /// </summary>
    /// <returns></returns>
    public static Map<String, String> GetAccessToken() {
        String errMsg = "";
        String accessToken = "";
        Map<String, String> mapQuery = new HashMap<String, String>();
        try {
            Map<String, Object> packageParams = new HashMap<>(3);
            packageParams.put("grant_type", "client_credential");
            packageParams.put("appid", new WxPayConfig().getAPPID());
            packageParams.put("secret", new WxPayConfig().getAPPSECRET());
            String url = "https://api.weixin.qq.com/cgi-bin/token";

            String s = HttpUtil.get(url, packageParams);
            JSONObject jsonObject = JSONObject.parseObject(s);

            if (jsonObject.containsKey("access_token")) {
                accessToken = jsonObject.getString("access_token");
            } else if (jsonObject.containsKey("errmsg")) {
                errMsg = jsonObject.getString("errmsg");
            }
        } catch (Exception e) {
            errMsg = e.getMessage();
        }
        mapQuery.put("accessToken", accessToken);
        mapQuery.put("errMsg", errMsg);
        return mapQuery;
    }


    //发送短信
    //调用接口
    public static boolean sendWxMsg(Map<String, Object> message, String accessToken) {
        boolean flag = false;
        //方法体
        try {

            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;

            String result = HttpUtil.post(url, message);

            JSONObject jsonObject = JSONObject.parseObject(result);


            String errcode = "";
            String msgid = "";
            String errmsg = "";

            if (jsonObject.containsKey("errcode")) {
                errcode = jsonObject.getString("errcode");
                if (errcode == "0") {
                    // 发送成功
                    flag = true;
                    if (jsonObject.containsKey("msgid")) {
                        msgid = jsonObject.getString("msgid");
                    }
//                            m_message._fszt = "1";
//                            m_message._bz = "发送成功,msgid:" + msgid;
                } else {
                    // 发送失败
                    flag = false;
                    if (jsonObject.containsKey("errmsg")) {
                        errmsg = jsonObject.getString("errmsg");
                    }
//                            m_message._fszt = "2";
//                            m_message._bz = "发送失败,errmsg:" + errmsg;
                }
            }


//                // 更新消息状态
//                m_message._lock = "0";
//                m_message._clsj = DateTime.Now;
//                m_message._clry = "service";
//                b_message.Update(m_message, messageId);

        } catch (Exception e) {
//                msg = e.Message;
        }

        return flag;
    }


    /// <summary>
    /// 获取关注用户列表
    /// </summary>
    /// <param name="accessToken">调用接口凭证</param>
    /// <param name="nextOpenId">第一个拉取的OPENID，不填默认从头开始拉取</param>
    /// <returns></returns>
    public static UserListJsonResult GetUserList(String accessToken, String nextOpenId)
    {

        String url = String.format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}", accessToken);
        if (!StringUtils.isEmpty(nextOpenId))
        {
            url += "&next_openid=" + nextOpenId;
        }
        String result = HttpUtil.get(url);

        UserListJsonResult UserList = JSONObject.parseObject(result, UserListJsonResult.class);
        return UserList;
    }

}
