package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;

import java.io.IOException;

/**
 * @Auther: ZouLF
 * @Date: 2018/5/29 10:49
 * @Description: 短信验证码
 */
public class JuSms {


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static boolean aliSendSms(String mobile,String code) throws IOException {
        com.aliyun.dysmsapi20170525.Client client = null;
        boolean result  = false;
        try {
            client = createClient("LTAI5tJtXDnbJVKozqjaVTca", "KESTkUIIv3s42wMpkwt1Bkr3kcgFRr");
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName("蓝领商务")
                    .setTemplateCode("SMS_225385757")
                    .setPhoneNumbers(mobile)
                    .setTemplateParam("{\"code\":\""+code+"\"}");
            SendSmsResponse resp = client.sendSms(sendSmsRequest);
            String s = JSON.toJSONString(TeaModel.buildMap(resp));
            System.out.println("发送手机验证码短信结果=>"+s);
            JSONObject jsonObject = JSON.parseObject(s);
            JSONObject body = jsonObject.getJSONObject("body");

            result = "OK".equals(body.getString("Code"));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }

    }

}
