Login.vue 13.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
<template>
  <div class="AllHome">
    <div class="title">登录</div>
    <div class="titleTwo">欢迎使用宏天EIP微办公</div>
    <van-form @submit="onSubmit" validate-trigger="onSubmit">
      <van-field
        v-model="loginForm.account"
        name="账号"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <template #left-icon>
          <i class="icon-wode_nor icon"></i>
        </template>
      </van-field>
      <van-field
        v-model="loginForm.password"
        :type="isShowPassword ? 'text' : 'password'"
        name="密码"
        placeholder="请输入密码"
        clearable
        class="password"
        :rules="[{ required: true, message: '请输入密码' }]"
        @click-right-icon="clickPassword"
      >
        <template #left-icon>
          <i class="icon-mima1 icon"></i>
        </template>
        <template #right-icon>
          <i
            class="icon"
            :class="!isShowPassword ? 'icon-yincang' : 'icon-xianshi'"
          ></i>
        </template>
      </van-field>
      <van-field
        v-if="isShowVerificationCode"
        v-model="loginForm.verificationCode"
        size="large"
        name="验证码"
        left-icon="smile-o"
        placeholder="请输入验证码"
        class="verificationCode"
        :rules="[{ required: true, message: '请输入验证码' }]"
      >
        <template #left-icon>
          <i class="icon-yanzhengma1 icon"></i>
        </template>
        <template #right-icon>
          <van-icon
            class="clearIcon"
            v-if="loginForm.verificationCode"
            name="clear"
            @click="clickIcon"
          />
          <img
            :src="captchaImage"
            width="112px"
            height="32px"
            @click="flushVerCode"
            class="ver-code"
          />
        </template>
      </van-field>

      <div>
        <van-checkbox icon-size="18" v-model="loginForm.checked" shape="square">
          记住密码
        </van-checkbox>
      </div>

      <van-button
        :loading="submitLoading"
        loading-type="spinner"
        round
        block
        type="info"
        native-type="submit"
        class="clickLogin"
      >
        <span>立即登录</span>
      </van-button>
    </van-form>
    <ResetPassword
      ref="ResetPassword"
      :account="loginForm.account"
      :loginPage="true"
      @close="
        () => {
          $router.go(0)
        }
      "
    ></ResetPassword>
  </div>
</template>

<script>
  import ResetPassword from '@/components/ResetPassword.vue'
  import { mapMutations, mapActions, mapState } from 'vuex'
  import { Notify, Dialog } from 'vant'

  import {
    getTenantByCode,
    updateClientInfo,
    bindOpenId,
    bindMpOpenId,
  } from '@/api/uc.js'
  import { getCaptcha } from '@/api/auth.js'

  import { encryptWithRsa, appUpdate } from '@/utils/index.js'

  export default {
    name: 'Login',
    props: {
      tenant: {
        type: String,
        default: 'platform',
      },
    },
    components: { ResetPassword },
    data() {
      return {
        loginForm: {
          account: '',
          password: '',
          verificationCode: '',
          verificationKey: '',
          checked: false,
        },

        captchaImage: '',
        submitLoading: false,
        isShowVerificationCode: false,
        tenantId: '',
        isShowPassword: false,
      }
    },
    created() {
      let loginForm = localStorage.getItem('loginForm')
      if (loginForm) {
        this.loginForm = JSON.parse(loginForm)
      }
    },
    computed: {
      ...mapState('login', ['loginStatus', 'currentUser']),
    },
    watch: {
      loginForm: {
        handler(val) {},
        deep: true,
      },
      isShowVerificationCode(val) {
        if (val) {
          this.$nextTick(() => {
            //获取二维码图片
            this.flushVerCode()
          })
        }
      },
    },
    mounted() {
      this.$storage.set(`loginRoutePath`, this.$route.path)
      //获取租户信息
      this.getTenantInfo()
    },
    methods: {
      ...mapMutations('login', ['setAccount']),
      ...mapActions('login', ['loginByPrincipal']),
      onSubmit() {
        if (this.loginForm.checked) {
          localStorage.setItem(
            'loginForm',
            JSON.stringify({
              account: this.loginForm.account,
              password: this.loginForm.password,
              checked: this.loginForm.checked,
            })
          )
        } else {
          let loginForm = localStorage.getItem('loginForm')
          if (loginForm) {
            localStorage.removeItem('loginForm')
          }
        }

        this.submitLoading = true
        this.setRouterPath()
        const params = {
          username: this.loginForm.account,
          password: encryptWithRsa(this.loginForm.password),
          verificationKey: this.loginForm.verificationKey,
          verificationCode: this.loginForm.verificationCode,
        }

        this.loginByPrincipal({ params, tenantId: this.tenantId })
          .then((res) => {
            if (
              res &&
              res.errorCode &&
              ['4012', '4013'].includes(res.errorCode)
            ) {
              this.$toast.fail(res.message)
              this.isShowVerificationCode = true
              if (this.isShowVerificationCode) {
                this.clickIcon()
                this.flushVerCode()
              }

              // Notify({ type: 'danger', message: res.message })

              return
            } else if (res.errorCode && res.errorCode === '2001') {
              this.isShowVerificationCode = true
              return
            } else {
              if (this.loginStatus) {
                this.isShowVerificationCode = false
                //登录成功后,判断缓存中是否有openid,有则绑定到userUnite的openid属性中,
                //绑定成功后务必删除缓存的openid以免多次绑定。另:openid在wcoa_jump.vue跳转时写入缓存的。
                this.hasOpenId()
                //登录成功后,判断缓存中是否有mpOpenid,有则绑定到userUnite的mpOpenid属性中,
                //绑定成功后务必删除缓存的mpOpenid以免多次绑定。另:openid在miniprogram_jump.vue跳转时写入缓存的。
                this.hasMpOpenId()
                this.isApp()

                this.$route.path !== '/' &&
                  this.$store
                    .dispatch('user/loadCurrentUserDetail')
                    .then((res) => {
                      this.$store.dispatch('user/getCurrent')
                    })
                if(this.$route.query.hasOwnProperty('redirect') && this.$route.query.redirect.indexOf('login')==-1){
                  this.$router.push({path:this.$route.query.redirect})
                }else{
                  this.$router.push({
                  name: 'home',
                  params: {
                    fistLogin: true,
                  },
                })
               }   
              } else {
                this.$toast.fail('该密码不符合密码策略,请重置密码')

                setTimeout(() => {
                  this.$refs.ResetPassword.openResetPasswordDialog()
                }, 1000)
                this.submitLoading = false
              }
            }
          })
          .catch((msg) => {
            this.flushVerCode()
            this.clickIcon()
            if (msg && msg.startsWith && msg.startsWith('timeout of')) {
              msg = '登录超时'
              // Notify({ type: 'danger', message: msg })
              this.$toast.fail(msg)
            }
          })
          .finally(() => {
            this.submitLoading = false
          })
      },
      //是否有openid
      hasOpenId() {
        const openid = this.$storage.session.get('openid')
        if (openid) {
          Dialog.confirm({
            title: '温馨提示',
            message: '是否绑定公众号,以便下次免登录',
          })
            .then(() => {
              this.bindWxOffAcc(openid)
            })
            .catch()
        }
      },
      hasMpOpenId() {
        const mpOpenid = this.$storage.session.get('mpOpenid')
        const hideBindMp = this.$storage.get('hideBindMp')
        if (mpOpenid && !hideBindMp) {
          Dialog.confirm({
            title: '温馨提示',
            message: '是否绑定小程序,以便下次免登录',
            confirmButtonText: '绑定',
            cancelButtonText: '以后再说',
          })
            .then(() => {
              this.bindMiniprogram(mpOpenid)
            })
            .catch((e) => {
              //不绑定企业微信的状态保存在localStorage
              this.$storage.set('hideBindMp', true)
            })
        }
      },
      //在手机app环境中
      isApp() {
        if (window.plus) {
          //把设备信息绑定到用户中,以便进行app消息推送
          const clientInfo = plus.push.getClientInfo()
          if (clientInfo) {
            const data = {
              account: this.principal.account,
              clientToken: clientInfo.token,
              clientId: clientInfo.clientid,
            }
            updateClientInfo(data)
          }
          //把token传给app应用进行保存
          window.uni &&
            uni.postMessage({
              data: {
                action: 'login',
                token: this.currentUser.token,
              },
            })
          setTimeout(() => {
            //检查软件更新
            appUpdate()
          }, 5000)
        }
      },
      getTenantInfo() {
        getTenantByCode(this.tenant).then((res) => {
          if (res && res.state) {
            this.tenantId = res.value.id
            //是否启用图形验证码 0-不启用;1-启用且限制失败次数;2-一直启用
            if (res.value.captchaEnabled && res.value.captchaEnabled === 2) {
              this.isShowVerificationCode = true
            }
          }
        })
      },
      async flushVerCode() {
        //获取二维码图片
        await getCaptcha().then((res) => {
          this.loginForm.verificationKey = res.key
          this.captchaImage = res.image
        })
      },
      setRouterPath() {
        this.setAccount(this.loginForm.account)
        //本地存储路由path
        this.$storage.set(
          `${this.loginForm.account}loginRoutePath`,
          this.$route.path
        )
      },
      bindWxOffAcc(openid) {
        bindOpenId({ account: this.loginForm.account, openid }).then((res) => {
          if (res.state) {
            this.$storage.session.remove('openid')
          }
        })
      },
      bindMiniprogram(mpOpenid) {
        bindMpOpenId({ account: this.loginForm.account, mpOpenid }).then(
          (res) => {
            if (res.state) {
              this.$storage.session.remove('mpOpenid')
            }
          }
        )
      },
      clickPassword() {
        this.isShowPassword = !this.isShowPassword
      },
      clickIcon() {
        this.loginForm.verificationCode = ''
      },
    },
  }
</script>

<style lang="scss" scoped>
  @mixin placeholder {
    color: #cccccc;
    font-weight: 400;
    font-size: 14px;
  }
  .clearIcon {
    margin-right: 5px;
  }
  .AllHome {
    background: url(../assets/images/loginBackground.png);

    height: $vh;
    width: calc(100vw - 48px);
    background-repeat: no-repeat;
    background-size: cover;
    background-attachment: fixed;

    padding: 0 24px 0;
    color: #262626;
    .title {
      font-weight: 800;
      font-size: 24px;
      padding-top: 140px;
    }
    .titleTwo {
      font-weight: 400;
      font-size: 16px;
      margin: 8px 0 56px;
    }
    ::v-deep {
      .van-cell {
        background-color: transparent;
        margin-bottom: 32px;
        padding: 0 0 8px;

        &::after {
          right: 0;
          left: 0;
        }
      }
      .van-field__left-icon {
        margin-right: 8px;
        display: flex;
        justify-content: center;
        align-items: center;
        .van-icon {
          font-size: 22px;
        }
      }
      .password,
      .verificationCode {
        margin-bottom: 42px;
      }

      .van-checkbox {
        padding-top: 8px;
        margin: 0 0 16px;
        .van-icon {
          font-size: 15px;
          width: 18px;
          height: 18px;
          border: 1px solid #e6e6e6;
        }
        .van-checkbox__label {
          font-weight: 500;
          font-size: 14px;
          color: #8c8c8c;
        }
      }
      .clickLogin {
        background: #409eff;
        border-radius: 8px 8px 8px 8px;
        .van-button__text {
          font-weight: bold;
          font-size: 16px;
        }
      }
      input::-webkit-input-placeholder {
        /* 使用webkit内核的浏览器 */
        @include placeholder;
      }
      input:-moz-placeholder {
        /* Firefox版本4-18 */
        @include placeholder;
      }
      input::-moz-placeholder {
        /* Firefox版本19+ */
        @include placeholder;
      }
      input:-ms-input-placeholder {
        /* IE浏览器 */
        @include placeholder;
      }
    }
  }
  ::v-deep .icon {
    font-size: 22px !important;
  }
  ::v-deep .van-field__control {
    font-size: 16px !important;
  }
  ::v-deep {
    .van-icon-clear {
      z-index: 99 !important;
      margin-right: 6px !important;
    }
    .van-field__right-icon {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    input:-webkit-autofill {
      box-shadow: 0 0 0 1000px white inset !important;
    }
    input:-internal-autofill-previewed,
    input:-internal-autofill-selected {
      -webkit-text-fill-color: #262626 !important;
      transition: background-color 5000s ease-in-out 0s !important;
    }

    // input:-webkit-autofill::first-line: {
    // }
  }
</style>