Browse Source

添加教材版本和教材保存事件

吴朋磊 1 month ago
parent
commit
1e33146e87
2 changed files with 160 additions and 3 deletions
  1. 4 0
      src/api/school.ts
  2. 156 3
      src/views/school/teacherList.vue

+ 4 - 0
src/api/school.ts

@@ -106,3 +106,7 @@ export const resetSchoolPassWord = (data: any) => {
 export const getAllTeacher = (params: any) => {
   return http.get('/adminApi/v1/teacher/teacher_list', params)
 }
+// 学校管理--更新教师教材信息
+export const updateTeacherTextbook = (userId: number | string, data: any) => {
+  return http.get(`/adminApi/v1/teacher/add_textbook/${userId}`, data)
+}

+ 156 - 3
src/views/school/teacherList.vue

@@ -66,6 +66,62 @@
             {{ row.subjectName || '-' }}
           </template>
         </el-table-column>
+        <el-table-column prop="textbookVersions" label="教材版本" width="180" align="center">
+          <template #default="{ row }">
+            <el-input
+              v-if="IsEditing(row, 'textbookVersions')"
+              v-model="editingCell!.value"
+              size="small"
+              placeholder="请输入教材版本"
+              @click.stop
+            >
+              <template #suffix>
+                <el-icon
+                  class="save_icon"
+                  :class="{ saving: savingField === GetEditKey(row, 'textbookVersions') }"
+                  @click.stop="SaveEdit(row, 'textbookVersions')"
+                >
+                  <Check />
+                </el-icon>
+              </template>
+            </el-input>
+            <span
+              v-else
+              class="editable_text"
+              @click="StartEdit(row, 'textbookVersions')"
+            >
+              {{ row.textbookVersions || '-' }}
+            </span>
+          </template>
+        </el-table-column>
+        <el-table-column prop="textbook" label="教材" width="180" align="center">
+          <template #default="{ row }">
+            <el-input
+              v-if="IsEditing(row, 'textbook')"
+              v-model="editingCell!.value"
+              size="small"
+              placeholder="请输入教材"
+              @click.stop
+            >
+              <template #suffix>
+                <el-icon
+                  class="save_icon"
+                  :class="{ saving: savingField === GetEditKey(row, 'textbook') }"
+                  @click.stop="SaveEdit(row, 'textbook')"
+                >
+                  <Check />
+                </el-icon>
+              </template>
+            </el-input>
+            <span
+              v-else
+              class="editable_text"
+              @click="StartEdit(row, 'textbook')"
+            >
+              {{ row.textbook || '-' }}
+            </span>
+          </template>
+        </el-table-column>
         <el-table-column prop="binXKW" label="是否绑定学科网" width="160" align="center">
           <template #default="{ row }">
             {{ row.binXKW === 1 ? '是' : '否' }}
@@ -96,7 +152,7 @@
         </el-table-column>
       </el-table>
     </div>
-
+  
     <!-- 分页器 -->
     <div class="page_pagination">
       <el-pagination 
@@ -116,11 +172,13 @@
 <script lang="ts" setup>
 import { ref, onMounted, onUnmounted, nextTick } from 'vue'
 import { useRoute, useRouter } from 'vue-router'
-import { Search } from '@element-plus/icons-vue'
+import { Search, Check } from '@element-plus/icons-vue'
 import { ElMessage } from 'element-plus'
 import { throttle } from 'lodash'
 import * as TabSearch from '@/baseComponents/TabSearch.vue'
-import { getAllTeacher, getTicket } from '@/api/school'
+import { getAllTeacher, getTicket, updateTeacherTextbook } from '@/api/school'
+
+type EditField = 'textbookVersions' | 'textbook'
 
 const route = useRoute()
 const router = useRouter()
@@ -158,6 +216,8 @@ interface Teacher {
   phone: string
   status: number
   binXKW: number
+  textbookVersions?: string
+  textbook?: string
   [key: string]: any
 }
 
@@ -170,6 +230,75 @@ const total = ref(0)
 const tableHeight = ref(0)
 const API_PAGE_SIZE = 7000
 
+const editingCell = ref<{
+  userId: number | string
+  field: EditField
+  value: string
+} | null>(null)
+const savingField = ref('')
+
+const GetEditKey = (row: Teacher, field: EditField) => `${row.userId}_${field}`
+
+const IsEditing = (row: Teacher, field: EditField) => {
+  return editingCell.value?.userId === row.userId && editingCell.value?.field === field
+}
+
+const StartEdit = (row: Teacher, field: EditField) => {
+  editingCell.value = {
+    userId: row.userId,
+    field,
+    value: row[field] || '',
+  }
+}
+
+const UpdateTeacherField = (userId: number | string, field: EditField, value: string) => {
+  const updateItem = (item: Teacher) => {
+    if (item.userId === userId) {
+      item[field] = value
+    }
+  }
+  allData.value.forEach(updateItem)
+  tableData.value.forEach(updateItem)
+}
+
+const GetTextbookPayload = (row: Teacher, field: EditField, value: string) => {
+  const textbookVersions = (field === 'textbookVersions' ? value : (row.textbookVersions || '')).trim()
+  const textbook = (field === 'textbook' ? value : (row.textbook || '')).trim()
+  return { textbookVersions, textbook }
+}
+
+const SaveEdit = async (row: Teacher, field: EditField) => {
+  if (!editingCell.value || !IsEditing(row, field)) return
+
+  const value = editingCell.value.value.trim()
+  const editKey = GetEditKey(row, field)
+  if (savingField.value === editKey) return
+
+  const { textbookVersions, textbook } = GetTextbookPayload(row, field, value)
+
+  savingField.value = editKey
+  try {
+    const res = await updateTeacherTextbook(row.userId, {
+      schoolId: schoolId.value,
+      textbookVersions,
+      textbook,
+    })
+    if (res.code === 200) {
+      UpdateTeacherField(row.userId, 'textbookVersions', textbookVersions)
+      UpdateTeacherField(row.userId, 'textbook', textbook)
+      editingCell.value = null
+      ElMessage.success('保存成功')
+    } else {
+      ElMessage.error(res.msg || '保存失败')
+    }
+  } catch (error) {
+    console.error('保存教材信息失败', error)
+    ElMessage.error('保存失败')
+  } finally {
+    savingField.value = ''
+  }
+}
+
 // 跨页连续序号
 const GetTableIndex = (index: number) => {
   return (pageNum.value - 1) * pageSize.value + index + 1
@@ -426,4 +555,28 @@ onUnmounted(() => {
     text-decoration: underline;
   }
 }
+
+.editable_text {
+  color: #2E64FA;
+  cursor: pointer;
+
+  &:hover {
+    text-decoration: underline;
+  }
+}
+
+.save_icon {
+  cursor: pointer;
+  color: #2E64FA;
+  font-size: 16px;
+
+  &:hover {
+    color: #1d4ed8;
+  }
+
+  &.saving {
+    cursor: not-allowed;
+    opacity: 0.5;
+  }
+}
 </style>