NoticeService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace addons\Community\common\service;
  3. use addons\Community\common\enums\CommunityEnum;
  4. use addons\Community\common\logic\SmsMessage;
  5. use addons\Community\common\models\CommunityNotice;
  6. use common\enums\RabbitMqEnum;
  7. use common\enums\StatusEnum;
  8. use common\helpers\FormatHelper;
  9. use common\models\mall\Mall;
  10. use Overtrue\EasySms\PhoneNumber;
  11. use common\helpers\sms\Sms;
  12. class NoticeService
  13. {
  14. public function task()
  15. {
  16. $mall_ids = [];
  17. foreach (Mall::getEnableMallIds() as $mall_id) {
  18. // 判断是否开启当前插件
  19. $config = \Yii::$app->services->setting->addons->get($mall_id, CommunityEnum::ADDONS_NAME, 'basic');
  20. if (!empty($config['is_enable'])) {
  21. $mall_ids[] = $mall_id;
  22. }
  23. }
  24. if (!empty($mall_ids)) {
  25. foreach (CommunityNotice::find()->where(['mall_id' => $mall_ids])
  26. ->andWhere(['<=', 'send_at', time()])
  27. ->andWhere(['status' => StatusEnum::ENABLED])->each() as $item) {
  28. $item->status = StatusEnum::DISABLED;
  29. if ($item->save()) {
  30. // 入列
  31. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  32. 'id' => $item->id,
  33. ],
  34. self::class, 'async_handle');
  35. }
  36. }
  37. }
  38. }
  39. public function async_handle(array $params)
  40. {
  41. if (empty($params)) return;
  42. $data = CommunityNotice::getOne([
  43. ['id' => $params['id']]
  44. ]);
  45. if (empty($data)) return;
  46. try {
  47. $sms = new Sms($data->mall_id, $data->gateway);
  48. $message = new SmsMessage($data->content);
  49. // 接受角色不一样,有可能是买家,有可能是团长
  50. $mobile = new PhoneNumber($data->mobile, $data->area_code);
  51. $resp = $sms->easySms->send($mobile, $message);
  52. $data->status = StatusEnum::DELETE;
  53. } catch (\Throwable $e) {
  54. $err = [
  55. 'file' => $e->getFile(),
  56. 'msg' => $e->getMessage(),
  57. 'line' => $e->getLine()
  58. ];
  59. }
  60. $data->res = [
  61. 'resp' => $resp ?? [],
  62. 'err' => $err ?? [],
  63. ];
  64. if (!$data->save() || !empty($err)) {
  65. \Yii::error(__METHOD__ . " -> " . $data->getErrorMessage());
  66. return false;
  67. }
  68. return true;
  69. }
  70. }