zm
2021-04-01 802c31297a1ae5b8c2e8fb5c62e9790dfc5d7583
commit | author | age
90c639 1 <?php
Z 2 /**
3  * Created by PhpStorm.
4  * User: 风哀伤
5  * Date: 2019/2/20
6  * Time: 14:11
7  * @copyright: ©2019 浙江禾匠信息科技
8  * @link: http://www.zjhejiang.com
9  */
10
11 namespace app\jobs;
12
13
14 use app\core\response\ApiCode;
15 use app\forms\common\platform\PlatformConfig;
16 use app\forms\common\template\TemplateSender;
17 use app\forms\common\template\tplmsg\BaseTemplate;
18 use app\models\Formid;
19 use app\models\Mall;
20 use app\models\TemplateRecord;
21 use app\models\User;
22 use app\models\UserInfo;
23 use yii\base\BaseObject;
24 use yii\queue\JobInterface;
25
26 class TemplateSendJob extends BaseJob implements JobInterface
27 {
28     public $user;
29     public $templateTpl;
30     public $templateId;
31     public $page;
32     public $data;
33     public $token;
34     public $titleStyle;
35     public $dataKey;
36     public $tplClass;
37
38     /* @var TemplateSender */
39     public $sender;
40     private $platform;
41
42     /**
43      * @param \yii\queue\Queue $queue
44      * @throws \Exception
45      */
46     public function execute($queue)
47     {
48         try {
49             \Yii::warning('---订阅消息发送提醒---');
50             $this->setRequest();
51             $userList = $this->getUserList();
52             $mall = Mall::findOne(['id' => $this->user->mall_id]);
53             \Yii::$app->setMall($mall);
54             $userListId = [];
55             foreach ($userList as $item) {
56                 $userListId[] = $item->id;
57             }
58             $formList = $this->getFormList($userListId);
59
60             $formIdList = [];
61             $templateRecord = [];
62             foreach ($userList as $item) {
63                 $platform = PlatformConfig::getInstance()->getPlatform($item);
64                 $platformList = explode('_', $platform);
65                 foreach ($platformList as $value) {
66                     $this->platform = $value;
67                     $templateRecord[] = $this->send($item, $formList, $formIdList);
68                 }
69             }
70             if (!empty($formIdList)) {
71                 // 模板消息发送成功相应的form_id剩余发放次数减1
72                 Formid::updateAll(['remains' => '`remains`-1'], ['id' => $formIdList]);
73             }
74             if (count($templateRecord) > 0) {
75                 // 批量存储发送模板消息数据
76                 \Yii::$app->db->createCommand()->batchInsert(
77                     TemplateRecord::tableName(),
78                     ['user_id', 'mall_id', 'status', 'data', 'error', 'created_at', 'token'],
79                     $templateRecord
80                 )->execute();
81             }
82         } catch (\Exception $e) {
83             \Yii::warning($e);
84             throw $e;
85         }
86     }
87
88     /**
89      * @return mixed
90      * @throws \Exception
91      */
92     private function sender()
93     {
94         $plugin = \Yii::$app->plugin->getPlugin($this->platform);
95         return $plugin->templateSender();
96     }
97
98     private function getUserList()
99     {
100         /* @var User[] $userList */
101         if (isset($this->user)) {
102             if (is_array($this->user)) {
103                 // 群发模板消息
104                 $userList = $this->user;
105             } else {
106                 // 单发模板消息
107                 $userList = [$this->user];
108             }
109         } else {
110             throw new \Exception('缺少参数user');
111         }
112         return $userList;
113     }
114
115     private function getFormList($userListId)
116     {
117         // 获取对应用户可用的form_id
118         $form = Formid::find()->where(['user_id' => $userListId])
119             ->andWhere(['>=', 'created_at', date('Y-m-d H:i:s', time() - 7 * 86400)])
120             ->andWhere(['>', 'remains', 0])->orderBy(['created_at' => SORT_ASC])->groupBy('user_id')->all();
121
122         $formList = [];
123         /* @var Formid[] $form */
124         foreach ($form as $item) {
125             $formList[$item->user_id] = $item;
126         }
127         return $formList;
128     }
129
130     /**
131      * @param User $item
132      * @param $formList
133      * @param $formIdList
134      * @return array
135      */
136     private function send($item, $formList, &$formIdList)
137     {
138         $data = $this->data;
139         if ($this->tplClass && $this->tplClass instanceof BaseTemplate) {
140             $data = $this->tplClass->adaptive($this->platform);
141         }
142         // 发送模板消息需要的数据
143         $arg = [
144             'user' => $item,
145             'templateId' => $this->templateId,
146             'templateTpl' => $this->templateTpl,
147             'page' => $this->page,
148             'data' => $data,
149             'dataKey' => $this->dataKey,
150             'formId' => '',
151             'titleStyle' => $this->titleStyle == 2 ? 'keyword1.DATA' : '',
152         ];
153
154         // 存储发送模板消息的数据
155         $newItem = [
156             'user_id' => $item->id,
157             'mall_id' => $item->mall_id,
158             'status' => 1,
159             'data' => \Yii::$app->serializer->encode($arg),
160             'error' => '',
161             'created_at' => mysql_timestamp(),
162             'token' => $this->token
163         ];
164         try {
165             $this->sender = $this->sender();
166             if ($this->sender->is_need_form_id) {
167                 if (!isset($formList[$item->id]) || !$formList[$item->id]) {
168                     throw new \Exception('没有有效的form_id');
169                 }
170                 $form = $formList[$item->id];
171                 $arg['formId'] = $form->form_id;
172                 $formIdList[] = $form->id;
173             }
174             $this->sender->sendTemplate($arg);
175         } catch (\Exception $e) {
176             $newItem['error'] = $e->getMessage();
177             $newItem['status'] = 0;
178         }
179         return $newItem;
180     }
181 }