Container.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <script setup>
  2. import Empty from "../Empty/Empty.vue";
  3. import pages from "@/pages.json";
  4. import { ref, onMounted, watchEffect } from "vue";
  5. import { getRoute, router } from "../../utils/router";
  6. import { getRect, debounce } from "../../utils";
  7. import { getCurrentInstance } from "vue";
  8. const props = defineProps({
  9. showBottom: {
  10. type: Boolean,
  11. default: false,
  12. },
  13. bottomText: {
  14. type: String,
  15. default: "~已经到底了~",
  16. },
  17. scrollX: {
  18. type: Boolean,
  19. default: false,
  20. },
  21. scrollY: {
  22. type: Boolean,
  23. default: true,
  24. },
  25. className: {
  26. type: String,
  27. default: "",
  28. },
  29. scrollIntoView: {
  30. type: String,
  31. default: "",
  32. },
  33. scrollStyle: {
  34. type: Object,
  35. default: () => ({}),
  36. },
  37. onBack: Function,
  38. showBack: {
  39. type: Boolean,
  40. default: true,
  41. },
  42. showTitle: {
  43. type: Boolean,
  44. default: true,
  45. },
  46. bgColor: {
  47. type: String,
  48. default: "#fff",
  49. },
  50. title: String,
  51. empty: {
  52. type: [Boolean],
  53. default: false,
  54. },
  55. border: {
  56. type: Boolean,
  57. default: false,
  58. },
  59. scrollTop: {
  60. type: Number,
  61. default: 0,
  62. },
  63. });
  64. const emit = defineEmits(["onSafeAreaChange", "onScroll"]);
  65. const onBack = async () => {
  66. if (props.onBack) {
  67. const res = await props.onBack();
  68. if (!res) return;
  69. router.back();
  70. return;
  71. }
  72. router.back();
  73. };
  74. const safeArea = ref({
  75. footer: {},
  76. title: {},
  77. });
  78. const footerHeight = ref(0);
  79. onMounted(() => {
  80. const systemInfo = uni.getWindowInfo();
  81. // 判断是否为tabbar页面
  82. safeArea.value = {
  83. ...systemInfo.safeArea,
  84. source: systemInfo.safeArea,
  85. };
  86. // 解决部分安卓手机安全顶部为0 的bug
  87. if (!safeArea.value.top) {
  88. safeArea.value.top = systemInfo.statusBarHeight || 40;
  89. }
  90. const isTarbarPage = pages.tabBar.list
  91. .map((item) => item.pagePath)
  92. .includes(getRoute().routeList[0].path);
  93. if (isTarbarPage) {
  94. // 高度再减掉tabbar高度
  95. safeArea.value.height -= 50;
  96. // 减去系统导航栏高度
  97. safeArea.value.height -= systemInfo.statusBarHeight - 18;
  98. }
  99. // 24是内边距
  100. safeArea.value.width -= 24;
  101. const instance = getCurrentInstance();
  102. // 获取头部高度
  103. getRect({
  104. name: ".title",
  105. onSuccess(res) {
  106. safeArea.value.title = res;
  107. safeArea.value.height -= props.showTitle
  108. ? safeArea.value.title?.height
  109. : 0;
  110. },
  111. instance,
  112. });
  113. // 获取底部高度
  114. getRect({
  115. name: ".bottom-button",
  116. onSuccess(res) {
  117. safeArea.value.footer = res;
  118. safeArea.value.height -= safeArea.value.footer?.height;
  119. safeArea.value.height += 22; // 剩余高度
  120. },
  121. instance,
  122. });
  123. });
  124. watchEffect(() => {
  125. emit("onSafeAreaChange", safeArea.value);
  126. });
  127. const onScroll = debounce((e) => {
  128. emit("onScroll", e);
  129. }, 100);
  130. defineExpose({
  131. safeArea: safeArea.value,
  132. });
  133. </script>
  134. <template>
  135. <view class="container" :style="{ background: bgColor }">
  136. <view
  137. class="title"
  138. :style="{
  139. paddingTop: `${safeArea.top}px`,
  140. background: bgColor,
  141. }"
  142. v-if="showTitle"
  143. >
  144. <uni-nav-bar
  145. :left-icon="showBack ? 'left' : ''"
  146. @clickLeft="onBack"
  147. :border="border"
  148. :title="title"
  149. fixed
  150. :backgroundColor="bgColor"
  151. />
  152. </view>
  153. <scroll-view
  154. enable-flex
  155. enable-passive
  156. enhanced
  157. paging-enabled
  158. show-scrollbar
  159. enable-back-to-top
  160. scroll-with-animation
  161. scroll-anchoring
  162. :scroll-x="scrollX"
  163. :scroll-y="scrollY"
  164. :scroll-top="scrollTop"
  165. :scroll-into-view="scrollIntoView"
  166. @scroll="onScroll"
  167. v-if="!empty"
  168. >
  169. <view
  170. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  171. :style="{
  172. height: `${safeArea.height}px`,
  173. whiteSpace: scrollX ? 'nowrap' : 'normal',
  174. ...scrollStyle,
  175. }"
  176. >
  177. <slot></slot>
  178. </view>
  179. <!-- 底部文字 -->
  180. <view v-if="showBottom && bottomText" class="bottom-text">
  181. {{ bottomText }}
  182. </view>
  183. </scroll-view>
  184. <view class="empty" v-else>
  185. <Empty />
  186. </view>
  187. <view
  188. v-if="$slots.footer"
  189. class="bottom-button"
  190. :style="{
  191. width: `${safeArea.width}px`,
  192. background: bgColor,
  193. }"
  194. >
  195. <slot name="footer" :footer-height="footerHeight" />
  196. </view>
  197. </view>
  198. </template>
  199. <style lang="scss" scoped>
  200. @import "@/uni.scss";
  201. .title {
  202. position: sticky;
  203. top: 0;
  204. z-index: 9999;
  205. }
  206. .empty {
  207. display: flex;
  208. align-items: center;
  209. justify-content: center;
  210. flex: 1;
  211. }
  212. .container {
  213. display: flex;
  214. flex-direction: column;
  215. color: #333;
  216. box-sizing: content-box;
  217. position: relative;
  218. height: 100vh;
  219. }
  220. .scroll-view {
  221. position: relative;
  222. padding: 24rpx 24rpx 0;
  223. box-sizing: content-box;
  224. display: flex;
  225. flex-direction: column;
  226. gap: 24rpx;
  227. }
  228. .bottom-text {
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. width: 100%;
  233. }
  234. .bottom-button {
  235. width: 100%;
  236. padding: 24rpx;
  237. position: absolute;
  238. bottom: 0;
  239. }
  240. .topic-scroll {
  241. white-space: nowrap;
  242. display: inline-flex;
  243. }
  244. </style>