WXL
7 天以前 e353f0e17dc46203512dd272c5b559b094e40761
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
<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">无锡儿童医院智慧随访平台</h3>
 
      <!-- 登录方式切换 -->
      <div class="login-method">
        <el-radio-group v-model="loginMethod" @change="changeLoginMethod">
          <el-radio-button label="password">密码登录</el-radio-button>
          <el-radio-button label="sms">验证码登录</el-radio-button>
        </el-radio-group>
      </div>
 
      <!-- 用户名/手机号输入 -->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          :placeholder="loginMethod === 'password' ? '账号' : '手机号'"
        >
          <svg-icon
            slot="prefix"
            :icon-class="loginMethod === 'password' ? 'user' : 'phone'"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
 
      <!-- 密码登录部分 -->
      <template v-if="loginMethod === 'password'">
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">
          记住密码
        </el-checkbox>
      </template>
 
      <!-- 验证码登录部分 -->
      <template v-else>
        <el-form-item prop="smsCode">
          <el-input
            v-model="loginForm.smsCode"
            auto-complete="off"
            placeholder="短信验证码"
            style="width: 60%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <el-button
            :disabled="countdown > 0"
            @click="phoneCode"
            style="width: 38%; margin-left: 2%"
          >
            {{ countdown > 0 ? `${countdown}秒后重新获取` : '获取验证码' }}
          </el-button>
        </el-form-item>
      </template>
 
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
  </div>
  <!-- <div class="block">
    <div class="smerry">
      <el-image >
        <div slot="error" class="image-slot">无权限访问</div>
      </el-image>
    </div>
  </div> -->
</template>
 
<script>
import { getCodeImg, phoneCode } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
 
export default {
  name: "Login",
  data() {
    // 验证手机号规则
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入手机号"));
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
 
    return {
      loginMethod: "password", // 登录方式,password或sms
      countdown: 0, // 短信验证码倒计时
      timer: null, // 倒计时定时器
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        smsCode: "",
        rememberMe: false,
        code: "",
        orgid: "1",
      },
      options: [
        { value: "1", label: "景宁畲族自治县人民医院" },
        { value: "2", label: "丽水市中医院" },
      ],
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码", when: () => this.loginMethod === "password" },
        ],
        smsCode: [
          { required: true, trigger: "blur", message: "请输入验证码", when: () => this.loginMethod === "sms" },
          { pattern: /^\d{6}$/, message: "验证码为6位数字", trigger: "blur", when: () => this.loginMethod === "sms" }
        ],
      },
      loading: false,
      captchaEnabled: true,
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  methods: {
    // 切换登录方式
    changeLoginMethod() {
    this.$refs.loginForm.clearValidate();
    // 动态更新验证规则
    this.loginRules = {
      username: [
        { required: true, trigger: "blur", message: this.loginMethod === 'password' ? '请输入您的账号' : '请输入手机号' },
        {
          validator: (rule, value, callback) => {
            if (this.loginMethod === 'sms' && !/^1[3-9]\d{9}$/.test(value)) {
              callback(new Error("请输入正确的手机号"));
            } else {
              callback();
            }
          },
          trigger: "blur"
        }
      ],
      password: [
        {
          required: true,
          trigger: "blur",
          message: "请输入您的密码",
          when: () => this.loginMethod === "password"
        },
      ],
      smsCode: [
        {
          required: true,
          trigger: "blur",
          message: "请输入验证码",
          when: () => this.loginMethod === "sms"
        },
        {
          pattern: /^\d{6}$/,
          message: "验证码为6位数字",
          trigger: "blur",
          when: () => this.loginMethod === "sms"
        }
      ],
    };
  },
 
    // 发送短信验证码
    phoneCode() {
      this.$refs.loginForm.validateField("username", (error) => {
        if (!error) {
          this.loading = true;
          phoneCode({ phone: this.loginForm.username })
            .then(() => {
              this.$message.success("验证码已发送");
              this.startCountdown();
              this.loading = false;
 
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
 
    // 开始倒计时
    startCountdown() {
      this.countdown = 60;
      this.timer = setInterval(() => {
        this.countdown--;
        if (this.countdown <= 0) {
          clearInterval(this.timer);
        }
      }, 1000);
    },
 
    getCode() {
      getCodeImg().then((res) => {
        console.log(res);
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
 
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
 
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
 
          if (this.loginMethod === "password" && this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
 
          const loginData = {
            username: this.loginForm.username,
            orgid: "1",
            loginType: this.loginMethod
          };
 
          if (this.loginMethod === "password") {
            loginData.password = this.loginForm.password;
          } else {
            console.log(this.loginForm.smsCode);
 
            loginData.phoneCode = this.loginForm.smsCode;
          }
 
          this.$store
            .dispatch("Login", loginData)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>
 
<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  overflow: hidden !important;
  background-position: center center;
 
  /* 背景图不平铺 */
  background-repeat: no-repeat;
  /* 当内容高度大于图片高度时,背景图像的位置相对于viewport固定 */
  background-attachment: fixed;
  /* 让背景图基于容器大小伸缩 */
  background-size: cover;
  /* 设置背景颜色,背景图加载过程中会显示背景色 */
  background-color: #464646;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
 
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  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: 38px;
}
.smerry {
  text-align: center;
  align-items: center;
  .el-image {
    width: 95vw;
    height: 94vh;
    line-height: 94vh;
    font-size: 50px;
    justify-content: center;
    align-items: center;
    color: #c0c4cc;
    vertical-align: middle;
  }
}
</style>