public function send()
{
if (request()->isPost()) {
$phone = input('post.phone/s');
if (!$phone) {
return ['code' => '-1', 'data' => '', 'msg' => '請輸入手機號碼'];
}
if (!preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
return ['code' => '-1', 'data' => '', 'msg' => '請輸入正確的手機號碼'];
}
$code = $param['code'] = mt_rand(100000, 999999);
$param['product'] = 'abcdef';
Cache::set($phone, $code, 300);
$res = sendSms($phone, $param);
if ($res['code'] == 0) {
return ['code' => '0', 'data' => '', 'msg' => '發送成功'];
} else {
return ['code' => '-1', 'data' => '', 'msg' => '發送失敗'];
}
}
}
//引入 將阿里大魚API文件放在extend目錄下
use think\Loader;
use think\Config;
function sendSms($mobile='', $param=[])
{
if(empty($mobile) || empty($param)){
return ['code' => -2, 'data' => '', 'msg' => '參數錯誤'];
}
Loader::import('Alidayu.Sms',EXTEND_PATH);
$appkey = Config::get('sms_appkey'); //在config.php文件里配置自己的sms_appkey
$secretKey = Config::get('sms_secretKey'); //在config.php文件里配置自己的sms_secretKey
$sign = Config::get('sms_label');//在config.php文件里配置自己的sms_label
$sms = new \Sms($appkey,$secretKey);
$scenes_code = Config::get('scenes_code');//在config.php文件里配置自己的scenes_code
$quind = date('YmdHis',time()) . mt_rand(1000,9999);
$response = $sms->sendSms(
$sign, // 短信簽名
$scenes_code, // 短信模板編號
$mobile, // 短信接收者
$param, // 模板參數
$quind // 流水號
);
//返回結果
if($response->Code == 'OK'){
return ['code' => 0, 'data' => '', 'msg' => '發送成功'];
}
return ['code' => -1, 'data' => '', 'msg' => '發送失敗'];
}