regionId('cn-hangzhou') // ->asDefaultClient(); AlibabaCloud::accessKeyClient('LTAI5tKivJSckMYz5BHev3u8', 'nvetSdLB1YP7OgvpJcVeyhdJDVs4Ik') ->regionId('cn-hangzhou') ->asDefaultClient(); $templateParam['code'] = $code; $content = ''; log::info("发送短信方法"); switch ($templateId) { case 'AUTHCODE': // 移除所有非数字字符(保留+) $mobile = preg_replace('/[^0-9+]/', '', $mobile); if (strpos($mobile, '+') === false || strpos($mobile, '+86') === 0) { if (strpos($mobile, '+86') === 0) { $mobile = substr($mobile, 3); } $SignName = '北京融合华夏数字文化科技'; $TemplateCode = 'SMS_481025122'; $content = '【华夏幸福保】您的验证码是:' . $code . ',请勿泄露于他人!'; } else { if (strpos($mobile, '+') === 0) { $mobile = substr($mobile, 1); } $SignName = '融合华夏'; $TemplateCode = 'SMS_492105133'; $templateParam = [ 'code_zh' => $code, 'code_en' => $code ]; $content = '【融合华夏】您的验证码是:' . $code . ',请输入此验证码以完成验证流程。Your verification code is:' . $code . '. Please enter this code to complete your verification process.'; } break; case 'ONESELFCARRY': $SignName = '华夏幸福保'; $TemplateCode = 'SMS_465430830'; $content = '【华夏幸福保】您购买的商品动态码(自提码)为' . $code . ',提货时请出示自提码!'; break; case 'REDCHANGE': $SignName = '华夏幸福保'; $TemplateCode = 'SMS_465371794'; $content = '【华夏幸福保】您的短信验证码为' . $code . ',您正在人工调整红积分,如非本人操作,请忽略该短信。'; break; case 'COMMISSIONCHANGE': $SignName = '华夏幸福保'; $TemplateCode = 'SMS_465392073'; $content = '【华夏幸福保】您的短信验证码为' . $code . ',您正在人工调整用户佣金,如非本人操作,请忽略该短信。'; break; } try { $result = AlibabaCloud::rpc() ->product('Dysmsapi') ->version('2017-05-25') ->action('SendSms') ->method('POST') ->options([ 'query' => [ 'PhoneNumbers' => $mobile, 'SignName' => $SignName, 'TemplateCode' => $TemplateCode,//测试的 // 'TemplateCode' => 'SMS_464451169', 'TemplateParam' => json_encode($templateParam), ], ]) ->request(); log::info("短信:返回参数"); log::info(json_encode($result->toArray())); Db::name('sms_record')->insert(['phone' => $mobile, 'content' => $content, 'template' => $TemplateCode, 'create_time'=>date("Y-m-d H:i:s")]); return $result->toArray(); // } catch (ClientException $e) { // return ['code' => 500, 'message' => '阿里云短信客户端异常:' . $e->getErrorMessage()]; // } catch (ServerException $e) { // return ['code' => 500, 'message' => '阿里云短信服务端异常:' . $e->getErrorMessage()]; } catch (Exception $e) { return ['code' => 500, 'message' => '阿里云短信服务端异常:' . $e->getMessage()]; } } }