commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
/** |
|
3 |
* Created by PhpStorm |
|
4 |
* User: 风哀伤 |
|
5 |
* Date: 2021/1/7 |
|
6 |
* Time: 4:14 下午 |
|
7 |
* @copyright: ©2020 浙江禾匠信息科技 |
|
8 |
* @link: http://www.zjhejiang.com |
|
9 |
*/ |
|
10 |
|
|
11 |
namespace app\jobs; |
|
12 |
|
|
13 |
|
|
14 |
use app\forms\common\message\MessageService; |
|
15 |
use app\forms\common\platform\PlatformConfig; |
|
16 |
use app\forms\common\template\order_pay_template\TailMoneyInfo; |
|
17 |
use app\forms\common\template\TemplateList; |
|
18 |
use app\models\Goods; |
|
19 |
use app\models\GoodsRemind; |
|
20 |
use app\models\Mall; |
|
21 |
use yii\queue\JobInterface; |
|
22 |
|
|
23 |
class RemindJob extends BaseJob implements JobInterface |
|
24 |
{ |
|
25 |
/** |
|
26 |
* @var Goods |
|
27 |
*/ |
|
28 |
public $goods; |
|
29 |
|
|
30 |
/** |
|
31 |
* @var Mall $mall |
|
32 |
*/ |
|
33 |
public $mall; |
|
34 |
|
|
35 |
/** |
|
36 |
* @var integer $user_id |
|
37 |
*/ |
|
38 |
public $user_id; |
|
39 |
public $sell_start_time; |
|
40 |
|
|
41 |
public function execute($queue) |
|
42 |
{ |
|
43 |
try { |
|
44 |
$this->setRequest(); |
|
45 |
$mall = Mall::findOne($this->mall->id); |
|
46 |
\Yii::$app->setMall($mall); |
|
47 |
$isRemindSellTime = \Yii::$app->mall->getMallSettingOne('is_remind_sell_time'); |
|
48 |
if (!$isRemindSellTime) { |
|
49 |
return true; |
|
50 |
} |
|
51 |
$goods = Goods::findOne([ |
|
52 |
'is_delete' => 0, |
|
53 |
'status' => 1, |
|
54 |
'id' => $this->goods->id, |
|
55 |
'mall_id' => \Yii::$app->mall->id |
|
56 |
]); |
|
57 |
if (!$goods) { |
|
58 |
throw new \Exception('商品不存在或已下架'); |
|
59 |
} |
|
60 |
if (strtotime($this->sell_start_time) !== strtotime($goods->sell_begin_time)) { |
|
61 |
throw new \Exception('商品销售时间有更改,此次不做提醒'); |
|
62 |
} |
|
63 |
$remindList = GoodsRemind::find()->with('user') |
|
64 |
->where([ |
|
65 |
'goods_id' => $this->goods->id, 'mall_id' => \Yii::$app->mall->id, 'is_remind' => 1, |
|
66 |
'is_delete' => 0 |
|
67 |
])->all(); |
|
68 |
/** @var GoodsRemind[] $remindList */ |
|
69 |
foreach ($remindList as $remind) { |
|
70 |
if ($this->user_id && $remind->user_id != $this->user_id) { |
|
71 |
continue; |
|
72 |
} |
|
73 |
if ( |
|
74 |
$remind->remind_at |
|
75 |
&& $remind->remind_at != '0000-00-00 00:00:00' |
|
76 |
&& strtotime($remind->remind_at) + 5 * 60 >= time() |
|
77 |
) { |
|
78 |
continue; |
|
79 |
} |
|
80 |
$remind->is_remind = 0; |
|
81 |
$remind->remind_at = mysql_timestamp(); |
|
82 |
if (!$remind->save()) { |
|
83 |
\Yii::warning($remind); |
|
84 |
continue; |
|
85 |
} |
|
86 |
$this->sendTemplate($remind->user); |
|
87 |
$this->sendSmsToUser($remind->user); |
|
88 |
} |
|
89 |
return true; |
|
90 |
} catch (\Exception $exception) { |
|
91 |
\Yii::warning('--商品开售提醒' . $this->goods->id . '--'); |
|
92 |
\Yii::warning($exception); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
protected function sendTemplate($user) |
|
97 |
{ |
|
98 |
try { |
|
99 |
try { |
|
100 |
if ($this->goods->sign !== '') { |
|
101 |
$plugin = \Yii::$app->plugin->getPlugin($this->goods->sign); |
|
102 |
$pageUrl = $plugin->getGoodsUrl($this->goods); |
|
103 |
} else { |
|
104 |
$pageUrl = sprintf("/pages/goods/goods?id=%u", $this->goods->id); |
|
105 |
} |
|
106 |
} catch (\Exception $exception) { |
|
107 |
$pageUrl = sprintf("/pages/goods/goods?id=%u", $this->goods->id); |
|
108 |
} |
|
109 |
TemplateList::getInstance()->getTemplateClass(TailMoneyInfo::TPL_NAME)->send([ |
|
110 |
'desc' => '您设置提醒的商品即将开售,请尽快前往购买', |
|
111 |
'goodsName' => $this->goods->name, |
|
112 |
'price' => $this->goods->price, |
|
113 |
'time' => $this->goods->sell_end_time, |
|
114 |
'user' => $user, |
|
115 |
'page' => ltrim($pageUrl, '/') |
|
116 |
]); |
|
117 |
} catch (\Exception $exception) { |
|
118 |
\Yii::error('模板消息发送: ' . $exception->getMessage()); |
|
119 |
} |
|
120 |
} |
|
121 |
protected function sendSmsToUser($user) |
|
122 |
{ |
|
123 |
try { |
|
124 |
\Yii::warning('----消息发送提醒----'); |
|
125 |
if (!$user->mobile) { |
|
126 |
throw new \Exception('用户未绑定手机号无法发送'); |
|
127 |
} |
|
128 |
$messageService = new MessageService(); |
|
129 |
$messageService->user = $user; |
|
130 |
$name = mb_substr($this->goods->name, 0, 20); |
|
131 |
if (mb_strlen($this->goods->name) > 20) { |
|
132 |
$name = mb_substr($this->goods->name, 0, 17) . '...'; |
|
133 |
} |
|
134 |
$messageService->content = [ |
|
135 |
'mch_id' => 0, |
|
136 |
'args' => [$name] |
|
137 |
]; |
|
138 |
$messageService->platform = PlatformConfig::getInstance()->getPlatform($user); |
|
139 |
$messageService->tplKey = 'tailMoney'; |
|
140 |
$res = $messageService->templateSend(); |
|
141 |
} catch (\Exception $exception) { |
|
142 |
\Yii::error('向用户发送短信消息失败'); |
|
143 |
\Yii::error($exception); |
|
144 |
} |
|
145 |
return $this; |
|
146 |
} |
|
147 |
} |