ShortLetterRepositories.php 2.8 KB

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