commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
/** |
|
3 |
* link: http://www.zjhejiang.com/ |
|
4 |
* copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司 |
|
5 |
* author: xay |
|
6 |
*/ |
|
7 |
|
|
8 |
namespace app\jobs; |
|
9 |
|
|
10 |
use app\forms\common\CommonOption; |
|
11 |
use app\forms\common\mptemplate\MpTplMessage; |
|
12 |
use app\models\MpTemplateRecord; |
|
13 |
use app\models\Option; |
|
14 |
use yii\base\BaseObject; |
|
15 |
use yii\queue\JobInterface; |
|
16 |
|
|
17 |
class MpTplMessageJob extends BaseJob implements JobInterface |
|
18 |
{ |
|
19 |
public $mall; |
|
20 |
public $token; |
|
21 |
public $url; |
|
22 |
public $templateId; |
|
23 |
public $data; |
|
24 |
public $miniprogram; |
|
25 |
|
|
26 |
public $admin_open_list = []; |
|
27 |
public $app_id; |
|
28 |
public $app_secret; |
|
29 |
/** |
|
30 |
* @param \yii\queue\Queue $queue |
|
31 |
* @throws \Exception |
|
32 |
*/ |
|
33 |
public function execute($queue) |
|
34 |
{ |
|
35 |
try { |
|
36 |
$this->setRequest(); |
|
37 |
$logs = []; |
|
38 |
$count = 0; |
|
39 |
|
|
40 |
foreach ($this->admin_open_list as $item) { |
|
41 |
//发送数据 |
|
42 |
$args = [ |
|
43 |
'touser' => $item['open_id'], |
|
44 |
'template_id' => $this->templateId, |
|
45 |
'data' => $this->data, |
|
46 |
]; |
|
47 |
|
|
48 |
$log = [ |
|
49 |
'open_id' => $args['touser'], |
|
50 |
'mall_id' => $this->mall->id, |
|
51 |
'status' => 1, |
|
52 |
'data' => \Yii::$app->serializer->encode($args), |
|
53 |
'error' => '', |
|
54 |
'created_at' => mysql_timestamp(), |
|
55 |
'token' => $this->token, |
|
56 |
]; |
|
57 |
|
|
58 |
try { |
|
59 |
$res = (new MpTplMessage())->senderMsg($args, [ |
|
60 |
'app_id' => $this->app_id, |
|
61 |
'app_secret' => $this->app_secret, |
|
62 |
'mall_id' => $this->mall->id, |
|
63 |
]); |
|
64 |
} catch (\Exception $e) { |
|
65 |
\Yii::error($e); |
|
66 |
$log['error'] = $e->getMessage(); |
|
67 |
$log['status'] = 0; |
|
68 |
} |
|
69 |
$count++; |
|
70 |
$logs[] = $log; |
|
71 |
} |
|
72 |
|
|
73 |
if ($count > 0) { |
|
74 |
\Yii::$app->db->createCommand()->batchInsert( |
|
75 |
MpTemplateRecord::tableName(), |
|
76 |
['open_id', 'mall_id', 'status', 'data', 'error', 'created_at', 'token'], |
|
77 |
$logs |
|
78 |
)->execute(); |
|
79 |
} |
|
80 |
} catch (\Exception $e) { |
|
81 |
\Yii::warning($e); |
|
82 |
throw $e; |
|
83 |
} |
|
84 |
} |
|
85 |
} |