diff --git a/FM-Wechat-Fronted/api/auth.js b/FM-Wechat-Fronted/api/auth.js index 19a94a32..40ed96ba 100644 --- a/FM-Wechat-Fronted/api/auth.js +++ b/FM-Wechat-Fronted/api/auth.js @@ -10,6 +10,13 @@ module.exports = { }) }, + getLoginShops(account) { + return request({ + url: `/auth/login-shops/${encodeURIComponent(account)}`, + method: 'GET' + }) + }, + register(data) { return request({ url: '/auth/register', @@ -57,6 +64,16 @@ module.exports = { }) }, + getPhoneLoginShops(phone) { + return request({ + url: `/user/check/login-shops/phone/${encodeURIComponent(phone)}`, + method: 'GET', + params: { + __baseUrl: API_DOMAIN + } + }) + }, + getCurrentAgreements() { return request({ url: '/legal-agreement/current', diff --git a/FM-Wechat-Fronted/pages/login/index.js b/FM-Wechat-Fronted/pages/login/index.js index 50a5cba5..556e20c5 100644 --- a/FM-Wechat-Fronted/pages/login/index.js +++ b/FM-Wechat-Fronted/pages/login/index.js @@ -20,6 +20,35 @@ function getSelectableShopIds(session) { return Array.isArray(session.shopIds) ? session.shopIds : [] } +function getSelectableShopNames(session) { + const userInfo = session.userInfo || {} + if (Array.isArray(userInfo.shopNames)) { + return userInfo.shopNames + } + return Array.isArray(session.shopNames) ? session.shopNames : [] +} + +function buildShopOptions(session) { + const shopIds = getSelectableShopIds(session) + const shopNames = getSelectableShopNames(session) + return shopIds.map((shopId, index) => ({ + id: shopId, + name: shopNames[index] || `店铺 ${shopId}` + })) +} + +function buildShopOptionsFromSelection(selection) { + const shops = selection && Array.isArray(selection.shops) ? selection.shops : [] + return shops.map(shop => ({ + id: shop.id, + name: shop.shopName || shop.name || `店铺 ${shop.id}` + })) +} + +function isShopSelectionRequiredError(error) { + return !!(error && error.message && error.message.indexOf('请选择登录店铺') !== -1) +} + Page({ data: { loginMode: 'phone', @@ -35,7 +64,11 @@ Page({ countdownTimer: null, agreementAccepted: false, agreements: [], - appName: BRAND_CONFIG.appName + appName: BRAND_CONFIG.appName, + shopSelectVisible: false, + shopSelectMode: '', + shopOptions: [], + switchingShop: false }, onInput(event) { @@ -122,13 +155,119 @@ Page({ async resolveLoginSession(data) { const session = buildSession(data) const userInfo = session.userInfo || {} - const shopIds = getSelectableShopIds(session) - if (userInfo.shopId || shopIds.length !== 1) { + if (userInfo.shopId) { return session } + const shopOptions = buildShopOptions(session) + if (!shopOptions.length) { + throw new Error('当前账号没有可用店铺,无法进入管理师端') + } const app = getApp() app.setSession(session) - return buildSession(await authApi.switchShop(shopIds[0])) + if (shopOptions.length === 1) { + return buildSession(await authApi.switchShop(shopOptions[0].id)) + } + this.setData({ + shopOptions, + shopSelectVisible: true, + shopSelectMode: 'switch' + }) + return null + }, + + buildLoginRequestData(shopId) { + const form = this.data.form + if (this.data.loginMode === 'phone') { + return { + phone: String(form.phone || '').trim(), + verificationCode: String(form.code || '').trim(), + ...this.buildAgreementPayload(), + ...(shopId ? { shopId } : {}) + } + } + return { + account: form.account, + password: form.password, + ...(shopId ? { shopId } : {}) + } + }, + + requestLogin(shopId) { + const data = this.buildLoginRequestData(shopId) + return this.data.loginMode === 'phone' + ? authApi.phoneLogin(data) + : authApi.login(data) + }, + + async loadLoginShopOptions() { + const form = this.data.form + const selection = this.data.loginMode === 'phone' + ? await authApi.getPhoneLoginShops(String(form.phone || '').trim()) + : await authApi.getLoginShops(String(form.account || '').trim()) + const shopOptions = buildShopOptionsFromSelection(selection) + if (!shopOptions.length) { + throw new Error((selection && selection.message) || '当前账号没有可用店铺,无法进入管理师端') + } + return shopOptions + }, + + async openLoginShopSelector() { + const shopOptions = await this.loadLoginShopOptions() + this.setData({ + shopOptions, + shopSelectVisible: true, + shopSelectMode: 'login' + }) + }, + + closeShopSelector() { + this.setData({ + shopOptions: [], + shopSelectMode: '', + shopSelectVisible: false + }) + }, + + async selectLoginShop(event) { + if (this.data.switchingShop) { + return + } + const shopId = event.currentTarget.dataset.id + if (!shopId) { + wx.showToast({ title: '请选择店铺', icon: 'none' }) + return + } + this.setData({ switchingShop: true }) + try { + if (this.data.shopSelectMode === 'login') { + await this.loginWithSelectedShop(shopId) + } else { + await this.switchSelectedShop(shopId) + } + } catch (error) { + wx.showToast({ title: error.message || '进入店铺失败', icon: 'none' }) + } finally { + this.setData({ switchingShop: false }) + } + }, + + async loginWithSelectedShop(shopId) { + const session = await this.resolveLoginSession(await this.requestLogin(shopId)) + if (!session) { + return + } + const app = getApp() + app.setSession(session) + this.closeShopSelector() + wx.reLaunch({ url: '/pages/planning/index' }) + }, + + async switchSelectedShop(shopId) { + const session = buildSession(await authApi.switchShop(shopId)) + const app = getApp() + app.setSession(session) + this.closeShopSelector() + wx.reLaunch({ url: '/pages/planning/index' }) }, startCountdown() { @@ -172,21 +311,23 @@ Page({ } this.setData({ loading: true }) try { - const data = this.data.loginMode === 'phone' - ? await authApi.phoneLogin({ - phone: String(form.phone || '').trim(), - verificationCode: String(form.code || '').trim(), - ...this.buildAgreementPayload() - }) - : await authApi.login({ - account: form.account, - password: form.password - }) + const data = await this.requestLogin('') const app = getApp() const session = await this.resolveLoginSession(data) + if (!session) { + return + } app.setSession(session) wx.reLaunch({ url: '/pages/planning/index' }) } catch (error) { + if (isShopSelectionRequiredError(error)) { + try { + await this.openLoginShopSelector() + } catch (shopError) { + wx.showToast({ title: shopError.message || '店铺列表加载失败', icon: 'none' }) + } + return + } wx.showToast({ title: error.message || '登录失败', icon: 'none' }) } finally { this.setData({ loading: false }) diff --git a/FM-Wechat-Fronted/pages/login/index.wxml b/FM-Wechat-Fronted/pages/login/index.wxml index c4709ddd..d2cbc161 100644 --- a/FM-Wechat-Fronted/pages/login/index.wxml +++ b/FM-Wechat-Fronted/pages/login/index.wxml @@ -44,4 +44,29 @@ + + + + + 选择店铺 + 请选择本次登录的店铺 + + + + 店 + + {{item.name}} + ID {{item.id}} + + + + 正在进入店铺... + + diff --git a/FM-Wechat-Fronted/pages/login/index.wxss b/FM-Wechat-Fronted/pages/login/index.wxss index 1139aad4..f54f5531 100644 --- a/FM-Wechat-Fronted/pages/login/index.wxss +++ b/FM-Wechat-Fronted/pages/login/index.wxss @@ -139,3 +139,105 @@ .wechat-btn { margin-top: 34rpx; } + +.shop-select-mask { + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; + z-index: 20; + display: flex; + align-items: center; + justify-content: center; + padding: 48rpx; + background: rgba(36, 33, 29, 0.38); +} + +.shop-select-dialog { + width: 100%; + max-height: 76vh; + padding: 28rpx; + border-radius: 8px; + background: #fffdf8; + box-shadow: 0 24rpx 64rpx rgba(58, 48, 34, 0.18); +} + +.shop-select-header { + display: flex; + flex-direction: column; + gap: 8rpx; + margin-bottom: 22rpx; +} + +.shop-select-title { + color: #24211d; + font-size: 32rpx; + font-weight: 900; +} + +.shop-select-subtitle { + color: #928a80; + font-size: 24rpx; +} + +.shop-option-list { + max-height: 520rpx; +} + +.shop-option { + display: flex; + align-items: center; + gap: 18rpx; + min-height: 112rpx; + margin-bottom: 14rpx; + padding: 18rpx; + border: 1rpx solid #ece3d7; + border-radius: 8px; + background: #fbf7f0; +} + +.shop-option.disabled { + opacity: 0.6; +} + +.shop-option-icon { + display: flex; + align-items: center; + justify-content: center; + width: 72rpx; + min-width: 72rpx; + height: 72rpx; + border-radius: 8px; + background: #e5eee9; + color: #55786d; + font-size: 26rpx; + font-weight: 900; +} + +.shop-option-copy { + display: flex; + min-width: 0; + flex: 1; + flex-direction: column; + gap: 8rpx; +} + +.shop-option-name { + color: #2f2a25; + font-size: 28rpx; + font-weight: 800; +} + +.shop-option-id { + color: #9a9288; + font-size: 22rpx; +} + +.shop-switching { + margin-top: 12rpx; + color: #6f8f83; + font-size: 24rpx; + font-weight: 700; + text-align: center; +}