123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <template>
- <view class="login-container">
- <view class="logo">
- <image :src="logoImage" mode="aspectFill"></image>
- </view>
- <view class="form">
- <view class="form-item">
- <view class="form-input-item">
- <view class="form-input-left">
- <uni-icons class="form-input-icons" type="person" size="24"></uni-icons>
- </view>
- <view class="form-input-content">
- <input type="number" class="form-input" placeholder="请输入手机号" maxlength="11"
- v-model="loginForm.phone" />
- </view>
- </view>
- </view>
- <!-- 获取验证码 -->
- <view class="form-item">
- <view class="form-input-item">
- <view class="form-input-left">
- <uni-icons class="form-input-icons" type="email" size="24"></uni-icons>
- </view>
- <view class="form-input-content code-input-wrapper">
- <input type="number" class="form-input" maxlength="6" placeholder="请输入验证码"
- v-model="loginForm.phoneCode" />
- <button class="code-input-btn" :class="isDisabled ? 'disabled':''" @click="getImageCode">
- {{codeName}}
- </button>
- </view>
- </view>
- </view>
- <view class="form-btn-container">
- <button class="form-btn" @click="submitLogin">登录</button>
- </view>
- <!-- #ifdef MP-WEIXIN -->
- <view class="form-btn-container">
- <button type="primary" class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
- 微信手机号一键登录
- </button>
- </view>
- <!-- #endif -->
- </view>
- <model :show="captchaShow" title="请填写图形验证码" @onConfirm="confirmHandle" @onCancel="captchaShow = false">
- <view class="code-input-container">
- <input type="text" class="code-input" placeholder="输入图形验证码" maxlength="6" v-model="imgCode" />
- <view class="code-canvas-wrapper">
- <img v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc">
- </view>
- </view>
- </model>
- </view>
- </template>
- <script>
- import {
- login,
- getImgCode,
- sendPhoneCode,
- getUserInfo
- } from '@/request/api/login'
- import {
- isTel
- } from '@/uitls/validate'
- import model from "@/components/login/model.vue";
- export default {
- data() {
- return {
- logoImage: require('@/static/image/login/logo.png'),
- loginForm: {
- phone: '18888888888',
- phoneCode: '888888'
- },
- captchaShow: false,
- imgCode: '',
- codeImg: '',
- codeName: '获取验证码', //获取验证码text
- isDisabled: false, //获取验证码按钮状态
- }
- },
- components: {
- model
- },
- onLoad() {},
- methods: {
- getImageCode() {
- if (!isTel(this.loginForm.phone)) return this.$toast('请输入正确的手机号');
- if (this.isDisabled) return;
- this.imgCodeFunc();
- this.captchaShow = true;
- },
- imgCodeFunc() {
- getImgCode().then(res => {
- if (res.code === 200) {
- this.codeImg = res.data.pngBase64;
- }
- })
- },
- confirmHandle() {
- if (!this.imgCode) return this.$toast('请输入图片验证码');
- sendPhoneCode(this.loginForm.phone, this.imgCode).then(res => {
- if (res.code === 200) {
- this.codeReset();
- this.$toast('发送成功');
- this.captchaShow = false;
- }
- })
- },
- codeReset() {
- //重置获取验证码倒计时
- let codeNumber = 60;
- codeNumber--;
- this.handleCode(codeNumber);
- let codeRestFn = setInterval(() => {
- codeNumber--;
- this.handleCode(codeNumber);
- if (codeNumber == 0) clearInterval(codeRestFn); //停止
- }, 1000);
- },
- handleCode(codeNumber) {
- //code操作
- this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
- this.isDisabled = codeNumber == 0 ? false : true;
- },
- wxPhoneLogin(e) { //微信手机号一键登录
- if (!e.detail.code) return;
- this.login({
- pCode: e.detail.code
- })
- },
- submitLogin() { //手机验证码登录
- if (!this.loginForm.phoneCode) return this.$toast('请输入短信验证码');
- this.login(this.loginForm);
- },
- login(postData) {
- uni.showLoading({
- title: '登录中'
- })
- // #ifdef MP-WEIXIN
- uni.login({
- provider: 'weixin',
- success: loginRes => {
- if (postData.phone != '18888888888') postData['code'] = loginRes.code;
- login(postData).then(this.successFunc);
- }
- })
- // #endif
- // #ifndef MP-WEIXIN
- login(postData).then(this.successFunc);
- // #endif
- },
- successFunc(res) {
- if (res.code === 200) {
- uni.setStorageSync('token', res.data.token);
- getUserInfo().then(user => {
- if (user.code === 200) {
- let userInfo = user.data;
- this.$store.dispatch('app/changeOrganization', userInfo.organization);
- this.$store.dispatch('app/changeUser', userInfo);
- this.$toast('登录成功');
- setTimeout(() => {
- uni.hideLoading();
- uni.navigateBack();
- }, 400)
- } else {
- uni.hideLoading();
- }
- })
- } else {
- uni.hideLoading();
- }
- }
- }
- }
- </script>
- <style lang="scss">
- .login-container {
- width: 100%;
- height: 100%;
- display: flex;
- flex-direction: column;
- align-items: center;
- .logo {
- margin-top: 80rpx;
- width: 150rpx;
- height: 150rpx;
- image {
- width: 100%;
- height: 100%;
- }
- }
- //弹出输入框
- .code-input-container {
- display: flex;
- align-items: center;
- margin-top: 20rpx;
- .code-input {
- background: #F7F8FA;
- width: 306rpx;
- height: 84rpx;
- line-height: 84rpx;
- padding: 0 20rpx;
- border-radius: 20rpx;
- box-sizing: border-box;
- &::placeholder {
- line-height: 84rpx;
- }
- }
- .code-canvas-wrapper {
- flex: 1;
- .code-image {
- width: 100%;
- height: 84rpx;
- }
- }
- }
- .form {
- margin-top: 70rpx;
- display: flex;
- flex-direction: column;
- align-items: center;
- width: 686rpx;
- margin: 70rpx auto;
- // 登录表单项
- .form-item {
- width: 100%;
- border-radius: 200rpx;
- height: 100rpx;
- background-color: $uni-white;
- display: flex;
- margin-bottom: 50rpx;
- &:last-of-type {
- margin-bottom: 0;
- }
- }
- // 登录的input项
- .form-input-item {
- height: 100%;
- width: 100%;
- display: flex;
- align-items: center;
- box-sizing: border-box;
- // input左边区域
- .form-input-left {
- display: flex;
- align-items: center;
- margin-left: 30rpx;
- .form-input-icons {
- /* #ifdef H5 */
- color: $uni-secondary-color !important;
- /* #endif */
- /* #ifdef MP-WEIXIN */
- text {
- color: $uni-secondary-color !important;
- }
- /* #endif */
- }
- }
- // 手机号信息
- .phone-info {
- margin-left: 20rpx;
- // 手机号信息文字
- .phone-info-txt {
- margin-right: 8rpx;
- }
- // 下拉按钮图片大小
- .phone-info-img {
- width: 24rpx;
- height: 24rpx;
- }
- }
- // input内容区域
- .form-input-content {
- flex: 1 1 auto;
- padding-right: 20rpx;
- position: relative;
- // 具体的input样式
- .form-input {
- width: 100%;
- padding-left: 20rpx;
- // 覆盖uni的默认的提示
- ::v-deep .uni-input-placeholder {
- color: #C8C9CC;
- }
- }
- }
- }
- // 验证码
- .code-input-wrapper {
- display: flex;
- align-items: center;
- .code-input-btn {
- width: auto;
- width: 200rpx;
- height: 64rpx;
- font-size: 24rpx;
- padding: 12rpx 40rpx;
- white-space: nowrap;
- display: flex;
- align-items: center;
- justify-content: center;
- border-radius: 200rpx;
- background: $uni-primary;
- color: #fff;
- &.disabled {
- opacity: 0.5;
- }
- &::after {
- display: none;
- }
- }
- }
- // 登录按钮容器
- .form-btn-container {
- margin-top: 20rpx;
- width: 100%;
- }
- // 登录按钮
- .form-btn {
- height: 88rpx;
- border-radius: 200rpx;
- padding: 24rpx 32rpx;
- line-height: 40rpx;
- color: $uni-white;
- background-color: $uni-primary;
- font-size: 32rpx;
- &.disabled {
- opacity: 0.5;
- }
- &::after {
- display: none;
- }
- }
- .wx-btn {
- height: 88rpx;
- padding: 24rpx 32rpx;
- border-radius: 200rpx;
- line-height: 40rpx;
- font-size: 32rpx;
- color: $uni-white;
- background-color: $uni-success;
- &::after {
- display: none;
- }
- }
- }
- }
- </style>
|