| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace addons\Wechat\merchant\controllers;
- use Yii;
- use yii\data\Pagination;
- use yii\helpers\Json;
- use yii\web\UnprocessableEntityHttpException;
- use common\enums\StatusEnum;
- use common\helpers\ResultHelper;
- use addons\Wechat\common\models\Attachment;
- use addons\Wechat\merchant\forms\PreviewForm;
- use addons\Wechat\merchant\forms\SendForm;
- use addons\Wechat\common\enums\AttachmentTypeEnum;
- use addons\Wechat\common\enums\AttachmentLinkTypeEnum;
- /**
- * 资源
- *
- * Class AttachmentController
- * @package addons\Wechat\merchant\controllers
- * @author jianyan74 <751393839@qq.com>
- */
- 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;
- }
- }
|