123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace App\DataApiNew\Helper;
- use TencentCloud\Common\Credential;
- use TencentCloud\Common\Profile\HttpProfile;
- use TencentCloud\Sms\V20190711\SmsClient;
- use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- use TencentCloud\Common\Profile\ClientProfile;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- /**
- * 腾讯云Sms
- */
- class TencentSmsHelper
- {
- private $secretId = "AKIDPAQdpqhSzOafqmY4PXRDapuywoVzHWaO";
- private $secretKey = "emiQt9AC5sRyIQNI1VkkWDfrpcaMza0A";
- private $sign = "集客仓";
- private $SmsSdkAppid = "1400803304";
- /**
- * 发送短信
- * @param $phoneNumberSet 号码必须加86
- * @param $templateID
- * @param $templateParamSet
- * @return array
- */
- public function send($phoneNumberSet, $templateID, $templateParamSet) {
- try {
- // 初始化认证对象
- $cred = new Credential($this->secretId, $this->secretKey);
- // 初始化HTTP Profile
- $httpProfile = new HttpProfile();
- $httpProfile->setReqMethod("POST"); // 使用POST请求方式
- // 初始化SMS客户端并关联HTTP Profile
- $clientProfile = new ClientProfile();
- $clientProfile->setHttpProfile($httpProfile);
- $client = new SmsClient($cred, "", $clientProfile);
- // 准备发送短信的参数
- $params = [
- 'PhoneNumberSet' => [$phoneNumberSet],
- 'TemplateID' => $templateID,
- 'Sign' => $this->sign,
- 'TemplateParamSet' => $templateParamSet,
- 'SmsSdkAppid' => $this->SmsSdkAppid,
- ];
- // 创建发送短信请求
- $req = new SendSmsRequest();
- $req->fromJsonString(json_encode($params));
- // 发送短信
- $resp = $client->SendSms($req);
- Log::info("短信接口返回:".json_encode($resp));
- $result = json_decode(json_encode($resp), true);
- $SendStatusSet = $result['SendStatusSet'];
- $list = [];
- foreach ($SendStatusSet as $key => $value) {
- if ($value['Code'] == "Ok") {
- $status = true;
- } else {
- $status = false;
- }
- $list[] = ['phone' => $value['PhoneNumber'], 'status' => $status, 'msg' => $value['Message']];
- }
- return ['code' => 1, 'msg' => "发送成功", 'data' => $list];
- } catch (TencentCloudSDKException $e) {
- Log::info("短信发送失败:".$e->getMessage());
- return ['code' => 0, 'msg' => $e->getMessage()];
- }
- }
- /**
- * 发送验证码
- *
- * @param [type] $phone
- * @return array
- */
- public static function getSmsCode($request)
- {
- $request->validate([
- 'phone' => 'required|integer',
- ]);
- UtilsHelper::printRequestInfo($request,'发送验证码');
- $phone = $request->input('phone');
- if (Redis::exists('CODE_' . $phone)) return error("该手机号已发送短信,请稍后再试");
- $tencentMessage = new TencentSmsHelper();
- $code = (string)rand(1111, 9999);
- $res = $tencentMessage->send('86' . $phone, '1734411', [$code, '5']);
- if (!empty($res['code']) and $res['code'] == 1) {
- Redis::setex('CODE_' . $phone, 60 * 5, $code);
- return success([], "发送成功,验证码5分钟内有效");
- } else {
- return error($res['msg']);
- }
- }
- /**
- * 验证验证码
- *
- * @param [type] $phone
- * @return bool
- */
- public static function validSmsCode($request)
- {
- $phone = $request->input('mobile');
- $code = $request->input('sms_code');
- if ($code == Redis::get('CODE_' . $phone)){
- Redis::del('CODE_' . $phone);
- return ['code'=>1,'msg'=>'验证成功','data'=>[]];
- }else{
- return ['code'=>0,'msg'=>'验证码错误','data'=>[]];
- }
- }
- }
|