left-1.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <div class="main">
  3. <div class="one">
  4. <el-col :span="24" class="add">
  5. <el-button type="primary" size="large" @click="toAdd('add')">添加项目</el-button>
  6. </el-col>
  7. <el-col :span="24">
  8. <el-col v-if="data.id">
  9. <el-button class="button" type="primary" @click="toAdd('edit')">修改项目</el-button>
  10. <el-button class="button" type="danger" @click="toDelete()">删除项目</el-button>
  11. </el-col>
  12. <el-col v-else>
  13. <el-button class="button" type="primary" disabled>修改项目</el-button>
  14. <el-button class="button" type="danger" disabled>删除项目</el-button>
  15. </el-col>
  16. </el-col>
  17. </div>
  18. <el-col :span="24" wrap>
  19. <div class="two">
  20. <el-menu class="sidebar-el-menu" :collapse="false" unique-opened>
  21. <template v-for="item in items" :key="item">
  22. <el-menu-item class="first" :index="item._id" @click="onMenu(item)">
  23. <el-col :span="4" class="icon">
  24. <el-icon :size="20">
  25. <Opportunity />
  26. </el-icon>
  27. </el-col>
  28. <el-col :span="18" class="title">
  29. <span>{{ item.name }}</span>
  30. </el-col>
  31. </el-menu-item>
  32. </template>
  33. </el-menu>
  34. </div>
  35. </el-col>
  36. </div>
  37. <el-dialog v-model="data.dialog" title="项目管理" :before-close="handleClose">
  38. <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" :size="formSize" status-icon>
  39. <el-form-item label="项目名" prop="name">
  40. <el-input v-model="ruleForm.name" />
  41. </el-form-item>
  42. <el-form-item label="描述" prop="desc">
  43. <el-input v-model="ruleForm.desc" type="textarea" />
  44. </el-form-item>
  45. <el-form-item label="备注" prop="remark">
  46. <el-input v-model="ruleForm.remark" type="textarea" />
  47. </el-form-item>
  48. <el-form-item>
  49. <el-button type="primary" @click="submitForm(ruleFormRef)"> 提交 </el-button>
  50. <el-button @click="resetForm(ruleFormRef)">重置</el-button>
  51. </el-form-item>
  52. </el-form>
  53. </el-dialog>
  54. </template>
  55. <script setup lang="ts">
  56. import { reactive, ref, onMounted } from 'vue';
  57. import type { Ref } from 'vue';
  58. import _ from 'lodash';
  59. import { useCounterStore as useStore } from '@/stores/counter';
  60. import type { IQueryResult } from '@/util/types.util';
  61. import type { FormInstance, FormRules } from 'element-plus';
  62. import { ElMessageBox } from 'element-plus';
  63. const store = useStore();
  64. const formSize = ref('default');
  65. const ruleFormRef = ref<FormInstance>();
  66. // #region 请求列表
  67. const data = reactive({
  68. id: '',
  69. dialog: ref(false),
  70. });
  71. // 请求列表
  72. // ref方式
  73. let items: Ref<any[]> = ref([]);
  74. const search = async () => {
  75. const res: IQueryResult = await store.projectQuery();
  76. items.value = res.data as any[];
  77. };
  78. onMounted(() => {
  79. search();
  80. });
  81. const emit = defineEmits(['onMenu', 'toDelete']);
  82. // 点击菜单
  83. const onMenu = (item: { _id: string; name: string }) => {
  84. data['id'] = item._id;
  85. emit('onMenu', item);
  86. };
  87. // #endregion
  88. // #region 表单添加修改
  89. // 表单
  90. let ruleForm: Ref<{ name: String; desc: String; remark: String }> = ref({ name: '', desc: '', remark: '' });
  91. // 验证
  92. const rules = reactive<FormRules>({
  93. name: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
  94. });
  95. // 添加
  96. const toAdd = async (type: string) => {
  97. if (type == 'edit') {
  98. const aee: IQueryResult = await store.projectFetch(data.id);
  99. ruleForm.value = aee.data as { name: String; desc: String; remark: String };
  100. } else if (type == 'add') {
  101. ruleForm.value = { name: '', desc: '', remark: '' };
  102. }
  103. data.dialog = true;
  104. };
  105. // 提交保存
  106. const submitForm = async (formEl: FormInstance | undefined) => {
  107. if (!formEl) return;
  108. await formEl.validate(async (valid) => {
  109. if (valid) {
  110. if (_.get(ruleForm.value, '_id')) {
  111. const res: IQueryResult = await store.projecUpdate(ruleForm.value);
  112. if (res.errcode == 0) handleClose();
  113. } else {
  114. const res: IQueryResult = await store.projectCreate(ruleForm.value);
  115. if (res.errcode == 0) handleClose();
  116. }
  117. }
  118. });
  119. };
  120. // 重置
  121. const resetForm = (formEl: FormInstance | undefined) => {
  122. if (!formEl) return;
  123. formEl.resetFields();
  124. };
  125. // 关闭弹窗
  126. const handleClose = () => {
  127. ruleForm.value = { name: '', desc: '', remark: '' };
  128. search();
  129. data.dialog = false;
  130. };
  131. // #endregion
  132. // 删除;
  133. const toDelete = async () => {
  134. ElMessageBox.confirm('是否确认删除该项目', '提示', {
  135. confirmButtonText: '确认',
  136. cancelButtonText: '取消',
  137. type: 'warning',
  138. }).then(async () => {
  139. emit('toDelete');
  140. search();
  141. });
  142. };
  143. </script>
  144. <style scoped>
  145. .one {
  146. height: 14vh;
  147. background: #1b5644;
  148. padding: 10px 30px 10px 30px;
  149. }
  150. .one .button {
  151. margin: 0 4px;
  152. }
  153. .one .add {
  154. margin: 0 0 10px 0;
  155. text-align: center;
  156. }
  157. .two {
  158. height: 78vh;
  159. padding: 0 0 30px 0;
  160. background: #73ad9b;
  161. overflow-y: scroll;
  162. }
  163. .sidebar-el-menu {
  164. background: #73ad9b;
  165. }
  166. .sidebar-el-menu .first span {
  167. font-size: 16px;
  168. font-weight: 500;
  169. }
  170. .el-menu {
  171. border-right: none;
  172. }
  173. .el-menu-item {
  174. color: #fff;
  175. }
  176. .el-menu-item.is-active {
  177. color: #fdda13;
  178. background-color: #78c1aa;
  179. }
  180. .two::-webkit-scrollbar {
  181. width: 2px;
  182. }
  183. .two::-webkit-scrollbar-thumb {
  184. border-radius: 10px;
  185. background: rgba(108, 43, 43, 0.2);
  186. }
  187. .two::-webkit-scrollbar-track {
  188. border-radius: 0;
  189. background: rgba(255, 254, 254, 0.1);
  190. }
  191. .two .name {
  192. height: 50px;
  193. color: #fff;
  194. margin: 10px 0;
  195. padding: 0 0 0 10px;
  196. display: flex;
  197. }
  198. .two .name:first-child {
  199. height: 50px;
  200. color: #fff;
  201. margin: 0 0 10px 0;
  202. }
  203. .two .name:last-child {
  204. height: 50px;
  205. color: #fff;
  206. margin: 10px 0 0 0;
  207. }
  208. .two .name .icon {
  209. padding: 15px 0 0 8px;
  210. }
  211. .two .name .title {
  212. line-height: 50px;
  213. font-size: 18px;
  214. }
  215. </style>