| | |
| | | <view class="title">欢迎登录</view> |
| | | |
| | | <view class="tabs"> |
| | | <view class="tab" :class="loginType === 'code' ? 'active' : 'normal'" @tap="switchLoginType('code')">验证码登录</view> |
| | | <view class="tab" :class="loginType === 'password' ? 'active' : 'normal'" @tap="switchLoginType('password')">密码登录</view> |
| | | <view class="tab" :class="loginType === 'code' ? 'active' : 'normal'" @tap="switchLoginType('code')">验证码登录 |
| | | </view> |
| | | <view class="tab" :class="loginType === 'password' ? 'active' : 'normal'" |
| | | @tap="switchLoginType('password')">密码登录</view> |
| | | </view> |
| | | |
| | | <view class="form"> |
| | | <view class="input-group"> |
| | | <input |
| | | class="input" |
| | | type="number" |
| | | maxlength="11" |
| | | v-model="phone" |
| | | placeholder="输入手机号" |
| | | placeholder-class="placeholder-style" |
| | | /> |
| | | <view |
| | | class="get-code-btn" |
| | | :class="{ disabled: countdown > 0 }" |
| | | v-if="loginType === 'code'" |
| | | @tap="handleSendCode" |
| | | > |
| | | <input class="input" type="number" maxlength="11" v-model="phone" placeholder="输入手机号" |
| | | placeholder-class="placeholder-style" /> |
| | | <view class="get-code-btn" :class="{ disabled: countdown > 0 }" v-if="loginType === 'code'" |
| | | @tap="handleSendCode"> |
| | | {{ countdown > 0 ? `${countdown}s` : '获取验证码' }} |
| | | </view> |
| | | </view> |
| | | |
| | | <view class="input-group"> |
| | | <input |
| | | class="input" |
| | | :type="loginType === 'code' ? 'number' : 'text'" |
| | | :password="loginType === 'password'" |
| | | :maxlength="loginType === 'code' ? 6 : 32" |
| | | v-model="credential" |
| | | :placeholder="loginType === 'code' ? '输入验证码' : '输入密码'" |
| | | placeholder-class="placeholder-style" |
| | | /> |
| | | <input class="input" :type="loginType === 'code' ? 'number' : 'text'" |
| | | :password="loginType === 'password'" :maxlength="loginType === 'code' ? 6 : 32" v-model="credential" |
| | | :placeholder="loginType === 'code' ? '输入验证码' : '输入密码'" placeholder-class="placeholder-style" /> |
| | | </view> |
| | | </view> |
| | | |
| | | <view class="login-btn" @tap="handleLogin">{{ loading ? '登录中...' : '登录' }}</view> |
| | | |
| | | <view class="action-links" :class="{ 'center': loginType === 'code', 'space-between': loginType === 'password' }"> |
| | | <view class="action-links" |
| | | :class="{ 'center': loginType === 'code', 'space-between': loginType === 'password' }"> |
| | | <text class="register-link" @tap="goRegister">没有账号?点击注册</text> |
| | | <text class="forgot-password" v-if="loginType === 'password'" @tap="goForgotPassword">忘记密码?</text> |
| | | </view> |
| | |
| | | url: '/pages/home/home' |
| | | }) |
| | | }, 300) |
| | | } catch (error) { |
| | | } catch (error: any) { |
| | | const errorCode = error?.code |
| | | const errorMessage = typeof error?.errMsg === 'string' ? error.errMsg : '' |
| | | uni.showToast({ |
| | | title: '微信登录失败', |
| | | title: errorCode === -7 |
| | | ? '请重新云打包,并检查 manifest 中微信登录配置' |
| | | : (errorMessage || '微信登录失败'), |
| | | icon: 'none' |
| | | }) |
| | | } finally { |
| | |
| | | */ |
| | | const requestWechatAuthorization = () => { |
| | | return new Promise<WechatLoginResult>((resolve, reject) => { |
| | | // #ifdef APP-PLUS |
| | | plus.oauth.getServices((services) => { |
| | | const weixinService = services.find((service) => service.id === 'weixin') |
| | | if (!weixinService) { |
| | | reject({ |
| | | code: -3, |
| | | errMsg: '当前环境不支持微信登录' |
| | | }) |
| | | return |
| | | } |
| | | if (!weixinService.nativeClient) { |
| | | reject({ |
| | | code: -8, |
| | | errMsg: '请先安装微信客户端' |
| | | }) |
| | | return |
| | | } |
| | | weixinService.authorize((event : any) => { |
| | | const code = normalizeOptionalText(event?.code) |
| | | if (!code) { |
| | | reject({ |
| | | code: -99, |
| | | errMsg: '微信授权未返回有效code' |
| | | }) |
| | | return |
| | | } |
| | | resolve({ |
| | | code |
| | | }) |
| | | }, (error : any) => { |
| | | reject({ |
| | | code: error?.code, |
| | | errMsg: error?.message || '微信授权失败' |
| | | }) |
| | | }, { |
| | | scope: 'snsapi_userinfo', |
| | | state: 'login' |
| | | }) |
| | | }, (error : any) => { |
| | | reject({ |
| | | code: error?.code, |
| | | errMsg: error?.message || '获取微信登录服务失败' |
| | | }) |
| | | }) |
| | | return |
| | | // #endif |
| | | |
| | | uni.login({ |
| | | provider: 'weixin', |
| | | success: (res) => { |