index.jsx 9 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9
import DraggableDialog from "@/components/DraggableDialog";
import InitForm from "@/components/InitForm";
import PremButton from "@/components/PremButton";
import ShopProductCard from "@/components/ProductCard";
import ShopProductLoadingCard from "@/components/ProductCard/loading";
import { doFetch } from "@/utils/doFetch";
import PRODUCTS from "@/_mock/products";
import { Box, Container, Grid, Stack, Typography } from "@mui/material";
import { useRequest } from "ahooks";
wuhao's avatar
wuhao committed
10
import { Empty, Input, message,Tabs } from "antd";
krysent's avatar
krysent committed
11
import { useMemo, useState } from "react";
wuhao's avatar
wuhao committed
12 13
import "./index.less";

wuhao's avatar
wuhao committed
14 15


wuhao's avatar
wuhao committed
16 17 18 19 20 21 22
function Lessons() {
  const [dialogprops, setdialogprops] = useState({
    open: false,
  });
  const [params, setparams] = useState({
    courseName: "",
    status: null,
wuhao's avatar
wuhao committed
23
    typeList: ["1", "2", "3"],
wuhao's avatar
wuhao committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  });

  const handleClose = () => {
    setdialogprops((s) => ({
      ...s,
      open: false,
    }));
  };

  const { runAsync, loading } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, parames) => {
      if (res?.code == "0000") {
        handleClose();
        message.success("操作成功");
        datalist?.refresh();
      }
    },
  });

  const datalist = useRequest(
    async () => {
      let res = await doFetch({ url: "/sysCourse/list", params });
      return res?.data?.dataList;
    },
    {
      debounceWait: 400,
wuhao's avatar
wuhao committed
51
      refreshDeps: [params],
wuhao's avatar
wuhao committed
52 53 54 55 56 57 58 59 60 61 62
    }
  );

  const edit = (row) => {
    setdialogprops({
      open: true,
      defaultFormValue: { ...row },
      title: "编辑",
    });
  };

wuhao's avatar
wuhao committed
63 64 65 66 67 68 69 70
  const copy = (row) => {
    setdialogprops({
      open: true,
      defaultFormValue: { ...row },
      title: "复制创建",
    });
  };

krysent's avatar
krysent committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  const authorized = (row) => {
    doFetch({
      url: "/sysCourseTeacher/queryRelationTeacher",
      params: { courseId: row?.id },
    }).then((res) => {
      if (res.code === "0000") {
        setdialogprops({
          open: true,
          maxWidth: "xl",
          defaultFormValue: { ...row },
          teacherIdList: res?.data?.dataList,
          title: "授权",
        });
      }
    });
  };

wuhao's avatar
wuhao committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  const remove = (row) => {
    runAsync({
      url: "/sysCourse/delete",
      params: { id: row.id },
    });
  };

  const publish = (row, params) => {
    const type = row?.type === 1 ? 2 : row?.type === 2 ? 1 : null;
    const extra = params ?? { type };
    runAsync({
      url: "/sysCourse/pubOrNotPub",
      params: { id: row.id, ...extra },
    });
  };

  const columns = useMemo(
    () => [
      {
        title: "课程封面",
        dataIndex: "pic",
        key: "pic",
        valueType: "uploadImage",
        fieldProps: {
          limit: 1,
        },
      },
      {
        title: "课程名称",
        dataIndex: "courseName",
        key: "courseName",
        formItemProps: {
          rules: [{ required: true, message: "此项为必填项" }],
        },
        colProps: {
          span: 24,
        },
      },
wuhao's avatar
wuhao committed
126 127 128 129 130 131 132 133 134
      {
        title: "课程时间",
        dataIndex: "courseTime",
        key: "courseTimeList",
        valueType: "dateRange",
        colProps: {
          span: 24,
        },
      },
wuhao's avatar
wuhao committed
135 136 137 138 139 140 141 142 143 144 145 146
    ],
    []
  );

  return (
    <Container maxWidth={false}>
      <DraggableDialog
        handleClose={handleClose}
        loading={loading}
        dialogprops={dialogprops}
        maxWidth={dialogprops?.maxWidth ?? "xs"}
      >
wuhao's avatar
wuhao committed
147 148 149
        {dialogprops?.title === "编辑" ||
        dialogprops?.title === "新增课程" ||
        dialogprops?.title === "复制创建" ? (
krysent's avatar
krysent committed
150 151 152 153
          <InitForm
            fields={columns}
            defaultFormValue={dialogprops?.defaultFormValue}
            onFinish={(val, extra) => {
wuhao's avatar
wuhao committed
154 155
              let postdata = { ...val },
                url = "/sysCourse/saveOrUpdate";
krysent's avatar
krysent committed
156 157 158 159 160 161 162
              switch (dialogprops?.title) {
                case "编辑":
                  postdata = {
                    ...val,
                    id: dialogprops?.defaultFormValue?.id,
                  };
                  break;
wuhao's avatar
wuhao committed
163 164 165 166 167 168 169
                case "复制创建":
                  url = "/sysCourse/copy";
                  postdata = {
                    ...val,
                    id: dialogprops?.defaultFormValue?.id,
                  };
                  break;
krysent's avatar
krysent committed
170 171 172 173
                default:
                  break;
              }
              runAsync({
wuhao's avatar
wuhao committed
174
                url,
krysent's avatar
krysent committed
175 176 177 178 179 180
                params: postdata,
              });
            }}
          ></InitForm>
        ) : (
          <InitForm
wuhao's avatar
wuhao committed
181 182 183
            defaultFormValue={{
              teacherIdList: dialogprops?.teacherIdList,
            }}
krysent's avatar
krysent committed
184 185 186
            fields={[
              {
                rowKey: "id",
wuhao's avatar
wuhao committed
187
                rowName: "name",
krysent's avatar
krysent committed
188 189 190 191 192 193 194 195 196 197
                valueType: "FormSelectList",
                dataIndex: "teacherIdList",
                colProps: {
                  span: 24,
                },
                columns: [
                  {
                    title: "账号",
                    key: "userAccount",
                    dataIndex: "userAccount",
wuhao's avatar
wuhao committed
198
                    editable: false,
krysent's avatar
krysent committed
199 200 201 202 203
                  },
                  {
                    title: "教师姓名",
                    key: "name",
                    dataIndex: "name",
wuhao's avatar
wuhao committed
204
                    editable: false,
krysent's avatar
krysent committed
205 206 207 208 209
                  },
                  {
                    title: "学校名称",
                    key: "schoolName",
                    dataIndex: "schoolName",
wuhao's avatar
wuhao committed
210
                    editable: false,
krysent's avatar
krysent committed
211 212 213 214 215
                  },
                  {
                    title: "院系名称",
                    key: "departmentName",
                    dataIndex: "departmentName",
wuhao's avatar
wuhao committed
216
                    editable: false,
krysent's avatar
krysent committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                  },
                ],
                path: "/user/page",
                params: {
                  type: "2",
                },
              },
            ]}
            onFinish={(val) => {
              const teacherIdList = val?.teacherIdList?.map((it) => it?.id);
              runAsync({
                url: "/sysCourseTeacher/relationCourseTeacher",
                params: {
                  teacherIdList,
                  courseId: dialogprops?.defaultFormValue?.id,
                },
              });
            }}
          />
        )}
wuhao's avatar
wuhao committed
237 238 239 240 241 242 243 244 245
      </DraggableDialog>
      <Box
        display={"flex"}
        justifyContent={"space-between"}
        alignItems={"center"}
        sx={{ mb: 2.5 }}
        mt={0}
      >
        <Typography variant="h5">课程管理</Typography>
wuhao's avatar
wuhao committed
246
        <Stack spacing={2} direction="row" flex={1} justifyContent={"flex-end"}>
wuhao's avatar
wuhao committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

        <Tabs
            items={[
              {
                key: null,
                label: `全部`,
              },
              {
                key: "1",
                label: `待发布`,
              },
              {
                key: "2",
                label: `已发布`,
              },
              {
                key: "3",
                label: `已结束`,
              },
            ]}
            activeKey={params?.type}
            onChange={(val) => {
              setparams((s) => ({
                ...s,
                type: val,
              }));
wuhao's avatar
wuhao committed
273
            }}
wuhao's avatar
wuhao committed
274
          ></Tabs>
wuhao's avatar
wuhao committed
275

wuhao's avatar
wuhao committed
276 277
          <Input
            placeholder="请输入课程名称"
wuhao's avatar
wuhao committed
278
            style={{ width: 200, marginLeft: 24  }}
wuhao's avatar
wuhao committed
279 280 281 282 283 284 285 286
            value={params?.courseName}
            onChange={(e) => {
              setparams((s) => ({
                ...s,
                courseName: e.target.value,
              }));
            }}
          ></Input>
wuhao's avatar
wuhao committed
287

wuhao's avatar
wuhao committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
          <PremButton
            btn={{
              variant: "contained",
              onClick: (e) => {
                e.stopPropagation();
                setdialogprops({
                  open: true,
                  defaultFormValue: {},
                  title: "新增课程",
                });
              },
            }}
          >
            新增课程
          </PremButton>
        </Stack>
      </Box>

      <Box mt={2.5}>
        <Grid container spacing={3}>
          {datalist?.loading && !datalist?.data ? (
            PRODUCTS?.map((product, i) => {
              return (
wuhao's avatar
wuhao committed
311 312 313 314 315 316 317 318 319
                <Grid
                  key={product.id}
                  item
                  xs={12}
                  sm={6}
                  md={4}
                  lg={3}
                  xl={2.4}
                >
wuhao's avatar
wuhao committed
320 321 322 323 324 325 326 327 328 329
                  <ShopProductLoadingCard product={product} />
                </Grid>
              );
            })
          ) : datalist?.data?.length === 0 ? (
            <Grid xs={12} mt={12}>
              <Empty></Empty>
            </Grid>
          ) : (
            datalist?.data?.map?.((product) => (
wuhao's avatar
wuhao committed
330
              <Grid key={product.id} item xs={12} sm={6} md={4} lg={3} xl={2.4}>
wuhao's avatar
wuhao committed
331 332 333 334
                <ShopProductCard
                  product={product}
                  loading={datalist?.loading}
                  edit={edit}
wuhao's avatar
wuhao committed
335
                  copy={copy}
wuhao's avatar
wuhao committed
336 337
                  remove={remove}
                  publish={publish}
krysent's avatar
krysent committed
338
                  authorized={authorized}
wuhao's avatar
wuhao committed
339 340 341 342 343 344 345 346 347 348 349
                />
              </Grid>
            ))
          )}
        </Grid>
      </Box>
    </Container>
  );
}

export default Lessons;