import { create } from 'zustand'; import { devtools } from 'zustand/middleware'; import { fetchJSON, postForm, postJSON } from '@/utils/request'; import { HT_HOST } from '@/config'; import { groupBy } from '@/utils/commons'; export const searchAgencyAction = async (param) => { const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/products_search`, param); return errcode !== 0 ? [] : result; }; export const copyAgencyDataAction = async (postbody) => { const formData = new FormData(); Object.keys(postbody).forEach((key) => { formData.append(key, postbody[key]); }); const { errcode, result } = await postForm(`${HT_HOST}/agency/products/copy`, formData); return errcode === 0 ? true : false; }; export const getAgencyProductsAction = async (param) => { const _param = { ...param, use_year: (param.use_year || '').replace('all', ''), audit_state: (param.audit_state || '').replace('all', '') }; const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/travel_agency_products`, _param); return errcode !== 0 ? { agency: {}, products: [] } : result; }; /** * */ export const addProductExtraAction = async (body) => { const { errcode, result } = await postJSON(`${HT_HOST}/Service_BaseInfoWeb/products_extras_add`, body); return errcode === 0 ? true : false; }; /** * */ export const delProductExtrasAction = async (body) => { const { errcode, result } = await postJSON(`${HT_HOST}/Service_BaseInfoWeb/products_extras_del`, body); return errcode === 0 ? true : false; }; /** * 获取指定产品的附加项目 * @param {object} param { id, travel_agency_id, use_year } */ export const getAgencyProductExtrasAction = async (param) => { const _param = { ...param, use_year: (param.use_year || '').replace('all', '') }; const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/products_extras`, _param); return errcode !== 0 ? [] : result; }; export const postProductsQuoteAuditAction = async (auditState, quoteRow) => { const postbody = { audit_state: auditState, id: quoteRow.id, travel_agency_id: quoteRow.travel_agency_id, }; const formData = new FormData(); Object.keys(postbody).forEach((key) => { formData.append(key, postbody[key]); }); const json = await postForm(`${HT_HOST}/Service_BaseInfoWeb/quotation_audit`, formData); return json; // return errcode !== 0 ? {} : result; }; export const postProductsAuditAction = async (auditState, infoRow) => { const postbody = { audit_state: auditState, id: infoRow.id, travel_agency_id: infoRow.travel_agency_id, }; const formData = new FormData(); Object.keys(postbody).forEach((key) => { formData.append(key, postbody[key]); }); const json = await postForm(`${HT_HOST}/Service_BaseInfoWeb/travel-agency-products-audit`, formData); return json; // const { errcode, result } = json; // return errcode !== 0 ? {} : result; }; const initialState = { loading: false, searchValues: {}, agencyList: [], activeAgency: {}, agencyProducts: {}, editingProduct: {}, }; export const useProductsStore = create( devtools((set, get) => ({ // 初始化状态 ...initialState, // state actions setLoading: (loading) => set({ loading }), setSearchValues: (searchValues) => set({ searchValues }), setAgencyList: (agencyList) => set({ agencyList }), setActiveAgency: (activeAgency) => set({ activeAgency }), setAgencyProducts: (agencyProducts) => set({ agencyProducts }), setEditingProduct: (editingProduct) => set({ editingProduct }), reset: () => set(initialState), // side effects searchAgency: async (param) => { const { setLoading, setAgencyList } = get(); setLoading(true); const res = await searchAgencyAction(param); setAgencyList(res); setLoading(false); }, getAgencyProducts: async (param) => { const { setLoading, setActiveAgency, setAgencyProducts } = get(); setLoading(true); const res = await getAgencyProductsAction(param); const productsData = groupBy(res.products, (row) => row.info.product_type_id); setAgencyProducts(productsData); setActiveAgency(res.agency); setLoading(false); }, getAgencyProductExtras: async (param) => { const res = await getAgencyProductExtrasAction(param); // todo: }, })) ); export default useProductsStore;