コード例 #1
0
 /**
  * @sendmsg
  */
 public function actionSendmsg()
 {
     //查找我关注的【满足条件才能发邮件】
     $uid = Yii::$app->user->getId();
     $follows = Follow::find()->where(['uid' => $uid])->all();
     if (count($follows) <= 0) {
         Yii::$app->session->setFlash('error', '请您先关注朋友后,再来发消息!');
         return $this->redirect(['index/users']);
     }
     $ids = array();
     foreach ($follows as $v) {
         array_push($ids, $v->fid);
     }
     //发送对象
     $uses = YiiUser::findAll($ids);
     $to = array();
     foreach ($uses as $v) {
         $to[$v->id] = $v->nickname;
     }
     $model = new Msg();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $model->fid = $uid;
         $model->send_time = time();
         if ($model->save()) {
             Yii::$app->session->setFlash('success', '发送成功!');
         } else {
             Yii::$app->session->setFlash('error', '发送失败!');
         }
         //echo "<pre/>";print_r(Yii::$app->request->post());die();
     }
     return $this->render('sendmsg', ['model' => $model, 'to' => $to]);
 }