SmsSendServices.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 app\services\message\sms;
  12. use app\services\BaseServices;
  13. use app\services\serve\ServeServices;
  14. use app\jobs\TaskJob;
  15. use think\exception\ValidateException;
  16. /**
  17. * 短信发送
  18. * Class SmsSendServices
  19. * @package app\services\message\sms
  20. */
  21. class SmsSendServices extends BaseServices
  22. {
  23. /**
  24. * 发送短信
  25. * @param bool $switch
  26. * @param $phone
  27. * @param array $data
  28. * @param string $template
  29. * @return bool
  30. */
  31. public function send(bool $switch, $phone, array $data, string $template)
  32. {
  33. if ($switch && $phone) {
  34. /** @var ServeServices $services */
  35. $services = app()->make(ServeServices::class);
  36. $res = $services->sms()->send($phone, $template, $data);
  37. if ($res === false) {
  38. throw new ValidateException($services->getError());
  39. } else {
  40. /** @var SmsRecordServices $recordServices */
  41. $recordServices = app()->make(SmsRecordServices::class);
  42. $recordServices->save([
  43. 'uid' => sys_config('sms_account'),
  44. 'phone' => $phone,
  45. 'content' => $res['content'] ?? '',
  46. 'add_time' => time(),
  47. 'template' => $res['template'] ?? '',
  48. 'record_id' => $res['id'] ?? ''
  49. ]);
  50. }
  51. TaskJob::dispatchDo('modifyResultCode');
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. }