package com.pj.sms.handler.impl;

import cn.hutool.core.net.URLEncoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpUtil;
import com.pj.api.consts.FeignFactory;
import com.pj.sms.handler.ISmsHandler;
import com.pj.sms.brand.SmsType;
import com.pj.sms.properties.DuanXinBaoProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

@Service
@Slf4j
public class DuanXinBaoHandlerImpl implements ISmsHandler {
    @Resource
    private DuanXinBaoProperties duanXinBaoProperties;
    @Override
    public void sendSMS(String phone,String content) {
        String pwd = MD5.create().digestHex(duanXinBaoProperties.getPwd());
        final String url = "https://api.smsbao.com/sms?u=" + duanXinBaoProperties.getUsername() +
                "&p=" + pwd + "&m=" + phone + "&c=" +
                URLEncoder.createDefault().encode("[" + duanXinBaoProperties.getSign() + "]" + content, StandardCharsets.UTF_8);
        String result = HttpUtil.get(url);
        log.info("send duan xin bao  sms result:{},{},{}", phone, content, result);
    }

    @Override
    public void sendSMS(String phone, String templateCode, String content) {

    }

    @Override
    public SmsType smsType() {
        return SmsType.DUAN_XIN_BAO;
    }
}
