DefaultController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/8/7
  6. * Time: 下午6:12
  7. */
  8. namespace common\modules\message\frontend\controllers;
  9. use common\modules\message\models\Message;
  10. use common\modules\message\models\MessageForm;
  11. use Yii;
  12. use yii\data\ActiveDataProvider;
  13. use yii\filters\AccessControl;
  14. use yii\web\Controller;
  15. use yii\web\NotFoundHttpException;
  16. class DefaultController extends Controller
  17. {
  18. public function behaviors()
  19. {
  20. return [
  21. 'access' => [
  22. 'class' => AccessControl::className(),
  23. 'rules' => [
  24. [
  25. 'allow' => true,
  26. 'roles' => ['@'],
  27. ],
  28. ],
  29. ],
  30. ];
  31. }
  32. public function actionIndex()
  33. {
  34. $query = Message::find()->where(['to_uid' => Yii::$app->user->id])->innerJoinWith('data')->orderBy('id desc');
  35. $dataProvider = new ActiveDataProvider([
  36. 'query' => $query
  37. ]);
  38. return $this->render('index', [
  39. 'dataProvider' => $dataProvider
  40. ]);
  41. }
  42. public function actionView($id)
  43. {
  44. $userId = Yii::$app->user->id;
  45. $model = Message::find()->where(['id' => $id, 'to_uid' => $userId])->one();
  46. if ($model == null) {
  47. throw new NotFoundHttpException('消息不存在');
  48. }
  49. return $this->render('view', [
  50. 'model' => $model
  51. ]);
  52. }
  53. public function actionCreate($id = null)
  54. {
  55. $model = new MessageForm();
  56. if (!is_null($id)) {
  57. $toUser = Yii::$app->user->identity->findIdentity($id);
  58. $model->toUsername = $toUser->username;
  59. $model->toUid = $id;
  60. }
  61. if ($model->load(Yii::$app->request->post()) && $model->send()) {
  62. Yii::$app->session->setFlash('success', '发送成功');
  63. return $this->redirect(['index']);
  64. }
  65. return $this->render('create', [
  66. 'model' => $model
  67. ]);
  68. }
  69. }