xiangpei
2025-05-13 4c4995771ce83925a2d69dedc11c4404d9b77875
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
<template>
  <div class="login" @click="$refs.verify.show = false">
    <Row type="flex" @keydown.enter.native="submitLogin">
      <Col style="width: 368px">
      <Header />
      <Row style="flex-direction: column">
        <Tabs v-model="loginType">
          <Tab-pane label="账号密码登录" name="passwordLogin">
            <!--账号密码登录-->
            <Form ref="usernameLoginForm" :model="form" :rules="rules" class="form">
              <FormItem prop="username">
                <Input v-model="form.username" prefix="ios-contact" clearable placeholder="请输入用户名"
                  autocomplete="off" />
              </FormItem>
              <FormItem prop="password">
                <Input type="password" v-model="form.password" prefix="ios-lock" password
                  placeholder="请输入密码" autocomplete="off" />
              </FormItem>
            </Form>
            <div class="register">
              <a @click="$router.push('forgetPassword')">忘记密码</a>
            </div>
          </Tab-pane>
          <Tab-pane label="验证码登录" name="mobileLogin">
            <!-- 验证码登录 -->
            <Form ref="formSms" :model="formSms" :rules="ruleInline" @click.self='$refs.verify.show = false'>
              <FormItem prop="mobile">
                <i-input type="text" maxlength="11" v-model="formSms.mobile" clearable placeholder="手机号">
                  <Icon type="md-lock" slot="prepend"></Icon>
                </i-input>
              </FormItem>
              <FormItem prop="code">
                <i-input type="text" v-model="formSms.code" placeholder="手机验证码">
                  <Icon type="ios-text-outline" style="font-weight: bold" slot="prepend" />
                  <Button slot="append" @click="sendCode" :loading="sendCodeLoading">{{ codeMsg }}</Button>
                </i-input>
              </FormItem>
              <FormItem>
                <!-- <Button @click.stop="verifyBtnClick" long :type="verifyStatus ? 'success' : 'default'">{{ verifyStatus ?
                  '验证通过' :
                  '点击完成安全验证' }}
                </Button> -->
              </FormItem>
            </Form>
          </Tab-pane>
        </Tabs>
        <Row>
          <div class="login-btn" type="primary" size="large" :loading="loading" @click="submitLogin" long>
            <span v-if="!loading">登录</span>
            <span v-else>登录中</span>
          </div>
        </Row>
      </Row>
      <Footer />
      <!-- 拼图验证码 -->
      <verify ref="verify" class="verify-con" verifyType="LOGIN" @change="verifyChange"></verify>
      </Col>
    </Row>
  </div>
</template>
 
<script>
import { sendSms } from "@/api/common.js";
import { login, storeSmsLogin, userMsg } from "@/api/index";
import util from "@/libs/util.js";
import Footer from "@/views/main-components/footer";
import Header from "@/views/main-components/header";
import verify from "@/views/my-components/verify";
import Cookies from "js-cookie";
export default {
  components: {
    Header,
    Footer,
    verify,
  },
  data() {
    return {
      saveLogin: true, // 保存登录状态
      sendCodeLoading:false,
      loading: false, // 加载状态
      verifyStatus: false, // 是否图片验证通过
      time: 60, // 倒计时
      loginType: 'passwordLogin', //登陆类型
      form: {
        // 表单数据
        username: "",
        password: "",
        mobile: "",
        code: "",
      },
      formSms: {
        mobile: '',
        code: '',
      },
      rules: {
        // 验证规则
        username: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
      },
      ruleInline: {
        // 验证规则
        username: [{ required: true, message: "请输入用户名" }],
        password: [
          { required: true, message: "请输入密码" },
          { type: "string", min: 6, message: "密码不能少于6位" },
        ],
        mobile: [
          { required: true, message: "请输入手机号码" },
          {
            pattern: RegExp.mobile,
            message: "请输入正确的手机号",
          },
        ],
        code: [{ required: true, message: "请输入手机验证码" }],
      },
      codeMsg: "发送验证码", // 验证码文字
    };
  },
  created() {
    window.localStorage.setItem("menuData", "");
  },
 
  methods: {
    afterLogin(res) {
      let accessToken = res.result.accessToken;
      this.setStore("accessToken", accessToken);
      this.setStore("refreshToken", res.result.refreshToken);
 
      // 获取用户信息
      userMsg().then((res) => {
        if (res.success) {
          // location.reload();
          // this.$router.go(0);
 
          this.setStore("saveLogin", this.saveLogin);
          if (this.saveLogin) {
            // 保存7天
            Cookies.set("userInfoSeller", JSON.stringify(res.result), {
              expires: 7,
            });
          } else {
            Cookies.set("userInfoSeller", JSON.stringify(res.result));
          }
 
          util.initRouter(this);
          this.$store.commit("setAvatarPath", res.result.storeLogo);
 
          const redirectRouter = this.$route.query.redirect;
          // 加载菜单
          const push = {
            path: redirectRouter || "/home",
          }
 
          this.$router.push(push);
        } else {
          this.loading = false;
        }
      });
    },
    // 发送手机验证码
    sendCode() {
      if (this.formSms.mobile === "") {
        this.$Message.warning("请先填写手机号");
        return;
      }
      if (!this.verifyStatus) {
        this.$refs.verify.init();
        return
      }
      if (this.time === 60) {
        this.sendCodeLoading = true
        let params = {
          mobile: this.formSms.mobile,
          verificationEnums: "LOGIN",
        };
        sendSms(params).then((res) => {
 
          if (res.success) {
            this.$Message.success("验证码发送成功");
            let that = this;
            this.interval = setInterval(() => {
              // this.sendCodeLoading = false
              that.time--;
              if (that.time === 0) {
                this.sendCodeLoading = false
                that.time = 60;
                that.codeMsg = "重新发送";
                that.verifyStatus = false;
                clearInterval(that.interval);
              } else {
                that.codeMsg = that.time;
              }
            }, 1000);
          } else {
            this.$Message.warning(res.message);
          }
        }).catch(() => {
          this.sendCodeLoading = false
        });
      }
    },
    submitLogin() {
      // 登录提交
      if (this.loginType == 'passwordLogin') {
        this.$refs.usernameLoginForm.validate((valid) => {
          if (valid) {
            this.$refs.verify.init();
          }
        });
      } else if (this.loginType == 'mobileLogin') {
        this.$refs['formSms'].validate((valid) => {
          if (valid) {
            this.loading = true;
 
            storeSmsLogin(this.formSms).then(res => {
              this.loading = false;
 
              if (res.success) {
                this.afterLogin(res)
              }
            }).catch(() => {
              this.loading = false;
            })
          }
        })
      }
    },
    verifyChange(con) {
      // 拼图验证码回显
      if (!con.status) return;
 
      if (this.loginType == 'passwordLogin') {
        this.loading = true;
        let fd = new FormData();
        fd.append("username", this.form.username);
        fd.append("password", this.md5(this.form.password));
        login(fd)
          .then((res) => {
            this.loading = false;
            if (res && res.success) {
              this.afterLogin(res);
            }
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        this.verifyStatus = true;
 
        this.sendCode()
      }
 
      this.$refs.verify.show = false;
    },
 
    // 开启滑块验证
    verifyBtnClick() {
      if (!this.verifyStatus) {
        this.$refs.verify.init();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  height: 100%;
  background: url("../assets/background.png") no-repeat;
  background-size: 100%;
  background-position-y: bottom;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
 
  .verify-con {
    position: absolute;
    top: 126px;
    z-index: 10;
    left: 20px;
  }
 
  .form {
    padding-top: 1vh;
  }
 
  .login-btn {
    background: linear-gradient(135deg, $theme_color 0%, $warning_color 100%);
    height: 40px;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #fff;
    width: 100%;
    text-align: center;
    transition: 0.35s;
  }
 
  .login-btn:hover {
    opacity: 0.9;
    border-radius: 10px;
  }
 
}
</style>