Compare commits

...

2 Commits

Author SHA1 Message Date
阿梦
d13f75ddc0 Merge branch 'master' of https://gitlab.lingxixue.com/dmg/lingxixue-mini-program 2026-05-28 16:32:43 +08:00
阿梦
a4317f2786 feat: 登录页家长模式修改 2026-05-28 16:32:41 +08:00
3 changed files with 401 additions and 76 deletions

View File

@ -22,6 +22,36 @@ export const psdLogin = (data: AccountLoginParams) =>
}, },
}); });
export interface ParentSmsLoginParams {
mobile: string;
smsCode: string;
}
/** 家长端 - 发送登录验证码 */
export const sendParentLoginCode = (mobile: string) =>
request({
url: '/parent/sendLoginCode',
method: 'POST',
headers: { Authorization: '' },
silent: true,
data: { mobile },
});
/** 家长端 - 手机号验证码登录 */
export const parentSmsLogin = (data: ParentSmsLoginParams) =>
request<string>({
url: '/parent/loginBySms',
method: 'POST',
headers: { Authorization: '' },
data: {
mobile: data.mobile,
smsCode: data.smsCode,
clientType: 'PARENT',
simSerialNumber: 'miniProgram',
model: 'wechat',
},
});
/** 退出登录 */ /** 退出登录 */
export const logout = () => export const logout = () =>
request({ request({

View File

@ -14,7 +14,7 @@
<image :src="logoUrl" mode="aspectFit" class="brand-logo-img" /> <image :src="logoUrl" mode="aspectFit" class="brand-logo-img" />
</view> </view>
<view class="brand-name">学小乐<text class="brand-name-ai">AI</text></view> <view class="brand-name">学小乐<text class="brand-name-ai">AI</text></view>
<view class="brand-slogan">智能学习助手 · 让学习更轻松</view> <view class="brand-slogan">智能学习助手 · 让学习更轻松1</view>
</view> </view>
<!-- 角色切换 --> <!-- 角色切换 -->
@ -24,64 +24,119 @@
v-for="item in roleOptions" v-for="item in roleOptions"
:key="item.value" :key="item.value"
:class="['role-tab', { active: role === item.value }]" :class="['role-tab', { active: role === item.value }]"
@click="role = item.value" @click="switchRole(item.value)"
> >
<text>{{ item.label }}</text> <text>{{ item.label }}</text>
</view> </view>
</view> </view>
<!-- 表单 --> <!-- 表单key 确保切换角色时微信小程序重新渲染 -->
<view class="form"> <view class="form" :key="role">
<view class="form-item" :class="{ focus: focus === 'account' }"> <block v-if="isParentLogin">
<view class="form-icon"> <block v-if="parentStage === 'bind'">
<svg viewBox="0 0 24 24" fill="none"> <view class="form-item" :class="{ focus: focus === 'phone' }">
<circle cx="12" cy="8" r="4" stroke="#4F46E5" stroke-width="1.6" /> <view class="form-icon">
<path d="M4 21c0-4.4 3.6-8 8-8s8 3.6 8 8" stroke="#4F46E5" stroke-width="1.6" <svg viewBox="0 0 24 24" fill="none">
stroke-linecap="round" /> <rect x="6" y="3" width="12" height="18" rx="2.5" stroke="#0891B2" stroke-width="1.6" />
</svg> <path d="M10 18h4" stroke="#0891B2" stroke-width="1.6" stroke-linecap="round" />
</view> </svg>
<input </view>
v-model="account" <input
class="form-input" v-model="phone"
placeholder="请输入账号" class="form-input"
placeholder-class="form-placeholder" type="number"
:adjust-position="false" maxlength="11"
@focus="focus = 'account'" placeholder="请输入手机号"
@blur="focus = ''" placeholder-class="form-placeholder"
@confirm="onLogin" :adjust-position="false"
/> @focus="focus = 'phone'"
</view> @blur="focus = ''"
@confirm="onLogin"
/>
</view>
<view class="form-item" :class="{ focus: focus === 'password' }"> <view class="form-item" :class="{ focus: focus === 'code' }">
<view class="form-icon"> <view class="form-icon">
<svg viewBox="0 0 24 24" fill="none"> <svg viewBox="0 0 24 24" fill="none">
<rect x="4" y="11" width="16" height="9" rx="2" stroke="#4F46E5" stroke-width="1.6" /> <rect x="4" y="9" width="16" height="12" rx="2" stroke="#0891B2" stroke-width="1.6" />
<path d="M8 11V8a4 4 0 0 1 8 0v3" stroke="#4F46E5" stroke-width="1.6" /> <path d="M8 9V7a4 4 0 1 1 8 0v2" stroke="#0891B2" stroke-width="1.6" />
</svg> </svg>
</view>
<input
v-model="smsCode"
class="form-input"
type="number"
maxlength="6"
placeholder="请输入验证码"
placeholder-class="form-placeholder"
:adjust-position="false"
@focus="focus = 'code'"
@blur="focus = ''"
@confirm="onLogin"
/>
<view
class="sms-btn"
:class="{ disabled: smsCountdown > 0 || sendingCode }"
@click="onSendCode"
>
<text>{{ smsBtnText }}</text>
</view>
</view>
</block>
</block>
<block v-else>
<view class="form-item" :class="{ focus: focus === 'account' }">
<view class="form-icon">
<svg viewBox="0 0 24 24" fill="none">
<circle cx="12" cy="8" r="4" stroke="#4F46E5" stroke-width="1.6" />
<path d="M4 21c0-4.4 3.6-8 8-8s8 3.6 8 8" stroke="#4F46E5" stroke-width="1.6"
stroke-linecap="round" />
</svg>
</view>
<input
v-model="account"
class="form-input"
placeholder="请输入账号"
placeholder-class="form-placeholder"
:adjust-position="false"
@focus="focus = 'account'"
@blur="focus = ''"
@confirm="onLogin"
/>
</view> </view>
<input
v-model="password" <view class="form-item" :class="{ focus: focus === 'password' }">
class="form-input" <view class="form-icon">
:password="hidePassword" <svg viewBox="0 0 24 24" fill="none">
placeholder="请输入密码" <rect x="4" y="11" width="16" height="9" rx="2" stroke="#4F46E5" stroke-width="1.6" />
placeholder-class="form-placeholder" <path d="M8 11V8a4 4 0 0 1 8 0v3" stroke="#4F46E5" stroke-width="1.6" />
:adjust-position="false" </svg>
@focus="focus = 'password'" </view>
@blur="focus = ''" <input
@confirm="onLogin" v-model="password"
/> class="form-input"
<view class="form-eye" @click="hidePassword = !hidePassword"> :password="hidePassword"
<svg v-if="hidePassword" viewBox="0 0 24 24" fill="none"> placeholder="请输入密码"
<path d="m2 2 20 20M10 5c.7-.1 1.3-.1 2-.1 7 0 10 7 10 7-.6 1.4-1.5 2.6-2.5 3.6M6.6 6.6C3.5 8.6 2 12 2 12s3 7 10 7c1.4 0 2.6-.3 3.7-.7" placeholder-class="form-placeholder"
stroke="#94A3B8" stroke-width="1.6" stroke-linecap="round" /> :adjust-position="false"
</svg> @focus="focus = 'password'"
<svg v-else viewBox="0 0 24 24" fill="none"> @blur="focus = ''"
<path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7z" stroke="#4F46E5" @confirm="onLogin"
stroke-width="1.6" /> />
<circle cx="12" cy="12" r="3" stroke="#4F46E5" stroke-width="1.6" /> <view class="form-eye" @click="hidePassword = !hidePassword">
</svg> <svg v-if="hidePassword" viewBox="0 0 24 24" fill="none">
<path d="m2 2 20 20M10 5c.7-.1 1.3-.1 2-.1 7 0 10 7 10 7-.6 1.4-1.5 2.6-2.5 3.6M6.6 6.6C3.5 8.6 2 12 2 12s3 7 10 7c1.4 0 2.6-.3 3.7-.7"
stroke="#94A3B8" stroke-width="1.6" stroke-linecap="round" />
</svg>
<svg v-else viewBox="0 0 24 24" fill="none">
<path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7z" stroke="#4F46E5"
stroke-width="1.6" />
<circle cx="12" cy="12" r="3" stroke="#4F46E5" stroke-width="1.6" />
</svg>
</view>
</view> </view>
</view> </block>
</view> </view>
<view class="agreement"> <view class="agreement">
@ -92,25 +147,40 @@
</view> </view>
<!-- 登录按钮 --> <!-- 登录按钮 -->
<view :class="['btn-login', { loading: submitting }]" @click="onLogin"> <block v-if="isParentLogin && parentStage === 'authorize'">
<text v-if="!submitting"> </text> <view :class="['btn-login', 'btn-wx', { loading: submitting }]" @click="onParentAuthorize">
<view v-else class="btn-loader"> <text v-if="!submitting"> </text>
<view class="btn-loader-dot"></view> <view v-else class="btn-loader">
<view class="btn-loader-dot"></view> <view class="btn-loader-dot"></view>
<view class="btn-loader-dot"></view> <view class="btn-loader-dot"></view>
<view class="btn-loader-dot"></view>
</view>
</view> </view>
</view> </block>
<block v-else>
<view :class="['btn-login', { loading: submitting, parent: isParentLogin }]" @click="onLogin">
<text v-if="!submitting">{{ isParentLogin ? ' ' : ' ' }}</text>
<view v-else class="btn-loader">
<view class="btn-loader-dot"></view>
<view class="btn-loader-dot"></view>
<view class="btn-loader-dot"></view>
</view>
</view>
</block>
<view class="hint"> <view class="hint">
<text>登录账号由学校老师统一发放</text> <text v-if="isParentLogin && parentStage === 'authorize'">授权后将自动检测绑定关系</text>
<text v-else-if="isParentLogin">未绑定将需要手机号验证绑定</text>
<text v-else>登录账号由学校老师统一发放</text>
</view> </view>
</view> </view>
</view> </view>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue'; import { computed, onUnmounted, ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app'; import { onLoad } from '@dcloudio/uni-app';
import { sendParentLoginCode } from '@/api/login';
import { useUserStore } from '@/state/modules/user'; import { useUserStore } from '@/state/modules/user';
import { useTeacherStore } from '@/state/modules/teacher'; import { useTeacherStore } from '@/state/modules/teacher';
import { setCache, getCache } from '@/utils/cache'; import { setCache, getCache } from '@/utils/cache';
@ -118,12 +188,19 @@ import { setCache, getCache } from '@/utils/cache';
const OSS_URL = 'https://xxl-1313840333.cos.ap-guangzhou.myqcloud.com'; const OSS_URL = 'https://xxl-1313840333.cos.ap-guangzhou.myqcloud.com';
const logoUrl = `${OSS_URL}/urm/logo.png`; const logoUrl = `${OSS_URL}/urm/logo.png`;
const PHONE_REG = /^1[3-9]\d{9}$/;
const account = ref(''); const account = ref('');
const password = ref(''); const password = ref('');
const phone = ref('');
const smsCode = ref('');
const hidePassword = ref(true); const hidePassword = ref(true);
const redirect = ref(''); const redirect = ref('');
const submitting = ref(false); const submitting = ref(false);
const sendingCode = ref(false);
const smsCountdown = ref(0);
const focus = ref<string>(''); const focus = ref<string>('');
let smsTimer: ReturnType<typeof setInterval> | null = null;
type LoginRole = 'student' | 'teacher' | 'parent'; type LoginRole = 'student' | 'teacher' | 'parent';
const role = ref<LoginRole>('student'); const role = ref<LoginRole>('student');
const enterDebug = ref(0); const enterDebug = ref(0);
@ -136,6 +213,63 @@ const roleOptions: { value: LoginRole; label: string }[] = [
const userStore = useUserStore(); const userStore = useUserStore();
const teacherStore = useTeacherStore(); const teacherStore = useTeacherStore();
const isParentLogin = computed(() => role.value === 'parent');
type ParentStage = 'authorize' | 'bind';
const parentStage = ref<ParentStage>('authorize');
const MOCK_PARENT_WX_BOUND = false;
const smsBtnText = computed(() => {
if (smsCountdown.value > 0) return `${smsCountdown.value}s`;
return '获取验证码';
});
const switchRole = (value: LoginRole) => {
if (role.value === value) return;
role.value = value;
focus.value = '';
if (value === 'parent') {
parentStage.value = 'authorize';
phone.value = '';
smsCode.value = '';
smsCountdown.value = 0;
sendingCode.value = false;
if (smsTimer) {
clearInterval(smsTimer);
smsTimer = null;
}
}
};
const isValidPhone = (value: string) => PHONE_REG.test(value.trim());
const startSmsCountdown = (seconds = 60) => {
if (smsTimer) {
clearInterval(smsTimer);
smsTimer = null;
}
smsCountdown.value = seconds;
smsTimer = setInterval(() => {
if (smsCountdown.value <= 1) {
smsCountdown.value = 0;
if (smsTimer) {
clearInterval(smsTimer);
smsTimer = null;
}
return;
}
smsCountdown.value -= 1;
}, 1000);
};
onUnmounted(() => {
if (smsTimer) {
clearInterval(smsTimer);
smsTimer = null;
}
});
onLoad((options: any) => { onLoad((options: any) => {
const cachedRole = getCache('role'); const cachedRole = getCache('role');
if (cachedRole === 'teacher' || cachedRole === 'student' || cachedRole === 'parent') { if (cachedRole === 'teacher' || cachedRole === 'student' || cachedRole === 'parent') {
@ -169,7 +303,7 @@ const getLoginTargetUrl = () => {
return value.startsWith('/pages/teacher/') ? value : teacherHome; return value.startsWith('/pages/teacher/') ? value : teacherHome;
} }
if (role.value === 'parent') { if (isParentLogin.value) {
if (!value) return parentHome; if (!value) return parentHome;
if (value === 'parent-home' || value === '/pages/parent') return parentHome; if (value === 'parent-home' || value === '/pages/parent') return parentHome;
return value.startsWith('/pages/parent/') ? value : parentHome; return value.startsWith('/pages/parent/') ? value : parentHome;
@ -180,13 +314,121 @@ const getLoginTargetUrl = () => {
return value.startsWith('/pages/teacher') || value.startsWith('/pages/parent') ? studentHome : value; return value.startsWith('/pages/teacher') || value.startsWith('/pages/parent') ? studentHome : value;
}; };
const onLogin = async () => { const onSendCode = async () => {
if (submitting.value) return; if (smsCountdown.value > 0 || sendingCode.value) return;
if (role.value === 'parent') { const mobile = phone.value.trim();
setCache('role', 'parent'); if (!isValidPhone(mobile)) {
uni.reLaunch({ url: '/pages/parent/home/index' }); uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
return; return;
} }
sendingCode.value = true;
try {
await sendParentLoginCode(mobile);
uni.showToast({ title: '验证码已发送', icon: 'none' });
startSmsCountdown();
} catch (e: any) {
uni.showToast({ title: e?.message || '发送失败', icon: 'none' });
} finally {
sendingCode.value = false;
}
};
const checkParentBoundByWxCode = async (_code: string): Promise<{ bound: boolean }> => {
await new Promise((resolve) => setTimeout(resolve, 350));
return { bound: MOCK_PARENT_WX_BOUND };
};
const onParentAuthorize = async () => {
if (submitting.value) return;
submitting.value = true;
uni.showLoading({ title: '授权中...', mask: true });
try {
const loginRes = await new Promise<UniApp.LoginRes>((resolve, reject) => {
uni.login({
provider: 'weixin',
success: (res) => resolve(res),
fail: (err) => reject(err),
});
});
const code = loginRes?.code || '';
if (!code) {
uni.hideLoading();
uni.showToast({ title: '获取微信 code 失败', icon: 'none' });
return;
}
const { bound } = await checkParentBoundByWxCode(code);
if (bound) {
setCache('role', 'parent');
uni.hideLoading();
uni.showToast({ title: '登录成功', icon: 'success' });
setTimeout(() => navigateAfterLogin(), 800);
return;
}
parentStage.value = 'bind';
uni.hideLoading();
uni.showToast({ title: '请先绑定手机号', icon: 'none' });
} catch (e: any) {
uni.hideLoading();
uni.showToast({ title: e?.message || '授权失败', icon: 'none' });
} finally {
submitting.value = false;
}
};
const navigateAfterLogin = () => {
const targetUrl = getLoginTargetUrl();
if (
targetUrl === '/pages/index/index'
|| targetUrl === '/pages/teacher/index/index'
|| targetUrl === '/pages/parent/home/index'
) {
uni.reLaunch({ url: targetUrl });
} else {
uni.redirectTo({ url: targetUrl });
}
};
const onLogin = async () => {
if (submitting.value) return;
if (isParentLogin.value) {
if (parentStage.value === 'authorize') {
await onParentAuthorize();
return;
}
const mobile = phone.value.trim();
const code = smsCode.value.trim();
if (!isValidPhone(mobile)) {
uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
return;
}
if (!code) {
uni.showToast({ title: '请输入验证码', icon: 'none' });
return;
}
submitting.value = true;
uni.showLoading({ title: '登录中...', mask: true });
try {
await userStore.parentLogin({ mobile, smsCode: code });
uni.hideLoading();
uni.showToast({ title: '登录成功', icon: 'success' });
setTimeout(() => navigateAfterLogin(), 800);
} catch (e: any) {
uni.hideLoading();
uni.showToast({ title: e?.message || '登录失败', icon: 'none' });
} finally {
submitting.value = false;
}
return;
}
if (!account.value.trim()) { if (!account.value.trim()) {
uni.showToast({ title: '请输入账号', icon: 'none' }); uni.showToast({ title: '请输入账号', icon: 'none' });
return; return;
@ -221,16 +463,7 @@ const onLogin = async () => {
console.warn('teacher.getLoginUser err', e); console.warn('teacher.getLoginUser err', e);
} }
} }
const targetUrl = getLoginTargetUrl(); navigateAfterLogin();
if (
targetUrl === '/pages/index/index'
|| targetUrl === '/pages/teacher/index/index'
|| targetUrl === '/pages/parent/home/index'
) {
uni.reLaunch({ url: targetUrl });
} else {
uni.redirectTo({ url: targetUrl });
}
}, 800); }, 800);
} catch (e: any) { } catch (e: any) {
uni.hideLoading(); uni.hideLoading();
@ -457,6 +690,26 @@ const onLogin = async () => {
font-size: 28rpx; font-size: 28rpx;
} }
.sms-btn {
flex-shrink: 0;
min-width: 168rpx;
height: 64rpx;
padding: 0 20rpx;
border-radius: 18rpx;
display: flex;
align-items: center;
justify-content: center;
color: #0891b2;
background: #ecfeff;
font-size: 24rpx;
font-weight: 700;
&.disabled {
color: #94a3b8;
background: #f1f5f9;
}
}
.form-eye { .form-eye {
width: 56rpx; width: 56rpx;
height: 56rpx; height: 56rpx;
@ -518,6 +771,36 @@ const onLogin = async () => {
&.loading { &.loading {
opacity: 0.85; opacity: 0.85;
} }
&.parent {
background: linear-gradient(180deg, #22d3ee 0%, #0891b2 100%);
box-shadow:
0 12rpx 28rpx rgba(8, 145, 178, 0.4),
inset 0 -6rpx 0 rgba(0, 0, 0, 0.15),
inset 0 4rpx 0 rgba(255, 255, 255, 0.2);
&:active {
box-shadow:
0 4rpx 12rpx rgba(8, 145, 178, 0.4),
inset 0 -3rpx 0 rgba(0, 0, 0, 0.15),
inset 0 4rpx 0 rgba(255, 255, 255, 0.2);
}
}
}
.btn-wx {
background: linear-gradient(180deg, #22c55e 0%, #16a34a 100%);
box-shadow:
0 12rpx 28rpx rgba(22, 163, 74, 0.4),
inset 0 -6rpx 0 rgba(0, 0, 0, 0.15),
inset 0 4rpx 0 rgba(255, 255, 255, 0.2);
&:active {
box-shadow:
0 4rpx 12rpx rgba(22, 163, 74, 0.4),
inset 0 -3rpx 0 rgba(0, 0, 0, 0.15),
inset 0 4rpx 0 rgba(255, 255, 255, 0.2);
}
} }
.btn-loader { .btn-loader {

View File

@ -3,9 +3,11 @@ import { ref, computed } from 'vue';
import { setCache, getCache, removeCache } from '@/utils/cache'; import { setCache, getCache, removeCache } from '@/utils/cache';
import { import {
psdLogin as psdLoginApi, psdLogin as psdLoginApi,
parentSmsLogin as parentSmsLoginApi,
getUserInfo as getUserInfoApi, getUserInfo as getUserInfoApi,
logout as logoutApi, logout as logoutApi,
type AccountLoginParams, type AccountLoginParams,
type ParentSmsLoginParams,
} from '@/api/login'; } from '@/api/login';
export interface UserInfo { export interface UserInfo {
@ -88,6 +90,15 @@ export const useUserStore = defineStore('user', () => {
await fetchUserInfo(); await fetchUserInfo();
}; };
/** 家长端 - 手机号验证码登录 */
const parentLogin = async (data: ParentSmsLoginParams) => {
await clear();
const res = await parentSmsLoginApi(data);
token.value = res.data;
setCache('token', res.data);
setCache('role', 'parent');
};
/** 退出登录 */ /** 退出登录 */
const logout = async () => { const logout = async () => {
try { try {
@ -111,6 +122,7 @@ export const useUserStore = defineStore('user', () => {
isLogin, isLogin,
gradeId, gradeId,
accountLogin, accountLogin,
parentLogin,
fetchUserInfo, fetchUserInfo,
logout, logout,
clear, clear,