import MediaApi from '@/service/media'; import { showToast } from 'vant'; import { onMounted, onUnmounted, Ref } from 'vue'; import eventBus from '@/utils/eventBus'; import { AcgEventBusKey } from '@/enums/app'; /** * @Description 收藏acg * @param {string} objID * @param {string} mediaType * @param {boolean} isCollect * @return {*} */ const handleCollected = async (objID: string, isCollect: boolean) => { try { return (isCollect ? MediaApi.postBookshelfAdd : MediaApi.postBookshelfDel)({ id: objID, }).then(() => { showToast(isCollect ? '成功收藏' : '成功取消'); }); } catch (e) { console.log(e); } }; /** * @Description eventBus监听 * @param {string} id * @param {Ref} hasCollected * @return {*} */ const Collected_on = (id: string, hasCollected: Ref, callback?: any) => { const update = (bool: any) => { if (callback) { callback(bool); return; } hasCollected.value = bool; }; onMounted(() => { eventBus.on(AcgEventBusKey.Collected + id, update); }); onUnmounted(() => { eventBus.off(AcgEventBusKey.Collected + id, update); }); }; /** * @Description eventBus赋值 * @param {string} id * @param {boolean} bool * @return {*} */ const Collected_emit = (id: string, bool: boolean) => { eventBus.emit(AcgEventBusKey.Collected + id, bool); }; export { handleCollected, Collected_on, Collected_emit };