package cn.pluss.platform.util;

import cn.pluss.platform.exception.MsgException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
public class SendMsgUtil {

    @Value("${juhe.send.url}")
    private String URL;
    /**
     * 短信接口appKey
     */
    @Value("${juhe.send.key}")
    private String KEY ;

    /**
     * 微信实名认证申请通过后，发送给商户的短信
     */
    public void  verifyMsg(String phone, String merchantCode) {
        Map<String, Object> sendDataMap = new HashMap<>();
        sendDataMap.put("mobile", phone);
        sendDataMap.put("tpl_id", "221945");
        sendDataMap.put("tpl_value", "#merchantCode#=" + merchantCode);
        sendDataMap.put("key", KEY);
        HttpResult httpResult;
        try {
            httpResult = HttpUtil.doPost(URL, sendDataMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MsgException(e.getMessage());
        }
        JSONObject json = JSON.parseObject(httpResult.getBody());

        if (!"0".equalsIgnoreCase(json.getString("error_code"))) {
            throw new MsgException(json.getString("reason"));
        }
    }
}
