Container.vue 5.3 KB

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