perf: 添加任务模板

This commit is contained in:
kuaifan 2024-12-01 19:56:46 +08:00
parent 94cefe52dd
commit e78d850138
7 changed files with 629 additions and 0 deletions

View File

@ -39,6 +39,7 @@ use App\Module\BillMultipleExport;
use Illuminate\Support\Facades\DB;
use App\Models\ProjectTaskFlowChange;
use App\Models\ProjectTaskVisibilityUser;
use App\Models\ProjectTaskTemplate;
/**
* @apiDefine project
@ -2693,4 +2694,170 @@ class ProjectController extends AbstractController
$projectPermission = ProjectPermission::updatePermissions($projectId, Base::newArrayRecursive('intval', $permissions));
return Base::retSuccess("success", $projectPermission);
}
/**
* @api {get} api/project/task/template_list 47. 任务模板列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__template_list
*
* @apiParam {Number} project_id 项目ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function task__template_list()
{
$user = User::auth();
$projectId = intval(Request::input('project_id'));
if (!$projectId) {
return Base::retError('缺少参数project_id');
}
$project = Project::userProject($projectId);
if (!$project) {
return Base::retError('项目不存在或已被删除');
}
$templates = ProjectTaskTemplate::where('project_id', $projectId)
->orderBy('sort')
->orderByDesc('id')
->get();
return Base::retSuccess('success', $templates);
}
/**
* @api {post} api/project/task/template_save 48. 保存任务模板
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__template_save
*
* @apiParam {Number} project_id 项目ID
* @apiParam {Number} [id] 模板ID
* @apiParam {String} name 模板名称
* @apiParam {String} title 任务标题
* @apiParam {String} content 任务内容
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function task__template_save()
{
$user = User::auth();
//
$projectId = intval(Request::input('project_id'));
if (!$projectId) {
return Base::retError('缺少参数project_id');
}
Project::userProject($projectId, true, true);
//
$id = intval(Request::input('id', 0));
$name = trim(Request::input('name', ''));
$title = trim(Request::input('title', ''));
$content = trim(Request::input('content', ''));
if (empty($name)) {
return Base::retError('请输入模板名称');
}
if (empty($title)) {
return Base::retError('请输入任务标题');
}
$data = [
'project_id' => $projectId,
'name' => $name,
'title' => $title,
'content' => $content,
'userid' => $user->userid
];
if ($id > 0) {
$template = ProjectTaskTemplate::where('id', $id)
->where('project_id', $projectId)
->first();
if (!$template) {
return Base::retError('模板不存在或已被删除');
}
$template->update($data);
} else {
$templateCount = ProjectTaskTemplate::where('project_id', $projectId)->count();
if ($templateCount >= 20) {
return Base::retError('每个项目最多添加10个模板');
}
$data['sort'] = ProjectTaskTemplate::where('project_id', $projectId)->max('sort') + 1;
$template = ProjectTaskTemplate::create($data);
}
return Base::retSuccess('保存成功', $template);
}
/**
* @api {get} api/project/task/template_delete 49. 删除任务模板
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__template_delete
*
* @apiParam {Number} id 模板ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function task__template_delete()
{
User::auth();
//
$id = intval(Request::input('id'));
if (!$id) {
return Base::retError('缺少参数id');
}
$template = ProjectTaskTemplate::find($id);
if (!$template) {
return Base::retError('模板不存在或已被删除');
}
Project::userProject($template->project_id, true, true);
$template->delete();
return Base::retSuccess('删除成功');
}
/**
* @api {get} api/project/task/template_default 50. 设置任务模板为默认
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__template_default
*
* @apiParam {Number} id 模板ID
* @apiParam {Number} project_id 项目ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function task__template_default()
{
User::auth();
//
$id = intval(Request::input('id'));
$projectId = intval(Request::input('project_id'));
if (!$id || !$projectId) {
return Base::retError('参数错误');
}
Project::userProject($projectId, true, true);
//
$template = ProjectTaskTemplate::where('id', $id)
->where('project_id', $projectId)
->first();
if (!$template) {
return Base::retError('模板不存在或已被删除');
}
// 先将所有模板设为非默认
ProjectTaskTemplate::where('project_id', $projectId)->update(['is_default' => false]);
// 设置当前模板为默认
$template->update(['is_default' => true]);
return Base::retSuccess('设置成功');
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace App\Models;
/**
* App\Models\ProjectTaskTemplate
*
* @property int $id
* @property int $project_id 项目ID
* @property string $name 模板名称
* @property string|null $title 任务标题
* @property string|null $content 任务内容
* @property int $sort 排序
* @property bool $is_default 是否默认模板
* @property int $userid 创建人
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\Project|null $project
* @property-read \App\Models\User|null $user
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereProjectId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereContent($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereSort($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereIsDefault($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereUserid($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereUpdatedAt($value)
* @mixin \Eloquent
*/
class ProjectTaskTemplate extends AbstractModel
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'project_id',
'name',
'title',
'content',
'sort',
'is_default',
'userid'
];
/**
* 关联项目
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function project()
{
return $this->belongsTo(Project::class);
}
/**
* 关联创建者
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'userid');
}
}

View File

@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProjectTaskTemplatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable('project_task_templates')) {
Schema::create('project_task_templates', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('project_id')->index()->comment('项目ID');
$table->string('name', 100)->comment('模板名称');
$table->string('title', 255)->nullable()->comment('任务标题');
$table->text('content')->nullable()->comment('任务内容');
$table->unsignedTinyInteger('sort')->default(0)->comment('排序');
$table->boolean('is_default')->default(false)->comment('是否默认模板');
$table->unsignedBigInteger('userid')->index()->comment('创建人');
$table->timestamps();
// 外键约束
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
$table->foreign('userid')->references('userid')->on('users');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('project_task_templates');
}
}

View File

@ -53,6 +53,7 @@
<EDropdownMenu v-if="projectData.owner_userid === userId" slot="dropdown">
<EDropdownItem command="setting">{{$L('项目设置')}}</EDropdownItem>
<EDropdownItem command="permissions">{{$L('权限设置')}}</EDropdownItem>
<EDropdownItem command="task_template">{{$L('任务模板')}}</EDropdownItem>
<EDropdownItem command="workflow">{{$L('工作流设置')}}</EDropdownItem>
<EDropdownItem command="user" divided>{{$L('成员管理')}}</EDropdownItem>
<EDropdownItem command="invite">{{$L('邀请链接')}}</EDropdownItem>
@ -452,6 +453,14 @@
</div>
</Modal>
<!--任务模板-->
<DrawerOverlay
v-model="taskTemplateShow"
placement="right"
:size="720">
<ProjectTaskTemplate ref="taskTemplate" v-if="taskTemplateShow" :project-id="projectId"/>
</DrawerOverlay>
<!--工作流程设置-->
<DrawerOverlay
v-model="workflowShow"
@ -497,6 +506,7 @@ import TaskRow from "./TaskRow";
import TaskArchived from "./TaskArchived";
import ProjectLog from "./ProjectLog";
import DrawerOverlay from "../../../components/DrawerOverlay";
import ProjectTaskTemplate from "./ProjectTaskTemplate";
import ProjectWorkflow from "./ProjectWorkflow";
import ProjectPermission from "./ProjectPermission";
import TaskMenu from "./TaskMenu";
@ -513,6 +523,7 @@ export default {
UserAvatarTip,
UserSelect,
TaskMenu,
ProjectTaskTemplate,
ProjectWorkflow,
ProjectPermission,
DrawerOverlay,
@ -559,6 +570,8 @@ export default {
transferData: {},
transferLoad: 0,
taskTemplateShow: false,
workflowShow: false,
logShow: false,
archivedTaskShow: false,
@ -1319,6 +1332,10 @@ export default {
this.inviteGet()
break;
case "task_template":
this.taskTemplateShow = true;
break;
case "workflow":
this.workflowShow = true;
break;

View File

@ -0,0 +1,223 @@
<template>
<div class="project-task-template">
<div class="header">
<div class="title">{{$L('任务模板')}}</div>
<div class="actions">
<Button type="primary" icon="md-add" @click="handleAdd">
{{$L('新建模板')}}
</Button>
</div>
</div>
<div class="content">
<div v-if="!templates.length" class="empty">
<div class="empty-text">{{$L('暂无任务模板')}}</div>
</div>
<div v-else class="template-list">
<div v-for="item in templates" :key="item.id" class="template-item">
<div class="template-title">
<span>{{ item.name }}</span>
<span v-if="item.is_default" class="default-tag">{{$L('默认')}}</span>
</div>
<div class="template-content">
<div class="task-title">{{ item.title }}</div>
<div class="task-content">
<VMPreviewNostyle ref="descPreview" :value="item.content"/>
</div>
</div>
<div class="template-actions">
<Button @click="handleSetDefault(item)" type="primary" :icon="item.is_default ? 'md-checkmark' : ''">
{{$L('设为默认')}}
</Button>
<Button @click="handleEdit(item)" type="primary">
{{$L('编辑')}}
</Button>
<Button @click="handleDelete(item)" type="error">
{{$L('删除')}}
</Button>
</div>
</div>
</div>
</div>
<!-- 编辑模板弹窗 -->
<Modal
v-model="showEditModal"
:title="editingTemplate.id ? $L('编辑模板') : $L('新建模板')"
:mask-closable="false">
<Form
ref="editForm"
:model="editingTemplate"
:rules="formRules"
v-bind="formOptions"
@submit.native.prevent>
<FormItem prop="name" :label="$L('模板名称')">
<Input ref="templateName" v-model="editingTemplate.name" :placeholder="$L('请输入模板名称')"/>
</FormItem>
<FormItem prop="title" :label="$L('任务标题')">
<Input v-model="editingTemplate.title" :placeholder="$L('请输入任务标题')"/>
</FormItem>
<FormItem prop="content" :label="$L('任务内容')">
<Input
type="textarea"
v-model="editingTemplate.content"
:placeholder="$L('请输入任务内容')"
:autosize="{ minRows: 4, maxRows: 12 }"/>
</FormItem>
</Form>
<div slot="footer" class="adaption">
<Button type="default" @click="showEditModal=false">{{$L('取消')}}</Button>
<Button type="primary" :loading="loading" @click="handleSave">{{$L('保存')}}</Button>
</div>
</Modal>
</div>
</template>
<script>
import {mapState} from 'vuex'
import VMPreviewNostyle from "../../../components/VMEditor/nostyle.vue";
export default {
name: 'ProjectTaskTemplate',
components: {VMPreviewNostyle},
props: {
projectId: {
type: [Number, String],
required: true
}
},
data() {
return {
loading: false,
templates: [],
showEditModal: false,
editingTemplate: this.getEmptyTemplate(),
formRules: {
name: [
{ required: true, message: this.$L('请输入模板名称'), trigger: 'blur' }
],
title: [
{ required: true, message: this.$L('请输入任务标题'), trigger: 'blur' }
]
}
}
},
computed: {
...mapState(['formOptions'])
},
created() {
this.loadTemplates()
},
methods: {
//
getEmptyTemplate() {
return {
id: null,
project_id: this.projectId,
name: '',
title: '',
content: '',
is_default: false
}
},
//
async loadTemplates() {
this.loading = true
try {
const {data} = await this.$store.dispatch("call", {
url: 'project/task/template_list',
data: {
project_id: this.projectId
},
spinner: 300
})
this.templates = data || []
} catch ({msg}) {
this.$Message.error(msg || this.$L('加载模板失败'))
}
this.loading = false
},
//
handleAdd() {
this.editingTemplate = this.getEmptyTemplate()
this.showEditModal = true
},
//
handleEdit(template) {
this.editingTemplate = { ...template }
this.showEditModal = true
},
//
async handleSave() {
if (!this.editingTemplate.name) {
this.$Message.warning(this.$L('请输入模板名称'))
return
}
try {
await this.$store.dispatch("call", {
url: 'project/task/template_save',
data: this.editingTemplate,
method: 'post',
spinner: 300
})
this.$Message.success(this.$L('保存成功'))
this.showEditModal = false
this.loadTemplates()
} catch ({msg}) {
this.$Message.error(msg || this.$L('保存失败'))
}
},
//
async handleDelete(template) {
this.$Modal.confirm({
title: this.$L('确认删除'),
content: this.$L('确定要删除该模板吗?'),
onOk: async () => {
try {
await this.$store.dispatch("call", {
url: 'project/task/template_delete',
data: {
id: template.id
},
spinner: 300
})
this.$Message.success(this.$L('删除成功'))
this.loadTemplates()
} catch ({msg}) {
this.$Message.error(msg || this.$L('删除失败'))
}
}
})
},
//
async handleSetDefault(template) {
if (template.is_default) {
return
}
try {
await this.$store.dispatch("call", {
url: 'project/task/template_default',
data: {
id: template.id,
project_id: this.projectId
},
spinner: 300
})
this.$Message.success(this.$L('设置成功'))
this.loadTemplates()
} catch ({msg}) {
this.$Message.error(msg || this.$L('设置失败'))
}
}
}
}
</script>

View File

@ -15,6 +15,7 @@
@import "project-panel";
@import "project-workflow";
@import "project-permission";
@import "project-task-template";
@import "task-add";
@import "task-add-simple";
@import "task-archived";

View File

@ -0,0 +1,100 @@
.project-task-template {
height: 100%;
display: flex;
flex-direction: column;
.header {
padding: 20px 20px 10px;
display: flex;
align-items: center;
justify-content: space-between;
.title {
color: $primary-title-color;
font-size: 20px;
font-weight: 500;
line-height: 1;
padding-right: 24px;
}
}
.content {
flex: 1;
padding: 10px 20px 20px;
overflow-y: auto;
position: relative;
.empty {
text-align: center;
padding: 40px 0;
color: $primary-text-color;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
.empty-text {
font-size: 14px;
}
}
}
.template-list {
.template-item {
margin-bottom: 16px;
padding: 16px;
border: 1px solid #F4F4F5;
border-radius: 4px;
&:hover {
border-color: #84C56A;
}
.template-title {
font-weight: 500;
margin-bottom: 8px;
display: flex;
align-items: center;
color: $primary-title-color;
.default-tag {
font-weight: normal;
margin-left: 8px;
font-size: 12px;
padding: 2px 8px;
border-radius: 3px;
background: #84C56A;
}
}
.template-content {
color: $primary-text-color;
font-size: 13px;
.task-title {
margin-bottom: 4px;
}
.task-content {
color: $primary-desc-color;
}
}
.template-actions {
margin-top: 12px;
text-align: right;
> button {
margin-right: 12px;
height: 28px;
padding: 0 12px;
font-size: 13px;
> i {
margin: 0 -2px;
}
}
}
}
}
}