接口/public/sendSms
SMS_******之类的数据和发送的模板及签名需要在阿里大鱼上设置
/**
* 发送短信验证码
* 登录确认验证码 SMS_212405177
* 用户注册验证码 SMS_212405175
* 修改密码验证码 SMS_212405174
*/
public function sendSms()
{
//验证数据合法性
$validate = new \think\Validate([
['account', 'require|number|/^1[3456789]\d{9}$/', '手机号码不能为空|手机号码格式不正确|手机号码格式不正确'],
['mold', 'require', '缺少信息分类'],
]);
if (!$validate->check($this->param)) {
return ajaxError($validate->getError());
}
$tplcode = [
'login' => 'SMS_212405177',
'register' => 'SMS_212405175',
'forget'=>'SMS_212405174'
];
$code = randNum(6);
return sendSms($this->param['account'], $tplcode[$this->param['mold']], $code);
}
生成6位验证码
/**
* 获取随机位数数字,用于生成短信验证码
* @param integer $len 长度
* @return string
*/
function randNum($len = 6)
{
$chars = str_repeat('0123456789', $len);
$chars = str_shuffle($chars);
$str = substr($chars, 0, $len);
return $str;
}
model层方法
/**
* 阿里大于短信
* $phone 手机号 字符串
* $tplcode 短信模板 id 字符串
* SMS_193015822(验证码${code},您正在注册成为新用户,感谢您的支持!)
* SMS_193015824(验证码${code},您正在登录,若非本人操作,请勿泄露。)
* $tplParam=['code'=>$code] 验证码 数组
*/
function sendSms($phone, $tplcode, $code)
{
$info = [];
$info['phone'] = $phone;
$info['smscode'] = $code;
$info['status'] = 0;
$info['create_time'] = time();
if (\think\Db::name('sms')->data($info)->insert()) {
//引入方法
vendor('alisms.sms');
$sms = new \sms();
//调用方法send
$data = $sms->send($phone, $tplcode, $code);
return ajaxSuccess('',$data);
return ajaxSuccess('发送成功');
} else {
return ajaxError('发送失败');
}
}
注意修改引入文件中的$accessKeyId/$accessKeySecret还有签名