index.jsx 10.6 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8
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";
wuhao's avatar
wuhao committed
9 10 11
import Checkbox from "@mui/material/Checkbox";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormGroup from "@mui/material/FormGroup";
wuhao's avatar
wuhao committed
12
import { useRequest } from "ahooks";
wuhao's avatar
wuhao committed
13
import { Empty, Input, message } from "antd";
krysent's avatar
krysent committed
14
import { useMemo, useState } from "react";
wuhao's avatar
wuhao committed
15 16
import "./index.less";

wuhao's avatar
wuhao committed
17 18


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

  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
54
      refreshDeps: [params],
wuhao's avatar
wuhao committed
55 56 57 58 59 60 61 62 63 64 65
    }
  );

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

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

krysent's avatar
krysent committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  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
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 126 127 128
  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
129 130 131 132 133 134 135 136 137
      {
        title: "课程时间",
        dataIndex: "courseTime",
        key: "courseTimeList",
        valueType: "dateRange",
        colProps: {
          span: 24,
        },
      },
wuhao's avatar
wuhao committed
138 139 140 141 142 143 144 145 146 147 148 149
    ],
    []
  );

  return (
    <Container maxWidth={false}>
      <DraggableDialog
        handleClose={handleClose}
        loading={loading}
        dialogprops={dialogprops}
        maxWidth={dialogprops?.maxWidth ?? "xs"}
      >
wuhao's avatar
wuhao committed
150 151 152
        {dialogprops?.title === "编辑" ||
        dialogprops?.title === "新增课程" ||
        dialogprops?.title === "复制创建" ? (
krysent's avatar
krysent committed
153 154 155 156
          <InitForm
            fields={columns}
            defaultFormValue={dialogprops?.defaultFormValue}
            onFinish={(val, extra) => {
wuhao's avatar
wuhao committed
157 158
              let postdata = { ...val },
                url = "/sysCourse/saveOrUpdate";
krysent's avatar
krysent committed
159 160 161 162 163 164 165
              switch (dialogprops?.title) {
                case "编辑":
                  postdata = {
                    ...val,
                    id: dialogprops?.defaultFormValue?.id,
                  };
                  break;
wuhao's avatar
wuhao committed
166 167 168 169 170 171 172
                case "复制创建":
                  url = "/sysCourse/copy";
                  postdata = {
                    ...val,
                    id: dialogprops?.defaultFormValue?.id,
                  };
                  break;
krysent's avatar
krysent committed
173 174 175 176
                default:
                  break;
              }
              runAsync({
wuhao's avatar
wuhao committed
177
                url,
krysent's avatar
krysent committed
178 179 180 181 182 183
                params: postdata,
              });
            }}
          ></InitForm>
        ) : (
          <InitForm
wuhao's avatar
wuhao committed
184 185 186
            defaultFormValue={{
              teacherIdList: dialogprops?.teacherIdList,
            }}
krysent's avatar
krysent committed
187 188 189
            fields={[
              {
                rowKey: "id",
wuhao's avatar
wuhao committed
190
                rowName: "name",
krysent's avatar
krysent committed
191 192 193 194 195 196 197 198 199 200
                valueType: "FormSelectList",
                dataIndex: "teacherIdList",
                colProps: {
                  span: 24,
                },
                columns: [
                  {
                    title: "账号",
                    key: "userAccount",
                    dataIndex: "userAccount",
wuhao's avatar
wuhao committed
201
                    editable: false,
krysent's avatar
krysent committed
202 203 204 205 206
                  },
                  {
                    title: "教师姓名",
                    key: "name",
                    dataIndex: "name",
wuhao's avatar
wuhao committed
207
                    editable: false,
krysent's avatar
krysent committed
208 209 210 211 212
                  },
                  {
                    title: "学校名称",
                    key: "schoolName",
                    dataIndex: "schoolName",
wuhao's avatar
wuhao committed
213
                    editable: false,
krysent's avatar
krysent committed
214 215 216 217 218
                  },
                  {
                    title: "院系名称",
                    key: "departmentName",
                    dataIndex: "departmentName",
wuhao's avatar
wuhao committed
219
                    editable: false,
krysent's avatar
krysent committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                  },
                ],
                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
240 241 242 243 244 245 246 247 248
      </DraggableDialog>
      <Box
        display={"flex"}
        justifyContent={"space-between"}
        alignItems={"center"}
        sx={{ mb: 2.5 }}
        mt={0}
      >
        <Typography variant="h5">课程管理</Typography>
wuhao's avatar
wuhao committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        <Stack spacing={2} direction="row" flex={1} justifyContent={"flex-end"}>
          <FormControlLabel
            control={
              <Checkbox
                checked={params?.typeList?.length === 3}
                indeterminate={
                  params?.typeList?.length > 0 && params?.typeList?.length < 3
                }
              />
            }
            label="全部"
            onChange={(e) => {
              setparams((s) => {
                let news = [];
                if (e.target.checked) {
                  news = ["1", "2", "3"];
                }
                return {
                  ...s,
                  typeList: news,
                };
              });
            }}
          />
          <FormGroup
            row
            value={params?.typeList ?? []}
            onChange={(e) => {
              let val = e.target.value;
              setparams((s) => {
                let news = [...s?.typeList];
                if (news.includes(val)) {
                  news = news.filter((it) => it !== val);
                } else {
                  news = [...news, val];
                }
                console.log(news);
                return {
                  ...s,
                  typeList: news,
                };
              });
            }}
          >
            <FormControlLabel
              control={
                <Checkbox
                  checked={params?.typeList?.includes("1")}
                  color={"warning"}
                />
              }
              label="待发布"
              value={1}
            />
            <FormControlLabel
              control={
                <Checkbox
                  checked={params?.typeList?.includes("2")}
                  color={"info"}
                />
              }
              label="已发布"
              value={2}
            />
            <FormControlLabel
              control={
                <Checkbox
                  checked={params?.typeList?.includes("3")}
                  color={"default"}
                />
              }
wuhao's avatar
wuhao committed
320
              label="已结束"
wuhao's avatar
wuhao committed
321 322 323 324
              value={3}
            />
          </FormGroup>

wuhao's avatar
wuhao committed
325 326 327 328 329 330 331 332 333 334 335
          <Input
            placeholder="请输入课程名称"
            style={{ width: 200 }}
            value={params?.courseName}
            onChange={(e) => {
              setparams((s) => ({
                ...s,
                courseName: e.target.value,
              }));
            }}
          ></Input>
wuhao's avatar
wuhao committed
336

wuhao's avatar
wuhao committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
          <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
360 361 362 363 364 365 366 367 368
                <Grid
                  key={product.id}
                  item
                  xs={12}
                  sm={6}
                  md={4}
                  lg={3}
                  xl={2.4}
                >
wuhao's avatar
wuhao committed
369 370 371 372 373 374 375 376 377 378
                  <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
379
              <Grid key={product.id} item xs={12} sm={6} md={4} lg={3} xl={2.4}>
wuhao's avatar
wuhao committed
380 381 382 383
                <ShopProductCard
                  product={product}
                  loading={datalist?.loading}
                  edit={edit}
wuhao's avatar
wuhao committed
384
                  copy={copy}
wuhao's avatar
wuhao committed
385 386
                  remove={remove}
                  publish={publish}
krysent's avatar
krysent committed
387
                  authorized={authorized}
wuhao's avatar
wuhao committed
388 389 390 391 392 393 394 395 396 397 398
                />
              </Grid>
            ))
          )}
        </Grid>
      </Box>
    </Container>
  );
}

export default Lessons;