bingyu-duanxinwangzhan/message-code/message_api/application/index/controller/MessageSendMess.php

122 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2020/11/10
* Time: 6:45 PM
*/
namespace app\index\controller;
use app\common\SignatureHelper;
use app\index\model\Code;
class MessageSendMess extends Common
{
/**
* sendSms
* @return \think\response\Json
*/
public function sendSms()
{
if(self::$result[self::$error_code_name] == 0)
{
$code_model = new Code();
$field_arr = array(
array('field_name' => $code_model::TEL,'rule' => 'require|isMobile')
);
$check_arr = $this->checkHave($field_arr);
if($check_arr[self::$error_code_name] == 0)
{
$ip = getIp();
$beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$today_num = $code_model->where(array(
$code_model::SYS_CREATE_TIME => array('between',array($beginToday,$endToday)),
$code_model::TEL => $check_arr[self::$data_name][$code_model::TEL]
))->count();
$today_ip_num = $code_model->where(array(
$code_model::SYS_CREATE_TIME => array('between',array($beginToday,$endToday)),
$code_model::IP => $ip
))->count();
if($today_num < 3 && $today_ip_num < 1000)
{
$rand_num = rand(1000,9999);
$params = array ();
// *** 需用户填写部分 ***
// fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
$accessKeyId = "LTAI4G9vPF4RFU7T74p4cJ9a";
$accessKeySecret = "PGWrVQN9ze8N8ze58zV1TjaVKDbPKm";
// fixme 必填: 短信接收号码
$params["PhoneNumbers"] = $check_arr[self::$data_name][$code_model::TEL];
// fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$params["SignName"] = "南京真网科技有限公司";
// fixme 必填: 短信模板Code应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
$params["TemplateCode"] = "SMS_205433459";
// fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
$params['TemplateParam'] = Array (
"code" => strval($rand_num)
);
// fixme 可选: 设置发送短信流水号
$params['OutId'] = "12345";
// fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下无特殊需求用户请忽略此字段
$params['SmsUpExtendCode'] = "1234567";
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
}
// 初始化SignatureHelper实例用于设置参数签名以及发送请求
$helper = new SignatureHelper();
// 此处可能会抛出异常注意catch
$content = $helper->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
);
if($content->Message == 'OK')
{
$data = array();
$data[$code_model::TEL] = $check_arr[self::$data_name][$code_model::TEL];
$data[$code_model::CODE] = $rand_num;
$data[$code_model::IP] = $ip;
$data[$code_model::SYS_CREATE_TIME] = time();
$add_rs = $code_model->addData($code_model,$data);
if(!$add_rs)
{
self::$result[self::$error_code_name] = 10002;
}
}else{
self::$result[self::$error_code_name] = 10002;
}
}else{
self::$result[self::$error_code_name] = 10006;
}
}else{
if($check_arr[self::$error_code_name] == 1)
{
self::$result[self::$error_code_name] = 10004;
self::$result[self::$error_message] = $check_arr[self::$error_message];
}else{
self::$result[self::$error_code_name] = 10001;
}
}
}
return json(self::$result);
}
}