chore:表单弹窗正常
This commit is contained in:
@@ -82,6 +82,10 @@
|
||||
</block>
|
||||
<block v-else>
|
||||
<ns-empty :text="emptyText" :isIndex="false"></ns-empty>
|
||||
<!-- 空态追加手动登录按钮:任何自动弹窗机制失效时的最后兜底,用户一点就开 -->
|
||||
<view v-if="!complete" style="padding: 20rpx 40rpx 40rpx; display: flex; justify-content: center;">
|
||||
<button type="primary" size="mini" @click="forceOpenLogin">点击登录查看表单</button>
|
||||
</view>
|
||||
</block>
|
||||
<loading-cover ref="loadingCover"></loading-cover>
|
||||
<ns-login ref="login"></ns-login>
|
||||
@@ -120,6 +124,8 @@ export default {
|
||||
backUrl: '',
|
||||
// 登录拦截时暂存待提交数据:组件内存(MP 不丢态)+ 本地存储(H5 登录整页回跳后从存储恢复)
|
||||
pendingSubmit: null,
|
||||
// onLoad→getData 比 ns-login 挂载早时,用此标志在 mounted 再兜底弹登录(避免 ref 空报错)
|
||||
pendingLoginRequired: false,
|
||||
|
||||
// #ifndef H5
|
||||
navbarHeight: 44, // 自绘 navbar 内容区高度(不含状态栏),跨端兜底 44px
|
||||
@@ -290,6 +296,51 @@ export default {
|
||||
})
|
||||
}
|
||||
|
||||
// #ifdef H5
|
||||
// H5:从 window.location.href 解析参数,兼容 history 路由 (?key=val) 与 hash 路由 (#/path?key=val) 两种形式
|
||||
// 保证 recommend_code / uniacid 等参数在任何路由模式下都不丢失
|
||||
try {
|
||||
const href = window.location.href;
|
||||
let queryStr = '';
|
||||
const hashIdx = href.indexOf('#');
|
||||
const qIdx = href.indexOf('?');
|
||||
if (qIdx !== -1 && (hashIdx === -1 || qIdx < hashIdx)) {
|
||||
// history 模式:? 在 # 之前(或无 #)
|
||||
queryStr = href.substring(qIdx + 1, hashIdx !== -1 ? hashIdx : href.length);
|
||||
} else if (hashIdx !== -1) {
|
||||
// hash 模式:# 之后可能还有 ?
|
||||
const afterHash = href.substring(hashIdx + 1);
|
||||
const hq = afterHash.indexOf('?');
|
||||
if (hq !== -1) queryStr = afterHash.substring(hq + 1);
|
||||
}
|
||||
if (queryStr) {
|
||||
const pairs = queryStr.split('&');
|
||||
const reservedH5 = ['scene', 'from', 'hide_navbar', 'show_home', 'title', 'enable_set_title'];
|
||||
for (const p of pairs) {
|
||||
if (!p) continue;
|
||||
const eq = p.indexOf('=');
|
||||
let k, v;
|
||||
if (eq === -1) { k = decodeURIComponent(p); v = ''; }
|
||||
else { k = decodeURIComponent(p.substring(0, eq)); v = decodeURIComponent(p.substring(eq + 1)); }
|
||||
if (k === 'id') { if (!this.id) this.id = v; }
|
||||
else if (k === 'uniacid') { if (!this.uniacid) this.uniacid = v; }
|
||||
else if (reservedH5.indexOf(k) !== -1) {
|
||||
// 导航/显示类参数:仅当 options 没有传时才从 href 补
|
||||
if (options[k] === undefined && this[k] === undefined) {
|
||||
if (k === 'from') this.isFromMpWebView = (v === 'mp-webview');
|
||||
else if (k === 'hide_navbar') this.hideNavbar = this.$util.coerceBool(v);
|
||||
else if (k === 'show_home') this.showHomeBtn = this.$util.coerceBool(v);
|
||||
else if (k === 'title') this.formTitle = v;
|
||||
else if (k === 'enable_set_title') this.enableSetTitle = (v == 1);
|
||||
}
|
||||
} else {
|
||||
if (this.urlParams[k] === undefined) this.urlParams[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {}
|
||||
// #endif
|
||||
|
||||
// #ifdef H5
|
||||
if (this.isFromMpWebView || this.hideNavbar) {
|
||||
this.$nextTick(() => {
|
||||
@@ -299,12 +350,25 @@ export default {
|
||||
}
|
||||
// #endif
|
||||
|
||||
// 登录后回跳地址:由原始 options 重建(供 FORM_LOGIN_REQUIRED 时 ns-login 使用)
|
||||
// 登录后回跳地址:基于最终状态(this.id / this.uniacid + urlParams + 导航类参数)重建,
|
||||
// 确保 options/scene/H5 href 三个来源的参数都被带到登录回跳 URL 中(尤其 recommend_code)
|
||||
const backQuery = [];
|
||||
for (let key in options) {
|
||||
if (key === 'scene') continue;
|
||||
backQuery.push(key + '=' + encodeURIComponent(options[key]));
|
||||
if (this.uniacid) backQuery.push('uniacid=' + encodeURIComponent(this.uniacid));
|
||||
if (this.id) backQuery.push('id=' + encodeURIComponent(this.id));
|
||||
for (const k in this.urlParams) {
|
||||
const v = this.urlParams[k];
|
||||
if (v !== undefined && v !== null && v !== '') {
|
||||
backQuery.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
|
||||
}
|
||||
}
|
||||
// 导航类参数(不在 urlParams 中,单独收集)
|
||||
if (options && options.from) backQuery.push('from=' + encodeURIComponent(options.from));
|
||||
if (this.hideNavbar) backQuery.push('hide_navbar=1');
|
||||
if (this.showHomeBtn) backQuery.push('show_home=1');
|
||||
// #ifdef H5
|
||||
if (this.formTitle) backQuery.push('title=' + encodeURIComponent(this.formTitle));
|
||||
if (this.enableSetTitle) backQuery.push('enable_set_title=1');
|
||||
// #endif
|
||||
this.backUrl = '/pages_tool/form/formdata' + (backQuery.length ? '?' + backQuery.join('&') : '');
|
||||
|
||||
this.getData()
|
||||
@@ -322,6 +386,26 @@ export default {
|
||||
onShow() {
|
||||
this.updateNavbarTitle();
|
||||
},
|
||||
mounted() {
|
||||
// 1) 兜底 A:onLoad→getData 回调比 ns-login 挂载还早时,pendingLoginRequired=true
|
||||
// 挂载完成再最后尝试一次自动弹窗
|
||||
if (this.pendingLoginRequired) {
|
||||
const tryOpen = () => {
|
||||
try {
|
||||
if (this.$refs.login && typeof this.$refs.login.open === 'function') {
|
||||
this.$refs.login.open(this.backUrl);
|
||||
}
|
||||
} catch (e) { console.warn('[FORMDATA] mounted login.open failed:', e); }
|
||||
};
|
||||
this.$nextTick(tryOpen);
|
||||
this.pendingLoginRequired = false;
|
||||
}
|
||||
// 2) 兜底 B:登录态在组件创建之前就已写入(token 提前 race),但 info 因时序没拿到数据
|
||||
// 此时用户实际上已经登录,再拉一次 info 就能拿到真实表单数据
|
||||
if (this.storeToken && !this.detail) {
|
||||
this.$nextTick(() => { this.getData(); });
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// #ifndef H5
|
||||
// 返回上一页;用 navigateBack,栈深度>1 时由 hasBackStack 控制按钮可见性
|
||||
@@ -344,31 +428,123 @@ export default {
|
||||
form_id: this.id,
|
||||
uniacid: this.uniacid
|
||||
},
|
||||
success: res => {
|
||||
if (res.code == 0 && res.data) {
|
||||
this.detail = res.data
|
||||
this.updateNavbarTitle();
|
||||
// 行为设置(后端已合并默认值):验证码开启时拉取图形验证码
|
||||
this.behavior = (res.data.form_config && res.data.form_config.behavior) || {}
|
||||
if (this.behavior.captcha == 1 && !this.captchaLoaded) this.getCaptcha()
|
||||
// 登录回跳后自动续提交(填写需登录场景:H5 整页回跳 / MP 弹窗登录后)
|
||||
this.resumeSubmitAfterLogin()
|
||||
} else if (res.message == 'FORM_LOGIN_REQUIRED') {
|
||||
// 打开需登录:弹登录组件,登录成功后 watch storeToken 重新拉取
|
||||
this.$refs.login ? this.$refs.login.open(this.backUrl)
|
||||
: (this.formError = this.$lang('form.form.loginRequired'))
|
||||
} else if (res.message == 'FORM_NOT_OPEN') {
|
||||
this.formError = this.$lang('form.form.notOpen')
|
||||
} else {
|
||||
this.formError = res.message || ''
|
||||
}
|
||||
this.$refs.loadingCover && this.$refs.loadingCover.hide()
|
||||
},
|
||||
fail: () => {
|
||||
this.$refs.loadingCover && this.$refs.loadingCover.hide()
|
||||
}
|
||||
success: res => { this._handleInfoResponse(res); },
|
||||
fail: err => { this._handleInfoFailure(err); }
|
||||
})
|
||||
},
|
||||
// ====== 表单 info 响应统一处理(剥离自 getData,便于严格判定与复用) ======
|
||||
// 成功必须同时满足:code==0 + data 是对象 + 内含 json_data 或 form_config
|
||||
// 不满足 → 宽网判定是否是登录拦截:
|
||||
// - 显式消息:FORM_LOGIN_REQUIRED / LOGIN_REQUIRED / 请先登录
|
||||
// - 兜底:数据为空 + 用户未登录 + 有 id
|
||||
// 命中任一 → 一律弹登录 + 友好文案"请先登录",不再误显示"未获取到表单信息"
|
||||
_handleInfoResponse(res) {
|
||||
try {
|
||||
const payload = (res && typeof res === 'object') ? res : {};
|
||||
const dataObj = payload.data;
|
||||
|
||||
// —— 1) 严格判定:是否拿到了真正的表单结构? ——
|
||||
const hasValidFormData =
|
||||
payload.code == 0 &&
|
||||
dataObj !== null && dataObj !== undefined && dataObj !== '' &&
|
||||
typeof dataObj === 'object' &&
|
||||
(dataObj.json_data !== undefined || dataObj.form_config !== undefined);
|
||||
|
||||
if (hasValidFormData) {
|
||||
this.detail = dataObj;
|
||||
this.formError = '';
|
||||
this.updateNavbarTitle();
|
||||
this.behavior = (dataObj.form_config && dataObj.form_config.behavior) || {};
|
||||
if (this.behavior.captcha == 1 && !this.captchaLoaded) this.getCaptcha();
|
||||
this.resumeSubmitAfterLogin();
|
||||
// 本地缓存:下次再打开时若被登录拦截,可作为 skeleton 数据使用
|
||||
try { uni.setStorageSync('form_fields_cache_' + this.id, dataObj.json_data); } catch (e) {}
|
||||
return;
|
||||
}
|
||||
|
||||
// —— 2) 没拿到有效结构 → 宽网判定:是否属于"需要登录"场景? ——
|
||||
const msg = (payload.message || '').toString();
|
||||
const isFormNotOpen = (msg === 'FORM_NOT_OPEN');
|
||||
|
||||
const isLoginMsg =
|
||||
msg === 'FORM_LOGIN_REQUIRED' ||
|
||||
msg === 'LOGIN_REQUIRED' ||
|
||||
msg.indexOf('请先登录') !== -1 ||
|
||||
(msg.indexOf('登录') !== -1 && msg.indexOf('请') !== -1) ||
|
||||
(msg.toLowerCase().indexOf('login') !== -1 && msg.toLowerCase().indexOf('required') !== -1);
|
||||
|
||||
const dataIsEmpty =
|
||||
!dataObj ||
|
||||
dataObj === '' ||
|
||||
dataObj === null ||
|
||||
dataObj === undefined ||
|
||||
(typeof dataObj === 'object' && !dataObj.json_data && !dataObj.form_config &&
|
||||
((Array.isArray(dataObj) && dataObj.length === 0) ||
|
||||
(!Array.isArray(dataObj) && Object.keys(dataObj).length === 0)));
|
||||
|
||||
// 最宽判定:只要不是"表单未开放",且(显式登录消息 OR 空数据+未登录+有 id)都按登录拦截处理
|
||||
const shouldTriggerLogin =
|
||||
!isFormNotOpen &&
|
||||
(isLoginMsg || (dataIsEmpty && !this.storeToken && this.id));
|
||||
|
||||
if (shouldTriggerLogin) {
|
||||
this.formError = this.$lang('form.form.loginRequired');
|
||||
this._openLoginWithRetry();
|
||||
} else if (isFormNotOpen) {
|
||||
this.formError = this.$lang('form.form.notOpen');
|
||||
} else {
|
||||
this.formError = msg || '';
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[FORMDATA] _handleInfoResponse exception:', e && e.message ? e.message : e);
|
||||
this.formError = '';
|
||||
}
|
||||
this.$refs.loadingCover && this.$refs.loadingCover.hide();
|
||||
},
|
||||
// info 接口网络失败时同样判定:未登录+有 id 就先走登录流程
|
||||
_handleInfoFailure(err) {
|
||||
try {
|
||||
if (this.id && !this.storeToken) {
|
||||
this.formError = this.$lang('form.form.loginRequired');
|
||||
this._openLoginWithRetry();
|
||||
} else {
|
||||
this.formError = '';
|
||||
}
|
||||
} catch (e) {
|
||||
this.formError = '';
|
||||
}
|
||||
this.$refs.loadingCover && this.$refs.loadingCover.hide();
|
||||
},
|
||||
// 打开登录:3 次 setTimeout 重试(0ms / 60ms / 320ms)
|
||||
// 彻底覆盖 onLoad→回调→ns-login 尚未挂载的所有时序问题
|
||||
_openLoginWithRetry() {
|
||||
const url = this.backUrl;
|
||||
let attempts = 0;
|
||||
const delays = [0, 60, 320];
|
||||
const tryOnce = () => {
|
||||
try {
|
||||
if (this.$refs.login && typeof this.$refs.login.open === 'function') {
|
||||
this.$refs.login.open(url);
|
||||
this.pendingLoginRequired = false;
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[FORMDATA] login.open attempt ' + (attempts + 1) + ' failed:', e);
|
||||
}
|
||||
attempts++;
|
||||
if (attempts < delays.length) {
|
||||
setTimeout(tryOnce, delays[attempts]);
|
||||
} else {
|
||||
this.pendingLoginRequired = true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
tryOnce();
|
||||
},
|
||||
// 模板空态按钮的手动入口(最后一道防线)
|
||||
forceOpenLogin() {
|
||||
this._openLoginWithRetry();
|
||||
},
|
||||
updateNavbarTitle() {
|
||||
if (!this.detail?.form_name) return;
|
||||
let formName = this.detail.form_name;
|
||||
|
||||
Reference in New Issue
Block a user