AliMessageService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use AlibabaCloud\Client\AlibabaCloud;
  13. use AlibabaCloud\Client\Exception\ClientException;
  14. use AlibabaCloud\Client\Exception\ServerException;
  15. /**
  16. * 阿里云短信
  17. * Class AliMessageService
  18. * @package service
  19. */
  20. class AliMessageService
  21. {
  22. /**
  23. * 初始化
  24. * @throws ClientException
  25. */
  26. public static function init()
  27. {
  28. AlibabaCloud::accessKeyClient(SystemConfigService::get('accessKeyId'), SystemConfigService::get('accessKeySecret'))
  29. ->regionId('cn-hangzhou')
  30. ->asDefaultClient();
  31. }
  32. /**
  33. * 发送短信
  34. * @param string $tel 短信接收号码
  35. * @param string $setSignName 短信签名
  36. * @param string $setTemplateCode 短信模板ID
  37. * @param array $setTemplateParam 短信内容
  38. * @param string $setOutId 外部流水扩展字段
  39. */
  40. public static function sendmsg($tel = '', $setTemplateParam = [], $setOutId = '')
  41. {
  42. try {
  43. self::init();
  44. $result = AlibabaCloud::rpc()
  45. ->product('Dysmsapi')
  46. ->version('2017-05-25')
  47. ->action('SendSms')
  48. ->method('POST')
  49. ->host('dysmsapi.aliyuncs.com')
  50. ->options([
  51. 'query' => [
  52. 'RegionId' => "cn-hangzhou",
  53. 'PhoneNumbers' => $tel,
  54. 'SignName' => SystemConfigService::get('smsSignName'),
  55. 'TemplateCode' => SystemConfigService::get('smsTemplateCode'),
  56. 'TemplateParam' => json_encode(is_array($setTemplateParam) ? $setTemplateParam : ['code' => $setTemplateParam]),
  57. ],
  58. ])->request()->toArray();
  59. return $result;
  60. } catch (ClientException $e) {
  61. return false;
  62. } catch (ServerException $e) {
  63. return false;
  64. }
  65. }
  66. /**
  67. * 生成随机验证码
  68. * @return int
  69. */
  70. public static function getVerificationCode($length = 6)
  71. {
  72. $str = '123456789';
  73. $code = '';
  74. for ($i = 0; $i < $length; $i++) {
  75. $code .= $str[mt_rand(0, strlen($str) - 1)];
  76. }
  77. return $code;
  78. }
  79. }