feat: 新增身份选择弹窗并优化登录跳转逻辑
- 新增身份选择组件,用户登录后未选择身份时弹出选择框 - 优化登录跳转逻辑,支持页面栈返回模式 - 修复跳转时参数处理的边界情况 - 新增服务配置缓存机制,减少重复请求 - 优化小程序跳转逻辑,增加防重复点击锁 - 修复订单页联系人信息初始化逻辑
这个提交包含在:
@@ -4,6 +4,26 @@
|
||||
import { get, post, uploadFile } from './request';
|
||||
import config from '@/config/config.js';
|
||||
|
||||
const SERVICE_CONFIG_CACHE_TTL = 5 * 60 * 1000;
|
||||
|
||||
function resolveTargetMiniProgramEnv() {
|
||||
// #ifdef MP-WEIXIN
|
||||
try {
|
||||
const getter = (typeof uni !== 'undefined' && typeof uni.getAccountInfoSync === 'function')
|
||||
? uni.getAccountInfoSync
|
||||
: (typeof wx !== 'undefined' && typeof wx.getAccountInfoSync === 'function' ? wx.getAccountInfoSync : null);
|
||||
if (getter) {
|
||||
const info = getter();
|
||||
const envVersion = info && info.miniProgram && info.miniProgram.envVersion;
|
||||
if (envVersion === 'develop' || envVersion === 'trial') {
|
||||
return envVersion;
|
||||
}
|
||||
}
|
||||
} catch (e) {}
|
||||
// #endif
|
||||
return 'release';
|
||||
}
|
||||
|
||||
export default {
|
||||
/* ---------- 公共 / 上传 / 字典 ---------- */
|
||||
upload: (filePath, category = 'other') => uploadFile(filePath, category),
|
||||
@@ -126,6 +146,7 @@ export default {
|
||||
service_corp_id: String(cached.service_corp_id || '').trim(),
|
||||
service_kf_url: String(cached.service_kf_url || '').trim(),
|
||||
master_appid: String(cached.master_appid || '').trim(),
|
||||
updated_at: Number(cached.updated_at || 0),
|
||||
};
|
||||
}
|
||||
} catch (e) {}
|
||||
@@ -136,11 +157,13 @@ export default {
|
||||
service_corp_id: '',
|
||||
service_kf_url: '',
|
||||
master_appid: '',
|
||||
updated_at: 0,
|
||||
};
|
||||
},
|
||||
getServiceConfig: async function (force = false) {
|
||||
const cached = this.getCachedServiceConfig();
|
||||
if (!force && (cached.service_phone || cached.service_corp_id || cached.service_kf_url) && cached.master_appid) {
|
||||
const isFresh = cached.updated_at > 0 && (Date.now() - cached.updated_at < SERVICE_CONFIG_CACHE_TTL);
|
||||
if (!force && isFresh && cached.master_appid) {
|
||||
return cached;
|
||||
}
|
||||
try {
|
||||
@@ -152,6 +175,7 @@ export default {
|
||||
service_corp_id: String(res.service_corp_id || '').trim(),
|
||||
service_kf_url: String(res.service_kf_url || '').trim(),
|
||||
master_appid: String(res.master_appid || '').trim(),
|
||||
updated_at: Date.now(),
|
||||
};
|
||||
uni.setStorageSync('yjyf_service_config', cfg);
|
||||
if (cfg.service_phone) {
|
||||
@@ -166,33 +190,58 @@ export default {
|
||||
}
|
||||
return cached;
|
||||
},
|
||||
_openingMasterMini: false,
|
||||
openMasterMiniProgram: async function (path = 'pages/master/hall') {
|
||||
let cfg = await this.getServiceConfig();
|
||||
if (!cfg.master_appid) {
|
||||
cfg = await this.getServiceConfig(true);
|
||||
}
|
||||
const targetAppId = cfg.master_appid;
|
||||
if (this._openingMasterMini) return;
|
||||
this._openingMasterMini = true;
|
||||
const releaseLock = () => {
|
||||
setTimeout(() => { this._openingMasterMini = false; }, 600);
|
||||
};
|
||||
const cached = this.getCachedServiceConfig();
|
||||
// #ifdef MP-WEIXIN
|
||||
if (!targetAppId) {
|
||||
uni.showModal({
|
||||
title: '优匠优服师傅端',
|
||||
content: '后台暂未配置师傅端小程序 AppID,请在微信搜索打开「优匠师傅端」小程序或联系平台管理员在后台系统配置中完善。',
|
||||
showCancel: false,
|
||||
confirmText: '知道了',
|
||||
confirmColor: '#F0710D'
|
||||
const launch = (appId) => {
|
||||
if (!appId) {
|
||||
releaseLock();
|
||||
uni.showModal({
|
||||
title: '优匠优服师傅端',
|
||||
content: '后台暂未配置师傅端小程序 AppID,请在微信搜索打开「优匠优服师傅端」小程序或联系平台管理员在后台系统配置中完善。',
|
||||
showCancel: false,
|
||||
confirmText: '知道了',
|
||||
confirmColor: '#F0710D'
|
||||
});
|
||||
return;
|
||||
}
|
||||
uni.navigateToMiniProgram({
|
||||
appId: appId,
|
||||
path: path,
|
||||
envVersion: resolveTargetMiniProgramEnv(),
|
||||
complete: () => {
|
||||
releaseLock();
|
||||
},
|
||||
fail: (err) => {
|
||||
if (err && err.errMsg && err.errMsg.indexOf('cancel') !== -1) return;
|
||||
console.warn('[openMasterMiniProgram] navigateToMiniProgram fail:', err);
|
||||
uni.showToast({ title: '打开师傅端小程序失败,请稍后重试', icon: 'none' });
|
||||
}
|
||||
});
|
||||
};
|
||||
if (cached.master_appid) {
|
||||
launch(cached.master_appid);
|
||||
if (!cached.updated_at || Date.now() - cached.updated_at >= SERVICE_CONFIG_CACHE_TTL) {
|
||||
this.getServiceConfig(true).catch(() => {});
|
||||
}
|
||||
return;
|
||||
}
|
||||
uni.navigateToMiniProgram({
|
||||
appId: targetAppId,
|
||||
path: path,
|
||||
fail: (err) => {
|
||||
if (err && err.errMsg && err.errMsg.indexOf('cancel') !== -1) return;
|
||||
uni.showToast({ title: '打开师傅端小程序失败,请稍后重试', icon: 'none' });
|
||||
}
|
||||
});
|
||||
try {
|
||||
const cfg = await this.getServiceConfig(true);
|
||||
launch(cfg.master_appid);
|
||||
} catch (e) {
|
||||
releaseLock();
|
||||
uni.showToast({ title: '获取师傅端配置失败,请稍后重试', icon: 'none' });
|
||||
}
|
||||
// #endif
|
||||
// #ifndef MP-WEIXIN
|
||||
releaseLock();
|
||||
uni.showModal({
|
||||
title: '前往优匠优服师傅端',
|
||||
content: '请在微信中搜索「优匠优服师傅端」小程序进行师傅实名入驻与接单。',
|
||||
|
||||
115
yjyf-app/utils/login-navigation.mjs
普通文件
115
yjyf-app/utils/login-navigation.mjs
普通文件
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* 登录跳转与回返路由决策助手
|
||||
* 区分“保留原页面栈直接返回”与“重定向刷新/切换目标页”两种模式。
|
||||
*/
|
||||
|
||||
const PRESERVE_STATE_ROUTES = new Set([
|
||||
'pages/order/quick'
|
||||
]);
|
||||
|
||||
/**
|
||||
* 规范化页面路由(去除前导斜杠与查询参数)
|
||||
* @param {string} routeOrUrl
|
||||
* @returns {string}
|
||||
*/
|
||||
export function normalizeRoutePath(routeOrUrl) {
|
||||
const raw = String(routeOrUrl || '').trim();
|
||||
if (!raw) return '';
|
||||
const pathOnly = raw.split('?')[0].split('#')[0];
|
||||
return pathOnly.replace(/^\/+/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前路由在触发登录后是否应直接 navigateBack 保留原页面实例状态
|
||||
* @param {string} route
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function shouldPreservePageOnLogin(route) {
|
||||
return PRESERVE_STATE_ROUTES.has(normalizeRoutePath(route));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建登录页跳转 URL
|
||||
* @param {string} targetRedirect
|
||||
* @param {{ back?: boolean }} [options]
|
||||
* @returns {string}
|
||||
*/
|
||||
export function buildLoginUrl(targetRedirect = '', options = {}) {
|
||||
const params = [];
|
||||
if (targetRedirect) {
|
||||
params.push(`redirect=${encodeURIComponent(targetRedirect)}`);
|
||||
}
|
||||
if (options && options.back) {
|
||||
params.push('back=1');
|
||||
}
|
||||
return params.length ? `/pages/login/login?${params.join('&')}` : '/pages/login/login';
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析登录页 onLoad 参数
|
||||
* @param {Record<string, any>} options
|
||||
* @returns {{ redirectUrl: string, preferBack: boolean }}
|
||||
*/
|
||||
export function parseLoginParams(options = {}) {
|
||||
let redirectUrl = '';
|
||||
if (options && options.redirect) {
|
||||
let decoded = String(options.redirect);
|
||||
try {
|
||||
decoded = decodeURIComponent(decoded);
|
||||
} catch (e) {}
|
||||
if (decoded.startsWith('%2F') || decoded.startsWith('%2f')) {
|
||||
try {
|
||||
decoded = decodeURIComponent(decoded);
|
||||
} catch (e) {}
|
||||
}
|
||||
redirectUrl = decoded;
|
||||
}
|
||||
const preferBack = String((options && options.back) || '') === '1';
|
||||
return { redirectUrl, preferBack };
|
||||
}
|
||||
|
||||
/**
|
||||
* 决策登录完成后的页面流转动作
|
||||
* @param {{ redirectUrl?: string, preferBack?: boolean, pages?: Array<{ route?: string }> }} ctx
|
||||
* @returns {{ type: 'navigateBack' | 'redirectTo' | 'reLaunch', url?: string, fallbackType?: 'reLaunch', fallbackUrl?: string }}
|
||||
*/
|
||||
export function resolvePostLoginAction(ctx = {}) {
|
||||
const redirectUrl = String(ctx.redirectUrl || '').trim();
|
||||
const preferBack = !!ctx.preferBack;
|
||||
const pages = Array.isArray(ctx.pages) ? ctx.pages : [];
|
||||
|
||||
if (preferBack && pages.length > 1) {
|
||||
const prevPage = pages[pages.length - 2];
|
||||
const prevRoute = normalizeRoutePath(prevPage && prevPage.route);
|
||||
const targetRoute = normalizeRoutePath(redirectUrl);
|
||||
if (!targetRoute || !prevRoute || prevRoute === targetRoute) {
|
||||
return {
|
||||
type: 'navigateBack',
|
||||
fallbackType: 'reLaunch',
|
||||
fallbackUrl: redirectUrl || '/pages/index/index'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (redirectUrl) {
|
||||
if (redirectUrl.startsWith('/pages/index/') || redirectUrl.startsWith('/pages/mine/')) {
|
||||
return { type: 'reLaunch', url: redirectUrl };
|
||||
}
|
||||
return {
|
||||
type: 'redirectTo',
|
||||
url: redirectUrl,
|
||||
fallbackType: 'reLaunch',
|
||||
fallbackUrl: redirectUrl
|
||||
};
|
||||
}
|
||||
|
||||
if (pages.length > 1) {
|
||||
return {
|
||||
type: 'navigateBack',
|
||||
fallbackType: 'reLaunch',
|
||||
fallbackUrl: '/pages/index/index'
|
||||
};
|
||||
}
|
||||
|
||||
return { type: 'reLaunch', url: '/pages/index/index' };
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
* - 内置 POST 请求 500ms 防并发快速连击锁
|
||||
*/
|
||||
import config from '@/config/config.js';
|
||||
import { buildLoginUrl, shouldPreservePageOnLogin } from '@/utils/login-navigation.mjs';
|
||||
|
||||
export const BASE_URL = (config.baseUrl || config.dev).replace(/\/+$/, '') + '/api';
|
||||
|
||||
@@ -51,19 +52,21 @@ export function isLogin() {
|
||||
|
||||
let loginRedirectLock = false;
|
||||
|
||||
export function toLogin(redirect) {
|
||||
export function toLogin(redirect, options = {}) {
|
||||
clearAuth();
|
||||
if (loginRedirectLock) return;
|
||||
loginRedirectLock = true;
|
||||
setTimeout(() => {
|
||||
loginRedirectLock = false;
|
||||
}, 800);
|
||||
let targetRedirect = redirect || '';
|
||||
if (!targetRedirect) {
|
||||
try {
|
||||
const pages = getCurrentPages() || [];
|
||||
const cur = pages[pages.length - 1];
|
||||
if (cur && cur.route && cur.route !== 'pages/login/login') {
|
||||
let targetRedirect = typeof redirect === 'string' ? redirect : '';
|
||||
let currentRoute = '';
|
||||
try {
|
||||
const pages = getCurrentPages() || [];
|
||||
const cur = pages[pages.length - 1];
|
||||
if (cur && cur.route && cur.route !== 'pages/login/login') {
|
||||
currentRoute = cur.route;
|
||||
if (!targetRedirect) {
|
||||
const opts = cur.options || {};
|
||||
const qs = Object.keys(opts)
|
||||
.filter((k) => opts[k] !== undefined && opts[k] !== '')
|
||||
@@ -71,11 +74,13 @@ export function toLogin(redirect) {
|
||||
.join('&');
|
||||
targetRedirect = '/' + cur.route + (qs ? `?${qs}` : '');
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
const url = targetRedirect
|
||||
? `/pages/login/login?redirect=${encodeURIComponent(targetRedirect)}`
|
||||
: '/pages/login/login';
|
||||
}
|
||||
} catch (e) {}
|
||||
const shouldBack = Boolean(
|
||||
(options && options.back) ||
|
||||
(typeof redirect !== 'string' && shouldPreservePageOnLogin(currentRoute))
|
||||
);
|
||||
const url = buildLoginUrl(targetRedirect, { back: shouldBack });
|
||||
uni.navigateTo({
|
||||
url,
|
||||
fail: () => {
|
||||
|
||||
在新工单中引用
屏蔽一个用户