package com.ruoyi.web.controller.app.baidu;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;

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

public class BaiduSMS {

    public static boolean sendSMS(String phone, String code, String time) {
        String ACCESS_KEY_ID = "84bd8ab3c233461dba7f254fd3b13e12";
        String SECRET_ACCESS_KEY = "ea723b36154c42c0bac3f3864929a724";
        String ENDPOINT = "http://smsv3.bj.baidubce.com";

        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
        config.setEndpoint(ENDPOINT);
        SmsClient client = new SmsClient(config);

        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(phone);
        request.setSignatureId("sms-sign-MiTUWW85061");
        request.setTemplate("sms-tmpl-iLlROF73209");
        Map<String, String> contentVar = new HashMap<>();
        contentVar.put("SMSvCode", code);
        contentVar.put("time", time);
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        // 解析请求响应 response.isSuccess()为true 表示成功
        if (response != null && response.isSuccess()) {
            return true;
            //  submit success
        } else {
            //  fail
            return false;
        }
    }
}
