登录/忘记密码+验证

不啰嗦直接上代码

1、有使用全局的class名,请忽略或修改;
2、有本人自定的接口名称,请忽略或修改;
3、SlideAssembly 登录滑块,无需要请忽略,有需要,在下方链接自取;
https://www.jianshu.com/p/2cb40676d71b

  <template>
  <div class="login">
    <div class="right" v-if="active == 1">
      <h2>用户登录</h2>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="formSty demo-ruleForm"
      >
        <el-form-item prop="phone">
          <input
            class="inputSty"
            v-model="ruleForm.phone"
            placeholder="请输入账号"
            :onkeyup="check(ruleForm.phone)"
          />
        </el-form-item>
        <el-form-item prop="password">
          <input
            class="inputSty"
            type="password"
            v-model="ruleForm.password"
            placeholder="请输入密码"
          />
        </el-form-item>
      </el-form>
      <!-- 登录滑块 -->
      <SlideAssembly
        class="slide"
        :successFun="successFun"
        :errorFun="errorFun"
        v-if="SlideAssemblyOk"
      ></SlideAssembly>
      <div class="loginBtn" @click="loginPhone('ruleForm')">
        登录
        <!-- <img class="banner" src="@/assets/images/loginbtn.png" alt="" /> -->
      </div>
      <div style="position: relative">
        <el-button
          style="
            position: absolute;
            bottom: -67px;
            right: 15px;
            border: 1px solid rgb(47, 169, 253);
            padding: 6px;
          "
          type="text"
          @click="active = 2"
          >忘记密码</el-button
        >
      </div>
    </div>
    <div class="right" v-if="active == 2">
      <h2>修改密码</h2>
      <el-form :model="setForm" ref="ruleForm" class="formSty demo-ruleForm">
        <el-form-item>
          <input
            class="inputSty"
            v-model="setForm.tel"
            placeholder="请输入手机号"
            :onkeyup="check2(setForm.tel)"
          />
        </el-form-item>
        <el-form-item>
          <div class="df" style="justify-content: center">
            <input
              class="inputSty"
              style="width: 220px"
              v-model="setForm.code"
              placeholder="请输入验证码"
            />
            <div>
              <el-button
                type="warning"
                @click="setBtn(1)"
                v-if="show"
                class="yanzhengBtn"
                >获取验证码</el-button
              >
              <el-button type="warning" v-if="!show" class="yanzhengBtn"
                >{{ count }}s后重新获取</el-button
              >
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <input
            class="inputSty"
            :type="showpass1 ? 'password' : 'text'"
            v-model="setForm.new_password"
            placeholder="新密码(6~16位字母/数字/符号)"
            maxlength="16"
            :onkeyup="check3(setForm.new_password)"
          />
          <i class="el-icon-view iconsty" @click="eyscli(1)"></i>
        </el-form-item>
        <el-form-item>
          <input
            class="inputSty"
            :type="showpass2 ? 'password' : 'text'"
            v-model="setForm.confirm_password"
            placeholder="确认密码(6~16位字母/数字/符号)"
            maxlength="16"
            :onkeyup="check4(setForm.confirm_password)"
          />
          <i class="el-icon-view iconsty" @click="eyscli(2)"></i>
        </el-form-item>
      </el-form>

      <div class="setBtns" @click="setBtn(2)">修改密码</div>
      <div style="position: relative">
        <el-button
          style="
            position: absolute;
            bottom: -67px;
            right: 15px;
            border: 1px solid rgb(47, 169, 253);
            padding: 6px;
          "
          type="text"
          @click="active = 1"
          >返回登录</el-button
        >
      </div>
    </div>
  </div>
</template>
//js文件
import { login, forgetPassword, sendCode } from "@/api/common";
export default {
  data() {
    return {
      active: 1,
      ruleForm: {
        phone: "",
        password: "",
        status: false,
      },
      SlideAssemblyOk: true,
      rules: {
        phone: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 5,
            max: 18,
            message: "请输入5~18位的正确账号",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "请输入正确密码", trigger: "blur" },
        ],
      },
      setForm: {
        tel: "",
        code: "",
        new_password: "",
        confirm_password: "",
      },
      show: true,
      count: "",
      timer: null,
      showpass1: true,
      showpass2: true,
    };
  },
  created() {
    let that = this;
    document.addEventListener("keydown", that.watchEnter);
  },
  methods: {
    //监听电脑回车键
    watchEnter(e) {
      //获取被按下的键值
      let keyNum = window.event ? e.keyCode : e.which;
      //判断如果用户按下了回车键(keycody=13)
      if (keyNum == 13) {
        this.loginPhone("ruleForm");
      }
    },
    check(str) {
      var temp = "";
      for (let i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 255) {
          temp += str.charAt(i);
        }
      }
      this.ruleForm.phone = temp.replace(/\s*/g, "");
    },
    check2(str) {
      var temp = "";
      for (let i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 255) {
          temp += str.charAt(i);
        }
      }
      this.setForm.tel = temp.replace(/\s*/g, "");
    },
    check3(str) {
      var temp = "";
      for (let i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 255) {
          temp += str.charAt(i);
        }
      }
      this.setForm.new_password = temp.replace(/\s*/g, "");
    },
    check4(str) {
      var temp = "";
      for (let i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 255) {
          temp += str.charAt(i);
        }
      }
      this.setForm.confirm_password = temp.replace(/\s*/g, "");
    },
    // 登陆
    loginPhone(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.ruleForm.status) {
            this.$message.error("请先完成滑块验证");
            return;
          }
          let param = {
            account: this.ruleForm.phone,
            password: this.ruleForm.password,
          };
          login(param)
            .then((res) => {
              let user = res.data.data;
              localStorage.setItem("userInfo", JSON.stringify(user));
              setTimeout(() => {
                location.reload();
              }, 1000);
            })
            .catch((err) => {});
        } else {
          return false;
        }
      });
    },
    // 登录滑块成功
    successFun(data) {
      this.ruleForm.status = true;
    },
    // 登录滑块失败
    errorFun(data) {
      this.ruleForm.status = false;
    },
    // 开启倒计时
    getCode() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    // 获取验证码/修改密码
    setBtn(val) {
      if (val == 1) {
        if (this.setForm.tel) {
          sendCode({ tel: this.setForm.tel }).then((res) => {
            this.$message.success("验证码已发送!");
            this.getCode();
          });
        } else {
          this.$message.info("请输入正确手机号码!");
        }
      }
      if (val == 2) {
        if (!this.setForm.tel) {
          this.$message.info("请输入正确手机号码!");
          return;
        }
        if (!this.setForm.code) {
          this.$message.info("请输入正确验证码!");
          return;
        }
        if (
          !this.setForm.new_password ||
          this.setForm.new_password.length < 6
        ) {
          this.$message.info("请输入新密码!");
          return;
        }
        if (
          !this.setForm.confirm_password ||
          this.setForm.confirm_password != this.setForm.new_password
        ) {
          this.$message.info("请输入正确密码!");
          return;
        }
        forgetPassword(this.setForm).then((res) => {
          this.$message.success("修改成功");
          setTimeout(() => {
            this.ruleForm = {
              phone: this.setForm.tel,
              password: this.setForm.new_password,
              status: true,
            };
            this.loginPhone("ruleForm");
          }, 500);
        });
      }
    },
    eyscli(val) {
      if (val == 1) {
        this.showpass1 = !this.showpass1;
      }
      if (val == 2) {
        this.showpass2 = !this.showpass2;
      }
    },
  },
};
//scss文件
//scss文件
.login {
  min-width: 922px;
  width: 100%;
  height: 100vh;
  position: relative;

  .left {
    width: 100%;
    height: 100%;
    position: relative;

    h1 {
      position: absolute;
      top: 9%;
      left: 6%;
      font-size: 28px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: rgba(50, 75, 88, 1);
    }

    .banner {
      position: absolute;
      top: 18%;
      left: 15%;
    }

    .bg {
      width: 100%;
      height: 100%;
    }
  }

  .right {
    position: absolute;
    top: 17%;
    right: 20%;
    width: 400px;
    padding: 50px 20px 80px 20px;
    box-sizing: border-box;
    width: 410px;
    background: rgba(255, 255, 255, 1);
    border-radius: 8px;
    box-shadow: 0 2px 18px 0 rgba(229, 233, 239, 1);

    h2 {
      font-size: 30px;
      font-family: Microsoft YaHei;
      font-weight: normal;
      color: rgba(51, 51, 51, 1);
      margin-left: 10px;
    }

    .formSty {
      margin-top: 30px;
      text-align: center;

      .inputSty {
        text-indent: 15px;
        border: 1px solid #f2f7fd;
        width: 338px;
        height: 54px;
        color: #111a38;
        background: #f2f7fd;
        border-radius: 10px;

        &:focus {
          outline: none;
          border: 1px solid #1d7cff;
        }
      }
    }

    .slide {
      border-radius: 10px;
      margin-bottom: 15px;
      margin-top: 20px;
      padding: 0 13px;
    }

    .loginBtn {
      height: 48px;
      margin: 20px 12px 0;
      cursor: pointer;
      text-align: center;
      background: linear-gradient(-38deg, rgba(36, 95, 244, 1) 0%, rgba(36, 163, 244, 1) 100%);
      line-height: 48px;
      font-size: 20px;
      font-weight: bold;
      color: #fff;
    }

    .setBtns {
      height: 50px;
      width: 90%;
      background: #278ffc;
      margin: 0 auto;
      border-radius: 8px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      padding: 0 8px;
      cursor: pointer;
    }
  }
}

/deep/.el-form-item__error {
  left: 5%;
}
.df{
  display: flex;
}
.yanzhengBtn {
  width: 100px;
  padding: 15px 0;
  margin-top: 7px;
  margin-left: 17px;
}

.iconsty {
  position: absolute;
  top: 20px;
  right: 30px;
  font-size: 18px;
  color: #999;
  cursor: pointer;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容