Explorar el Código

feat:讲师多选

sungongwei hace 10 meses
padre
commit
bde79fa7c0

+ 2 - 1
db/models/course.js

@@ -47,7 +47,8 @@ const model = connect.define(
       comment: '标签图',
     },
     teacherId: {
-      type: DataTypes.INTEGER,
+      type: DataTypes.JSON,
+      defaultValue: [],
       comment: '讲师',
     },
     recordId: {

+ 10 - 2
server/routes/course.js

@@ -45,7 +45,11 @@ router.get('/:id', async (req, res) => {
     where: { type: cdata.type, id: { [Op.not]: id }, status: COURSE_STATUS.已上线 },
     attributes: ['id', 'detailimg', 'title', 'price'],
   });
-  const tdata = await teacher.findOne({ where: { id: cdata.teacherId } });
+  const tdata = await Promise.all(
+    cdata.teacherId.map(item => {
+      return teacher.findOne({ where: { id: item } });
+    })
+  );
 
   const option = {
     title: '课程详情页',
@@ -95,7 +99,11 @@ router.get('/:id/:chapterId', async (req, res) => {
     where: { type: cdata.type, id: { [Op.not]: id }, status: COURSE_STATUS.已上线 },
     attributes: ['id', 'detailimg', 'title', 'price'],
   });
-  const tdata = await teacher.findOne({ where: { id: cdata.teacherId } });
+  const tdata = await Promise.all(
+    cdata.teacherId.map(item => {
+      return teacher.findOne({ where: { id: item } });
+    })
+  );
   const chdata = await chapter.findAll({
     where: { cid: id, status: 1 },
   });

+ 7 - 6
server/routes/master/course.js

@@ -14,12 +14,13 @@ const _COURSE_TYPE = _.invert(COURSE_TYPE);
 
 async function fillSingle(data) {
   const { teacherId, recordId } = data;
-  const tdata = await Promise.all([
-    Teacher.findOne({ where: { id: teacherId } }),
-    Teacher.findOne({ where: { id: recordId } }),
-  ]);
-  tdata[0] && (data.teacherName = tdata[0].name);
-  tdata[1] && (data.recordName = tdata[1].name);
+  const recorder = await Teacher.findOne({ where: { id: recordId } });
+  data.teachers = await Promise.all(
+    teacherId.map(item => {
+      return Teacher.findOne({ where: { id: item } });
+    })
+  );
+  recorder && (data.recordName = recorder.name);
   data.publishAt && (data.publishAt = dateFormat(data.publishAt));
   data.statusStr = _COURSE_STATUS[data.status];
   data.typeStr = _COURSE_TYPE[data.type];

+ 3 - 1
server/routes/master/teacher.js

@@ -107,7 +107,9 @@ router.put('/:id', async (req, res) => {
  */
 router.delete('/:id', async (req, res) => {
   const { id } = req.params;
-  const ccount = await Course.count({ where: { [Op.or]: [{ teacherId: id }, { recordId: id }] } });
+  const ccount = await Course.count({
+    where: { [Op.or]: [{ teacherId: { [Op.like]: `%${id}%` } }, { recordId: id }] },
+  });
   if (ccount) {
     return res.send(onError('请先解绑相关课程'));
   }

+ 6 - 4
server/views/components/course-section.html

@@ -27,18 +27,20 @@
           <h4>授课教师</h4>
         </div>
         <ul class="masters-list">
+          <% teacher.forEach(item => { %>
           <li>
             <div class="master-intro">
               <div class="avatar">
-                <img src="<%= teacher.img%>" alt="讲师" />
+                <img src="<%= item.img%>" alt="讲师" />
               </div>
               <div class="info">
-                <h5><%= teacher.name%></h5>
-                <div class="title"><%= teacher.title%></div>
+                <h5><%= item.name%></h5>
+                <div class="title"><%= item.title%></div>
               </div>
             </div>
-            <div class="master-desc"><%= teacher.desc%></div>
+            <div class="master-desc"><%= item.desc%></div>
           </li>
+          <% }) %>
         </ul>
       </div>
       <% if(otherCourse.length){ %>