MassRecordController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use common\traits\MerchantCurd;
  6. use addons\Wechat\common\models\MassRecord;
  7. use common\enums\StatusEnum;
  8. use addons\Wechat\merchant\forms\SendForm;
  9. use yii\web\UnprocessableEntityHttpException;
  10. /**
  11. * 群发消息控制器
  12. *
  13. * Class MassRecordController
  14. * @package addons\Wechat\merchant\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class MassRecordController extends BaseController
  18. {
  19. use MerchantCurd;
  20. /**
  21. * @var MassRecord
  22. */
  23. public $modelClass = MassRecord::class;
  24. /**
  25. * 首页
  26. *
  27. * @return mixed
  28. */
  29. public function actionIndex()
  30. {
  31. $data = MassRecord::find()->where(['merchant_id' => $this->getMerchantId()]);
  32. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  33. $models = $data->offset($pages->offset)
  34. ->orderBy('id desc')
  35. ->limit($pages->limit)
  36. ->all();
  37. return $this->render($this->action->id, [
  38. 'models' => $models,
  39. 'pages' => $pages,
  40. ]);
  41. }
  42. /**
  43. * 编辑/创建
  44. *
  45. * @return mixed|string|\yii\web\Response
  46. * @throws UnprocessableEntityHttpException
  47. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  48. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  49. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  50. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  51. * @throws \Psr\SimpleCache\InvalidArgumentException
  52. * @throws \yii\base\ExitException
  53. */
  54. public function actionEdit()
  55. {
  56. $request = Yii::$app->request;
  57. $id = $request->get('id', null);
  58. $model = $this->findModel($id);
  59. $model->send_status == StatusEnum::DISABLED && $model->send_type = 2;
  60. $this->activeFormValidate($model);
  61. if ($model->load($request->post())) {
  62. try {
  63. if (!$model->save()) {
  64. throw new UnprocessableEntityHttpException($this->getError($model));
  65. }
  66. return $this->redirect(['index']);
  67. } catch (\Exception $e) {
  68. return $this->message($e->getMessage(), $this->redirect(['index']), 'error');
  69. }
  70. }
  71. return $this->render($this->action->id, [
  72. 'model' => $model,
  73. 'tags' => Yii::$app->wechatService->fansTags->getList(),
  74. ]);
  75. }
  76. /**
  77. * @param $id
  78. * @return SendForm|null
  79. */
  80. protected function findModel($id)
  81. {
  82. if (empty($id) || empty(($model = SendForm::findOne($id)))) {
  83. $model = new SendForm();
  84. return $model->loadDefaultValues();
  85. }
  86. return $model;
  87. }
  88. }
粤ICP备19079148号