分享功能的准备工作
This commit is contained in:
parent
e821362b6c
commit
5a8c4f1ac8
146
controllers/ShareController.php
Normal file
146
controllers/ShareController.php
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\controllers;
|
||||||
|
|
||||||
|
use app\models\Share;
|
||||||
|
use app\models\ShareSearch;
|
||||||
|
use Yii;
|
||||||
|
use yii\web\Controller;
|
||||||
|
use yii\web\NotFoundHttpException;
|
||||||
|
use yii\filters\VerbFilter;
|
||||||
|
use yii\web\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ShareController implements the CRUD actions for Share model.
|
||||||
|
*/
|
||||||
|
class ShareController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function behaviors()
|
||||||
|
{
|
||||||
|
return array_merge(
|
||||||
|
parent::behaviors(),
|
||||||
|
[
|
||||||
|
'verbs' => [
|
||||||
|
'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.');
|
||||||
|
}
|
||||||
|
}
|
68
models/Share.php
Normal file
68
models/Share.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\models;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the model class for table "share".
|
||||||
|
*
|
||||||
|
* @property int $share_id 分享ID
|
||||||
|
* @property int $sharer_id 分享者ID
|
||||||
|
* @property string $file_relative_path 文件的相对路径
|
||||||
|
* @property string $access_code 分享密钥
|
||||||
|
* @property string $creation_date 分享创建日期
|
||||||
|
*
|
||||||
|
* @property User $sharer
|
||||||
|
*/
|
||||||
|
class Share extends \yii\db\ActiveRecord
|
||||||
|
{
|
||||||
|
const SCENARIO_UPDATE = 'update';
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function tableName()
|
||||||
|
{
|
||||||
|
return 'share';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[['file_relative_path', 'access_code'], 'required'],
|
||||||
|
[['sharer_id'], 'integer'],
|
||||||
|
[['creation_date'], 'safe'],
|
||||||
|
[['file_relative_path'], 'string', 'max' => 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']);
|
||||||
|
}
|
||||||
|
}
|
72
models/ShareSearch.php
Normal file
72
models/ShareSearch.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\models;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
|
use yii\base\Model;
|
||||||
|
use yii\data\ActiveDataProvider;
|
||||||
|
use app\models\Share;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ShareSearch represents the model behind the search form of `app\models\Share`.
|
||||||
|
*/
|
||||||
|
class ShareSearch extends Share
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[['share_id', 'sharer_id'], 'integer'],
|
||||||
|
[['file_relative_path', 'access_code', 'creation_date'], 'safe'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function scenarios()
|
||||||
|
{
|
||||||
|
// bypass scenarios() implementation in the parent class
|
||||||
|
return Model::scenarios();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates data provider instance with search query applied
|
||||||
|
*
|
||||||
|
* @param array $params
|
||||||
|
*
|
||||||
|
* @return ActiveDataProvider
|
||||||
|
*/
|
||||||
|
public function search($params)
|
||||||
|
{
|
||||||
|
$query = Share::find()->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;
|
||||||
|
}
|
||||||
|
}
|
@ -42,7 +42,7 @@ $this->registerLinkTag(['rel' => 'icon', 'type' => 'image/x-icon', 'href' => Yii
|
|||||||
'items' => [
|
'items' => [
|
||||||
['label' => '首页', 'url' => ['/site/index']],
|
['label' => '首页', 'url' => ['/site/index']],
|
||||||
['label' => '我的文件', 'url' => ['/home/index']],
|
['label' => '我的文件', 'url' => ['/home/index']],
|
||||||
['label' => '分享管理', 'url' => ['/site/about']],
|
['label' => '分享管理', 'url' => ['/share/index']],
|
||||||
['label' => '个人设置', 'url' => ['/site/about']],
|
['label' => '个人设置', 'url' => ['/site/about']],
|
||||||
['label' => '系统设置', 'url' => ['/site/contact']],
|
['label' => '系统设置', 'url' => ['/site/contact']],
|
||||||
['label' => '应用下载', 'url' => ['/site/contact']],
|
['label' => '应用下载', 'url' => ['/site/contact']],
|
||||||
|
32
views/share/_form.php
Normal file
32
views/share/_form.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use yii\helpers\Html;
|
||||||
|
use yii\widgets\ActiveForm;
|
||||||
|
|
||||||
|
/** @var yii\web\View $this */
|
||||||
|
/** @var app\models\Share $model */
|
||||||
|
/** @var yii\widgets\ActiveForm $form */
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="share-form">
|
||||||
|
|
||||||
|
<?php $form = ActiveForm::begin(); ?>
|
||||||
|
|
||||||
|
<?= Html::label($model->getAttributeLabel('sharer_id'), 'sharer_id', ['class' => 'control-label']) ?>
|
||||||
|
<?= Html::tag('p', Html::encode($model->sharer_id), ['class' => 'form-control-static']) ?>
|
||||||
|
|
||||||
|
<?= Html::label($model->getAttributeLabel('file_relative_path'), 'file_relative_path', ['class' => 'control-label']) ?>
|
||||||
|
<?= Html::tag('p', Html::encode($model->file_relative_path), ['class' => 'form-control-static']) ?>
|
||||||
|
|
||||||
|
<?= $form->field($model, 'access_code')->textInput(['maxlength' => 4]) ?>
|
||||||
|
|
||||||
|
<?= Html::label($model->getAttributeLabel('creation_date'), 'creation_date', ['class' => 'control-label']) ?>
|
||||||
|
<?= Html::tag('p', Html::encode($model->creation_date), ['class' => 'form-control-static']) ?>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php ActiveForm::end(); ?>
|
||||||
|
|
||||||
|
</div>
|
35
views/share/_search.php
Normal file
35
views/share/_search.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use yii\helpers\Html;
|
||||||
|
use yii\widgets\ActiveForm;
|
||||||
|
|
||||||
|
/** @var yii\web\View $this */
|
||||||
|
/** @var app\models\ShareSearch $model */
|
||||||
|
/** @var yii\widgets\ActiveForm $form */
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="share-search">
|
||||||
|
|
||||||
|
<?php $form = ActiveForm::begin([
|
||||||
|
'action' => ['index'],
|
||||||
|
'method' => 'get',
|
||||||
|
]); ?>
|
||||||
|
|
||||||
|
<?= $form->field($model, 'share_id') ?>
|
||||||
|
|
||||||
|
<!-- --><?php //= $form->field($model, 'sharer_id') ?>
|
||||||
|
|
||||||
|
<?= $form->field($model, 'file_relative_path') ?>
|
||||||
|
|
||||||
|
<?= $form->field($model, 'access_code') ?>
|
||||||
|
|
||||||
|
<?= $form->field($model, 'creation_date') ?>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
|
||||||
|
<?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php ActiveForm::end(); ?>
|
||||||
|
|
||||||
|
</div>
|
20
views/share/create.php
Normal file
20
views/share/create.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use yii\helpers\Html;
|
||||||
|
|
||||||
|
/** @var yii\web\View $this */
|
||||||
|
/** @var app\models\Share $model */
|
||||||
|
|
||||||
|
$this->title = 'Create Share';
|
||||||
|
$this->params['breadcrumbs'][] = ['label' => 'Shares', 'url' => ['index']];
|
||||||
|
$this->params['breadcrumbs'][] = $this->title;
|
||||||
|
?>
|
||||||
|
<div class="share-create">
|
||||||
|
|
||||||
|
<h1><?= Html::encode($this->title) ?></h1>
|
||||||
|
|
||||||
|
<?= $this->render('_form', [
|
||||||
|
'model' => $model,
|
||||||
|
]) ?>
|
||||||
|
|
||||||
|
</div>
|
47
views/share/index.php
Normal file
47
views/share/index.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use app\models\Share;
|
||||||
|
use yii\helpers\Html;
|
||||||
|
use yii\helpers\Url;
|
||||||
|
use yii\grid\ActionColumn;
|
||||||
|
use yii\grid\GridView;
|
||||||
|
|
||||||
|
/** @var yii\web\View $this */
|
||||||
|
/** @var app\models\ShareSearch $searchModel */
|
||||||
|
/** @var yii\data\ActiveDataProvider $dataProvider */
|
||||||
|
|
||||||
|
$this->title = '分享';
|
||||||
|
$this->params['breadcrumbs'][] = $this->title;
|
||||||
|
?>
|
||||||
|
<div class="share-index">
|
||||||
|
|
||||||
|
<h1><?= Html::encode($this->title) ?></h1>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<?= Html::a('创建分享', ['home/index'], ['class' => 'btn btn-success']) ?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
|
||||||
|
|
||||||
|
<?= GridView::widget([
|
||||||
|
'dataProvider' => $dataProvider,
|
||||||
|
'filterModel' => $searchModel,
|
||||||
|
'columns' => [
|
||||||
|
['class' => 'yii\grid\SerialColumn'],
|
||||||
|
|
||||||
|
'share_id',
|
||||||
|
// 'sharer_id',
|
||||||
|
'file_relative_path',
|
||||||
|
'access_code',
|
||||||
|
'creation_date',
|
||||||
|
[
|
||||||
|
'class' => ActionColumn::className(),
|
||||||
|
'urlCreator' => function ($action, Share $model, $key, $index, $column) {
|
||||||
|
return Url::toRoute([$action, 'share_id' => $model->share_id]);
|
||||||
|
}
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]); ?>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
21
views/share/update.php
Normal file
21
views/share/update.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use yii\helpers\Html;
|
||||||
|
|
||||||
|
/** @var yii\web\View $this */
|
||||||
|
/** @var app\models\Share $model */
|
||||||
|
|
||||||
|
$this->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';
|
||||||
|
?>
|
||||||
|
<div class="share-update">
|
||||||
|
|
||||||
|
<h1><?= Html::encode($this->title) ?></h1>
|
||||||
|
|
||||||
|
<?= $this->render('_form', [
|
||||||
|
'model' => $model,
|
||||||
|
]) ?>
|
||||||
|
|
||||||
|
</div>
|
40
views/share/view.php
Normal file
40
views/share/view.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use yii\helpers\Html;
|
||||||
|
use yii\widgets\DetailView;
|
||||||
|
|
||||||
|
/** @var yii\web\View $this */
|
||||||
|
/** @var app\models\Share $model */
|
||||||
|
|
||||||
|
$this->title = $model->share_id;
|
||||||
|
$this->params['breadcrumbs'][] = ['label' => 'Shares', 'url' => ['index']];
|
||||||
|
$this->params['breadcrumbs'][] = $this->title;
|
||||||
|
\yii\web\YiiAsset::register($this);
|
||||||
|
?>
|
||||||
|
<div class="share-view">
|
||||||
|
|
||||||
|
<h1><?= Html::encode($this->title) ?></h1>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<?= Html::a('Update', ['update', 'share_id' => $model->share_id], ['class' => 'btn btn-primary']) ?>
|
||||||
|
<?= Html::a('Delete', ['delete', 'share_id' => $model->share_id], [
|
||||||
|
'class' => 'btn btn-danger',
|
||||||
|
'data' => [
|
||||||
|
'confirm' => 'Are you sure you want to delete this item?',
|
||||||
|
'method' => 'post',
|
||||||
|
],
|
||||||
|
]) ?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<?= DetailView::widget([
|
||||||
|
'model' => $model,
|
||||||
|
'attributes' => [
|
||||||
|
'share_id',
|
||||||
|
// 'sharer_id',
|
||||||
|
'file_relative_path',
|
||||||
|
'access_code',
|
||||||
|
'creation_date',
|
||||||
|
],
|
||||||
|
]) ?>
|
||||||
|
|
||||||
|
</div>
|
Loading…
Reference in New Issue
Block a user