*/ class AttachmentController extends BaseController { /** * @var Attachment */ public $modelClass = Attachment::class; /** * @return string */ public function actionIndex() { $keywords = Yii::$app->request->get('keywords', ''); $type = Yii::$app->request->get('type', AttachmentTypeEnum::NEWS); $data = Attachment::find() ->where(['media_type' => $type, 'status' => StatusEnum::ENABLED]) ->andWhere(['merchant_id' => $this->getMerchantId()]) ->andFilterWhere(['like', 'file_name', $keywords]); $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 15]); $type == AttachmentTypeEnum::NEWS && $data = $data->with('news'); $models = $data->offset($pages->offset) ->orderBy('id desc') ->limit($pages->limit) ->all(); return $this->render($type, [ 'models' => $models, 'pages' => $pages, 'mediaType' => $type, 'keywords' => $keywords, 'allMediaType' => AttachmentTypeEnum::getMap(), ]); } /** * 图文编辑 * * @return array|string * @throws \EasyWeChat\Kernel\Exceptions\HttpException * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \yii\web\UnprocessableEntityHttpException */ public function actionNewsEdit() { $request = Yii::$app->request; $attach_id = $request->get('attach_id', ''); $attachment = $this->findModel($attach_id); $attachment->link_type = $request->get('link_type', AttachmentLinkTypeEnum::WECHAT); $attachment->media_type = AttachmentTypeEnum::NEWS; if ($request->isAjax) { // 事务 $transaction = Yii::$app->db->beginTransaction(); try { $isNewRecord = $attachment->isNewRecord; $attachment->save(); $list = Json::decode($request->post('list')); Yii::$app->wechatService->attachment->editNews($attachment, $list, $isNewRecord); $transaction->commit(); return ResultHelper::json(200, '修改成功'); } catch (\Exception $e) { $transaction->rollBack(); return ResultHelper::json(422, $e->getMessage()); } } return $this->render('news-edit', [ 'attachment' => $attachment, 'list' => Yii::$app->wechatService->attachmentNews->formattingList($attach_id), ]); } /** * 创建 * * @param $type * @return mixed|string * @throws \Psr\SimpleCache\InvalidArgumentException */ public function actionCreate($type) { $model = new Attachment; $model->media_type = $type; if ($model->load(Yii::$app->request->post())) { try { Yii::$app->wechatService->attachment->saveCreate($model); return $this->message("创建成功", $this->redirect(['index', 'type' => $type])); } catch (\Exception $e) { return $this->message($e->getMessage(), $this->redirect(['index', 'type' => $type]), 'error'); } } if (Yii::$app->request->isPost && empty($model->local_url)) { return $this->message("请上传资源文件", $this->redirect(['index', 'type' => $type]), 'error'); } return $this->renderAjax($type . '-create', [ 'model' => $model ]); } /** * 删除永久素材 * * @param $attach_id * @param $mediaType * @return mixed * @throws \EasyWeChat\Kernel\Exceptions\HttpException * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \Throwable * @throws \yii\db\StaleObjectException * @throws \yii\web\UnprocessableEntityHttpException */ public function actionDelete($attach_id, $mediaType) { // 删除数据库 $model = $this->findModel($attach_id); if ($model->delete()) { // 删除微信服务器数据 if ($model->media_type == AttachmentTypeEnum::NEWS) { $result = Yii::$app->wechat->app->draft->delete($model['media_id']); } else { $result = Yii::$app->wechat->app->material->delete($model['media_id']); } if ($error = Yii::$app->services->base->getWechatError($result, false)) { return $this->message($error, $this->redirect(['index', 'type' => $mediaType]), 'error'); } return $this->message("删除成功", $this->redirect(['index', 'type' => $mediaType])); } return $this->message("删除失败", $this->redirect(['index', 'type' => $mediaType]), 'error'); } /** * 手机预览 * * @param $attach_id * @param $mediaType * @return mixed|string * @throws \EasyWeChat\Kernel\Exceptions\HttpException * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \yii\web\UnprocessableEntityHttpException */ public function actionPreview($attach_id, $mediaType) { $model = new PreviewForm(); if ($model->load(Yii::$app->request->post())) { try { Yii::$app->wechatService->attachment->preview($attach_id, $model->type, $model->content); return $this->message("发送成功", $this->redirect(['index', 'type' => $mediaType])); } catch (\Exception $e) { return $this->message($e->getMessage(), $this->redirect(['index', 'type' => $mediaType]), 'error'); } } return $this->renderAjax('preview', [ 'model' => $model, ]); } /** * 消息群发 * * @param $attach_id * @param $mediaType * @return mixed|string * @throws \EasyWeChat\Kernel\Exceptions\HttpException * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \yii\web\UnprocessableEntityHttpException */ public function actionSend($data, $mediaType) { $model = new SendForm(); $model = $model->loadDefaultValues(); $model->$mediaType = $data; $model->module = $mediaType; $model->send_time = time(); if ($model->load(Yii::$app->request->post())) { try { if (!$model->save()) { throw new UnprocessableEntityHttpException($this->getError($model)); } return $this->message('发送成功', $this->redirect(['attachment/index', 'type' => $mediaType])); } catch (\Exception $e) { return $this->message($e->getMessage(), $this->redirect(['attachment/index', 'type' => $mediaType]), 'error'); } } return $this->renderAjax('send', [ 'model' => $model, 'tags' => Yii::$app->wechatService->fansTags->getList(), ]); } /** * 同步 * * @param $type * @param int $offset * @param int $count * @return array * @throws \Psr\SimpleCache\InvalidArgumentException */ public function actionSync($type, $offset = 0, $count = 20) { // 查找素材 try { $res = Yii::$app->wechatService->attachment->sync($type, $offset, $count); if (is_array($res)) { return ResultHelper::json(200, '同步成功', $res); } return ResultHelper::json(201, '同步完成'); } catch (\Exception $e) { return ResultHelper::json(404, $e->getMessage()); } } /** * 返回模型 * * @param $id * @return \yii\db\ActiveRecord */ protected function findModel($id) { /* @var $model \yii\db\ActiveRecord */ if (empty($id) || empty($model = $this->modelClass::find() ->where(['id' => $id]) ->andFilterWhere(['merchant_id' => $this->getMerchantId()]) ->one()) ) { $model = new $this->modelClass; return $model->loadDefaultValues(); } return $model; } }