package com.ruoyi.system.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class MobileSMS {
    public void getcode(String phone,String code){
        String url = "http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get?" +
                "Brandname=CITYEXPRESS" +
                "&Phone=" + phone +
                "&Content=CITYEXPRESS thong bao ma xac thuc OTP la " + code + " qua App CTE. Luu y: Ma OTP se het han trong vong 3 phut. KHONG chia se ma OTP voi nguoi khac." +
                "&ApiKey=8E7C9BBB6DA676D66A20503831BB82" +
                "&SecretKey=23BDA7AF32E14EA1D4031A3A5C2D7A" +
                "&SmsType=2";
        String res="";
        res = HttpUtil.get(url);
//        JSONObject jsonObject =  JSON.parseObject(res);
        System.out.println("发送短信结果："+res);
    }

    /**
     * 发送自定义验证码（EngageLab 自定义消息下发）
     * 文档：https://www.engagelab.com/zh_CN/docs/otp/REST-API/CustomMessages-Send
     *
     * @param phone 手机号（含国家码，如 +8613700000000）
     * @param code  自定义验证码，模板类型为验证码时必填
     */
    public void getTwCode(String phone, String code) {
        try {
            String apiKey = "d6kk5d4ao9hcpovohsl0";
            String apiSecret = "t7bhxkdype5p6facdna4ehja";
            String authString = Base64.getEncoder()
                    .encodeToString((apiKey + ":" + apiSecret).getBytes(StandardCharsets.UTF_8));

            // 自定义消息：template.params.code 必填（验证码模板）
            JSONObject template = new JSONObject();
            template.put("id", "tw_foodie_code");
            JSONObject params = new JSONObject();
            params.put("code", code);
            template.put("params", params);

            JSONObject body = new JSONObject();
            body.put("to", phone);
            body.put("template", template);
            String bodyString = body.toJSONString();

            String res = HttpRequest.post("https://otp.api.engagelab.cc/v1/custom-messages")
                    .header("Authorization", "Basic " + authString)
                    .header("Content-Type", "application/json")
                    .body(bodyString)
                    .execute()
                    .body();

            System.out.println("发送短信结果：" + res);
        } catch (Exception e) {
            System.err.println("OTP 发送异常：" + e.getMessage());
        }
    }
}
