diff --git a/App.vue b/App.vue index e6f8add..822f9b4 100644 --- a/App.vue +++ b/App.vue @@ -151,16 +151,90 @@ if (e === undefined) return {}; + // 优先解析扫码参数,确保 store_id 在后续逻辑执行前被正确设置 + const sceneStr = e.query && e.query.scene ? decodeURIComponent(e.query.scene) : ''; + console.log('[扫码调试] 原始 scene 参数:', sceneStr); + + if (sceneStr) { + // 判断scene中是否包含sal_id + const isSalespersonId = sceneStr.includes('sal_id'); + console.log('[扫码调试] 是否包含 sal_id:', isSalespersonId); + + // 解析 STORE_QR_CODE_ 参数 + const sceneParts = sceneStr.split('STORE_QR_CODE_'); + console.log('[扫码调试] scene 分割结果:', sceneParts); + + if (sceneParts.length > 1 && sceneParts[1]) { + // 处理可能包含 & 的情况,提取 store_id + const storeIdPart = sceneParts[1].split('&')[0]; + const decodedStoreId = decodeURIComponent(storeIdPart); + + if (decodedStoreId && decodedStoreId !== 'undefined') { + console.log('[扫码调试] 设置 store_id 和 stores_id:', decodedStoreId); + uni.setStorageSync('stores_id', decodedStoreId); + uni.setStorageSync('store_id', decodedStoreId); + } + } + + // 解析其他参数 + const st_id = sceneStr.split('&'); + const doctors_id = sceneStr.split('='); + + // 解析 doctors_id + if (st_id[0] && st_id[0].includes('su_id=')) { + const doctorsId = st_id[0].split('su_id=')[1]; + if (doctorsId) { + uni.setStorageSync('doctors_id', doctorsId); + console.log('[扫码调试] 设置 doctors_id:', doctorsId); + } + } + + // 解析 sal_id + if (isSalespersonId && st_id.length > 1 && st_id[1].includes('sal_id=')) { + const salId = st_id[1].split('sal_id=')[1]; + if (salId) { + uni.setStorageSync('sal_id', salId); + console.log('[扫码调试] 设置 sal_id:', salId); + } + } + + // 解析 st_id + if (doctors_id.length > 2 && doctors_id[2]) { + uni.setStorageSync('st_id', doctors_id[2]); + console.log('[扫码调试] 设置 st_id:', doctors_id[2]); + } + + // 绑定推广员(如果已登录) + const userId = uni.getStorageSync('user_id'); + if (isSalespersonId && userId && st_id.length > 1 && st_id[1].includes('sal_id=')) { + const salId = st_id[1].split('sal_id=')[1]; + if (salId) { + bindUserApi({ + user_id: userId, + salesperson_id: salId, + }).then((res) => { + console.log('[扫码调试] 绑定推广员成功:', res); + }).catch((err) => { + console.error('[扫码调试] 绑定推广员失败:', err); + }); + } + } + } + // 检查是否已登录,如果未登录才检查配置并决定是否静默登录 const token = uni.getStorageSync('token'); const userInfo = uni.getStorageSync('userinfo'); if (!token || !userInfo) { // 未登录,获取系统配置,判断是否需要静默登录 + // 此时 store_id 应该已经从扫码参数中正确设置 try { + const currentStoreId = uni.getStorageSync('store_id') || '11001'; + console.log('[扫码调试] 获取系统配置,使用 store_id:', currentStoreId); + const configRes = getSystemConfig({ method: "get", data: { - store_id: uni.getStorageSync('store_id') || '11001' + store_id: currentStoreId } }); @@ -184,59 +258,20 @@ // 执行静默登录(不显示提示,不跳转) silentLogin().then((loginResult) => { if (loginResult.success) { - console.log('静默登录成功'); + console.log('[扫码调试] 静默登录成功'); } else { - console.error('静默登录失败:', loginResult.message); + console.error('[扫码调试] 静默登录失败:', loginResult.message); } }).catch((error) => { - console.error('静默登录异常:', error); + console.error('[扫码调试] 静默登录异常:', error); }); } // 如果配置为需要登录页(need_login: true),不执行任何操作,等待用户手动登录 } catch (error) { // 配置接口调用失败,不影响应用启动,仅记录日志 - console.error('获取系统配置失败:', error); + console.error('[扫码调试] 获取系统配置失败:', error); } } - const scene = decodeURIComponent(e.query.scene) - // 这里接收扫码参数 - if (scene) { - // 判断scene中是否包含sal_id - let isSalespersonId = decodeURIComponent(e.query.scene).includes('sal_id'); - const scene = decodeURIComponent(e.query.scene).split('STORE_QR_CODE_') - if (scene[1]) { - uni.setStorageSync('stores_id', scene[1]) - uni.setStorageSync('store_id', scene[1]) - } - // uni.setStorageSync('stores_id', scene[1]) - const st_id = decodeURIComponent(e.query.scene).split('&') - let store_id = decodeURIComponent(e.query.scene).split('STORE_QR_CODE_') - store_id = decodeURIComponent(store_id[1]).split('&') - const doctors_id = decodeURIComponent(e.query.scene).split('=') - if (store_id[0] !== 'undefined') { - uni.setStorageSync('stores_id', store_id[0]) - uni.setStorageSync('store_id', store_id[0]) - } - uni.setStorageSync('doctors_id', st_id[0].split('su_id=')[1]) - if (isSalespersonId) { - uni.setStorageSync('sal_id', st_id[1].split('sal_id=')[1]) - } - uni.setStorageSync('st_id', doctors_id[2]) - - let userId = uni.getStorageSync('user_id') - if (isSalespersonId && userId) { - bindUserApi({ - user_id: userId, - salesperson_id: st_id[1].split('sal_id=')[1], - }).then((res) => { - console.log('userId', userId, st_id[0].split('su_id=')[1], res); - }) - } - // console.log(st_id, '4444111111111'); - // console.log(scene, '4444'); - } - // const scene = decodeURIComponent(e.query.scene).split('STORE_QR_CODE_') - // console.log(scene, '11111'); }, onHide: function() { // console.log('App Hide') diff --git a/common/utils/auth.js b/common/utils/auth.js index b02fc48..190be4b 100644 --- a/common/utils/auth.js +++ b/common/utils/auth.js @@ -21,7 +21,7 @@ export function isGuestMode() { export function setGuestMode() { uni.setStorageSync(GUEST_MODE_KEY, true); // 同时设置store_id为11001 - uni.setStorageSync('store_id', GUEST_STORE_ID); + // uni.setStorageSync('store_id', GUEST_STORE_ID); } /** diff --git a/pages/home/home.vue b/pages/home/home.vue index 4b0126b..299dc3d 100644 --- a/pages/home/home.vue +++ b/pages/home/home.vue @@ -398,13 +398,20 @@ export default { this.getBannered(); } else { this.storeinfo = res.data.data.list.filter((item) => item.is_online == 1) - uni.setStorageSync('store_id', this.storeinfo[0].store.id) - this.headerTitle = this.storeinfo[0].store['name'] + // 只在 store_id 不存在时才设置,避免覆盖扫码设置的 store_id + if (!uni.getStorageSync('store_id') && this.storeinfo.length > 0) { + uni.setStorageSync('store_id', this.storeinfo[0].store.id) + } + if (this.storeinfo.length > 0) { + this.headerTitle = this.storeinfo[0].store['name'] + } this.getBannered(); } } else { - // 未登录时不跳转,使用默认store_id=11001 - uni.setStorageSync('store_id', '11001'); + // 接口失败时,只在 store_id 不存在时才设置默认值,避免覆盖扫码设置的 store_id + if (!uni.getStorageSync('store_id')) { + uni.setStorageSync('store_id', '11001'); + } this.getBannered(); } }) diff --git a/request/api/api.js b/request/api/api.js index 2c4cb03..d695f17 100644 --- a/request/api/api.js +++ b/request/api/api.js @@ -495,7 +495,8 @@ export async function getVersion(params) { // 获取系统配置信息 export async function getSystemConfig(params) { - let data = await http('/xkApi/platform/config', params) + // let data = await http('/xkApi/platform/config', params) + let data = await http('/xkApi/guest/guest-platform/config', params, 3) return data } diff --git a/utils/login.js b/utils/login.js index 9f51bfb..be86606 100644 --- a/utils/login.js +++ b/utils/login.js @@ -77,18 +77,28 @@ export async function performLogin(options = { silent: false, toastRef: null }) } // 5.3 处理门店切换/设置 - const storeId = uni.getStorageSync('store_id'); - if (storeId) { + // 优先使用已存在的 store_id(可能是扫码设置的),避免覆盖 + const existingStoreId = uni.getStorageSync('store_id'); + if (existingStoreId) { + // 如果已存在 store_id(可能是扫码设置的),使用现有的值进行门店切换 + console.log('[登录调试] 使用已存在的 store_id 进行门店切换:', existingStoreId); storeChange({ method: "post", data: { - store_id: storeId + store_id: existingStoreId } }).catch((err) => { - console.error('切换门店失败:', err); + console.error('[登录调试] 切换门店失败:', err); }); } else { - uni.setStorageSync('store_id', res.data.data.data.store_id); + // 只有在 store_id 不存在时才使用服务器返回的值 + const serverStoreId = res.data.data.data.store_id; + if (serverStoreId) { + console.log('[登录调试] store_id 不存在,使用服务器返回的值:', serverStoreId); + uni.setStorageSync('store_id', serverStoreId); + } else { + console.warn('[登录调试] 服务器未返回 store_id,保持默认值'); + } } // 5.4 清除 Guest 模式标记