Procházet zdrojové kódy

fix(mp): keep select dropdown inside viewport

apple před 6 dny
rodič
revize
65fc8d1e7a

+ 150 - 0
docs/24_星级班班主任移动报表/2026-11-26_小程序下拉框视口浮层修复实施记录.md

@@ -0,0 +1,150 @@
+# 2026-11-26 小程序下拉框视口浮层修复实施记录
+
+## 一、问题现象
+
+小程序学工录入页的长下拉列表只显示一部分,选项在表单区域下边缘被截断,无法完整查看和选择。
+
+## 二、根因
+
+公共下拉组件 `ss-select` 的选项层原来位于表单内部,并使用绝对定位:
+
+```css
+.ss-options {
+  position: absolute;
+  top: 100%;
+  z-index: 1000;
+}
+```
+
+现场页面使用 `.form-table { overflow: hidden; }`。祖先容器的裁切优先于选项层的 `z-index`,因此提高层级或单独增加高度都不能解决问题。
+
+正式环境核查还确认:
+
+- 独立组件文件已包含 `teleport` 修复。
+- 公共 bundle 由项目内较旧组件源生成,实际加载时 `teleport` 数量为 0。
+- 前一轮首屏优化改为加载公共 bundle 后,旧组件实现被带入运行时。
+
+## 三、修复方式
+
+本次统一修复公共 `ss-select` 组件,不修改任何业务页面的提交逻辑。
+
+- 使用 Vue `teleport` 将选项层挂载到 `body`。
+- 按触发框和可视区域动态计算 `top`、`left`、宽度及最大高度。
+- 下方空间不足时自动向上展开。
+- 长列表在浮层内部滚动。
+- 页面滚动、窗口尺寸变化时重新定位。
+- 点击浮层外时正常关闭,点击浮层内选项不会被误判为外部点击。
+- 浮层层级为 `10000`,字体保持 `14px`。
+
+## 四、修改文件
+
+```text
+web/js/mp_ss_components/mp-ss-components.js
+web/js/mp_bundle/mp-common.bundle.min.js
+web/js/mp_base/base.js
+```
+
+资源版本由:
+
+```text
+20261126-performance-v1
+```
+
+更新为:
+
+```text
+20261126-select-overlay-v1
+```
+
+## 五、发布文件哈希
+
+发布后测试环境、正式 Pod 和正式公网下载文件一致:
+
+| 文件 | SHA256 |
+|---|---|
+| `base.js` | `798ffb5fa4009bdc960cf44543be4d827a4877c211db0f3b000989f906ae6add` |
+| `mp-ss-components.js` | `8e7786131aa61bd9eecadfd20caa91981f66f2843fca30ed69362eb01b35b0a9` |
+| `mp-common.bundle.min.js` | `49e58ea4c799917fa7886e071faf98feca16e5186f1d47d8d27dbab7c3a3ebe2` |
+
+## 六、测试环境验证
+
+### 长列表
+
+个人宿舍违纪的违纪类别列表共 25 项,实际验证:
+
+- 选项浮层父节点为 `BODY`。
+- `position=fixed`。
+- `z-index=10000`。
+- 手机视口 `390x844` 下浮层高度约 `320px`。
+- 列表内容高度约 `1040px`,可在浮层内部滚动到最后一项。
+- 最后一项可正常选择,选择后浮层正常关闭。
+
+### 自动向上展开
+
+将视口压缩到 `390x320` 后重新验证:
+
+- 浮层自动移动到触发框上方。
+- 浮层顶部为 `12px`,底部为 `192.5px`。
+- 最大高度根据可用空间缩小到约 `180.5px`。
+
+### 其他入口冒烟
+
+以下页面返回 HTTP `200`:
+
+```text
+/page/mp_sswjjl_gr_inp.html
+/page/mp_sswjjl_jt_inp.html
+/page/mp_xxwjjl_inp.html
+/page/mp_cswsjc_inp.html
+/page/mp_xyqj_inp.html
+/page/mp_kqjl_inp.html
+/page/mp_ssnwjc_inp.html
+```
+
+正式环境中 `/page/mp_fjyrjc_inp.html` 原本返回 404;本次没有发布或修改该页面,不作为本次回归问题处理。
+
+## 七、正式环境发布
+
+发布方式:仅热更新三个静态文件。
+
+- 未重启 Pod。
+- 未修改数据库。
+- 未修改权限。
+- 未修改业务配置。
+- 未修改 Nginx。
+
+发布后正式 Pod:
+
+```text
+tomcat-pms-fbb778c87-2clgt
+1/1 Running
+RESTARTS=0
+```
+
+正式公网页面和三个资源均返回 HTTP `200`,公网下载哈希与本地构建一致。正式页面实际加载版本为 `20261126-select-overlay-v1`,长列表可在浮层内滚动,矮视口时自动向上展开。
+
+## 八、备份与回滚
+
+测试环境备份:
+
+```text
+/opt/data/pms/backup/20261126_select_overlay_pre
+```
+
+正式环境备份:
+
+```text
+/opt/data/backup/20261126_select_overlay_pre
+```
+
+两个备份目录均包含发布前三个文件及 `SHA256SUM.before`。需要回滚时,将目录内对应文件覆盖回 Web 根目录即可,无需重启 Pod。
+
+## 九、当前状态
+
+| 项目 | 状态 |
+|---|---|
+| 公共下拉组件 | 已修复 |
+| 公共 bundle | 已重建 |
+| 测试环境 | 已发布并验证 |
+| 正式环境 | 已热发布并验证 |
+| 数据库与业务配置 | 未修改 |

+ 1 - 1
web/js/mp_base/base.js

@@ -23,7 +23,7 @@ window.ss = {
 window.SS = window.ss; //最终都用小写ss,暂时先兼容大写SS
 
 (function () {
-  const H5_ASSET_VERSION = "20261126-performance-v1";
+  const H5_ASSET_VERSION = "20261126-select-overlay-v1";
 
   // ========== 移动端适配:设置根元素font-size ==========
   // 设计稿宽度430px,分成10份,1rem = 43px(在430px屏幕上)

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
web/js/mp_bundle/mp-common.bundle.min.js


+ 117 - 22
web/js/mp_ss_components/mp-ss-components.js

@@ -10,6 +10,7 @@
     onMounted,
     onBeforeUnmount,
     computed,
+    nextTick,
   } = Vue;
 
   async function ensureOptionalAssets(name) {
@@ -894,6 +895,8 @@
     setup(props, { emit }) {
       const isOpen = ref(false);
       const containerRef = ref(null);
+      const optionsRef = ref(null);
+      const optionsStyle = ref({});
       const inputRef = ref(null);
       const selectedValue = ref(props.modelValue);
       const searchKeyword = ref("");
@@ -1182,6 +1185,74 @@
         forceShowAll.value = false;
       };
 
+      // 将选项浮层挂到 body,并按可视区域动态定位,避免被页面滚动容器裁切。
+      const updateOptionsPosition = async () => {
+        await nextTick();
+        const container = containerRef.value;
+        const menu = optionsRef.value;
+        if (!container || !menu) return;
+
+        const rect = container.getBoundingClientRect();
+        const viewportWidth =
+          document.documentElement.clientWidth || window.innerWidth;
+        const viewportHeight =
+          document.documentElement.clientHeight || window.innerHeight;
+        const margin = 12;
+        const gap = 6;
+        const naturalHeight = Math.min(menu.scrollHeight || 300, 320);
+        const spaceBelow = viewportHeight - rect.bottom - margin - gap;
+        const spaceAbove = rect.top - margin - gap;
+        const openUp =
+          spaceBelow < Math.min(naturalHeight, 220) &&
+          spaceAbove > spaceBelow;
+        const available = Math.max(openUp ? spaceAbove : spaceBelow, 80);
+        const maxHeight = Math.min(320, available);
+        const top = openUp
+          ? Math.max(margin, rect.top - maxHeight - gap)
+          : Math.min(
+              viewportHeight - margin - maxHeight,
+              rect.bottom + gap
+            );
+        const menuWidth = Math.min(
+          viewportWidth - margin * 2,
+          Math.max(rect.width, 260)
+        );
+        const left = Math.max(
+          margin,
+          Math.min(rect.left, viewportWidth - margin - menuWidth)
+        );
+
+        optionsStyle.value = {
+          display: "block",
+          position: "fixed",
+          fontSize: "14px",
+          lineHeight: "20px",
+          top: `${top}px`,
+          left: `${left}px`,
+          minWidth: `${Math.min(rect.width, viewportWidth - margin * 2)}px`,
+          maxWidth: `${viewportWidth - margin * 2}px`,
+          maxHeight: `${Math.max(80, maxHeight)}px`,
+          width: `${menuWidth}px`,
+          overflowY: "auto",
+          overflowX: "hidden",
+          WebkitOverflowScrolling: "touch",
+          overscrollBehavior: "contain",
+          zIndex: 10000,
+        };
+
+        await nextTick();
+        const menuRect = menu.getBoundingClientRect();
+        if (menuRect.right > viewportWidth - margin) {
+          optionsStyle.value = {
+            ...optionsStyle.value,
+            left: `${Math.max(
+              margin,
+              viewportWidth - margin - menuRect.width
+            )}px`,
+          };
+        }
+      };
+
       const openDropdown = (showAll = false) => {
         if (props.disabled) return;
         if (activeSsSelectCloser && activeSsSelectCloser !== closeDropdown) {
@@ -1190,6 +1261,7 @@
         forceShowAll.value = showAll;
         isOpen.value = true;
         activeSsSelectCloser = closeDropdown;
+        updateOptionsPosition();
       };
 
       const closeDropdown = ({ restoreInput = true } = {}) => {
@@ -1242,6 +1314,7 @@
         filterKeyword.value = value;
         forceShowAll.value = false;
         isOpen.value = true;
+        updateOptionsPosition();
         emit("search", value);
       };
 
@@ -1254,6 +1327,7 @@
         filterKeyword.value = "";
         forceShowAll.value = true;
         isOpen.value = true;
+        updateOptionsPosition();
       };
 
       const selectOption = (option) => {
@@ -1269,11 +1343,18 @@
       };
 
       const handleClickOutside = (event) => {
-        if (!containerRef.value?.contains(event.target)) {
+        if (
+          !containerRef.value?.contains(event.target) &&
+          !optionsRef.value?.contains(event.target)
+        ) {
           closeDropdown();
         }
       };
 
+      const handleViewportChange = () => {
+        if (isOpen.value) updateOptionsPosition();
+      };
+
       watch(
         () => props.modelValue,
         (newValue) => {
@@ -1331,6 +1412,8 @@
 
       onMounted(() => {
         document.addEventListener("click", handleClickOutside);
+        window.addEventListener("resize", handleViewportChange);
+        window.addEventListener("scroll", handleViewportChange, true);
         loadRemoteOptions();
         if (!needRemoteData.value && Array.isArray(props.options)) {
           emit("loaded", props.options);
@@ -1344,10 +1427,14 @@
           activeSsSelectCloser = null;
         }
         document.removeEventListener("click", handleClickOutside);
+        window.removeEventListener("resize", handleViewportChange);
+        window.removeEventListener("scroll", handleViewportChange, true);
       });
 
       return {
         containerRef,
+        optionsRef,
+        optionsStyle,
         inputRef,
         isOpen,
         selectedValue,
@@ -1394,30 +1481,38 @@
 					</div>
 				</div>
 
-				<div class="ss-options" v-show="isOpen">
-					<div
-						v-if="finalLoading"
-						class="option-item loading-item"
-					>
-						<span class="loading-text">加载中...</span>
-					</div>
-					<div
-						v-else-if="filteredOptions.length === 0"
-						class="option-item no-options"
-					>
-						无选项
-					</div>
+				<teleport to="body">
 					<div
-						v-else
-						v-for="(option, index) in filteredOptions"
-						:key="index"
-						class="option-item"
-						:class="{ selected: option[mapping.value] === selectedValue }"
-						@click.stop="selectOption(option)"
+						ref="optionsRef"
+						class="ss-options"
+						:style="optionsStyle"
+						v-if="isOpen"
+						@click.stop
 					>
-						{{ option[mapping.text] }}
+						<div
+							v-if="finalLoading"
+							class="option-item loading-item"
+						>
+							<span class="loading-text">加载中...</span>
+						</div>
+						<div
+							v-else-if="filteredOptions.length === 0"
+							class="option-item no-options"
+						>
+							无选项
+						</div>
+						<div
+							v-else
+							v-for="(option, index) in filteredOptions"
+							:key="index"
+							class="option-item"
+							:class="{ selected: option[mapping.value] === selectedValue }"
+							@click.stop="selectOption(option)"
+						>
+							{{ option[mapping.text] }}
+						</div>
 					</div>
-				</div>
+				</teleport>
 			</div>
 		`,
   };

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů