setRequest(); $userList = $this->getUserList(); $mall = Mall::findOne(['id' => $this->user->mall_id]); \Yii::$app->setMall($mall); $userListId = []; foreach ($userList as $item) { $userListId[] = $item->id; } $formList = $this->getFormList($userListId); $formIdList = []; $templateRecord = []; foreach ($userList as $item) { $platform = PlatformConfig::getInstance()->getPlatform($item); $platformList = explode('_', $platform); foreach ($platformList as $value) { $this->platform = $value; $templateRecord[] = $this->send($item, $formList, $formIdList); } } if (!empty($formIdList)) { // 模板消息发送成功相应的form_id剩余发放次数减1 Formid::updateAll(['remains' => '`remains`-1'], ['id' => $formIdList]); } if (count($templateRecord) > 0) { // 批量存储发送模板消息数据 \Yii::$app->db->createCommand()->batchInsert( TemplateRecord::tableName(), ['user_id', 'mall_id', 'status', 'data', 'error', 'created_at', 'token'], $templateRecord )->execute(); } } catch (\Exception $e) { \Yii::warning($e); throw $e; } } /** * @return mixed * @throws \Exception */ private function sender() { $plugin = \Yii::$app->plugin->getPlugin($this->platform); return $plugin->templateSender(); } private function getUserList() { /* @var User[] $userList */ if (isset($this->user)) { if (is_array($this->user)) { // 群发模板消息 $userList = $this->user; } else { // 单发模板消息 $userList = [$this->user]; } } else { throw new \Exception('缺少参数user'); } return $userList; } private function getFormList($userListId) { // 获取对应用户可用的form_id $form = Formid::find()->where(['user_id' => $userListId]) ->andWhere(['>=', 'created_at', date('Y-m-d H:i:s', time() - 7 * 86400)]) ->andWhere(['>', 'remains', 0])->orderBy(['created_at' => SORT_ASC])->groupBy('user_id')->all(); $formList = []; /* @var Formid[] $form */ foreach ($form as $item) { $formList[$item->user_id] = $item; } return $formList; } /** * @param User $item * @param $formList * @param $formIdList * @return array */ private function send($item, $formList, &$formIdList) { $data = $this->data; if ($this->tplClass && $this->tplClass instanceof BaseTemplate) { $data = $this->tplClass->adaptive($this->platform); } // 发送模板消息需要的数据 $arg = [ 'user' => $item, 'templateId' => $this->templateId, 'templateTpl' => $this->templateTpl, 'page' => $this->page, 'data' => $data, 'dataKey' => $this->dataKey, 'formId' => '', 'titleStyle' => $this->titleStyle == 2 ? 'keyword1.DATA' : '', ]; // 存储发送模板消息的数据 $newItem = [ 'user_id' => $item->id, 'mall_id' => $item->mall_id, 'status' => 1, 'data' => \Yii::$app->serializer->encode($arg), 'error' => '', 'created_at' => mysql_timestamp(), 'token' => $this->token ]; try { $this->sender = $this->sender(); if ($this->sender->is_need_form_id) { if (!isset($formList[$item->id]) || !$formList[$item->id]) { throw new \Exception('没有有效的form_id'); } $form = $formList[$item->id]; $arg['formId'] = $form->form_id; $formIdList[] = $form->id; } $this->sender->sendTemplate($arg); } catch (\Exception $e) { $newItem['error'] = $e->getMessage(); $newItem['status'] = 0; } return $newItem; } }