TencentSmsHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\DataApiNew\Helper;
  3. use TencentCloud\Common\Credential;
  4. use TencentCloud\Common\Profile\HttpProfile;
  5. use TencentCloud\Sms\V20190711\SmsClient;
  6. use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
  7. use TencentCloud\Common\Exception\TencentCloudSDKException;
  8. use TencentCloud\Common\Profile\ClientProfile;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. /**
  12. * 腾讯云Sms
  13. */
  14. class TencentSmsHelper
  15. {
  16. private $secretId = "AKIDPAQdpqhSzOafqmY4PXRDapuywoVzHWaO";
  17. private $secretKey = "emiQt9AC5sRyIQNI1VkkWDfrpcaMza0A";
  18. private $sign = "集客仓";
  19. private $SmsSdkAppid = "1400803304";
  20. /**
  21. * 发送短信
  22. * @param $phoneNumberSet 号码必须加86
  23. * @param $templateID
  24. * @param $templateParamSet
  25. * @return array
  26. */
  27. public function send($phoneNumberSet, $templateID, $templateParamSet) {
  28. try {
  29. // 初始化认证对象
  30. $cred = new Credential($this->secretId, $this->secretKey);
  31. // 初始化HTTP Profile
  32. $httpProfile = new HttpProfile();
  33. $httpProfile->setReqMethod("POST"); // 使用POST请求方式
  34. // 初始化SMS客户端并关联HTTP Profile
  35. $clientProfile = new ClientProfile();
  36. $clientProfile->setHttpProfile($httpProfile);
  37. $client = new SmsClient($cred, "", $clientProfile);
  38. // 准备发送短信的参数
  39. $params = [
  40. 'PhoneNumberSet' => [$phoneNumberSet],
  41. 'TemplateID' => $templateID,
  42. 'Sign' => $this->sign,
  43. 'TemplateParamSet' => $templateParamSet,
  44. 'SmsSdkAppid' => $this->SmsSdkAppid,
  45. ];
  46. // 创建发送短信请求
  47. $req = new SendSmsRequest();
  48. $req->fromJsonString(json_encode($params));
  49. // 发送短信
  50. $resp = $client->SendSms($req);
  51. Log::info("短信接口返回:".json_encode($resp));
  52. $result = json_decode(json_encode($resp), true);
  53. $SendStatusSet = $result['SendStatusSet'];
  54. $list = [];
  55. foreach ($SendStatusSet as $key => $value) {
  56. if ($value['Code'] == "Ok") {
  57. $status = true;
  58. } else {
  59. $status = false;
  60. }
  61. $list[] = ['phone' => $value['PhoneNumber'], 'status' => $status, 'msg' => $value['Message']];
  62. }
  63. return ['code' => 1, 'msg' => "发送成功", 'data' => $list];
  64. } catch (TencentCloudSDKException $e) {
  65. Log::info("短信发送失败:".$e->getMessage());
  66. return ['code' => 0, 'msg' => $e->getMessage()];
  67. }
  68. }
  69. /**
  70. * 发送验证码
  71. *
  72. * @param [type] $phone
  73. * @return array
  74. */
  75. public static function getSmsCode($request)
  76. {
  77. $request->validate([
  78. 'phone' => 'required|integer',
  79. ]);
  80. UtilsHelper::printRequestInfo($request,'发送验证码');
  81. $phone = $request->input('phone');
  82. if (Redis::exists('CODE_' . $phone)) return error("该手机号已发送短信,请稍后再试");
  83. $tencentMessage = new TencentSmsHelper();
  84. $code = (string)rand(1111, 9999);
  85. $res = $tencentMessage->send('86' . $phone, '1734411', [$code, '5']);
  86. if (!empty($res['code']) and $res['code'] == 1) {
  87. Redis::setex('CODE_' . $phone, 60 * 5, $code);
  88. return success([], "发送成功,验证码5分钟内有效");
  89. } else {
  90. return error($res['msg']);
  91. }
  92. }
  93. /**
  94. * 验证验证码
  95. *
  96. * @param [type] $phone
  97. * @return bool
  98. */
  99. public static function validSmsCode($request)
  100. {
  101. $phone = $request->input('mobile');
  102. $code = $request->input('sms_code');
  103. if ($code == Redis::get('CODE_' . $phone)){
  104. Redis::del('CODE_' . $phone);
  105. return ['code'=>1,'msg'=>'验证成功','data'=>[]];
  106. }else{
  107. return ['code'=>0,'msg'=>'验证码错误','data'=>[]];
  108. }
  109. }
  110. }