diff --git a/controllers/ShareController.php b/controllers/ShareController.php new file mode 100644 index 0000000..8576e9e --- /dev/null +++ b/controllers/ShareController.php @@ -0,0 +1,146 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ] + ); + } + + /** + * Lists all Share models. + * + * @return string + */ + public function actionIndex() + { + $searchModel = new ShareSearch(); + $dataProvider = $searchModel->search($this->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Share model. + * @param int $share_id Share ID + * @return string + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($share_id) + { + return $this->render('view', [ + 'model' => $this->findModel($share_id), + ]); + } + + /** + * Creates a new Share model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return string|Response + */ + public function actionCreate() + { + $model = new Share(); + + if ($this->request->isPost) { + if ($model->load($this->request->post())) { + $model->sharer_id = Yii::$app->user->id; // 自动设置 sharer_id 为当前用户的 ID + if ($model->save()) { + return $this->redirect(['view', 'share_id' => $model->share_id]); + } + } + } else { + $model->loadDefaultValues(); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing Share model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param int $share_id Share ID + * @return string|Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($share_id) + { + $model = $this->findModel($share_id); + $model->scenario = Share::SCENARIO_UPDATE; // 设置模型为 'update' 场景 + + if ($this->request->isPost) { + $postData = $this->request->post(); + if (isset($postData['Share']['access_code'])) { // 只加载 'access_code' 字段 + $model->access_code = $postData['Share']['access_code']; + if ($model->save()) { + return $this->redirect(['view', 'share_id' => $model->share_id]); + } + } + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing Share model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param int $share_id Share ID + * @return Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($share_id) + { + $this->findModel($share_id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Share model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param int $share_id Share ID + * @return Share the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($share_id) + { + if (($model = Share::findOne(['share_id' => $share_id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/models/Share.php b/models/Share.php new file mode 100644 index 0000000..a2f5dce --- /dev/null +++ b/models/Share.php @@ -0,0 +1,68 @@ + 255], + [['access_code'], 'string', 'max' => 4], + [['sharer_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['sharer_id' => 'id']], + [['access_code'], 'required', 'on' => self::SCENARIO_UPDATE], // 在 'update' 场景中,只验证 'access_code' 字段 + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'share_id' => '分享ID', + 'sharer_id' => '分享者ID', + 'file_relative_path' => '文件位置', + 'access_code' => '访问密码', + 'creation_date' => '分享创建日期', + ]; + } + + /** + * Gets query for [[Sharer]]. + * + * @return \yii\db\ActiveQuery + */ + public function getSharer() + { + return $this->hasOne(User::class, ['id' => 'sharer_id']); + } +} diff --git a/models/ShareSearch.php b/models/ShareSearch.php new file mode 100644 index 0000000..da8818c --- /dev/null +++ b/models/ShareSearch.php @@ -0,0 +1,72 @@ +where(['sharer_id' => Yii::$app->user->id]); + + // add conditions that should always apply here + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + ]); + + $this->load($params); + + 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; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'share_id' => $this->share_id, +// 'sharer_id' => $this->sharer_id, + 'creation_date' => $this->creation_date, + ]); + + $query->andFilterWhere(['like', 'file_relative_path', $this->file_relative_path]) + ->andFilterWhere(['like', 'access_code', $this->access_code]); + + return $dataProvider; + } +} diff --git a/views/layouts/main.php b/views/layouts/main.php index 75296ca..4eeac07 100644 --- a/views/layouts/main.php +++ b/views/layouts/main.php @@ -42,7 +42,7 @@ $this->registerLinkTag(['rel' => 'icon', 'type' => 'image/x-icon', 'href' => Yii 'items' => [ ['label' => '首页', 'url' => ['/site/index']], ['label' => '我的文件', 'url' => ['/home/index']], - ['label' => '分享管理', 'url' => ['/site/about']], + ['label' => '分享管理', 'url' => ['/share/index']], ['label' => '个人设置', 'url' => ['/site/about']], ['label' => '系统设置', 'url' => ['/site/contact']], ['label' => '应用下载', 'url' => ['/site/contact']], diff --git a/views/share/_form.php b/views/share/_form.php new file mode 100644 index 0000000..7fd52ba --- /dev/null +++ b/views/share/_form.php @@ -0,0 +1,32 @@ + + +
\ No newline at end of file diff --git a/views/share/_search.php b/views/share/_search.php new file mode 100644 index 0000000..78a1142 --- /dev/null +++ b/views/share/_search.php @@ -0,0 +1,35 @@ + + + diff --git a/views/share/create.php b/views/share/create.php new file mode 100644 index 0000000..6ce53d3 --- /dev/null +++ b/views/share/create.php @@ -0,0 +1,20 @@ +title = 'Create Share'; +$this->params['breadcrumbs'][] = ['label' => 'Shares', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> + diff --git a/views/share/index.php b/views/share/index.php new file mode 100644 index 0000000..59ad186 --- /dev/null +++ b/views/share/index.php @@ -0,0 +1,47 @@ +title = '分享'; +$this->params['breadcrumbs'][] = $this->title; +?> + diff --git a/views/share/update.php b/views/share/update.php new file mode 100644 index 0000000..5c7c248 --- /dev/null +++ b/views/share/update.php @@ -0,0 +1,21 @@ +title = '更新分享ID' . $model->share_id . '的访问密码'; +$this->params['breadcrumbs'][] = ['label' => 'Shares', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->share_id, 'url' => ['view', 'share_id' => $model->share_id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> + diff --git a/views/share/view.php b/views/share/view.php new file mode 100644 index 0000000..a622de8 --- /dev/null +++ b/views/share/view.php @@ -0,0 +1,40 @@ +title = $model->share_id; +$this->params['breadcrumbs'][] = ['label' => 'Shares', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +