refactor: Notice 的Mobx store 迁移到 zustand; 仅保留管理未读数量

feature/price_manager
Lei OT 1 year ago
parent bff8a1ea78
commit c06df03754

@ -1,73 +1,54 @@
import { makeAutoObservable, runInAction } from "mobx";
import * as config from "@/config";
class Notice {
constructor(root) {
makeAutoObservable(this, { rootStore: false });
this.root = root;
}
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
noticeList = []; //公告列表
noticeUnRead = 0; //未读公告数量
noticeInfo = { CCP_BLID: 0, CCP_BLTitle: "", CCP_BLContent: "", CCP_LastEditTime: "" }; //公告详情
import { fetchJSON, postForm, } from '@/utils/request';
const { HT_HOST } = config;
/*
LMI_SN 登录用户SN用户sn用来判断是否已读公告
*/
getBulletinList(LMI_SN) {
let url = `/service-Cooperate/Cooperate/GetBulletinList`;
url += `?LMI_SN=${LMI_SN}`;
url+=`&token=${this.root.authStore.login.token}`;
fetch(config.HT_HOST + url)
.then(response => response.json())
.then(json => {
runInAction(() => {
this.noticeList = json.Result;
});
})
.catch(error => {
console.log("fetch data failed", error);
});
}
/**
* Notice 相关的请求
*/
/*
LMI_SN 登录用户sn 用户sn用来设置已读公告请求过一次详情页表示已读
CCP_BLID 公告sn
*/
getNoticeDetail(LMI_SN, CCP_BLID) {
let url = `/service-Cooperate/Cooperate/GetBulletinDetail`;
url += `?LMI_SN=${LMI_SN}&CCP_BLID=${CCP_BLID}`;
url+=`&token=${this.root.authStore.login.token}`;
fetch(config.HT_HOST + url)
.then(response => response.json())
.then(json => {
console.log(json);
runInAction(() => {
this.noticeInfo = json.Result;
});
})
.catch(error => {
console.log("fetch data failed", error);
});
}
export const fetchBulletinList = async (LMI_SN) => {
const { errcode, Result } = await fetchJSON(`${HT_HOST}/service-Cooperate/Cooperate/GetBulletinList`, { LMI_SN });
return errcode !== 0 ? [] : Result;
};
//检查是否有未读公告
getBulletinUnReadCount(LMI_SN) {
let url = `/service-Cooperate/Cooperate/GetBulletinUnReadCount`;
url += `?LMI_SN=${LMI_SN}`;
url+=`&token=${this.root.authStore.login.token}`;
fetch(config.HT_HOST + url)
.then(response => response.json())
.then(json => {
console.log(json);
runInAction(() => {
this.noticeUnRead = json.Result.CCP_BulletinCount;
});
})
.catch(error => {
console.log("fetch data failed", error);
});
}
export const fetchBulletinUnReadCount = async (LMI_SN) => {
const { errcode, Result } = await fetchJSON(`${HT_HOST}/service-Cooperate/Cooperate/GetBulletinUnReadCount`, { LMI_SN });
return errcode !== 0 ? 0 : Result.CCP_BulletinCount;
}
export default Notice;
export const fetchNoticeDetail = async (LMI_SN, CCP_BLID) => {
const { errcode, Result } = await fetchJSON(`${HT_HOST}/service-Cooperate/Cooperate/GetBulletinDetail`, { LMI_SN, CCP_BLID });
return errcode !== 0 ? {} : Result;
}
/**
* Notice Store
*/
const initialState = {
noticeUnRead: 0, //未读公告数量
};
export const useNoticeStore = create(
devtools((set, get) => ({
// 初始化状态
...initialState,
// state actions
setNoticeUnRead: (noticeUnRead) => set(() => ({ noticeUnRead })),
reset: () => set(initialState),
// side effects
getBulletinUnReadCount: async (LMI_SN) => {
const { setNoticeUnRead } = get();
const noticeUnRead = await fetchBulletinUnReadCount(LMI_SN);
setNoticeUnRead(noticeUnRead);
},
}))
);
export default useNoticeStore;

@ -1,51 +0,0 @@
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { fetchJSON, postForm, } from '@/utils/request';
import { HT_HOST } from '@/config';
/**
* Notice 相关的请求
*/
export const fetchBulletinList = async (LMI_SN) => {
const { errcode, Result } = await fetchJSON(`${HT_HOST}/service-Cooperate/Cooperate/GetBulletinList`, { LMI_SN });
return errcode !== 0 ? [] : Result;
};
export const fetchBulletinUnReadCount = async (LMI_SN) => {
const { errcode, Result } = await fetchJSON(`${HT_HOST}/service-Cooperate/Cooperate/GetBulletinUnReadCount`, { LMI_SN });
return errcode !== 0 ? 0 : Result.CCP_BulletinCount;
}
export const fetchNoticeDetail = async (LMI_SN, CCP_BLID) => {
const { errcode, Result } = await fetchJSON(`${HT_HOST}/service-Cooperate/Cooperate/GetBulletinDetail`, { LMI_SN, CCP_BLID });
return errcode !== 0 ? {} : Result;
}
/**
* Notice Store
*/
const initialState = {
noticeUnRead: 0, //未读公告数量
};
export const useNoticeStore = create(
devtools((set, get) => ({
// 初始化状态
...initialState,
// state actions
setNoticeUnRead: (noticeUnRead) => set(() => ({ noticeUnRead })),
reset: () => set(initialState),
// side effects
getBulletinUnReadCount: async (LMI_SN) => {
const { setNoticeUnRead } = get();
const noticeUnRead = await fetchBulletinUnReadCount(LMI_SN);
setNoticeUnRead(noticeUnRead);
},
}))
);
export default useNoticeStore;

@ -1,7 +1,6 @@
import { makeAutoObservable } from "mobx";
import Reservation from "./Reservation";
import Feedback from "./Feedback";
import Notice from "./Notice";
import Auth from "./Auth";
import Invoice from "./Invoice";
import Report from "./Report";
@ -10,14 +9,13 @@ class Root {
constructor() {
this.reservationStore = new Reservation(this);
this.feedbackStore = new Feedback(this);
this.noticeStore = new Notice(this);
this.authStore = new Auth(this);
this.invoiceStore = new Invoice(this);
this.reportStore = new Report(this);
makeAutoObservable(this);
}
clearSession() {
clearSession() {
if (window.sessionStorage) {
const sessionStorage = window.sessionStorage;
sessionStorage.clear();
@ -46,4 +44,4 @@ class Root {
}
}
export default Root;
export default Root;

@ -15,6 +15,7 @@ import zhLocale from 'antd/locale/zh_CN';
import enLocale from 'antd/locale/en_US';
import 'dayjs/locale/zh-cn';
import { BUILD_VERSION, } from '@/config';
import useNoticeStore from '@/stores/Notice';
const { Header, Content, Footer } = Layout;
const { Title } = Typography;
@ -23,10 +24,10 @@ function App() {
const { t, i18n } = useTranslation();
const [password, setPassword] = useState('');
const { authStore, noticeStore } = useStore();
const { authStore } = useStore();
const { notification } = AntApp.useApp();
const login = toJS(authStore.login);
const { noticeUnRead } = noticeStore;
const noticeUnRead = useNoticeStore((state) => state.noticeUnRead);
const href = useHref();
const loginToken = login.token;
const navigate = useNavigate();

@ -3,16 +3,19 @@ import { useEffect } from 'react';
import { Button, Checkbox, Form, Input, Row, App } from 'antd';
import { useStore } from '@/stores/StoreContext.js';
import { useTranslation } from 'react-i18next';
import useNoticeStore from '@/stores/Notice';
function Login() {
const { t, i18n } = useTranslation();
const { authStore, noticeStore } = useStore();
const { authStore } = useStore();
const { notification } = App.useApp();
const navigate = useNavigate();
const location = useLocation();
const [form] = Form.useForm();
const getBulletinUnReadCount = useNoticeStore((state) => state.getBulletinUnReadCount);
useEffect (() => {
if (location.search === '?out') {
authStore.logout();
@ -26,7 +29,7 @@ function Login() {
const onFinish = (values) => {
authStore.valdateUserPassword(values.username, values.password)
.then((userId) => {
noticeStore.getBulletinUnReadCount(userId);
getBulletinUnReadCount(userId);
authStore.fetchUserDetail()
.then((user) => {
// navigate(-1) is equivalent to hitting the back button.

@ -2,7 +2,7 @@ import { NavLink } from "react-router-dom";
import { useEffect, useState } from "react";
import { Row, Col, Space, Typography, Badge, List } from "antd";
import { useStore } from "@/stores/StoreContext.js";
import useNoticeStore, { fetchBulletinList } from '@/stores/Notice1';
import useNoticeStore, { fetchBulletinList } from '@/stores/Notice';
function Index() {
const { authStore } = useStore();

Loading…
Cancel
Save