index.jsx 10.5 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7
import DraggableDialog from "@/components/DraggableDialog";
import ImportExcel from "@/components/ImportExcel";
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";
wuhao's avatar
wuhao committed
8
import generateColor from "@/utils/pickColor";
wuhao's avatar
wuhao committed
9 10
import PRODUCTS from "@/_mock/products";
import { Box, Container, Grid, Stack, Typography } from "@mui/material";
wuhao's avatar
wuhao committed
11 12 13
import Checkbox from "@mui/material/Checkbox";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormGroup from "@mui/material/FormGroup";
wuhao's avatar
wuhao committed
14
import { useRequest } from "ahooks";
wuhao's avatar
wuhao committed
15
import { Empty, Input, message } from "antd";
krysent's avatar
krysent committed
16
import { useMemo, useState } from "react";
wuhao's avatar
wuhao committed
17 18 19 20 21 22 23 24 25
import "./index.less";

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 129 130 131 132 133 134 135 136 137 138 139 140
  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,
        },
      },
    ],
    []
  );

  return (
    <Container maxWidth={false}>
      <DraggableDialog
        handleClose={handleClose}
        loading={loading}
        dialogprops={dialogprops}
        maxWidth={dialogprops?.maxWidth ?? "xs"}
      >
wuhao's avatar
wuhao committed
141 142 143
        {dialogprops?.title === "编辑" ||
        dialogprops?.title === "新增课程" ||
        dialogprops?.title === "复制创建" ? (
krysent's avatar
krysent committed
144 145 146 147
          <InitForm
            fields={columns}
            defaultFormValue={dialogprops?.defaultFormValue}
            onFinish={(val, extra) => {
wuhao's avatar
wuhao committed
148 149
              let postdata = { ...val },
                url = "/sysCourse/saveOrUpdate";
krysent's avatar
krysent committed
150 151 152 153 154 155 156
              switch (dialogprops?.title) {
                case "编辑":
                  postdata = {
                    ...val,
                    id: dialogprops?.defaultFormValue?.id,
                  };
                  break;
wuhao's avatar
wuhao committed
157 158 159 160 161 162 163
                case "复制创建":
                  url = "/sysCourse/copy";
                  postdata = {
                    ...val,
                    id: dialogprops?.defaultFormValue?.id,
                  };
                  break;
krysent's avatar
krysent committed
164 165 166 167
                default:
                  break;
              }
              runAsync({
wuhao's avatar
wuhao committed
168
                url,
krysent's avatar
krysent committed
169 170 171 172 173 174
                params: postdata,
              });
            }}
          ></InitForm>
        ) : (
          <InitForm
wuhao's avatar
wuhao committed
175 176 177
            defaultFormValue={{
              teacherIdList: dialogprops?.teacherIdList,
            }}
krysent's avatar
krysent committed
178 179 180
            fields={[
              {
                rowKey: "id",
wuhao's avatar
wuhao committed
181
                rowName: "name",
krysent's avatar
krysent committed
182 183 184 185 186 187 188 189 190 191
                valueType: "FormSelectList",
                dataIndex: "teacherIdList",
                colProps: {
                  span: 24,
                },
                columns: [
                  {
                    title: "账号",
                    key: "userAccount",
                    dataIndex: "userAccount",
wuhao's avatar
wuhao committed
192
                    editable: false,
krysent's avatar
krysent committed
193 194 195 196 197
                  },
                  {
                    title: "教师姓名",
                    key: "name",
                    dataIndex: "name",
wuhao's avatar
wuhao committed
198
                    editable: false,
krysent's avatar
krysent committed
199 200 201 202 203
                  },
                  {
                    title: "学校名称",
                    key: "schoolName",
                    dataIndex: "schoolName",
wuhao's avatar
wuhao committed
204
                    editable: false,
krysent's avatar
krysent committed
205 206 207 208 209
                  },
                  {
                    title: "院系名称",
                    key: "departmentName",
                    dataIndex: "departmentName",
wuhao's avatar
wuhao committed
210
                    editable: false,
krysent's avatar
krysent committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
                  },
                ],
                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
231 232 233 234 235 236 237 238 239
      </DraggableDialog>
      <Box
        display={"flex"}
        justifyContent={"space-between"}
        alignItems={"center"}
        sx={{ mb: 2.5 }}
        mt={0}
      >
        <Typography variant="h5">课程管理</Typography>
wuhao's avatar
wuhao committed
240 241 242 243 244 245 246 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 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
        <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"}
                />
              }
              label="已关闭"
              value={3}
            />
          </FormGroup>

wuhao's avatar
wuhao committed
316 317 318 319 320 321 322 323 324 325 326
          <Input
            placeholder="请输入课程名称"
            style={{ width: 200 }}
            value={params?.courseName}
            onChange={(e) => {
              setparams((s) => ({
                ...s,
                courseName: e.target.value,
              }));
            }}
          ></Input>
wuhao's avatar
wuhao committed
327

wuhao's avatar
wuhao committed
328
          <ImportExcel></ImportExcel>
wuhao's avatar
wuhao committed
329

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

export default Lessons;