董国庆
6 天以前 3f0d4c3df5d21e48da5d4ec658d9bcf54ec10319
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
<template>
  <div class="bgImg">
    <div class="login_box">
      <div class="logo">
        <img src="@/assets/logo.png" alt="" />
        <div class="fs--25 fw-bold" style="letter-spacing: 4px">
          射洪市 两客一危 监管平台
        </div>
      </div>
      <div class="txt-center pb--20 px--20">
        <div class="mt--20">
          <el-input
            class="w100"
            prefix-icon="el-icon-user"
            placeholder="账号"
            v-model="username"
          />
        </div>
        <div class="mt--20">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="密码"
            v-model="password"
            show-password
          />
        </div>
        <div class="mt--20 flex a-center code_box">
          <div class="flex1">
            <el-input placeholder="请输入验证码" v-model="code" />
          </div>
          <div @click="resetCodeStr" class="fs--18 lh--40 border1 pointer">
            {{ codeStr }}
          </div>
        </div>
        <el-button
          :loading="loginLoading"
          @click="login"
          class="mt--40 w100 br--5 pointer bgcolor1 color1"
          >登录</el-button
        >
      </div>
    </div>
  </div>
</template>
 
<script>
import { generateVerificationCode, generateRandomString } from "@/utils/utils";
import { mapMutations } from "vuex";
import { loginPwd } from "./service";
import CryptoJS from "crypto-js";
import { getRoleInfo } from "@/view/systemManage/role/service";
 
import { Message } from "element-ui";
export default {
  components: {},
  props: {},
  data() {
    return {
      loginLoading: false, //登录loading
      username: "",
      password: "",
      code: "",
      codeStr: "",
    };
  },
  created() {
    this.resetCodeStr();
  },
  mounted() {
    document.addEventListener("keydown", this.handleKeyDown);
  },
  beforeDestroy() {
    document.removeEventListener("keydown", this.handleKeyDown);
  },
  methods: {
    ...mapMutations(["setToken", "setUserInfo"]),
    login() {
      if (!this.rulesLogin()) return;
      this.loginLoading = true;
      loginPwd({
        username: this.username,
        password: CryptoJS.MD5(this.password).toString(),
      })
        .then((res) => {
          localStorage.setItem("client", generateRandomString(16));
          this.loginLoading = false;
          this.setToken(res.token.access_token);
          this.setUserInfo(JSON.stringify(res.info.sysUser));
          getRoleInfo({ id: res.info.sysUser.roles[0].roleId }).then((res) => {
            this.$store.commit("SET_PERMISSON", res.menus);
            this.$router.push("/home");
          });
        })
        .catch(() => {
          this.resetCodeStr();
          this.loginLoading = false;
        });
    },
    rulesLogin() {
      if (!this.username) {
        Message({
          message: "请输入账号",
          type: "warning",
          duration: 2000,
        });
        return false;
      }
      if (!this.password) {
        Message({
          message: "请输入密码",
          type: "warning",
          duration: 2000,
        });
        return false;
      }
      if (!this.code) {
        Message({
          message: "请输入验证码",
          type: "warning",
          duration: 2000,
        });
        return false;
      }
      if (!this.matchCaseInsensitive(this.codeStr, this.code)) {
        Message({
          message: "验证码错误",
          type: "warning",
          duration: 1500,
        });
        this.resetCodeStr();
        return false;
      }
      return true;
    },
    handleKeyDown(event) {
      if (event.key === "Enter") {
        this.login();
      }
    },
    // 校验验证码
    matchCaseInsensitive(str, pattern) {
      return str.toLowerCase() === pattern.toLowerCase();
    },
    resetCodeStr() {
      this.codeStr = generateVerificationCode();
    },
  },
};
</script>
<style scoped lang="less">
.bgImg {
  background-image: url("../../assets/loginBG.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}
 
.login_box {
  width: 25%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  padding: 20px;
 
  .logo {
    display: flex;
    align-items: center;
    justify-content: center;
 
    img {
      width: 50px;
      height: 50px;
      margin-right: 15px;
    }
  }
}
 
.code_box {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
 
  ::v-deep .el-input__inner {
    border: unset !important;
  }
}
 
.bgcolor1 {
  background: #0e6efd;
}
 
.color1 {
  color: #fff;
}
 
.border1 {
  border-left: 1px solid #dcdfe6;
  min-width: 100px;
}
</style>