400, 'msg' => '请输入手机号']); $config = Yii::$app->params['smsconfig']; $easySms = new EasySms($config); $code = mt_rand(100000, 999999); $data = ['code' => $code, 'expire_time' => time() + 300]; $sess = Yii::$app->session; $sessionkey = 'live_' . $input['mobilephone']; $sess[$sessionkey] = $data; try { $easySms->send($input['mobilephone'], [ 'content' => '您的验证码为: ' . $code, 'template' => 'SMS_176928060', 'data' => [ 'code' => $code ], ]); } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $e) { $this->saverecord($input['mobilephone'], $this->Estatus, $code, $input['purpose']); return json_encode(['code' => 400, 'msg' => '短信发送失败']); } $this->saverecord($input['mobilephone'], $this->Sstatus, $code, $input['purpose']); return json_encode(['code' => 200, 'msg' => '短信发送成功']); } //存入短信记录到数据库 public function saverecord($mobile, $status, $code, $purpose) { $sendcoderecordnew = new \common\models\Sendcoderecord(); $sendcoderecordnew->mobilephone = $mobile; $sendcoderecordnew->status = $status; $sendcoderecordnew->code = $code; $sendcoderecordnew->purpose = $purpose; $sendcoderecordnew->content = '【品房阁】您的验证码是' . $code . ', 在五分钟内有效。如非本人操作请忽略本短信。'; $sendcoderecordnew->save(); } }