MessageController.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\Date;
  10. use common\models\Message;
  11. use common\models\MessageText;
  12. class MessageController extends BaseController
  13. {
  14. public $modelClass = Message::class;
  15. /**
  16. * 列表
  17. * @return mixed
  18. * @throws \yii\web\HttpException
  19. */
  20. public function actionList(){
  21. $condition = ' AND MT.IS_DEL=0 AND M.USER_ID=:USER_ID';
  22. $params = [':USER_ID'=>\Yii::$app->user->id];
  23. $data = Message::lists($condition, $params, [
  24. 'select' => 'M.ID,MT.TITLE,MT.CREATED_AT,M.IS_READ',
  25. 'from' => Message::tableName().' AS M',
  26. 'join' => [
  27. ['LEFT JOIN', MessageText::tableName().' AS MT', 'M.TEXT_ID=MT.ID'],
  28. ],
  29. 'orderBy' => 'MT.CREATED_AT DESC',
  30. 'useSlaves' => true,
  31. ]);
  32. return static::notice($data);
  33. }
  34. /**
  35. * 获取站内信详细
  36. * @return mixed
  37. * @throws \yii\web\HttpException
  38. */
  39. public function actionDetail(){
  40. $id = \Yii::$app->request->get('id');
  41. $data = null;
  42. if($id){
  43. $oneMessage = Message::findOne($id);
  44. if($oneMessage){
  45. $data = MessageText::find()->where('ID=:ID AND IS_DEL=0', [':ID'=>$oneMessage['TEXT_ID']])->select('ID,TITLE,CONTENT,CREATED_AT')->asArray()->one();
  46. $data['CONTENT_DATA'] = is_resource($data['CONTENT']) && $data['CONTENT'] ? base64_decode(stream_get_contents($data['CONTENT'])) : '';
  47. //$data['ATTACHMENT'] = json_decode(stripslashes($data['ATTACHMENT']));
  48. $data['ATTACHMENT'] = json_decode('');
  49. $oneMessage->IS_READ = 1;
  50. $oneMessage->READ_AT = Date::nowTime();
  51. $oneMessage->save();
  52. }
  53. }
  54. if($data){
  55. return static::notice($data);
  56. } else {
  57. return static::notice('站内信不存在', 400);
  58. }
  59. }
  60. /**
  61. * 抓取站内信
  62. * @return mixed
  63. * @throws \yii\base\Exception
  64. * @throws \yii\web\HttpException
  65. */
  66. public function actionPull(){
  67. Message::pullMsgByUser(\Yii::$app->user->id);
  68. return static::notice('抓取完成');
  69. }
  70. /**
  71. * 获取未读数量
  72. * @return mixed
  73. * @throws \yii\web\HttpException
  74. */
  75. public function actionUnreadNum(){
  76. return static::notice(Message::unreadNum(\Yii::$app->user->id));
  77. }
  78. /**
  79. * 获取前五条未读信息
  80. * @return mixed
  81. * @throws \yii\web\HttpException
  82. */
  83. public function actionUnreadText(){
  84. // 获取前5条为读站内信
  85. $allData = Message::find()->select('M.ID,MT.TITLE,MT.CREATED_AT')->from(Message::tableName().' AS M')->join('LEFT JOIN', MessageText::tableName().' AS MT', 'M.TEXT_ID=MT.ID')->where('M.USER_ID=:USER_ID AND M.IS_READ=0', [':USER_ID'=>\Yii::$app->user->id])->orderBy('M.CREATED_AT DESC')->limit(5)->asArray()->all();
  86. return static::notice($allData);
  87. }
  88. }