package com.muyu.server.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 调用微信服务器,内网穿透(微信公众号)
 * @Author：蓬叁
 * @Package：com.muyu.wxapplication.util
 * @Project：WXApplication
 * @name：WordUtil
 * @Date：2024/9/18 下午7:38
 */
public class WordUtil {

    //接口地址
    public static final String WORD_URL = "http://apis.juhe.cn/tyfy/query";

    //申请接口的请求key
    // TODO: 您需要改变自己的请求key
    public static final String KEY = "f9ef42f215679f6995585159c3e1073e";

    public static String getWords(String word){
        //发送http请求的url
        String url = String.format(WORD_URL, KEY);
        final String reponse = OkHttpUtils.sendPostRequest(url,JSONObject.toJSONString(word));
        System.out.println("接口返回:"+reponse);
        JSONObject jsonObject = JSONObject.parseObject(reponse) ;
        Integer error_code = jsonObject.getInteger("error_code");
        if (error_code == 0){
            System.out.println("调用接口成功");
            JSONObject result = jsonObject.getJSONObject("result");
            JSONArray words = result.getJSONArray("words");
            StringBuilder stringBuilder = new StringBuilder();
            words.stream().forEach(w -> stringBuilder.append(w + ""));
            System.out.println(stringBuilder);
            return stringBuilder.toString();
        } else {
            System.out.println("调用接口失败:" + jsonObject.getString("reason"));
        }
        return null;
    }

}
