<?php


namespace app\common\util;

class Alidayu
{
	private static $client;
	private function __construct()
	{
	}
	private static function getClient()
	{
		if (self::$client == null) {
			$accessKeyId = \think\Db::name("config")->where("code", "sms_id")->where("status", 0)->find();
			$accessKeySecret = \think\Db::name("config")->where("code", "sms_secret")->where("status", 0)->find();
			$config = ["accessKeyId" => $accessKeyId["value"], "accessKeySecret" => $accessKeySecret["value"]];
			self::$client = new \Flc\Dysms\Client($config);
		}
		return self::$client;
	}
	public static function sendSms($mobile, $code)
	{
		$sms_open = \think\Db::name("config")->where("code", "sms_open")->where("status", 0)->find();
		if ($sms_open && $sms_open["value"] == "1") {
			$sendSms = new \Flc\Dysms\Request\sendSms();
			$temp_code = \think\Db::name("config")->where("code", "sms_temp")->where("status", 0)->find();
			$sms_sign = \think\Db::name("config")->where("code", "sms_sign")->where("status", 0)->find();
			$sendSms->setPhoneNumbers($mobile);
			$sendSms->setSignName($sms_sign["value"]);
			$sendSms->setTemplateCode($temp_code["value"]);
			if ($code) {
				$sendSms->setTemplateParam(["code" => $code]);
			}
			$sendSms->setOutId("demo");
			return self::getClient()->execute($sendSms);
		} else {
			return false;
		}
	}
}