SmsSendServices.php 2.1 KB

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