Files
hjsq_h5/src/views/acgModule/utils/collect.ts
T
2026-11-05 12:01:36 +07:00

60 lines
1.4 KiB
TypeScript

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<boolean>, 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 };