123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <template>
- <view class="login-container">
- <view class="login-box-bg">
- <image
- src="https://images.unsplash.com/photo-1519751138087-5bf79df62d5b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wyMDUzMDJ8MHwxfHNlYXJjaHwyMHx8YmFja2dyb3VuZHxlbnwxfHx8fDE3MjExOTc4NjJ8MA&ixlib=rb-4.0.3&q=80&w=1080"
- mode="aspectFill"></image>
- </view>
- <view class="login-box">
- <view class="title">登录</view>
- <view class="sub-title">欢迎使用有极智慧园区管理软件</view>
- <view class="form">
- <view class="form-item">
- <view class="form-item-label">手机号</view>
- <view class="form-input-item">
- <uni-icons class="form-input-icons" type="person" size="24"></uni-icons>
- <view class="form-input-content">
- <input type="number" class="form-input" placeholder="请输入手机号" maxlength="11"
- v-model="form.phone" />
- </view>
- </view>
- </view>
- <!-- 获取验证码 -->
- <view class="form-item">
- <view class="form-item-label">验证码</view>
- <view class="form-input-item">
- <uni-icons class="form-input-icons" type="email" size="24"></uni-icons>
- <view class="form-input-content">
- <input type="number" class="form-input" maxlength="6" placeholder="请输入验证码"
- v-model="form.phoneCode" />
- </view>
- <text class="code-input-btn" :class="isDisabled ? 'disabled':''" @click="getImageCode">
- {{codeName}}
- </text>
- </view>
- </view>
- <view class="form-btn-container">
- <button class="form-btn" @click="submitLogin">登录</button>
- </view>
- </view>
- </view>
- <view class="login-bottom">
- <view class="line-text">or</view>
- <button class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
- <uni-icons type="weixin" size="24" color="#43b156"></uni-icons>
- </button>
- </view>
- <image-code :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>
- </image-code>
- </view>
- </template>
- <script>
- import {
- login,
- getImgCode,
- sendPhoneCode,
- getUserInfo
- } from '@/request/api/login'
- import {
- getProjectList
- } from '@/request/api/project'
- import {
- isTel
- } from '@/uitls/validate'
- import imageCode from "@/components/login/imageCode.vue";
- export default {
- data() {
- return {
- form: {
- phone: '',
- phoneCode: ''
- },
- captchaShow: false,
- imgCode: '',
- codeImg: '',
- codeName: '获取验证码', //获取验证码text
- isDisabled: false, //获取验证码按钮状态
- }
- },
- components: {
- imageCode
- },
- methods: {
- getImageCode() {
- if (!isTel(this.form.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.form.phone, this.imgCode).then(res => {
- if (res.code === 200) {
- this.codeReset();
- this.$toast('发送成功');
- this.imgCode = '';
- this.captchaShow = false;
- } else {
- this.imgCodeFunc();
- }
- })
- },
- 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.form.phoneCode) return this.$toast('请输入短信验证码');
- this.login(this.form);
- },
- login(postData) {
- uni.showLoading({
- title: '登录中'
- })
- // #ifdef MP-WEIXIN
- uni.login({
- provider: 'weixin',
- success: loginRes => {
- if (postData.pCode) 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);
- uni.removeStorageSync('chatToken');
- this.$chat.connect(userInfo.userId);
- this.initProjectData(userInfo.projectId);
- } else {
- uni.hideLoading();
- }
- })
- } else {
- uni.hideLoading();
- }
- },
- initProjectData(projectId) {
- getProjectList().then(res => {
- if (res.code == 200) {
- if (res.data.length === 0) {
- this.$store.dispatch('app/changeProject', {});
- this.$store.dispatch('app/changeIdentity', {
- id: 1,
- name: '客户',
- remark: '客户'
- });
- return this.successLogin();
- }
- let project = projectId === -1 ? res.data[0] : res.data.find(node => node.id ===
- projectId);
- this.$store.dispatch('app/changeProject', project);
- this.$store.dispatch('app/changeIdentity', project.projectListIdentity[0]);
- this.successLogin();
- } else {
- uni.hideLoading();
- }
- })
- },
- successLogin() {
- uni.setStorageSync('vuex_state', this.$store.state);
- this.$toast('登录成功');
- setTimeout(() => {
- uni.hideLoading();
- uni.navigateBack();
- }, 400)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .login-container {
- height: 100vh;
- background: #fff;
- display: flex;
- flex-direction: column;
- .login-box-bg {
- height: 300rpx;
- image {
- width: 100%;
- height: 100%;
- }
- }
- .login-box {
- padding: 0 30rpx;
- flex: 1;
- min-width: 640rpx;
- }
- .title {
- padding-top: 30rpx;
- font-size: 48rpx;
- font-weight: bold;
- }
- .sub-title {
- font-size: 24rpx;
- color: $uni-extra-color;
- margin-top: 10rpx;
- }
- //弹出输入框
- .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 {
- display: flex;
- flex-direction: column;
- align-items: center;
- margin-top: 30rpx;
- .form-item {
- width: 100%;
- margin-bottom: 30rpx;
- }
- .form-item-label {
- font-weight: 500;
- margin-bottom: 20rpx;
- }
- // 登录的input项
- .form-input-item {
- height: 88rpx;
- border-radius: 88rpx;
- border: 1px solid $uni-border-1;
- width: 100%;
- display: flex;
- align-items: center;
- box-sizing: border-box;
- .form-input-icons {
- padding: 0 20rpx 0 30rpx;
- .uni-icons {
- color: $uni-secondary-color !important;
- }
- }
- .form-input-content {
- flex: 1;
- width: 0;
- }
- .code-input-btn {
- color: $uni-primary;
- padding: 0 30rpx;
- }
- }
- // 登录按钮容器
- .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;
- }
- }
- }
- .login-bottom {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- padding-bottom: 30px;
- .wx-btn {
- margin-top: 40rpx;
- height: 80rpx;
- width: 160rpx;
- text-align: center;
- line-height: 80rpx;
- background: #fff;
- &::after {
- border-radius: 80rpx;
- border-color: $uni-border-1;
- }
- }
- .line-text {
- position: relative;
- text-align: center;
- color: $uni-secondary-color;
- width: 100%;
- &::before {
- content: '';
- position: absolute;
- left: 60rpx;
- right: calc(50% + 20px);
- height: 1px;
- background: $uni-border-1;
- top: 50%;
- }
- &::after {
- content: '';
- position: absolute;
- right: 60rpx;
- left: calc(50% + 20px);
- height: 1px;
- background: $uni-border-1;
- top: 50%;
- }
- }
- }
- }
- </style>
|