| xqd
@@ -8,7 +8,6 @@
|
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use App\Exceptions\ExitOutException;
|
|
|
-use App\Models\User;
|
|
|
|
|
|
//统一输出格式话的json数据
|
|
|
if (!function_exists('out')) {
|
| xqd
@@ -188,4 +187,118 @@ if (!function_exists('get_user_distance')) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//发送短信
|
|
|
+if (!function_exists('send_sms')) {
|
|
|
+ function send_sms($phone, $templateKey, $templateParam = [])
|
|
|
+ {
|
|
|
+ $sms_config = config('config.aly_sms');
|
|
|
+ //是否启用https
|
|
|
+ $security = false;
|
|
|
+ $params = [];
|
|
|
+ $params["PhoneNumbers"] = $phone;
|
|
|
+ $params["SignName"] = $sms_config['sign_name'];
|
|
|
+ $params["TemplateCode"] = $sms_config[$templateKey];
|
|
|
+ $params['TemplateParam'] = $templateParam;
|
|
|
+
|
|
|
+ if (is_array($params["TemplateParam"])) {
|
|
|
+ $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ $content = aly_sm_request(
|
|
|
+ $sms_config['access_key'],
|
|
|
+ $sms_config['access_secret'],
|
|
|
+ "dysmsapi.aliyuncs.com",
|
|
|
+ array_merge($params, array(
|
|
|
+ "RegionId" => "cn-hangzhou",
|
|
|
+ "Action" => "SendSms",
|
|
|
+ "Version" => "2017-05-25",
|
|
|
+ )),
|
|
|
+ $security
|
|
|
+ );
|
|
|
+
|
|
|
+ return $content;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('aly_sm_request')) {
|
|
|
+ function aly_sm_request($accessKeyId, $accessKeySecret, $domain, $params, $security = false, $method = 'POST')
|
|
|
+ {
|
|
|
+ $apiParams = array_merge(array(
|
|
|
+ "SignatureMethod" => "HMAC-SHA1",
|
|
|
+ "SignatureNonce" => uniqid(mt_rand(0, 0xffff), true),
|
|
|
+ "SignatureVersion" => "1.0",
|
|
|
+ "AccessKeyId" => $accessKeyId,
|
|
|
+ "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
|
|
|
+ "Format" => "JSON",
|
|
|
+ ), $params);
|
|
|
+ ksort($apiParams);
|
|
|
+
|
|
|
+ $sortedQueryStringTmp = "";
|
|
|
+ foreach ($apiParams as $key => $value) {
|
|
|
+ $sortedQueryStringTmp .= "&" . aly_sms_encode($key) . "=" . aly_sms_encode($value);
|
|
|
+ }
|
|
|
+
|
|
|
+ $stringToSign = "${method}&%2F&" . aly_sms_encode(substr($sortedQueryStringTmp, 1));
|
|
|
+
|
|
|
+ $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&", true));
|
|
|
+
|
|
|
+ $signature = aly_sms_encode($sign);
|
|
|
+
|
|
|
+ $url = ($security ? 'https' : 'http') . "://{$domain}/";
|
|
|
+
|
|
|
+ try {
|
|
|
+ $content = aly_sms_fetch_content($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");
|
|
|
+ return json_decode($content);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+if (!function_exists('aly_sms_encode')) {
|
|
|
+ function aly_sms_encode($str)
|
|
|
+ {
|
|
|
+ $res = urlencode($str);
|
|
|
+ $res = preg_replace("/\+/", "%20", $res);
|
|
|
+ $res = preg_replace("/\*/", "%2A", $res);
|
|
|
+ $res = preg_replace("/%7E/", "~", $res);
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('aly_sms_fetch_content')) {
|
|
|
+ function aly_sms_fetch_content($url, $method, $body)
|
|
|
+ {
|
|
|
+ $ch = curl_init();
|
|
|
+
|
|
|
+ if ($method == 'POST') {
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
|
|
+ } else {
|
|
|
+ $url .= '?' . $body;
|
|
|
+ }
|
|
|
+
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
|
+ "x-sdk-client" => "php/2.0.0"
|
|
|
+ ));
|
|
|
+
|
|
|
+ if (substr($url, 0, 5) == 'https') {
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ $rtn = curl_exec($ch);
|
|
|
+
|
|
|
+ if ($rtn === false) {
|
|
|
+ // 大多由设置等原因引起,一般无法保障后续逻辑正常执行,
|
|
|
+ // 所以这里触发的是E_USER_ERROR,会终止脚本执行,无法被try...catch捕获,需要用户排查环境、网络等故障
|
|
|
+ trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
|
|
|
+ }
|
|
|
+ curl_close($ch);
|
|
|
+
|
|
|
+ return $rtn;
|
|
|
+ }
|
|
|
+}
|