ExpireSmsController.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\Store\console\controllers;
  3. use addons\Store\common\enums\StoreEnum;
  4. use addons\Store\common\logic\store\StoreCensus;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\models\StoreCommission;
  7. use addons\Store\common\models\StoreExpireSmsLog;
  8. use addons\Store\common\sms\ShopExpireMessage;
  9. use common\enums\StatusEnum;
  10. use common\helpers\DateHelper;
  11. use common\helpers\sms\Sms;
  12. use common\models\mall\Mall;
  13. use Exception;
  14. use yii\console\Controller;
  15. use yii\console\ExitCode;
  16. use yii;
  17. class ExpireSmsController extends Controller
  18. {
  19. //初始化
  20. public function actionIndex()
  21. {
  22. try {
  23. $malls = Mall::getEnableMallList();
  24. foreach ($malls as $mall) {
  25. $this->actionSend(['mall_id' => $mall['id']]);
  26. }
  27. return ExitCode::OK;
  28. } catch (\Exception $e) {
  29. \Yii::getLogger()->flush(true);
  30. return false;
  31. }
  32. }
  33. public function actionSend($params)
  34. {
  35. try {
  36. $sms_setting = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'sms_setting');
  37. if (empty($sms_setting['is_alert_expire'])) {
  38. // echo 'o2o门店到期提醒未开启';
  39. return true;
  40. }
  41. if (empty($sms_setting['expire_template_id'])) {
  42. // echo 'o2o门店到期提醒,模板ID为空';
  43. return true;
  44. }
  45. if (empty($sms_setting['expire_day'])) {
  46. $remind_time = time() + 1800;
  47. } else {
  48. $remind_time = time() + ($sms_setting['expire_day'] * 3600 * 24);
  49. }
  50. //获取门店数据
  51. $store_list = Store::lists([
  52. 'where' => [
  53. ['mall_id' => $params['mall_id']],
  54. ['and', ['>=', 'expire_time', time()], ['<=', 'expire_time', $remind_time]],
  55. ['status' => StatusEnum::ENABLED],
  56. ],
  57. 'select' => 'id,mall_id,store_name,shop_mobile,user_id,expire_time'
  58. ]);
  59. if (!empty($store_list)) {
  60. $SmsConfig = new Sms($params['mall_id']);
  61. $easy_sms = $SmsConfig->easySms;
  62. foreach ($store_list as $key => $value) {
  63. //查找门店是否存在过期提醒短信记录
  64. $sms_log = StoreExpireSmsLog::getOne([
  65. ['mall_id' => $params['mall_id']],
  66. ['store_id' => $value['id']],
  67. ['date_time' => date('Ymd', $value['expire_time'])]
  68. ], true, [], 'id desc');
  69. if (!empty($sms_log)) continue;
  70. $message = new ShopExpireMessage($params['mall_id'], $sms_setting['expire_day'], $value['store_name']);
  71. $easy_sms->send($value['shop_mobile'], $message);
  72. //记录发送短信记录
  73. $data = [
  74. 'mall_id' => $params['mall_id'],
  75. 'store_id' => $value['id'],
  76. 'date_time' => date('Ymd', $value['expire_time']),
  77. 'status' => StatusEnum::ENABLED,
  78. ];
  79. $res = StoreExpireSmsLog::setData($data);
  80. if (!$res) var_dump('短信发送记录添加失败,store_id:'. $value['id']);
  81. }
  82. }
  83. return true;
  84. } catch (\Exception $e) {
  85. var_dump($e->getMessage());
  86. throw new Exception($e->getMessage());
  87. }
  88. }
  89. }