| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace addons\Community\common\service;
- use addons\Community\common\enums\CommunityEnum;
- use addons\Community\common\logic\SmsMessage;
- use addons\Community\common\models\CommunityNotice;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\Mall;
- use Overtrue\EasySms\PhoneNumber;
- use common\helpers\sms\Sms;
- class NoticeService
- {
- public function task()
- {
- $mall_ids = [];
- foreach (Mall::getEnableMallIds() as $mall_id) {
- // 判断是否开启当前插件
- $config = \Yii::$app->services->setting->addons->get($mall_id, CommunityEnum::ADDONS_NAME, 'basic');
- if (!empty($config['is_enable'])) {
- $mall_ids[] = $mall_id;
- }
- }
- if (!empty($mall_ids)) {
- foreach (CommunityNotice::find()->where(['mall_id' => $mall_ids])
- ->andWhere(['<=', 'send_at', time()])
- ->andWhere(['status' => StatusEnum::ENABLED])->each() as $item) {
- $item->status = StatusEnum::DISABLED;
- if ($item->save()) {
- // 入列
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'id' => $item->id,
- ],
- self::class, 'async_handle');
- }
- }
- }
- }
- public function async_handle(array $params)
- {
- if (empty($params)) return;
- $data = CommunityNotice::getOne([
- ['id' => $params['id']]
- ]);
- if (empty($data)) return;
- try {
- $sms = new Sms($data->mall_id, $data->gateway);
- $message = new SmsMessage($data->content);
- // 接受角色不一样,有可能是买家,有可能是团长
- $mobile = new PhoneNumber($data->mobile, $data->area_code);
- $resp = $sms->easySms->send($mobile, $message);
- $data->status = StatusEnum::DELETE;
- } catch (\Throwable $e) {
- $err = [
- 'file' => $e->getFile(),
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine()
- ];
- }
- $data->res = [
- 'resp' => $resp ?? [],
- 'err' => $err ?? [],
- ];
- if (!$data->save() || !empty($err)) {
- \Yii::error(__METHOD__ . " -> " . $data->getErrorMessage());
- return false;
- }
- return true;
- }
- }
|