Login.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script setup lang="ts">
  2. import { reactive, ref } from 'vue'
  3. import { ElNotification } from 'element-plus'
  4. import type { FormInstance, FormRules } from 'element-plus'
  5. import type { InLogin } from '@/typings/config'
  6. import { User, View, Lock, Hide } from '@element-plus/icons-vue'
  7. import { Login_verification } from '@/api/user/index'
  8. import { useRouter } from 'vue-router'
  9. import { GlobalStore } from '@/stores/index'
  10. import { fnMd5 } from '@/utils/common'
  11. const router = useRouter()
  12. const globalStore = GlobalStore()
  13. const ruleFormRef = ref<FormInstance>()
  14. const ruleForm = reactive<InLogin>({
  15. username: '',
  16. password: ''
  17. })
  18. const validatePass = (rule: any, value: any, callback: any) => {
  19. if (value === '') {
  20. callback(new Error('请输入密码'))
  21. } else {
  22. callback()
  23. }
  24. }
  25. const rules = reactive<FormRules>({
  26. username: [{ required: true, message: '请输入用户账号', trigger: 'blur' }],
  27. password: [{ required: true, validator: validatePass, trigger: 'blur' }]
  28. })
  29. const submitForm = (formEl: FormInstance | undefined) => {
  30. if (!formEl) return
  31. formEl.validate(async valid => {
  32. if (valid) {
  33. let res: any = {}
  34. res = await Login_verification({ bzd_username: ruleForm.username, bzd_password: fnMd5(ruleForm.password) })
  35. if (res.Code === 200) {
  36. localStorage.setItem('User_tokey', res.Data)
  37. globalStore.SET_User_Tokey(res.Data)
  38. router.replace('/')
  39. ElNotification.success({
  40. title: '登录成功',
  41. message: '欢迎进入宝智达ERP系统!',
  42. position: 'bottom-right'
  43. })
  44. }
  45. } else {
  46. return false
  47. }
  48. })
  49. }
  50. let passType = ref('password')
  51. const changeType = () => {
  52. passType.value = passType.value === 'password' ? 'text' : 'password'
  53. }
  54. </script>
  55. <template>
  56. <div class="login">
  57. <div class="content">
  58. <div class="title">
  59. <h2>宝智达ERP系统</h2>
  60. <span>ShenZhen Baozhida Technology ERP. system</span>
  61. </div>
  62. <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
  63. <el-form-item label="账号:" prop="username">
  64. <el-input v-model="ruleForm.username" type="text" :prefix-icon="User" />
  65. </el-form-item>
  66. <el-form-item label="密码:" prop="password">
  67. <el-input v-model="ruleForm.password" :type="passType" :prefix-icon="Lock" autocomplete="off">
  68. <template #suffix>
  69. <span class="view">
  70. <el-icon v-if="passType === 'password'" class="el-input__icon" @click="changeType">
  71. <Hide />
  72. </el-icon>
  73. <el-icon v-else class="el-input__icon" @click="changeType">
  74. <View />
  75. </el-icon>
  76. </span>
  77. </template>
  78. </el-input>
  79. </el-form-item>
  80. <el-form-item>
  81. <el-button class="submit" type="primary" @click.enter="submitForm(ruleFormRef)">登录</el-button>
  82. </el-form-item>
  83. </el-form>
  84. </div>
  85. </div>
  86. </template>
  87. <style scoped lang="scss">
  88. @import './login.scss';
  89. </style>