当前位置:   article > 正文

Spring Boot 笔记 023 注册页面

Spring Boot 笔记 023 注册页面

1.1 request.js请求工具

  1. //定制请求的实例
  2. //导入axios npm install axios
  3. import axios from 'axios';
  4. //定义一个变量,记录公共的前缀 , baseURL
  5. const baseURL = '/api';
  6. const instance = axios.create({baseURL})
  7. //添加响应拦截器
  8. instance.interceptors.response.use(
  9. result=>{
  10. return result.data;
  11. },
  12. err=>{
  13. alert('服务异常');
  14. return Promise.reject(err);//异步的状态转化成失败的状态
  15. }
  16. )
  17. export default instance;

2.1 编写注册接口

  1. //导入request.js请求工具
  2. import request from '@/utils/request.js'
  3. //提供调用注册接口的函数
  4. export const userRegisterService = (registerData)=>{
  5. //借助于UrlSearchParams完成传递
  6. const params = new URLSearchParams()
  7. for(let key in registerData){
  8. params.append(key,registerData[key]);
  9. }
  10. return request.post('/user/register',params);
  11. }

3.1 编写页面

3.2 给表单绑定数据

3.3 定义表单校验规则

3.4 调用注册接口

  1. ```html
  2. <script setup>
  3. import { User, Lock } from '@element-plus/icons-vue'
  4. import { ref } from 'vue'
  5. //控制注册与登录表单的显示, 默认显示注册
  6. const isRegister = ref(false)
  7. //定义数据模型
  8. const registerData = ref({
  9. username: '',
  10. password: '',
  11. rePassword: ''
  12. })
  13. //校验密码的函数
  14. const checkRePassword = (rule, value, callback) => {
  15. if (value === '') {
  16. callback(new Error('请再次确认密码'))
  17. } else if (value !== registerData.value.password) {
  18. callback(new Error('请确保两次输入的密码一样'))
  19. } else {
  20. callback()
  21. }
  22. }
  23. //定义表单校验规则
  24. const rules = {
  25. username: [
  26. { required: true, message: '请输入用户名', trigger: 'blur' },
  27. { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  28. ],
  29. password: [
  30. { required: true, message: '请输入密码', trigger: 'blur' },
  31. { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  32. ],
  33. rePassword: [
  34. { validator: checkRePassword, trigger: 'blur' }
  35. ]
  36. }
  37. //调用后台接口,完成注册
  38. import { userRegisterService} from '@/api/user.js'
  39. const register = async () => {
  40. //registerData是一个响应式对象,如果要获取值,需要.value
  41. let result = await userRegisterService(registerData.value);
  42. if (result.code === 0) {
  43. //成功了
  44. alert(result.msg ? result.msg : '注册成功');
  45. }else{
  46. //失败了
  47. alert('注册失败')
  48. }
  49. //alert(result.msg ? result.msg : '注册成功');
  50. // ElMessage.success(result.msg ? result.msg : '注册成功')
  51. }
  52. </script>
  53. <template>
  54. <el-row class="login-page">
  55. <el-col :span="12" class="bg"></el-col>
  56. <el-col :span="6" :offset="3" class="form">
  57. <!-- 注册表单 -->
  58. <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerData" :rules="rules">
  59. <el-form-item>
  60. <h1>注册</h1>
  61. </el-form-item>
  62. <el-form-item prop="username">
  63. <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
  64. </el-form-item>
  65. <el-form-item prop="password">
  66. <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
  67. v-model="registerData.password"></el-input>
  68. </el-form-item>
  69. <el-form-item prop="rePassword">
  70. <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码"
  71. v-model="registerData.rePassword"></el-input>
  72. </el-form-item>
  73. <!-- 注册按钮 -->
  74. <el-form-item>
  75. <el-button class="button" type="primary" auto-insert-space @click="register">
  76. 注册
  77. </el-button>
  78. </el-form-item>
  79. <el-form-item class="flex">
  80. <el-link type="info" :underline="false" @click="isRegister = false">
  81. ← 返回
  82. </el-link>
  83. </el-form-item>
  84. </el-form>
  85. <!-- 登录表单 -->
  86. <el-form ref="form" size="large" autocomplete="off" v-else>
  87. <el-form-item>
  88. <h1>登录</h1>
  89. </el-form-item>
  90. <el-form-item>
  91. <el-input :prefix-icon="User" placeholder="请输入用户名"></el-input>
  92. </el-form-item>
  93. <el-form-item>
  94. <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
  95. </el-form-item>
  96. <el-form-item class="flex">
  97. <div class="flex">
  98. <el-checkbox>记住我</el-checkbox>
  99. <el-link type="primary" :underline="false">忘记密码?</el-link>
  100. </div>
  101. </el-form-item>
  102. <!-- 登录按钮 -->
  103. <el-form-item>
  104. <el-button class="button" type="primary" auto-insert-space>登录</el-button>
  105. </el-form-item>
  106. <el-form-item class="flex">
  107. <el-link type="info" :underline="false" @click="isRegister = true">
  108. 注册 →
  109. </el-link>
  110. </el-form-item>
  111. </el-form>
  112. </el-col>
  113. </el-row>
  114. </template>
  115. <style lang="scss" scoped>
  116. /* 样式 */
  117. .login-page {
  118. height: 100vh;
  119. background-color: #fff;
  120. .bg {
  121. background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
  122. url('@/assets/login_bg.jpg') no-repeat center / cover;
  123. border-radius: 0 20px 20px 0;
  124. }
  125. .form {
  126. display: flex;
  127. flex-direction: column;
  128. justify-content: center;
  129. user-select: none;
  130. .title {
  131. margin: 0 auto;
  132. }
  133. .button {
  134. width: 100%;
  135. }
  136. .flex {
  137. width: 100%;
  138. display: flex;
  139. justify-content: space-between;
  140. }
  141. }
  142. }
  143. </style>
  144. ```

3.1 跨域问题

3.1.1 修改request.js

  1. //定制请求的实例
  2. //导入axios npm install axios
  3. import axios from 'axios';
  4. //定义一个变量,记录公共的前缀 , baseURL
  5. const baseURL = '/api';
  6. const instance = axios.create({baseURL})
  7. //添加响应拦截器
  8. instance.interceptors.response.use(
  9. result=>{
  10. return result.data;
  11. },
  12. err=>{
  13. alert('服务异常');
  14. return Promise.reject(err);//异步的状态转化成失败的状态
  15. }
  16. )
  17. export default instance;

3.1.2 修改vite.config.js

  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. // https://vitejs.dev/config/
  5. export default defineConfig({
  6. plugins: [
  7. vue(),
  8. ],
  9. resolve: {
  10. alias: {
  11. '@': fileURLToPath(new URL('./src', import.meta.url))
  12. }
  13. },
  14. server:{
  15. proxy:{
  16. '/api':{//获取路径中包含了/api的请求
  17. target:'http://localhost:8080',//后台服务所在的源
  18. changeOrigin:true,//修改源
  19. rewrite:(path)=>path.replace(/^\/api/,'')///api替换为''
  20. }
  21. }
  22. }
  23. })

4.1 测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/120987
推荐阅读
相关标签
  

闽ICP备14008679号