ShortLetterRepositories.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\admin\model\sms\SmsRecord;
  4. use crmeb\services\AlismsService;
  5. use crmeb\services\sms\Sms;
  6. use think\facade\Log;
  7. /**
  8. * 短信发送
  9. * Class ShortLetterRepositories
  10. * @package crmeb\repositories
  11. */
  12. class ShortLetterRepositories
  13. {
  14. /**
  15. * 发送短信
  16. * @param $switch 发送开关
  17. * @param $phone 手机号码
  18. * @param array $data 模板替换内容
  19. * @param string $template 模板编号
  20. * @param string $logMsg 错误日志记录
  21. * @return bool|string
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public static function send($switch, $phone, array $data, string $template, $logMsg = '')
  26. {
  27. if ($switch && $phone) {
  28. $sms = new Sms([
  29. 'sms_account' => sys_config('sms_account'),
  30. 'sms_token' => sys_config('sms_token'),
  31. 'site_url' => sys_config('site_url')
  32. ]);
  33. $res = $sms->send($phone, $template, $data);
  34. if ($res === false) {
  35. $errorSmg = $sms->getError();
  36. Log::info($logMsg ?? $errorSmg);
  37. return $errorSmg;
  38. } else {
  39. SmsRecord::sendRecord($phone, $res['data']['content'], $res['data']['template'], $res['data']['id']);
  40. }
  41. return true;
  42. } else {
  43. return false;
  44. }
  45. }
  46. /**
  47. * 发送短信
  48. * @param string $phone 手机号码
  49. * @param array $data 模板替换内容
  50. * @param string $template 模板编号
  51. * @return bool|string
  52. */
  53. public static function AliSend(bool $switch, string $phone, array $data, string $template, $logMsg = '')
  54. {
  55. if ($switch && $phone) {
  56. try {
  57. $sms = new AlismsService(sys_config('sms_account', 'LTAI5tHCH1HiBQvZn5TRAiBa', true), sys_config('sms_token', 'uHxAux7xtrI5xmCwtXD7NBVFRTIy6c', true));
  58. $res = $sms->setAction('SendSms')->setOptions([
  59. 'PhoneNumbers' => $phone,
  60. 'SignName' => '宏根蒂',
  61. 'TemplateCode' => \think\facade\Config::get('sms.stores.aliyun.template_id.' . $template, 'sms.stores.aliyun.template_id.DEFAULT'),
  62. 'TemplateParam' => json_encode($data),
  63. ])->execute();
  64. if ($res['Code'] != 'OK') {
  65. // halt($res);
  66. Log::info($logMsg ?? $res['Message']);
  67. return $res['Message'];
  68. } else {
  69. SmsRecord::sendRecord($phone, $data['code'], $template, '');
  70. }
  71. return true;
  72. } catch (\Exception $exception) {
  73. return $exception->getMessage();
  74. }
  75. } else {
  76. return false;
  77. }
  78. }
  79. }