个人学习注册登录与注册
前端Spring
usercontorller
点击查看代码
package com.example.demo.controller;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
@ApiOperation("用户登录")
public Result login(@RequestParam String studentId, @RequestParam String password) {
User user = userService.login(studentId, password);
if (user != null) {
return Result.success(user);
}
return Result.fail("用户名或密码错误");
}
@PostMapping("/register")
@ApiOperation("用户注册")
public Result register(@RequestBody User user) {
if (userService.register(user)) {
return Result.success();
}
return Result.fail("注册失败,该学号已存在");
}
}
user类
点击查看代码
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="User对象", description="用户信息表")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "学号")
@TableId(value = "student_id", type = IdType.INPUT)
private String studentId;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "手机号码")
private String phone;
@ApiModelProperty(value = "班级")
private String className;
@ApiModelProperty(value = "密码")
private String password;
public User(String studentId, String name, String phone, String className, String password) {
this.studentId = studentId;
this.name = name;
this.phone = phone;
this.className = className;
this.password = password;
}
public User() {
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
userServiceImpl
点击查看代码
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public User login(String studentId, String password) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("student_id", studentId)
.eq("password", password);
return this.getOne(queryWrapper);
}
@Override
public boolean register(User user) {
// 检查用户是否已存在
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("student_id", user.getStudentId());
if (this.count(queryWrapper) > 0) {
return false;
}
return this.save(user);
}
}
UserService
点击查看代码
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.User;
public interface UserService extends IService<User> {
User login(String studentId, String password);
boolean register(User user);
}
点击查看代码
package com.qi.demo.entity
data class User(
val studentId: String,
val name: String,
val phone: String,
val className: String,
val password: String
)
点击查看代码
package com.qi.demo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.qi.demo.entity.User
import com.qi.demo.network.ServiceCreator
import com.qi.demo.service.UserService
import com.qi.demo.model.Result
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class LoginActivity : AppCompatActivity() {
private lateinit var studentIdInput: EditText
private lateinit var passwordInput: EditText
private lateinit var loginButton: Button
private lateinit var registerButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
initViews()
val userService = ServiceCreator.create<UserService>()
loginButton.setOnClickListener {
val studentId = studentIdInput.text.toString()
val password = passwordInput.text.toString()
if (studentId.isNotEmpty() && password.isNotEmpty()) {
userService.login(studentId, password).enqueue(object : Callback<Result<User>> {
override fun onResponse(call: Call<Result<User>>, response: Response<Result<User>>) {
val result = response.body()
if (result?.code == 200) {
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
finish()
} else {
Toast.makeText(this@LoginActivity, result?.msg ?: "登录失败", Toast.LENGTH_SHORT).show()
}
}
override fun onFailure(call: Call<Result<User>>, t: Throwable) {
Toast.makeText(this@LoginActivity, "网络错误,请稍后重试", Toast.LENGTH_SHORT).show()
}
})
} else {
Toast.makeText(this, "请输入学号和密码", Toast.LENGTH_SHORT).show()
}
}
registerButton.setOnClickListener {
startActivity(Intent(this, RegisterActivity::class.java))
}
}
private fun initViews() {
studentIdInput = findViewById(R.id.studentIdInput)
passwordInput = findViewById(R.id.passwordInput)
loginButton = findViewById(R.id.loginButton)
registerButton = findViewById(R.id.registerButton)
}
}
点击查看代码
package com.qi.demo
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.qi.demo.entity.User
import com.qi.demo.network.ServiceCreator
import com.qi.demo.service.UserService
import com.qi.demo.model.Result
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class RegisterActivity : AppCompatActivity() {
private lateinit var studentIdInput: EditText
private lateinit var nameInput: EditText
private lateinit var phoneInput: EditText
private lateinit var classNameInput: EditText
private lateinit var passwordInput: EditText
private lateinit var submitButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
initViews()
val userService = ServiceCreator.create<UserService>()
submitButton.setOnClickListener {
val user = User(
studentId = studentIdInput.text.toString(),
name = nameInput.text.toString(),
phone = phoneInput.text.toString(),
className = classNameInput.text.toString(),
password = passwordInput.text.toString()
)
if (validateInputs(user)) {
userService.register(user).enqueue(object : Callback<Result<User>> {
override fun onResponse(call: Call<Result<User>>, response: Response<Result<User>>) {
val result = response.body()
if (result?.code == 200) {
Toast.makeText(this@RegisterActivity, "注册成功", Toast.LENGTH_SHORT).show()
finish()
} else {
Toast.makeText(this@RegisterActivity, result?.msg ?: "注册失败", Toast.LENGTH_SHORT).show()
}
}
override fun onFailure(call: Call<Result<User>>, t: Throwable) {
Toast.makeText(this@RegisterActivity, "网络错误,请稍后重试", Toast.LENGTH_SHORT).show()
}
})
}
}
}
private fun initViews() {
studentIdInput = findViewById(R.id.studentIdInput)
nameInput = findViewById(R.id.nameInput)
phoneInput = findViewById(R.id.phoneInput)
classNameInput = findViewById(R.id.classNameInput)
passwordInput = findViewById(R.id.passwordInput)
submitButton = findViewById(R.id.submitButton)
}
private fun validateInputs(user: User): Boolean {
when {
user.studentId.isEmpty() -> {
Toast.makeText(this, "请输入学号", Toast.LENGTH_SHORT).show()
return false
}
user.name.isEmpty() -> {
Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show()
return false
}
user.phone.isEmpty() || !user.phone.matches(Regex("^1[3-9]\\d{9}$")) -> {
Toast.makeText(this, "请输入有效的手机号码", Toast.LENGTH_SHORT).show()
return false
}
user.className.isEmpty() -> {
Toast.makeText(this, "请输入班级", Toast.LENGTH_SHORT).show()
return false
}
user.password.isEmpty() -> {
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show()
return false
}
}
return true
}
}
点击查看代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/studentIdInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="学号"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/registerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginTop="10dp"/>
</LinearLayout>
点击查看代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/studentIdInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="学号"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/phoneInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手机号码"
android:inputType="phone"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/classNameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="班级"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginTop="20dp"/>
</LinearLayout>
浙公网安备 33010602011771号