Skip to content

Commit

Permalink
增加文档评价体系
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Oct 10, 2020
1 parent 414926a commit a343bb5
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 39 deletions.
125 changes: 90 additions & 35 deletions app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use App\Policies\ProjectPolicy;
use App\Repositories\Document;
use App\Repositories\DocumentHistory;
use App\Repositories\DocumentScore;
use App\Repositories\Project;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use SoapBox\Formatter\Formatter;

Expand Down Expand Up @@ -53,7 +55,7 @@ public function newPage(Request $request, $id)
$this->authorize('page-add', $project);

$type = $request->input('type', 'markdown');
$pid = $request->input('pid', 0);
$pid = $request->input('pid', 0);
return view("doc.{$type}", [
'newPage' => true,
'project' => $project,
Expand Down Expand Up @@ -122,13 +124,13 @@ public function newPageHandle(Request $request, $id)

$this->authorize('page-add', $id);

$pid = $request->input('pid', 0);
$pid = $request->input('pid', 0);
$projectID = $request->input('project_id');
$title = $request->input('title');
$content = $request->input('content');
$type = $request->input('type', 'markdown');
$title = $request->input('title');
$content = $request->input('content');
$type = $request->input('type', 'markdown');
$sortLevel = $request->input('sort_level', 1000);
$syncUrl = $request->input('sync_url');
$syncUrl = $request->input('sync_url');

// 类型如果是表格,则需要检验表格内容是否合法
if ($type === 'table') {
Expand Down Expand Up @@ -209,15 +211,15 @@ public function editPageHandle(Request $request, $id, $page_id)
]
);

$pid = $request->input('pid', 0);
$projectID = $request->input('project_id');
$title = $request->input('title');
$content = $request->input('content');
$pid = $request->input('pid', 0);
$projectID = $request->input('project_id');
$title = $request->input('title');
$content = $request->input('content');
$lastModifiedAt = Carbon::parse($request->input('last_modified_at'));
$history_id = $request->input('history_id');
$forceSave = $request->input('force', false);
$sortLevel = $request->input('sort_level', 1000);
$syncUrl = $request->input('sync_url');
$history_id = $request->input('history_id');
$forceSave = $request->input('force', false);
$sortLevel = $request->input('sort_level', 1000);
$syncUrl = $request->input('sync_url');

/** @var Document $pageItem */
$pageItem = Document::where('id', $page_id)->firstOrFail();
Expand Down Expand Up @@ -251,12 +253,12 @@ public function editPageHandle(Request $request, $id, $page_id)
]);
}

$pageItem->pid = $pid;
$pageItem->pid = $pid;
$pageItem->project_id = $projectID;
$pageItem->title = $title;
$pageItem->content = $content;
$pageItem->title = $title;
$pageItem->content = $content;
$pageItem->sort_level = $sortLevel;
$pageItem->sync_url = $syncUrl;
$pageItem->sync_url = $syncUrl;

// 只有文档内容发生修改才进行保存
if ($pageItem->isDirty()) {
Expand Down Expand Up @@ -425,7 +427,7 @@ public function getJson($id, $page_id)
if (isJson($yaml)) {
$jsonContent = $yaml;
} else {
$formatter = Formatter::make($yaml, Formatter::YAML);
$formatter = Formatter::make($yaml, Formatter::YAML);
$jsonContent = $formatter->toJson();
}

Expand All @@ -452,8 +454,8 @@ private function getSwaggerContent($id, $page_id): string
}

$page = Document::where('project_id', $id)
->where('id', $page_id)
->firstOrFail();
->where('id', $page_id)
->firstOrFail();
if ($page->type != Document::TYPE_SWAGGER) {
abort(422, '该文档不是Swagger文档');
}
Expand All @@ -473,7 +475,7 @@ public function readMode($id, $page_id)
{
/** @var Project $project */
$project = Project::query()->findOrFail($id);
$policy = new ProjectPolicy();
$policy = new ProjectPolicy();
if (!$policy->view(\Auth::user(), $project)) {
abort(403, '您没有访问该项目的权限');
}
Expand All @@ -496,24 +498,22 @@ public function readMode($id, $page_id)
*
* @param $id
* @param $page_id
*
* @return array
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Exception
*/
public function syncFromRemote($id, $page_id)
{
/** @var Document $pageItem */
$pageItem = Document::where('id', $page_id)
->where('project_id', $id)
->firstOrFail();
->where('project_id', $id)
->firstOrFail();

$this->authorize('page-edit', $pageItem);

$synced = false;
if (!empty($pageItem->sync_url)) {
$client = new \GuzzleHttp\Client();
$resp = $client->get($pageItem->sync_url);
$client = new \GuzzleHttp\Client();
$resp = $client->get($pageItem->sync_url);
$respCode = $resp->getStatusCode();
$respBody = $resp->getBody()->getContents();

Expand All @@ -533,7 +533,7 @@ public function syncFromRemote($id, $page_id)
// 只有文档内容发生修改才进行保存
if ($pageItem->isDirty()) {
$pageItem->last_modified_uid = \Auth::user()->id;
$pageItem->last_sync_at = Carbon::now();
$pageItem->last_sync_at = Carbon::now();

$pageItem->save();

Expand All @@ -555,6 +555,16 @@ public function syncFromRemote($id, $page_id)
return redirect(wzRoute('project:home', ['id' => $id, 'p' => $page_id]));
}

/**
* 文档移动
*
* @param Request $request
* @param $project_id
* @param $page_id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Illuminate\Validation\ValidationException
*/
public function move(Request $request, $project_id, $page_id)
{
$this->validate(
Expand All @@ -572,7 +582,7 @@ public function move(Request $request, $project_id, $page_id)

// 检查目标项目权限
$targetProjectId = $request->input('target_project_id', 0);
$targetPageId = $request->input('target_page_id', 0);
$targetPageId = $request->input('target_page_id', 0);

/** @var Project $targetProject */
$targetProject = Project::where('id', $targetProjectId)->firstOrFail();
Expand All @@ -594,7 +604,7 @@ public function move(Request $request, $project_id, $page_id)
DB::transaction(function () use ($pageItem, $targetProject, $targetPage, $navigators) {
// 修改当前页面的pid和project_id
$pageItem->project_id = $targetProject->id;
$pageItem->pid = $targetPage->id ?? 0;
$pageItem->pid = $targetPage->id ?? 0;

$pageItem->save();

Expand Down Expand Up @@ -622,10 +632,55 @@ function ($id, array $parents) use ($targetProject) {
));
}

/**
* 更新文档评价
*
* @param Request $request
* @param $id
* @param $page_id
* @return array
* @throws \Illuminate\Validation\ValidationException
*/
public function updateDocumentScore(Request $request, $id, $page_id)
{
$this->validate($request, [
'score_type' => 'required|in:1,2,3',
]);

$policy = new ProjectPolicy();
if (!$policy->view(\Auth::user(), Project::where('id', $id)->firstOrFail())) {
abort(403, '您没有访问该项目的权限');
}

/** @var Document $pageItem */
$pageItem = Document::where('id', $page_id)->where('project_id', $id)->firstOrFail();
$scoreType = (int)$request->input('score_type');

/** @var DocumentScore $existedScore */
$existedScore = DocumentScore::where('page_id', $pageItem->id)->where('user_id', Auth::user()->id)->first();
if ($existedScore) {
if ($existedScore->score_type == $scoreType) {
$existedScore->delete();
} else {
$existedScore->score_type = $scoreType;
$existedScore->save();
}
} else {
DocumentScore::create([
'user_id' => Auth::user()->id,
'page_id' => $pageItem->id,
'score_type' => $scoreType,
]);
}

$this->alertSuccess('操作成功');
return [];
}

/**
* 过滤要导出的文档
*
* @param array $navigators
* @param array $navigators
* @param \Closure $filter
*
* @return array|mixed
Expand All @@ -644,9 +699,9 @@ private function filterNavigators(array $navigators, \Closure $filter)
/**
* 遍历所有目录
*
* @param array $navigators
* @param array $navigators
* @param \Closure $callback
* @param array $parents
* @param array $parents
*/
private function traverseNavigators(array $navigators, \Closure $callback, array $parents = [])
{
Expand Down
16 changes: 16 additions & 0 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Policies\ProjectPolicy;
use App\Repositories\Catalog;
use App\Repositories\Document;
use App\Repositories\DocumentScore;
use App\Repositories\Group;
use App\Repositories\OperationLogs;
use App\Repositories\PageShare;
Expand All @@ -24,6 +25,8 @@
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Translation\Exception\NotFoundResourceException;

class ProjectController extends Controller
Expand Down Expand Up @@ -198,6 +201,17 @@ public function project(Request $request, $id)
$share = PageShare::where('page_id', $page->id)
->where('project_id', $page->project_id)
->first();
// 文档评分
$scores = DocumentScore::select(DB::raw('score_type, count(*) as count'))
->where('page_id', $page->id)
->groupBy('score_type')
->get()
->mapWithKeys(function ($item) {
return [$item['score_type'] => $item['count']];
});
if (!Auth::guest()) {
$userScoreType = DocumentScore::where('page_id', $page->id)->where('user_id', Auth::user()->id)->value('score_type');
}
} else {
// 查询操作历史
$operationLogs = OperationLogs::where('project_id', $id)
Expand All @@ -210,6 +224,8 @@ public function project(Request $request, $id)
'project' => $project,
'pageID' => $pageID,
'pageItem' => $page,
'scores' => $scores ?? [],
'user_score_type' => $userScoreType ?? 0,
'type' => $type,
'code' => '',
'operationLogs' => isset($operationLogs) ? $operationLogs : [],
Expand Down
35 changes: 35 additions & 0 deletions app/Repositories/DocumentScore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Wizard
*
* @link https://aicode.cc/
* @copyright 管宜尧 <[email protected]>
*/

namespace App\Repositories;

/**
* 文档评价模型
*
* @property integer $id
* @property integer $page_id
* @property integer $user_id
* @property integer $score_type
*
* @package App\Repositories
*/
class DocumentScore extends Repository
{
const SCORE_USEFUL = 1;
const SCORE_HARD_TO_READ = 2;
const SCORE_NO_USE = 3;
const SCORE_GARBAGE = 4;

protected $table = 'wz_page_score';
protected $fillable
= [
'page_id',
'user_id',
'score_type',
];
}
35 changes: 35 additions & 0 deletions database/migrations/2020_10_10_180119_create_page_score.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePageScore extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('wz_page_score', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('page_id')->comment('页面 ID');
$table->unsignedInteger('user_id')->comment('用户 ID');
$table->tinyInteger('score_type', false, true)->comment('评分类型:');
$table->index('page_id', 'idx_page_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('wz_page_score');
}
}
15 changes: 14 additions & 1 deletion public/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ pre.prettyprint *,
}

.markdown-body .wz-sql-create {
border: 1px dashed #e6e6e6;
/*border: 1px dashed #e6e6e6;*/
box-shadow: none;
}

Expand Down Expand Up @@ -1453,4 +1453,17 @@ pre.prettyprint *,
.viewer-container img {
background: url(/assets/white-bg.jpg);
background-size: 30px 30px;
}

.wz-score-box {
margin: 10px 0;
padding-top: 20px;
}
.wz-score-box .wz-score-opt {
display: inline-block;
margin-right: 30px;
}
.wz-score-box .wz-score-opt p {
color: #989898;
font-size: 90%;
}
Loading

0 comments on commit a343bb5

Please sign in to comment.