index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="follow-container" v-show="!isOwner">
  3. <!-- 关注按钮 -->
  4. <view
  5. @click="handleFollow"
  6. :class="['follow-box', isFollowed ? 'follow' : '', currentStyle]"
  7. :style="currentStyle"
  8. >
  9. <text>
  10. <template v-if="textType === 'default'">{{
  11. isFollowed ? "已关注" : "关注"
  12. }}</template>
  13. <!-- 粉丝列表展示文案不一样 -->
  14. <template v-else>
  15. <template v-if="isFollowed">互相关注</template>
  16. <template v-else>回关</template>
  17. </template>
  18. </text>
  19. </view>
  20. <!-- 取关确认弹窗 -->
  21. <up-modal
  22. :showCancelButton="true"
  23. confirmText="不再关注"
  24. cancelText="取消"
  25. @confirm="confirmFollowModal"
  26. @cancel="cancelFollowModal"
  27. :show="showFollowModal"
  28. title="不再关注该作者?"
  29. >
  30. </up-modal>
  31. </view>
  32. </template>
  33. <script setup>
  34. import { ref, watch, computed, onMounted } from "vue";
  35. // import { setUserState } from "@/api/book";
  36. import { useToast } from "@/hooks/useToast";
  37. // import { useAppStore } from "@/stores/app";
  38. const defaultActiveStyle = {
  39. border: "1px solid #999",
  40. color: "#333",
  41. };
  42. const defaultInactiveStyle = {
  43. border: "1px solid #ff2442",
  44. color: "#ff2442",
  45. };
  46. const { Toast } = useToast();
  47. // const appStore = useAppStore();
  48. // 定义props
  49. const props = defineProps({
  50. // 是否已关注
  51. followed: {
  52. type: Boolean,
  53. default: false,
  54. },
  55. // 被关注用户ID
  56. followedId: {
  57. type: [String, Number],
  58. required: true,
  59. },
  60. textType: {
  61. type: String,
  62. default: "default", // 可选值 fans
  63. },
  64. showBtn: {
  65. type: Boolean,
  66. default: true,
  67. },
  68. activeStyle: {
  69. type: Object,
  70. default: () => ({}),
  71. },
  72. inactiveStyle: {
  73. type: Object,
  74. default: () => ({}),
  75. },
  76. });
  77. const isOwner = computed(() => {
  78. // return Number(props.followedId) === Number(appStore.uid);
  79. });
  80. const currentStyle = computed(() => {
  81. if (isFollowed.value) {
  82. return { ...defaultActiveStyle, ...(props.activeStyle || {}) };
  83. } else {
  84. return { ...defaultInactiveStyle, ...(props.inactiveStyle || {}) };
  85. }
  86. });
  87. // 定义事件
  88. const emit = defineEmits(["update:followed", "follow-change"]);
  89. // 响应式数据
  90. const isFollowed = ref(props.followed);
  91. const showFollowModal = ref(false);
  92. const followLoading = ref(false);
  93. // 监听props变化
  94. watch(
  95. () => props.followed,
  96. (newVal) => {
  97. isFollowed.value = newVal;
  98. }
  99. );
  100. // 点击关注按钮事件
  101. async function handleFollow() {
  102. console.log("点击关注按钮");
  103. if (isFollowed.value) {
  104. showFollowModal.value = true;
  105. } else {
  106. // setFollowState();
  107. }
  108. }
  109. // 确认取关事件
  110. function confirmFollowModal() {
  111. showFollowModal.value = false;
  112. // setFollowState();
  113. }
  114. // 取消事件
  115. function cancelFollowModal() {
  116. showFollowModal.value = false;
  117. }
  118. // 关注/取关请求
  119. // async function setFollowState() {
  120. // try {
  121. // if (followLoading.value) return;
  122. // followLoading.value = true;
  123. // if (!props.followed && isOwner.value) {
  124. // return Toast({ title: "无法关注自己!" });
  125. // }
  126. // const res = await setUserState({
  127. // type: 2,
  128. // followedId: props.followedId,
  129. // });
  130. // isFollowed.value = !isFollowed.value;
  131. // // 触发父组件更新
  132. // emit("update:followed", isFollowed.value);
  133. // emit("follow-change", isFollowed.value);
  134. // // 触发全局事件,通知其他页面更新关注状态
  135. // uni.$emit('followStateChanged', {
  136. // userId: props.followedId,
  137. // followMark: isFollowed.value
  138. // });
  139. // followLoading.value = false;
  140. // } catch (error) {
  141. // console.error("follow error", error);
  142. // followLoading.value = false;
  143. // Toast({ title: "操作失败", icon: "none" });
  144. // }
  145. // }
  146. </script>
  147. <style lang="scss" scoped>
  148. .follow-container {
  149. .follow-box {
  150. padding: 4rpx 25rpx;
  151. border: 1px solid #ff2442;
  152. border-radius: 50rpx;
  153. color: #ff2442;
  154. margin-right: 10rpx;
  155. cursor: pointer;
  156. transition: all 0.3s ease;
  157. &.follow {
  158. border-color: #999;
  159. color: #333;
  160. }
  161. text {
  162. font-size: 14px;
  163. }
  164. }
  165. }
  166. ::v-deep
  167. .u-modal__button-group__wrapper--confirm
  168. .u-modal__button-group__wrapper__text {
  169. color: #ff2442 !important;
  170. }
  171. </style>