AttachmentController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use yii\helpers\Json;
  6. use yii\web\UnprocessableEntityHttpException;
  7. use common\enums\StatusEnum;
  8. use common\helpers\ResultHelper;
  9. use addons\Wechat\common\models\Attachment;
  10. use addons\Wechat\merchant\forms\PreviewForm;
  11. use addons\Wechat\merchant\forms\SendForm;
  12. use addons\Wechat\common\enums\AttachmentTypeEnum;
  13. use addons\Wechat\common\enums\AttachmentLinkTypeEnum;
  14. /**
  15. * 资源
  16. *
  17. * Class AttachmentController
  18. * @package addons\Wechat\merchant\controllers
  19. * @author jianyan74 <751393839@qq.com>
  20. */
  21. class AttachmentController extends BaseController
  22. {
  23. /**
  24. * @var Attachment
  25. */
  26. public $modelClass = Attachment::class;
  27. /**
  28. * @return string
  29. */
  30. public function actionIndex()
  31. {
  32. $keywords = Yii::$app->request->get('keywords', '');
  33. $type = Yii::$app->request->get('type', AttachmentTypeEnum::NEWS);
  34. $data = Attachment::find()
  35. ->where(['media_type' => $type, 'status' => StatusEnum::ENABLED])
  36. ->andWhere(['merchant_id' => $this->getMerchantId()])
  37. ->andFilterWhere(['like', 'file_name', $keywords]);
  38. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 15]);
  39. $type == AttachmentTypeEnum::NEWS && $data = $data->with('news');
  40. $models = $data->offset($pages->offset)
  41. ->orderBy('id desc')
  42. ->limit($pages->limit)
  43. ->all();
  44. return $this->render($type, [
  45. 'models' => $models,
  46. 'pages' => $pages,
  47. 'mediaType' => $type,
  48. 'keywords' => $keywords,
  49. 'allMediaType' => AttachmentTypeEnum::getMap(),
  50. ]);
  51. }
  52. /**
  53. * 图文编辑
  54. *
  55. * @return array|string
  56. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  57. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  58. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  59. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  60. * @throws \Psr\SimpleCache\InvalidArgumentException
  61. * @throws \yii\web\UnprocessableEntityHttpException
  62. */
  63. public function actionNewsEdit()
  64. {
  65. $request = Yii::$app->request;
  66. $attach_id = $request->get('attach_id', '');
  67. $attachment = $this->findModel($attach_id);
  68. $attachment->link_type = $request->get('link_type', AttachmentLinkTypeEnum::WECHAT);
  69. $attachment->media_type = AttachmentTypeEnum::NEWS;
  70. if ($request->isAjax) {
  71. // 事务
  72. $transaction = Yii::$app->db->beginTransaction();
  73. try {
  74. $isNewRecord = $attachment->isNewRecord;
  75. $attachment->save();
  76. $list = Json::decode($request->post('list'));
  77. Yii::$app->wechatService->attachment->editNews($attachment, $list, $isNewRecord);
  78. $transaction->commit();
  79. return ResultHelper::json(200, '修改成功');
  80. } catch (\Exception $e) {
  81. $transaction->rollBack();
  82. return ResultHelper::json(422, $e->getMessage());
  83. }
  84. }
  85. return $this->render('news-edit', [
  86. 'attachment' => $attachment,
  87. 'list' => Yii::$app->wechatService->attachmentNews->formattingList($attach_id),
  88. ]);
  89. }
  90. /**
  91. * 创建
  92. *
  93. * @param $type
  94. * @return mixed|string
  95. * @throws \Psr\SimpleCache\InvalidArgumentException
  96. */
  97. public function actionCreate($type)
  98. {
  99. $model = new Attachment;
  100. $model->media_type = $type;
  101. if ($model->load(Yii::$app->request->post())) {
  102. try {
  103. Yii::$app->wechatService->attachment->saveCreate($model);
  104. return $this->message("创建成功", $this->redirect(['index', 'type' => $type]));
  105. } catch (\Exception $e) {
  106. return $this->message($e->getMessage(), $this->redirect(['index', 'type' => $type]), 'error');
  107. }
  108. }
  109. if (Yii::$app->request->isPost && empty($model->local_url)) {
  110. return $this->message("请上传资源文件", $this->redirect(['index', 'type' => $type]), 'error');
  111. }
  112. return $this->renderAjax($type . '-create', [
  113. 'model' => $model
  114. ]);
  115. }
  116. /**
  117. * 删除永久素材
  118. *
  119. * @param $attach_id
  120. * @param $mediaType
  121. * @return mixed
  122. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  123. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  124. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  125. * @throws \Psr\SimpleCache\InvalidArgumentException
  126. * @throws \Throwable
  127. * @throws \yii\db\StaleObjectException
  128. * @throws \yii\web\UnprocessableEntityHttpException
  129. */
  130. public function actionDelete($attach_id, $mediaType)
  131. {
  132. // 删除数据库
  133. $model = $this->findModel($attach_id);
  134. if ($model->delete()) {
  135. // 删除微信服务器数据
  136. if ($model->media_type == AttachmentTypeEnum::NEWS) {
  137. $result = Yii::$app->wechat->app->draft->delete($model['media_id']);
  138. } else {
  139. $result = Yii::$app->wechat->app->material->delete($model['media_id']);
  140. }
  141. if ($error = Yii::$app->services->base->getWechatError($result, false)) {
  142. return $this->message($error, $this->redirect(['index', 'type' => $mediaType]), 'error');
  143. }
  144. return $this->message("删除成功", $this->redirect(['index', 'type' => $mediaType]));
  145. }
  146. return $this->message("删除失败", $this->redirect(['index', 'type' => $mediaType]), 'error');
  147. }
  148. /**
  149. * 手机预览
  150. *
  151. * @param $attach_id
  152. * @param $mediaType
  153. * @return mixed|string
  154. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  155. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  156. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  157. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  158. * @throws \Psr\SimpleCache\InvalidArgumentException
  159. * @throws \yii\web\UnprocessableEntityHttpException
  160. */
  161. public function actionPreview($attach_id, $mediaType)
  162. {
  163. $model = new PreviewForm();
  164. if ($model->load(Yii::$app->request->post())) {
  165. try {
  166. Yii::$app->wechatService->attachment->preview($attach_id, $model->type, $model->content);
  167. return $this->message("发送成功", $this->redirect(['index', 'type' => $mediaType]));
  168. } catch (\Exception $e) {
  169. return $this->message($e->getMessage(), $this->redirect(['index', 'type' => $mediaType]), 'error');
  170. }
  171. }
  172. return $this->renderAjax('preview', [
  173. 'model' => $model,
  174. ]);
  175. }
  176. /**
  177. * 消息群发
  178. *
  179. * @param $attach_id
  180. * @param $mediaType
  181. * @return mixed|string
  182. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  183. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  184. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  185. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  186. * @throws \Psr\SimpleCache\InvalidArgumentException
  187. * @throws \yii\web\UnprocessableEntityHttpException
  188. */
  189. public function actionSend($data, $mediaType)
  190. {
  191. $model = new SendForm();
  192. $model = $model->loadDefaultValues();
  193. $model->$mediaType = $data;
  194. $model->module = $mediaType;
  195. $model->send_time = time();
  196. if ($model->load(Yii::$app->request->post())) {
  197. try {
  198. if (!$model->save()) {
  199. throw new UnprocessableEntityHttpException($this->getError($model));
  200. }
  201. return $this->message('发送成功', $this->redirect(['attachment/index', 'type' => $mediaType]));
  202. } catch (\Exception $e) {
  203. return $this->message($e->getMessage(), $this->redirect(['attachment/index', 'type' => $mediaType]), 'error');
  204. }
  205. }
  206. return $this->renderAjax('send', [
  207. 'model' => $model,
  208. 'tags' => Yii::$app->wechatService->fansTags->getList(),
  209. ]);
  210. }
  211. /**
  212. * 同步
  213. *
  214. * @param $type
  215. * @param int $offset
  216. * @param int $count
  217. * @return array
  218. * @throws \Psr\SimpleCache\InvalidArgumentException
  219. */
  220. public function actionSync($type, $offset = 0, $count = 20)
  221. {
  222. // 查找素材
  223. try {
  224. $res = Yii::$app->wechatService->attachment->sync($type, $offset, $count);
  225. if (is_array($res)) {
  226. return ResultHelper::json(200, '同步成功', $res);
  227. }
  228. return ResultHelper::json(201, '同步完成');
  229. } catch (\Exception $e) {
  230. return ResultHelper::json(404, $e->getMessage());
  231. }
  232. }
  233. /**
  234. * 返回模型
  235. *
  236. * @param $id
  237. * @return \yii\db\ActiveRecord
  238. */
  239. protected function findModel($id)
  240. {
  241. /* @var $model \yii\db\ActiveRecord */
  242. if (empty($id) || empty($model = $this->modelClass::find()
  243. ->where(['id' => $id])
  244. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  245. ->one())
  246. ) {
  247. $model = new $this->modelClass;
  248. return $model->loadDefaultValues();
  249. }
  250. return $model;
  251. }
  252. }
粤ICP备19079148号