| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace addons\Store\console\controllers;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\logic\store\StoreCensus;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCommission;
- use addons\Store\common\models\StoreExpireSmsLog;
- use addons\Store\common\sms\ShopExpireMessage;
- use common\enums\StatusEnum;
- use common\helpers\DateHelper;
- use common\helpers\sms\Sms;
- use common\models\mall\Mall;
- use Exception;
- use yii\console\Controller;
- use yii\console\ExitCode;
- use yii;
- class ExpireSmsController extends Controller
- {
- //初始化
- public function actionIndex()
- {
- try {
- $malls = Mall::getEnableMallList();
- foreach ($malls as $mall) {
- $this->actionSend(['mall_id' => $mall['id']]);
- }
- return ExitCode::OK;
- } catch (\Exception $e) {
- \Yii::getLogger()->flush(true);
- return false;
- }
- }
- public function actionSend($params)
- {
- try {
- $sms_setting = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'sms_setting');
- if (empty($sms_setting['is_alert_expire'])) {
- // echo 'o2o门店到期提醒未开启';
- return true;
- }
- if (empty($sms_setting['expire_template_id'])) {
- // echo 'o2o门店到期提醒,模板ID为空';
- return true;
- }
- if (empty($sms_setting['expire_day'])) {
- $remind_time = time() + 1800;
- } else {
- $remind_time = time() + ($sms_setting['expire_day'] * 3600 * 24);
- }
- //获取门店数据
- $store_list = Store::lists([
- 'where' => [
- ['mall_id' => $params['mall_id']],
- ['and', ['>=', 'expire_time', time()], ['<=', 'expire_time', $remind_time]],
- ['status' => StatusEnum::ENABLED],
- ],
- 'select' => 'id,mall_id,store_name,shop_mobile,user_id,expire_time'
- ]);
- if (!empty($store_list)) {
- $SmsConfig = new Sms($params['mall_id']);
- $easy_sms = $SmsConfig->easySms;
- foreach ($store_list as $key => $value) {
- //查找门店是否存在过期提醒短信记录
- $sms_log = StoreExpireSmsLog::getOne([
- ['mall_id' => $params['mall_id']],
- ['store_id' => $value['id']],
- ['date_time' => date('Ymd', $value['expire_time'])]
- ], true, [], 'id desc');
- if (!empty($sms_log)) continue;
- $message = new ShopExpireMessage($params['mall_id'], $sms_setting['expire_day'], $value['store_name']);
- $easy_sms->send($value['shop_mobile'], $message);
- //记录发送短信记录
- $data = [
- 'mall_id' => $params['mall_id'],
- 'store_id' => $value['id'],
- 'date_time' => date('Ymd', $value['expire_time']),
- 'status' => StatusEnum::ENABLED,
- ];
- $res = StoreExpireSmsLog::setData($data);
- if (!$res) var_dump('短信发送记录添加失败,store_id:'. $value['id']);
- }
- }
- return true;
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- throw new Exception($e->getMessage());
- }
- }
- }
|