| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <template>
- <view class="follow-container" v-show="!isOwner">
- <!-- 关注按钮 -->
- <view
- @click="handleFollow"
- :class="['follow-box', isFollowed ? 'follow' : '', currentStyle]"
- :style="currentStyle"
- >
- <text>
- <template v-if="textType === 'default'">{{
- isFollowed ? "已关注" : "关注"
- }}</template>
- <!-- 粉丝列表展示文案不一样 -->
- <template v-else>
- <template v-if="isFollowed">互相关注</template>
- <template v-else>回关</template>
- </template>
- </text>
- </view>
- <!-- 取关确认弹窗 -->
- <up-modal
- :showCancelButton="true"
- confirmText="不再关注"
- cancelText="取消"
- @confirm="confirmFollowModal"
- @cancel="cancelFollowModal"
- :show="showFollowModal"
- title="不再关注该作者?"
- >
- </up-modal>
- </view>
- </template>
- <script setup>
- import { ref, watch, computed, onMounted } from "vue";
- // import { setUserState } from "@/api/book";
- import { useToast } from "@/hooks/useToast";
- // import { useAppStore } from "@/stores/app";
- const defaultActiveStyle = {
- border: "1px solid #999",
- color: "#333",
- };
- const defaultInactiveStyle = {
- border: "1px solid #ff2442",
- color: "#ff2442",
- };
- const { Toast } = useToast();
- // const appStore = useAppStore();
- // 定义props
- const props = defineProps({
- // 是否已关注
- followed: {
- type: Boolean,
- default: false,
- },
- // 被关注用户ID
- followedId: {
- type: [String, Number],
- required: true,
- },
- textType: {
- type: String,
- default: "default", // 可选值 fans
- },
- showBtn: {
- type: Boolean,
- default: true,
- },
- activeStyle: {
- type: Object,
- default: () => ({}),
- },
- inactiveStyle: {
- type: Object,
- default: () => ({}),
- },
- });
- const isOwner = computed(() => {
- // return Number(props.followedId) === Number(appStore.uid);
- });
- const currentStyle = computed(() => {
- if (isFollowed.value) {
- return { ...defaultActiveStyle, ...(props.activeStyle || {}) };
- } else {
- return { ...defaultInactiveStyle, ...(props.inactiveStyle || {}) };
- }
- });
- // 定义事件
- const emit = defineEmits(["update:followed", "follow-change"]);
- // 响应式数据
- const isFollowed = ref(props.followed);
- const showFollowModal = ref(false);
- const followLoading = ref(false);
- // 监听props变化
- watch(
- () => props.followed,
- (newVal) => {
- isFollowed.value = newVal;
- }
- );
- // 点击关注按钮事件
- async function handleFollow() {
- console.log("点击关注按钮");
- if (isFollowed.value) {
- showFollowModal.value = true;
- } else {
- // setFollowState();
- }
- }
- // 确认取关事件
- function confirmFollowModal() {
- showFollowModal.value = false;
- // setFollowState();
- }
- // 取消事件
- function cancelFollowModal() {
- showFollowModal.value = false;
- }
- // 关注/取关请求
- // async function setFollowState() {
- // try {
- // if (followLoading.value) return;
- // followLoading.value = true;
- // if (!props.followed && isOwner.value) {
- // return Toast({ title: "无法关注自己!" });
- // }
- // const res = await setUserState({
- // type: 2,
- // followedId: props.followedId,
- // });
- // isFollowed.value = !isFollowed.value;
- // // 触发父组件更新
- // emit("update:followed", isFollowed.value);
- // emit("follow-change", isFollowed.value);
- // // 触发全局事件,通知其他页面更新关注状态
- // uni.$emit('followStateChanged', {
- // userId: props.followedId,
- // followMark: isFollowed.value
- // });
- // followLoading.value = false;
- // } catch (error) {
- // console.error("follow error", error);
- // followLoading.value = false;
- // Toast({ title: "操作失败", icon: "none" });
- // }
- // }
- </script>
- <style lang="scss" scoped>
- .follow-container {
- .follow-box {
- padding: 4rpx 25rpx;
- border: 1px solid #ff2442;
- border-radius: 50rpx;
- color: #ff2442;
- margin-right: 10rpx;
- cursor: pointer;
- transition: all 0.3s ease;
- &.follow {
- border-color: #999;
- color: #333;
- }
- text {
- font-size: 14px;
- }
- }
- }
- ::v-deep
- .u-modal__button-group__wrapper--confirm
- .u-modal__button-group__wrapper__text {
- color: #ff2442 !important;
- }
- </style>
|