Container.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. <div class="bg">
  149. <slot name="bg"></slot>
  150. </div>
  151. <view
  152. class="title"
  153. :style="{
  154. paddingTop: `${safeArea.top}px`,
  155. background: headerColor,
  156. }"
  157. v-if="showTitle"
  158. >
  159. <uni-nav-bar
  160. :left-icon="showBack ? 'left' : ''"
  161. @clickLeft="onBack"
  162. :border="border"
  163. :title="title"
  164. fixed
  165. :backgroundColor="headerColor"
  166. />
  167. </view>
  168. <scroll-view
  169. enable-flex
  170. enable-passive
  171. enhanced
  172. paging-enabled
  173. show-scrollbar
  174. enable-back-to-top
  175. scroll-with-animation
  176. scroll-anchoring
  177. :scroll-x="scrollX"
  178. :scroll-y="scrollY"
  179. :scroll-top="scrollTop"
  180. :scroll-into-view="scrollIntoView"
  181. @scroll="onScroll"
  182. v-if="!empty"
  183. >
  184. <view
  185. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  186. :style="{
  187. height: `${safeArea.height}px`,
  188. whiteSpace: scrollX ? 'nowrap' : 'normal',
  189. ...scrollStyle,
  190. }"
  191. >
  192. <slot></slot>
  193. </view>
  194. <!-- 底部文字 -->
  195. <view v-if="showBottom && bottomText" class="bottom-text">
  196. {{ bottomText }}
  197. </view>
  198. </scroll-view>
  199. <view class="empty" v-else>
  200. <Empty />
  201. </view>
  202. <view
  203. v-if="$slots.footer"
  204. class="bottom-button"
  205. :style="{
  206. width: `${safeArea.width}px`,
  207. background: bottomBgColor,
  208. borderTop: bottomBorder? '1px solid #eee' : 'none',
  209. }"
  210. >
  211. <slot name="footer" :footer-height="footerHeight" />
  212. </view>
  213. </view>
  214. </template>
  215. <style lang="scss" scoped>
  216. @import "@/uni.scss";
  217. .title {
  218. position: sticky;
  219. top: 0;
  220. z-index: 9999;
  221. }
  222. .bg {
  223. position: absolute;
  224. top: 0;
  225. }
  226. .empty {
  227. display: flex;
  228. align-items: center;
  229. justify-content: center;
  230. flex: 1;
  231. }
  232. .container {
  233. display: flex;
  234. flex-direction: column;
  235. color: #333;
  236. box-sizing: content-box;
  237. position: relative;
  238. height: 100vh;
  239. }
  240. .scroll-view {
  241. position: relative;
  242. padding: 24rpx 24rpx 0;
  243. box-sizing: content-box;
  244. display: flex;
  245. flex-direction: column;
  246. gap: 24rpx;
  247. }
  248. .bottom-text {
  249. display: flex;
  250. align-items: center;
  251. justify-content: center;
  252. width: 100%;
  253. }
  254. .bottom-button {
  255. width: 100%;
  256. padding: 24rpx;
  257. position: absolute;
  258. bottom: 0;
  259. }
  260. .topic-scroll {
  261. white-space: nowrap;
  262. display: inline-flex;
  263. }
  264. </style>