AliSms.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace plugin\ali_sms\service;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use app\service\ConfServiceFacade;
  7. use laytp\traits\Error;
  8. class AliSms
  9. {
  10. use Error;
  11. //发送手机短信
  12. public function send($mobile, $event, $templateParam){
  13. try {
  14. AlibabaCloud::accessKeyClient(ConfServiceFacade::get('plugin.ali_sms.accessKeyId'), ConfServiceFacade::get('plugin.ali_sms.accessKeySecret'))
  15. ->regionId('cn-hangzhou')
  16. ->asDefaultClient();
  17. $mobile_msg_template = ConfServiceFacade::get('plugin.ali_sms.template');
  18. $result = AlibabaCloud::rpc()
  19. ->product('Dysmsapi')
  20. // ->scheme('https') // https | http
  21. ->version('2017-05-25')
  22. ->action('SendSms')
  23. ->method('POST')
  24. ->host('dysmsapi.aliyuncs.com')
  25. ->options([
  26. 'query' => [
  27. 'RegionId' => "cn-hangzhou",
  28. 'PhoneNumbers' => $mobile,
  29. 'SignName' => ConfServiceFacade::get('plugin.ali_sms.sign'),
  30. 'TemplateCode' => $mobile_msg_template[$event],
  31. 'TemplateParam' => json_encode($templateParam, JSON_UNESCAPED_UNICODE),
  32. ],
  33. ])
  34. ->request()->toArray();
  35. if($result['Code'] == 'OK'){
  36. //插入短信表
  37. $data = [
  38. 'template_code' => $mobile_msg_template[$event],
  39. 'event' => $event,
  40. 'mobile' => $mobile,
  41. 'expire_time' => ConfServiceFacade::get('plugin.ali_sms.expireTime') ? time() + ConfServiceFacade::get('plugin.ali_sms.expireTime') : 0,
  42. 'params' => json_encode($templateParam, JSON_UNESCAPED_UNICODE),
  43. ];
  44. \plugin\ali_sms\model\AliSms::create($data);
  45. return true;
  46. }else{
  47. $this->setError($result['Message']);
  48. return false;
  49. }
  50. } catch (ClientException $e) {
  51. $this->setError($e->getErrorMessage());
  52. return false;
  53. } catch (ServerException $e) {
  54. $this->setError($e->getErrorMessage());
  55. return false;
  56. }
  57. }
  58. //检测验证码
  59. public function checkCode($mobile, $event, $code){
  60. if(!env('APP_DEBUG')) {
  61. $message = \plugin\ali_sms\model\AliSms::where([['mobile', '=', $mobile], ['event', '=', $event], ['params', '=', json_encode(['code' => $code], JSON_UNESCAPED_UNICODE)]])->find();
  62. if (!$message) {
  63. $this->setError('验证码错误');
  64. return false;
  65. }
  66. if ($message->status == 2) {
  67. $this->setError('验证码已使用');
  68. return false;
  69. }
  70. if ($message->status == 3) {
  71. $this->setError('验证码已过期');
  72. return false;
  73. }
  74. if ($message->expire_time && $message->expire_time < time()) {
  75. \plugin\ali_sms\model\AliSms::where('id', '=', $message->id)->update(['status' => 3]);
  76. $this->setError('验证码已过期');
  77. return false;
  78. }
  79. \plugin\ali_sms\model\AliSms::where('id', '=', $message->id)->update(['status' => 2]);
  80. }
  81. return true;
  82. }
  83. }