login.vue 15.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
<template>
  <el-dialog close-icon="CircleClose" center
             v-model="show" class="pcloginpop" width="450px" :append-to-body="true" :title="title"
             destroy-on-close :show-close="showClose" :close-on-click-modal="false" @close="close"
  >
    <div class="pd10"></div>
    <el-form v-if="loginStatus==0" ref="loginRef" :model="loginForm"
             :rules="language==0?loginRules:loginRules_en" class="login-form">
      <el-form-item prop="username">

        <el-input
            v-model.trim="loginForm.username"
            size="large"
            auto-complete="off"
            :placeholder="language==0?'账号(注册时使用的邮箱)':'Account (e-mail)'"
        >

        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model.trim="loginForm.password"
            type="password"
            size="large" show-password
            auto-complete="off"
            :placeholder="language==0?'密码':'Password'"
            @keyup.enter="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item v-if="captchaEnabled" prop="code">
        <el-input
            v-model.trim="loginForm.code"
            size="large"
            auto-complete="off"
            :placeholder="language==0?'验证码':'Code'"
            @keyup.enter="handleLogin"
        >
          <template #append>
            <div class="login-code">
              <img :src="codeUrl" class="login-code-img" @click="getCode">
            </div>
          </template>
        </el-input>

      </el-form-item>

      <el-form-item style="width:100%;">
        <el-button
            class="loginBtn"
            :loading="loading"
            size="large"
            type="primary"
            style="width:100%;"
            @click.prevent="handleLogin"
        >
          <span v-if="!loading">{{ language == 0 ? '登 录' : 'Login' }}</span>
          <span v-else>{{ language == 0 ? '登 录 中...' : 'Logging in' }}</span>
        </el-button>
      </el-form-item>
      <el-form-item>
        <!--        <span style="color: #999;font-size: 12px;">*默认密码是会员号+证件号后四位+)*%</span>-->
        <div style="display: flex;justify-content: space-between;width: 100%;">
          <a class="text-primary"
             @click="showChangePassword">{{ language == 0 ? '忘记密码' : 'Forgot password' }}?</a>
          <a @click="showRegister" class="text-primary">{{ language == 0 ? '账号注册' : 'Register' }}></a>
        </div>
      </el-form-item>
      <el-form-item>
        <div class="agreeLine" v-if="language==0">
          登录或注册即代表同意
          <a @click="goAgreement" target="_blank">《用户协议》</a>
          <a @click="goPolicy" target="_blank">《隐私政策》</a>
        </div>
        <div class="agreeLine" v-else>
          Registering and logging in means agreeing to the User
          <a @click="goAgreement" target="_blank">Agreement</a> and
          <a @click="goPolicy">Privacy Policy</a>
        </div>
      </el-form-item>
    </el-form>

    <div v-if="loginStatus==1">
      <a class="poLeft" @click="close()"><el-icon><ArrowLeftBold /></el-icon></a>
      <div class="rItem r1" @click="goRegister(1)">
        <h3>{{ language == 0 ? '机构用户' : 'Organization' }}</h3>
      </div>
      <div class="rItem r2" @click="goRegister(0)">
        <h3>{{ language == 0 ? '个人用户' : 'Individual' }}</h3>
      </div>
    </div>
    <div v-if="loginStatus==2">
      <a class="poLeft" @click="close()"><el-icon><ArrowLeftBold /></el-icon></a>
      <el-form ref="changePasswordRef" :model="changePasswordForm"
               :rules="language==0?changePasswordRules:changePasswordRules_en" class="login-form">
        <el-form-item prop="username">

          <el-input
              v-model.trim="changePasswordForm.username"
              size="large"
              auto-complete="off" @change="changePasswordFormUsername"
              :placeholder="language==0?'注册时使用的邮箱':'E-mail'"
          >

          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
              v-model.trim="changePasswordForm.code"
              size="large"
              auto-complete="off"
              :placeholder="language==0?'验证码':'Code'"
              @keyup.enter="handleLogin"
          >
            <template #append>
              <el-button type="primary" plain style="width: 110px;" @click="sendsmsMsg">
                <van-count-down  v-if="counting" :time="60000" format="ss" @finish="counting=false">
                  <template #default="timeData">
                    <span class="text-primary">{{ timeData.seconds }}{{ language == 0 ? '秒' : 's' }}</span>
                  </template>
                </van-count-down>
<!--                <count-down v-slot="{ totalSeconds }" :time="60000" @end="counting=false">-->
<!--                  {{ totalSeconds }} {{ language == 0 ? '秒' : 's' }}-->
<!--                </count-down>-->
                <span v-else>
                  {{ language == 0 ? '发送验证码' : 'Send' }}
                </span>
              </el-button>
            </template>
          </el-input>
          <!--          <div class="vcodeBox" :style="isShow?'height:240px':'height:0'">-->
          <Vcode :successText="successVcode" :failText="failVcode" :slider-text="sliderText" :zIndex="9999" :show="isShow" @success="codeSuccess"></Vcode>
          <!--          </div>-->
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model.trim="changePasswordForm.password"
              type="password"
              size="large" show-password
              auto-complete="off"
              :placeholder="language==0?'新密码':'New password'"
              @keyup.enter="handleLogin"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
              v-model.trim="changePasswordForm.confirmPassword"
              type="password"
              size="large" show-password
              auto-complete="off"
              :placeholder="language==0?'确认密码':'Confirm password'"
          >
          </el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
              class="loginBtn"
              :loading="loading"
              size="large"
              type="primary"
              style="width:100%;"
              @click.prevent="handleChangePassword"
          >
            <span v-if="language==0">确定</span>
            <span v-else>Submit</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script setup>
import {getCurrentInstance, ref, watch} from 'vue'
import Vcode from "vue3-puzzle-vcode"
import CountDown from '@chenfengyuan/vue-countdown'
import {forgetPassword, getCodeImg} from '@/api/login'
import * as match from '@/apiPc/match'
import Cookies from 'js-cookie'
import useUserStore from '@/store/modules/user'

const language = useStorage('language', 0)
import {useRouter} from 'vue-router'
import {ElMessage, ElMessageBox} from 'element-plus'
import cache from "@/plugins/cache";
import {useStorage} from "@vueuse/core/index";
import {getCaptchaSms, getCaptchaSms2} from "@/apiPc/match";

const emit = defineEmits(['submitForm'])
const userStore = useUserStore()
const router = useRouter()
const {proxy} = getCurrentInstance()
const loginForm = ref({
  username: '',
  password: '',
  rememberMe: false,
  code: '',
  uuid: ''
})
const countDown = ref(null)
const loginRules = {
  username: [{required: true, trigger: 'change', message: '请输入您的会员号'}],
  password: [{required: true, trigger: 'change', message: '请输入您账号密码'}],
  code: [{required: true, trigger: 'change', message: '请输入验证码'}]
}
const changePasswordRules = {
  username: [{required: true, trigger: 'change', message: '请输入您的账号'}],
  password: [{required: true, trigger: 'change', message: '请输入您的密码'}],
  confirmPassword: [{required: true, trigger: 'change', message: '请再次输入您的密码'}],
  code: [{required: true, trigger: 'change', message: '请输入验证码'}]
}
const loginRules_en = {
  username: [{required: true, trigger: 'change', message: 'Please enter '}],
  password: [{required: true, trigger: 'change', message: 'Please enter '}],
  code: [{required: true, trigger: 'change', message: 'Please enter '}]
}
const changePasswordRules_en = {
  username: [{required: true, trigger: 'change', message: 'Please enter '}],
  password: [{required: true, trigger: 'change', message: 'Please enter '}],
  confirmPassword: [{required: true, trigger: 'change', message: 'Please enter '}],
  code: [{required: true, trigger: 'change', message: 'Please enter '}]
}

const show = ref(false)
const loginStatus = ref(0)
const title = ref('')
const showClose = ref(true)
const codeUrl = ref('')
const loading = ref(false)

// 验证码开关
const captchaEnabled = ref(true)
//忘记密码
const isShow = ref(false)
const isCodeTrue = ref(false)
const counting = ref(false)
const changePasswordForm = ref({
  username: '',
  password: '',
  confirmPassword: ''
})
const failVcode = ref('验证失败,请重试')
const successVcode = ref('验证通过!')
const sliderText = ref('拖动滑块完成拼图')
const open = (params) => {
  console.log(params)
  show.value = true
  showClose.value = !(params?.notShowClose || false)
  loginStatus.value = params?.active || 0
  changePasswordForm.value.username = params?.account || ''
  if (language.value == 0) {
    title.value = '用户登录'
  }
  if (language.value == 1) {
    failVcode.value = 'Error!'
    successVcode.value = 'Success!'
    sliderText.value = 'Drag the slider to complete the puzzle'
    title.value = 'LOGIN'
  }
  getCode()
  getCookie()
}
defineExpose({
  open
})

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set('telno', loginForm.value.telno, {expires: 30})
        Cookies.set('rememberMe', loginForm.value.rememberMe, {expires: 30})
      } else {
        // 否则移除
        Cookies.remove('telno')
        Cookies.remove('rememberMe')
      }
      // 调用action的登录方法
      userStore.loginPc(loginForm.value).then(() => {
        // router.push({ path: redirect.value || '/index' })
        show.value = false
        emit('submitForm')
      }).catch(() => {
        loading.value = false
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}

function close() {
  loginStatus.value = 0
  title.value = language.value == 0?'用户登录':'LOGIN'
}

function showChangePassword() {
  loginStatus.value = 2
  title.value = language.value == 0?'忘记密码/密码重置':'FORGET PASSWORD'

}

function showRegister() {
  loginStatus.value = 1
  if (language.value == 0) {
    title.value = '账号注册'
  } else {
    title.value = 'REGISTER'
  }
}

function goRegister(n) {
  if (n == 0) {
    show.value = false
    router.push({
      path: `/register/0`
    })
  } else {
    show.value = false
    router.push({
      path: `/register/1`
    })
  }
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = 'data:image/gif;base64,' + res.data.img
      loginForm.value.uuid = res.data.uuid
    }
  })
}

function getCookie() {
  const telno = Cookies.get('telno')
  const rememberMe = Cookies.get('rememberMe')
  loginForm.value = {
    telno: telno === undefined ? loginForm.value.telno : telno,
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}

function sendsmsMsg() {
  // 忘记密码发信息
  if (!changePasswordForm.value.username) {
    if (language.value == 0) {
      ElMessage.warning('请填写邮箱')
    } else {
      ElMessage.warning('Please fill in your Email address')
    }
    return
  }
  if (counting.value) {

  } else {
    isShow.value = true
  }
}

function codeSuccess() {
  // 忘记密码验证成功
  isShow.value = false
  isCodeTrue.value = true
  getCaptchaSms2({account: changePasswordForm.value.username}).then(res => {
    counting.value = true
  })
}

function changePasswordFormUsername() {
  isCodeTrue.value = false
}

function handleChangePassword() {
  proxy.$refs.changePasswordRef.validate(valid => {
    if (valid) {
      if (isCodeTrue.value) {
        delete changePasswordForm.value.confirmPassword
        forgetPassword(changePasswordForm.value).then(res => {
          ElMessage.success(language.value == 0 ? '操作成功,请登录' :'Operation successful, please log in')
          close()
        })
      } else {
        ElMessage.warning(language.value == 0 ? '请发送验证码' :'Please send the verification code')
      }
    }
  })

}

const goAgreement = () => {
  const routeLocation = router.resolve({
    name: 'userAgreement',
  })
  window.open(routeLocation.href, '_blank')
}
const goPolicy = () => {
  const routeLocation = router.resolve({
    name: 'policy',
  })
  window.open(routeLocation.href, '_blank')
}
</script>

<style lang="scss" scoped>
.ltitle {
  border-bottom: 1px solid #e1e1e1;
  color: #1EC886;
  position: relative;
}

.ltitle::after {
  content: '';
  position: absolute;
  width: 4em;
  height: 2px;
  background: #1EC886;
  left: 0;
  bottom: -1px;
}

.loginLogo {
  display: block;
  margin: auto;
  position: absolute;
  top: -80px;
  left: 0;
  right: 0;
}

.loginBtn {
  height: 40px;
  background: linear-gradient(-90deg, #8623FC, #453DEA);
  border-radius: 20px;
  font-size: 18px;

  &:hover {
    //background: linear-gradient(90deg, #8623FC, #453DEA);
    box-shadow: 0 0 10px #453DEA;
  }
}


.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  position: relative;
  background: #ffffff;
  padding: 0 25px 5px;

  .el-input {
    height: 40px;

    :deep(.el-input__wrapper) {
      box-shadow: none;
      border-bottom: 1px solid #E5E5E5;
    }

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  height: 40px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  float: right;
}

:deep(.el-input-group__append) {
  background: #fff;
  padding: 0;
  color: #fff;
  box-shadow: none;

  div {
    color: #fff;
  }
}

.agreeLine {
  a {
    font-weight: 600;
    color: #000;

    &:hover {
      text-decoration: underline;
    }
  }
}

.rItem {
  height: 130px;
  cursor: pointer;
  width: 350px;
  padding: 1px;
  margin: 20px auto;

  h3 {
    font-size: 30px;
    margin: 50px;
  }

  &.r1 {
    background: url("@/assets/dance/r1b.png") no-repeat left;

    &:hover {
      background: url("@/assets/dance/r1.png") no-repeat left;
    }
  }

  &.r2 {
    background: url("@/assets/dance/r2b.png") no-repeat left;

    &:hover {
      background: url("@/assets/dance/r2.png") no-repeat left;
    }
  }

  &:hover {
    color: #fff;
  }
}

.el-input-group__append {
  flex: 1 1 auto;
}

.el-input-group__append button.el-button, .el-input-group__append button.el-button:hover {
  color: var(--el-color-primary);
  background: #efefff;
  border: var(--el-color-primary) solid 1px;
  border-radius: 0;
}
.poLeft{position: absolute;top:24px;font-size: 20px;left:20px}
</style>