public function actionSetMsg()
 {
     $data = Yii::$app->request->post();
     $phone = User::findOne(['phone' => $data['phone']]);
     $info = CollectInteract::findOne(['userid' => $phone['id'], 'msg' => $data['msg']]);
     if ($info) {
         echo json_encode(array('flag' => 0, 'msg' => 'Already collect!'));
         return;
     }
     $to = Message::findOne(['id' => $data['msg']]);
     $model2 = new Notify();
     $model2->from = $phone['id'];
     $model2->to = $to['userid'];
     $model2->message = '收藏';
     $model2->created_at = time();
     if (!$model2->save()) {
         echo json_encode(array('flag' => 0, 'msg' => 'Collect fail!'));
         return;
     }
     $model = new CollectInteract();
     $model->userid = $phone['id'];
     $model->msg = $data['msg'];
     $model->created_at = time();
     if ($model->save()) {
         echo json_encode(array('flag' => 1, 'msg' => 'Collect success!'));
     } else {
         echo json_encode(array('flag' => 0, 'msg' => 'Collect fail!'));
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Message::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if ($params != false && !empty($params['MessageSearch'])) {
         //$b=$a;
         //=app::find()->where("name= :name",[':name'=>'QQ'])->one();
         //if()
         foreach ($params['MessageSearch'] as $name => $value1) {
             if ($name === 'userid' && $value1 != null) {
                 $appinfo = User::findOne(['phone' => $params['MessageSearch']['userid']]);
                 $this->value = $appinfo['id'];
                 if ($appinfo == null) {
                     $this->userinc = 0;
                 }
             }
         }
     }
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'userid' => $this->value, 'created_at' => $this->created_at]);
     $query->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'kind', $this->kind])->andFilterWhere(['like', 'area', $this->area]);
     return $dataProvider;
 }
 public function actionReply()
 {
     $data = Yii::$app->request->post();
     $user = new User();
     $fromphone = $user->find()->select('id')->where(['phone' => $data['fphone']])->one();
     $model = new Reply();
     if ($data['tphone'] == '') {
         $model->toid = 0;
     } else {
         $tophone = $user->find()->select('id')->where(['phone' => $data['tphone']])->one();
         $model->toid = $tophone['id'];
     }
     $to = Message::findOne(['id' => $data['msgid']]);
     if ($fromphone['id'] != $to['id']) {
         $model3 = new Notify();
         $model3->from = $fromphone['id'];
         $model3->to = $to['userid'];
         $model3->message = '评论';
         $model3->created_at = time();
         if (!$model3->save()) {
             echo json_encode(array('flag' => 0, 'msg' => 'Reply failed!'));
             return;
         }
     }
     $model->fromid = $fromphone['id'];
     $model->msgid = $data['msgid'];
     $model->content = $data['content'];
     $model->isread = 0;
     $model->created_at = time();
     if ($model->save()) {
         echo json_encode(array('flag' => 1, 'msg' => 'Reply success!'));
     } else {
         echo json_encode(array('flag' => 0, 'msg' => 'Reply failed!'));
     }
 }
 /**
  * Finds the Message model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Message the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Message::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function actionGetmsg()
 {
     $data = Yii::$app->request->post();
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $phone = User::findOne(['phone' => $data['phone']]);
     //$data = Message::find ()->select ( 'msg.id' )->join ( 'INNER JOIN', 'friends', ' msg.userid =friends.friendid and msg.userid = :id ', [':id' => Yii::$app->user->id ]);
     $m = new Message();
     $data = $m->find()->where(['userid' => $phone['id']]);
     $pages = new \yii\data\Pagination(['totalCount' => $data->count(), 'pageSize' => '10']);
     $models = $data->orderBy("msg.created_at desc")->offset($pages->offset)->limit($pages->limit)->all();
     $result = array();
     $result['item'] = array();
     foreach ($models as $model) {
         $info = array();
         $infi['basic'] = array();
         $info['basic'] = $model;
         $info['apps'] = (new \yii\db\Query())->select(['app.*'])->from('msgtoapp')->join('INNER JOIN', 'app', 'msgtoapp.appid = app.id and msgtoapp.msgid = :id', [':id' => $model['id']])->all();
         $info['replys'] = (new \yii\db\Query())->select(['reply.*', 'user1.nickname as fromnickname', 'user1.phone as fromphone', 'user2.nickname as tonickname', 'user2.phone as tophone'])->from('reply')->join('INNER JOIN', 'user user1', 'user1.id = reply.fromid and reply.msgid= :id', [':id' => $model['id']])->join('Left JOIN', 'user user2', 'user2.id = reply.toid')->orderBy("reply.created_at")->all();
         $info['zan'] = (new \yii\db\Query())->select('u.phone,u.nickname')->from('zan z')->join('INNER JOIN', 'user u', 'u.id=z.myid and z.msgid=:id', [':id' => $model['id']])->all();
         $result['item'][] = $info;
     }
     $result['_meta'] = array('pageCount' => $pages->pageCount, 'currentPage' => $pages->page + 1);
     return $result;
 }