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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
| <template> <div class="login-pop"> <!-- 关闭按钮 --> <div class="close" @click="closeLogin"> <span class="iconfont icon-guanbi1"></span> </div>
<!-- 手机号登录 --> <div class="phone-login" v-if="loginWay === 0"> <h2>手机号登录</h2> <!-- logo --> <div class="imgs"> <span class="iconfont icon-tel"></span> </div> <!-- 表单 --> <div class="form"> <el-form label-width="70px"> <el-form-item label="手机号"> <el-input type="tlephone" v-model="PhoneNum" placeholder="请输入手机号"></el-input> </el-form-item> <el-form-item label="密码"> <el-input type="password" v-model="Password" placeholder="请输入密码"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="phoneLogin">登录</el-button> </el-form-item> <el-form-item> <el-button @click="changeLoginWay(2)">注册</el-button> </el-form-item> </el-form> </div> <div class="other-way" @click="changeLoginWay(1)" style="margin-top: 50px; padding-left: 25px"> <span><i class="iconfont icon-erweima1"></i> <i class="text">扫码登录</i> </span> </div> </div>
<!-- 扫码登录 --> <div class="qr-login" v-if="loginWay === 1"> <div class="qr"> <h2>扫码登录</h2> <img :src="qrurl" alt="" /> <p class="failqr" v-if="failqr">二维码已失效,<span @click="qrLogin">点击刷新</span></p> <p class="text" v-if="!scanQr">使用<span style="color: #5091ca">网易云音乐APP</span>扫码登录</p> <p class="text" v-if="scanQr">扫描成功,请在手机上确认登录</p> <!-- 其他登录方式 --> <div class="other-way" @click="changeLoginWay(0)" style="margin-top: 30px"> <span> <i class="iconfont icon-tel"></i> <i class="text"> 手机号登录</i></span > </div> <div class="other-way" style="padding-top: 50px" @click="changeLoginWay(2)"> <span>还没有账号,去注册</span> </div> </div> </div>
<!-- 注册新用户 --> <div class="register" v-show="loginWay === 2"> <h2>注册新用户</h2> <div class="reg-form"> <el-form label-position="right" label-width="80px" :model="ruleForm" :rules="rules" ref="ruleForm"> <el-form-item label="手机号" prop="phone"> <el-input type="telphone" v-model="ruleForm.phone" placeholder="请输入手机号"></el-input> </el-form-item> <el-form-item label="密码" prop="pass"> <el-input type="password" v-model="ruleForm.pass" placeholder="密码为8~20位,必须包含字母数字"></el-input> </el-form-item> <el-form-item label="确认密码" prop="checkPass"> <el-input type="password" v-model="ruleForm.checkPass" placeholder="请确认密码"></el-input> </el-form-item> <el-form-item label="昵称" prop="nickname"> <el-input type="text" v-model="ruleForm.nickname" placeholder="请输入昵称"></el-input> </el-form-item> <el-form-item label="验证码" prop="captcha" class="captcha-input"> <el-input v-model="ruleForm.captcha" placeholder="请输入验证码"></el-input> <div class="captcha-btn" @click="getCaptcha" v-if="!isGetCaptcha">获取验证码</div> <div class="captcha" v-else>{{ tip }}</div> </el-form-item> </el-form> </div> <div class="reg-btns"> <div class="reg-btn"> <el-button type="primary" @click="register('ruleForm')">注册</el-button> </div> <div class="reg-btn"> <el-button @click="changeLoginWay(0)">返回登录</el-button> </div> </div> </div> </div> </template> <script> import { login, getQrKey, getLoginQr, checkLoginQr, getUserAccount, getAuthcode, checkAuthcode, register, checkPhoneNum } from "network/login/login"; import { getUserSonglist, getUserDetail } from "network/userdetail/userdetail"; import { getLikSongList } from "network/playmusic/playmusic.js"; import { isPhone, throttle } from "common/utils.js"; export default { name: "LoginPop", data() { //注册表单验证规则 var validatePhone = (rule, value, callback) => { if (value === "") { callback(new Error("请输入手机号")); } else { if (!isPhone(value)) { callback(new Error("手机号格式不正确")); } callback(); } }; var validatePass = (rule, value, callback) => { if (value === "") { callback(new Error("请输入密码")); } else { if (this.ruleForm.checkPass !== "") { this.$refs.ruleForm.validateField("checkPass"); } callback(); } }; var validatePass2 = (rule, value, callback) => { if (value === "") { callback(new Error("请再次输入密码")); } else if (value !== this.ruleForm.pass) { callback(new Error("两次输入密码不一致!")); } else { callback(); } }; var validateName = (rule, value, callback) => { if (value === "") { callback(new Error("请输入昵称")); } callback(); }; var validateCaptcha = (rule, value, callback) => { if (value === "") { callback(new Error("请输入验证码")); } callback(); }; return { PhoneNum: "", //登录手机号 Password: "", //登录密码 loginWay: 0, //登录方式 qrurl: "", //二维码路径 timer: "", //轮询二维码的定时器 failqr: false, //控制二维码失效显示文本 scanQr: false, isGetCaptcha: false, //是否获取验证码 tip: "", //获取验证码后的文字提示 ruleForm: { phone: "", //注册手机号 pass: "", //注册密码 checkPass: "", //验证密码 nickname: "", //昵称 captcha: "", //验证码 }, rules: { phone: [{ validator: validatePhone, trigger: "blur", required: true }], pass: [{ validator: validatePass, trigger: "blur", required: true }], checkPass: [{ validator: validatePass2, trigger: "blur", required: true }], nickname: [{ validator: validateName, trigger: "blur", required: true }], captcha: [{ validator: validateCaptcha, trigger: "blur", required: true }], }, }; }, created() {}, methods: { // 关闭登录框 closeLogin() { this.$emit("closeLogin"); clearInterval(this.timer); }, // 切换登录方式 changeLoginWay(way) { this.loginWay = way; // 如果是手机号登录就清除二维码登录的定时器 if (way !== 1) { clearInterval(this.timer); if (way === 0) { this.$refs.ruleForm.resetFields(); //重置注册表单 } } // 二维码登录 if (way === 1) { this.qrLogin(); } },
// 二维码登录 async qrLogin() { this.failqr = false; // 用于隐藏二维码失效后的文本提示 this.scanQr = false; // 获取二维码key let res = await getQrKey(); let key = res.data.data.unikey; // 生成二维码 let res2 = await getLoginQr(key); this.qrurl = res2.data.data.qrimg; // 检查二维码状态(利用定时器不断轮询) this.timer = setInterval(async () => { let statusRes = await checkLoginQr(key); console.log(statusRes.data); if (statusRes.data.code === 800) { this.$message({ showClose: true, message: "二维码已失效", type: "error", center: true, }); clearInterval(this.timer); // 用于显示二维码失效后的文本提示, this.failqr = true; } if (statusRes.data.code === 802) { this.scanQr = true; } if (statusRes.data.code === 803) { clearInterval(this.timer); this.$message({ showClose: true, message: "登录成功", type: "success", center: true, }); // 获取用户账户信息 this.getUserLoginAccount(); } }, 2000); }, // 二维码登录 获取用户登录后的账户信息 getUserLoginAccount() { getUserAccount().then(res1 => { let uid = res1.data.account.id; // 获取用户个人信息 getUserDetail(uid).then(res => { this.loginSuccess(res); }); }); },
// 手机号登录事件 phoneLogin() { if (this.PhoneNum.trim() === "" || this.Password === "") { this.$message({ message: "手机号或密码不能为空", type: "warning", center: true, }); return; } else { login(this.PhoneNum, this.Password) .then(res => { if (res.data.code === 200) { this.$message({ showClose: true, message: "登录成功", type: "success", center: true, }); // 登录成功后的一些操作 this.loginSuccess(res); } else if (res.data.code === 502) { this.$message({ message: "密码错误", type: "warning", center: true, }); } else { this.$message({ message: "手机号或密码错误", type: "warning", center: true, }); } }) .catch(err => { this.$message({ message: "账号不存在", type: "warning", center: true, }); }); } },
// 登录成功后的一些操作 loginSuccess(res) { // 关闭登录框 this.closeLogin(); // 更新登录状态 this.$store.dispatch("updateLogin", true); this.setItem("isLogin", true); //缓存用户信息 防止刷新消失 this.setItem("userInfo", res.data.profile); // 提交vuex 保存用户信息 this.$store.dispatch("saveUserInfo", res.data.profile); // 获取用户歌单数据 this.getUserSonglistBy(res.data.profile.userId); },
// 获取用户歌单和喜欢的音乐数据 getUserSonglistBy(uid) { getUserSonglist(uid).then(res => { this.setItem("userSongList", res.data.playlist); this.$store.dispatch("saveUserSongList", res.data.playlist); }); getLikSongList(uid).then(res => { if (res.data.ids.length != 0) { this.$store.dispatch("saveLikeSongIds", res.data.ids); } }); },
// 点击获取验证码 getCaptcha() { // 检测手机号码是否输入 if (this.ruleForm.phone.trim() === "") { this.$message({ showClose: true, message: "请先输入手机号", type: "warning", center: true, }); } else { // 检测手机号是否注册 // checkPhoneNum(this.ruleForm.phone).then(res => { // if (res.data.exist === 1) { // this.$message({ // showClose: true, // message: "该手机号已注册", // type: "warning", // center: true, // }); // } else if (res.data.exist === -1) { // 获取验证码 getAuthcode(this.ruleForm.phone).then(res => { switch (res.data.code) { case 200: this.$message({ showClose: true, message: "验证码已发送", type: "success", center: true, }); this.isGetCaptcha = true; let count = 60; this.tip = `请${count}秒后再获取`; let timerC = setInterval(() => { count--; this.tip = `请${count}秒后再获取`; if (count === 0) { this.isGetCaptcha = false; clearInterval(timerC); } }, 1000); break; case 400: this.$message({ showClose: true, message: "发送验证码超过限制:每个手机号一天只能发5条验证码", type: "warning", center: true, }); break; case 405: this.$message({ showClose: true, message: "发送验证码间隔过短", type: "warning", center: true, }); break; default: this.$message({ showClose: true, message: "手机号不符合规范", type: "warning", center: true, }); break; } }); // } // }); } },
//点击注册按钮事件 register(formName) { this.$refs[formName].validate(valid => { if (valid) { // 检查验证码 checkAuthcode(this.ruleForm.phone, this.ruleForm.captcha) .then(res => { // 注册 if (res.data.code == 200) { register(this.ruleForm.phone, this.ruleForm.captcha, this.ruleForm.pass, this.ruleForm.nickname) .then(res => { // console.log(res); if (res.data.code == 200) { this.$message({ showClose: true, message: "注册成功", type: "success", center: true, }); setTimeout(() => { this.changeLoginWay(0); }, 500); } }) .catch(err => { this.$message({ showClose: true, message: "改昵称已被占用", type: "warning", center: true, }); }); } }) .catch(err => { this.$message({ showClose: true, message: "验证码错误", type: "warning", center: true, }); }); } else { this.$message({ showClose: true, message: "请填写完整表单", type: "warning", center: true, }); } }); }, }, }; </script>
<style lang="less" scoped> .login-pop { position: absolute; top: 120px; left: 50%; transform: translateX(-50%); z-index: 99; box-shadow: 0px 0px 5px 5px #eee; background: #dcdcdc; border-radius: 10px; width: 400px; height: 520px; background: #fff; text-align: center; .close { text-align: right; cursor: pointer; padding: 10px 20px 0 0; .iconfont { font-size: 22px; } } } .imgs { width: 150px; height: 150px; margin: 0 auto; line-height: 150px; .icon-tel { font-size: 82px; color: var(--themeColor); } padding-bottom: 20px; } .form { padding-right: 30px; } .phone-login { .el-form-item { margin-bottom: 12px; } .btn { width: 300px; margin: 0 auto 12px; transform: translateX(30px); } } .qr-login { .qr { h2 { margin-top: 20px; } width: 100%; margin-bottom: 50px; img { width: 220px; height: 220px; } .failqr { margin-bottom: 5px; span { color: var(--themeColor); cursor: pointer; } } } } .other-way { span:hover { color: var(--themeColor); cursor: pointer; } } .register { h2 { margin-top: 10px; } .reg-form { margin-top: 20px; padding-right: 20px; .captcha-input { position: relative; .captcha-btn { position: absolute; right: 5px; top: 0; cursor: pointer; font-size: 12px; color: var(--themeColor); } .captcha { position: absolute; right: 5px; top: 0; font-size: 12px; color: #999; } } } .reg-btns { width: 250px; display: flex; justify-content: space-between; margin: 0 auto 12px; transform: translateX(18px); .reg-btn { width: 48%; } } /deep/ .el-form-item__label { padding-right: 5px; } } .el-button { width: 100%; } </style>
|