surprise
2023-12-05 6ecef4176f6d9df60cd1a753a36e09cd96bce9b8
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
<template>
  <div class="login-wrap">
    <div class="ms-login">
      <el-tabs v-model="loginType" class="ms-title">
        <el-tab-pane label="密码登录" name="usecode" v-if="usecode">
          <el-form
            :model="param"
            :rules="rules"
            ref="login"
            label-width="0px"
            class="ms-content"
          >
            <el-form-item prop="phone">
              <el-input v-model="param.phone" placeholder="请输入手机号码">
                <el-button slot="prepend" icon="el-icon-phone"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="password" v-if="loginType === 'usecode'">
              <el-input
                v-model="param.password"
                :show-password="true"
                placeholder="请输入密码"
              >
              </el-input>
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="submitForm()">登录</el-button>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="短信登录" name="unusecode" v-if="unusecode">
          <el-form
            :model="param"
            :rules="rules"
            ref="login"
            label-width="0px"
            class="ms-content"
          >
            <el-form-item prop="phone">
              <el-input v-model="param.phone" placeholder="请输入手机号码">
                <el-button slot="prepend" icon="el-icon-phone"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="loginType === 'unusecode'">
              <el-input v-model="param.code" placeholder="请输入验证码">
                <template slot="append">
                  <el-button
                    :disabled="disabled"
                    class="send"
                    @click="sendhandle"
                    >{{ sendTxt }}</el-button
                  ></template
                ></el-input
              >
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="submitForm()">登录</el-button>
            </div>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
 
<script>
import baseVuex from "@mixin/baseVuex";
import { sendSms, verifyCode, isAdmin } from "@api/index";
let ms = 0;
export default {
  mixins: [baseVuex],
  data: function () {
    let phoneReg = /^1[3-9]\d{9}$/;
    let validatePhone = (rule, value, callback) => {
      if (!value) {
        this.isPhone = false;
        return callback(new Error("号码不能为空!"));
      }
      setTimeout(() => {
        if (!phoneReg.test(value)) {
          this.isPhone = false;
          callback(new Error("请输入正确手机格式!"));
        } else {
          this.isPhone = true;
          callback();
        }
      }, 100);
    };
    return {
      login: "all",
      loginType: "usecode",
      param: {
        password: "",
        code: "",
        phone: "",
      },
      disabled: false,
      isPhone: "",
      rules: {
        phone: [{ required: true, trigger: "blur", validator: validatePhone }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      sendTxt: "点击发送",
      time: 120,
      interval: null,
    };
  },
  mounted() {
    if (window.sceneConfig && sceneConfig.login) {
      this.login = sceneConfig.login;
      if (this.login === "onlySMS") {
        this.loginType = "unusecode";
      }
    }
  },
  computed: {
    usecode: function () {
      return this.login === "all" || this.login === "onlyPassword";
    },
    unusecode: function () {
      return this.login === "all" || this.login === "onlySMS";
    },
  },
  methods: {
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          verifyCode(this.loginType, this.param).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "登录成功",
              });
              this.setTreeData([]);
              this.changeUserData({ type: "phone", value: this.param.phone });
              this.checkAdminAndLogin();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        }
      });
    },
    sendhandle() {
      this.$refs.login.validateField(["phone"], (valid) => {
        if (this.isPhone) {
          sendSms({
            phone: this.param.phone,
          }).then((res) => {
            if (res.code === 200) {
              if (res.data.code === "OK") {
                this.$message({
                  type: "success",
                  message: "发送短信成功,请注意查收!",
                });
                this.disabled = true;
                ms = this.time;
                this.interval = setInterval(() => {
                  this.countDown();
                }, 1000);
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        }
      });
    },
    countDown() {
      if (!ms) {
        this.sendTxt = "重新发送";
        clearInterval(this.interval);
        this.interval = null;
        this.disabled = false;
      } else {
        this.sendTxt = `${ms} 秒后重新发送`;
        ms--;
      }
    },
    checkAdminAndLogin() {
      // 管理员验证
      isAdmin({
        phone: this.param.phone,
      }).then((data) => {
        this.changeUserData({
          type: "admin",
          value: data && data.data ? true : false,
        });
      });
 
      localStorage.setItem("loginscene", 1);
      this.$router.push({
        path: "/index",
      });
    },
  },
};
</script>
 
<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(~@/assets/login.png);
  background-size: 100% 100%;
}
.login-wrap /deep/ .el-input__inner {
  height: 45px;
}
.login-wrap /deep/ .el-form-item {
  margin-bottom: 30px;
}
 
.login-wrap /deep/ .el-tabs__nav {
  margin-left: 30px;
  /* left: 50%; */
}
 
.login-wrap /deep/ .el-tabs__nav-wrap::after {
  height: 0;
}
 
button {
  height: 45px !important;
  font-size: 15px;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 75%;
  top: 50%;
  width: 450px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: #fff;
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.send {
  cursor: pointer;
}
</style>