智能简历平台Plus——登录注册的实现(简历功能实现)(7)

概要

  • 前端页面实现
  • 后端与数据库相连
  • 主要功能讲解

前端页面实现

提示:这里可以添加技术架构

例如:
在语言模型中,编码器和解码器都是由一个个的 Transformer 组件拼接在一起形成的。
**

前端页面实现

**在这里插入图片描述
在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/86e0508c4f694303a7060ddb8d3b8863.png#pic_center

  • 注册

```css
 -<template>
  <div class="container">
    <div class="form-box">
      <h3>注册</h3>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" v-model="username" id="username" required style="width: 200px; height: 30px;"/>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" v-model="password" id="password" required style="width: 225px; height: 30px;"/>
        </div>
        <div class="form-group">
          <label for="phone">手机号:</label>
          <input type="text" v-model="phone" id="phone" required style="width: 200px; height: 30px;"/>
        </div>
        <button type="submit" class="form-button">注册</button>
      </form>
      <p v-if="message">{{ message }}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { API_ENDPOINT } from '@/config'; // 引入配置文件

export default {
  name: 'RegisterPage',
  data() {
    return {
      username: '',
      password: '',
      phone: '',
      message: ''
    };
  },
  methods: {
    async handleRegister() {
      // Check if username, password, and phone are not empty
      if (this.username && this.password && this.phone) {
        try {
          const response = await axios.post(`${API_ENDPOINT}/register`, {
            username: this.username,
            password: this.password,
            phone: this.phone
          });
          this.message = response.data.message;
          if (response.status === 201) {
            this.$router.push({ name: 'Home' });
          }
        } catch (error) {
          if (error.response && error.response.data) {
            this.message = error.response.data.message;
          } else {
            this.message = '注册失败,请重试';
          }
        }
      }
    }
  }
};
</script>

<style scoped>
/* Add some basic styling for better appearance */
.container {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh; /* Full height to center vertically */
  background-image: url('注册.png'); /* 设置背景图片 */
  background-size: cover; /* 背景图片覆盖整个容器 */
  background-position: center; /* 背景图片居中 */
  background-repeat: no-repeat; /* 背景图片不重复 */
}

.container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #a3bf61;
  z-index: -1; /* Place it behind the form-box */
}

.form-box {
  position: relative;
  background-color: #f0f0f0;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 350px;
  text-align: center;
  z-index: 1; /* Ensure it stays on top */
}

.form-box h3{
  font-size: 2em;
}

.form-group {
  margin-bottom: 15px; /* Adjust this value to control the spacing */
  text-align: left; /* 左对齐 */
}

.form-group label{
  font-size: 1.5em;
  margin-right: 10px;
}

.form-button {
  margin-top: 10px; /* Adjust this value to control the spacing */
  padding: 15px;
  background-color: #81b975;
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.2em;
}

.form-button:hover {
  background-color: #4a6a43e4;
}
</style>

 - ``登录`
<template>
  <div class="container">
    <div class="form-box">
      <h3>登录</h3>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" v-model="username" id="username" required style="width: 200px; height: 30px;"/>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" v-model="password" id="password" required style="width: 225px; height: 30px;"/>
        </div>
        <button type="submit" class="form-button">进入</button>
      </form>
      <p v-if="message">{{ message }}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { API_ENDPOINT } from '@/config'; // 引入配置文件

export default {
  name: 'LoginPage',
  data() {
    return {
      username: '',
      password: '',
      message: ''
    };
  },
  methods: {
    async handleLogin() {
      // Check if username and password are not empty
      if (this.username && this.password) {
        try {
          const response = await axios.post(`${API_ENDPOINT}/login`, {
            username: this.username,
            password: this.password
          });
          this.message = response.data.message;
          if (response.status === 200) {
            this.$router.push({ name: 'Home' });
          }
        } catch (error) {
          if (error.response && error.response.data) {
            this.message = error.response.data.message;
          } else {
            this.message = '登录失败,请重试';
          }
        }
      }
    }
  }
};
</script>

<style scoped>
/* Add some basic styling for better appearance */
.container {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh; /* Full height to center vertically */
  background-image: url('登录.png'); /* 设置背景图片 */
  background-size: cover; /* 背景图片覆盖整个容器 */
  background-position: center; /* 背景图片居中 */
  background-repeat: no-repeat; /* 背景图片不重复 */
}

.container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #a3bf61;
  z-index: -1; /* Place it behind the form-box */
}

.form-box {
  position: relative;
  background-color: #f0f0f0;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 300px;
  text-align: center;
  z-index: 1; /* Ensure it stays on top */

}

.form-box h3{
  font-size: 2em;
}

.form-group {
  margin-bottom: 15px; /* Adjust this value to control the spacing */
  text-align: left; /* 左对齐 */
  align-items: center; /* 垂直居中按钮和文字 */
}

.form-group label{
  font-size: 1.5em;
  margin-right: 10px;
}

.form-button {
  margin-top: 10px; /* Adjust this value to control the spacing */
  padding: 15px;
  background-color: #81b975;
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.2em;
}

.form-button:hover {
  background-color: #4a6a43e4;
}
</style>

后端和数据库相连

` 将前端数据传到后端数据库
注册用户
改写后端代码使它可以将数据传到前端,并根据数据库里的数据去判断账号是否被注册,是否注册成功等


```css
@app.route('/register', methods=['POST'])
def register():
    data = request.json
    username = data['username']
    password = data['password']

    conn = get_db_connection()
    cursor = conn.cursor()

    cursor.execute("SELECT * FROM users WHERE username=?", (username,))
    existing_user = cursor.fetchone()
    if existing_user:
        return jsonify({"message": "该用户名已被注册"}), 400

    hashed_password = hashlib.sha256(password.encode()).hexdigest()
    cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))
    conn.commit()

    cursor.close()
    conn.close()

    return jsonify({"message": "用户注册成功"}), 201

登录账户
改写登录数据库代码,使它可以和前端相连,并根据数据库里的已有数据实现登录

# 登录用户
@app.route('/login', methods=['POST'])
def login():
    data = request.json
    username = data['username']
    password = data['password']

    hashed_password = hashlib.sha256(password.encode()).hexdigest()

    conn = get_db_connection()
    cursor = conn.cursor()

    cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, hashed_password))
    user = cursor.fetchone()

    cursor.close()
    conn.close()

    if user:
        return jsonify({"message": "登录成功"}), 200
    else:
        return jsonify({"message": "用户名或密码错误"}), 401

后端数据库查询如图

在这里插入图片描述

实现功能

如果用户已被注册过,就会报注册失败
在这里插入图片描述
如果用户登录密码和用户名错误,或者用户从未注册,就会报登录失败
在这里插入图片描述

### 回答1: Mybatis-plus可以通过以下步骤实现登录注册功能: 1. 创建用户表,包括用户ID、用户名、密码等字段。 2. 创建对应的实体类,使用注解@Table和@Column来映射数据库表和字段。 3. 创建对应的Mapper接口,继承BaseMapper,使用注解@Mapper来标识。 4. 在Mapper接口中定义登录和注册的方法,使用注解@Select和@Insert来实现对应的SQL语句。 5. 在Service层中调用Mapper接口中的方法,实现登录和注册的业务逻辑。 6. 在Controller层中定义对应的接口,接收前端传递的参数,并调用Service层中的方法。 7. 在前端页面中实现登录和注册的表单,通过Ajax请求后端接口来实现登录和注册功能。 以上是使用Mybatis-plus实现登录注册的基本步骤,具体实现过程需要根据具体业务需求进行调整。 ### 回答2: 在使用Mybatis-Plus实现登录注册功能时,我们可以使用基本的Mybatis-Plus的CURD操作,同时也可以使用其提供的条件构造器和分页插件等功能来更好地实现功能。 注册: 在注册功能中,首先我们需要页面上有一个表单用于用户填写信息,然后将用户输入的信息存入数据库中。新建一个实体类用于接收前端传来的值,例如:User对象。 接着,我们可以使用Mybatis-Plus提供的BaseMapper实现新建一条记录的功能,在注册时插入一条数据,示例代码如下: ```java @Service public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService { @Override public boolean register(User user) { // 查询数据库中是否存在重名的用户 QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("username", user.getUsername()); User existUser = baseMapper.selectOne(wrapper); if (existUser != null) { return false; } // 没有重名的用户,则插入新用户 baseMapper.insert(user); return true; } } ``` 登录: 在登录功能中,我们需要实现用户输入用户名和密码,然后将该信息与数据库的对应记录进行比对,以判断该用户输入是否正确。首先,我们可以查询出对应用户名的数据,检查输入的密码是否与数据库中存储的密码一致。如果一致,则登录成功,返回登录成功的标记;否则登录失败,返回登录失败的标记。 示例代码如下: ```java @Service public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService { @Override public String login(String username, String password) { // 根据用户名查询用户信息 QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("username", username); User user = baseMapper.selectOne(wrapper); if (user == null) { // 用户不存在 return "用户不存在"; } if (!password.equals(user.getPassword())) { // 密码错误 return "密码错误"; } // 登录成功 return "登录成功!"; } } ``` 以上就是使用Mybatis-Plus实现登录注册功能的简单示例,在实际项目中可能需要进行更多的复杂操作,但基本的CURD和条件查询功能都可以方便地使用Mybatis-Plus完成。 ### 回答3: Mybatis-Plus是一个优秀的ORM框架,它提供了大量的便捷功能,可以降低开发难度和提高开发效率。在实现登录注册功能时,使用Mybatis-Plus可以大大简化开发流程。 首先,在数据库中创建用户表,字段包括用户名、密码、邮箱等。接着,创建对应的Java实体类,使用Mybatis-Plus提供的注解进行映射。在Mapper接口中继承Mybatis-Plus提供的BaseMapper,即可使用其默认的一些基础操作,如增删改查等。 在登录注册功能实现中,需要进行身份验证,即判断用户输入的用户名和密码是否与数据库中的记录匹配。使用Mybatis-Plus的QueryWrapper可以方便地进行查询,而lambda表达式则可以消除SQL注入的风险。 注册功能则需要判断用户名是否已存在,并对密码进行加密存储。Mybatis-Plus提供的InsertWrapper和UpdateWrapper可以方便地进行插入和更新操作。 需要注意的是,密码加密存储可以使用常用的加密算法,如MD5、SHA、BCrypt等,也可以选择使用框架提供的加密工具类。 同时,为了提高开发效率,可以使用Mybatis-Plus提供的自动代码生成器,根据数据库的表结构自动生成Java实体类、Mapper接口、Service接口和Controller等代码。 总之,使用Mybatis-Plus实现登录注册功能,可以简化开发流程、提高开发效率,同时提供了多种便捷的操作方式,方便开发人员进行二次开发和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值