register.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="register">
  3. <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
  4. <h3 class="title">{{ title }}</h3>
  5. <el-form-item prop="username">
  6. <el-input
  7. v-model="registerForm.username"
  8. type="text"
  9. size="large"
  10. auto-complete="off"
  11. placeholder="账号"
  12. >
  13. <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
  14. </el-input>
  15. </el-form-item>
  16. <el-form-item prop="password">
  17. <el-input
  18. v-model="registerForm.password"
  19. type="password"
  20. size="large"
  21. auto-complete="off"
  22. placeholder="密码"
  23. @keyup.enter="handleRegister"
  24. >
  25. <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
  26. </el-input>
  27. </el-form-item>
  28. <el-form-item prop="confirmPassword">
  29. <el-input
  30. v-model="registerForm.confirmPassword"
  31. type="password"
  32. size="large"
  33. auto-complete="off"
  34. placeholder="确认密码"
  35. @keyup.enter="handleRegister"
  36. >
  37. <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
  38. </el-input>
  39. </el-form-item>
  40. <el-form-item prop="code" v-if="captchaEnabled">
  41. <el-input
  42. size="large"
  43. v-model="registerForm.code"
  44. auto-complete="off"
  45. placeholder="验证码"
  46. style="width: 63%"
  47. @keyup.enter="handleRegister"
  48. >
  49. <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
  50. </el-input>
  51. <div class="register-code">
  52. <img :src="codeUrl" @click="getCode" class="register-code-img"/>
  53. </div>
  54. </el-form-item>
  55. <el-form-item style="width:100%;">
  56. <el-button
  57. :loading="loading"
  58. size="large"
  59. type="primary"
  60. style="width:100%;"
  61. @click.prevent="handleRegister"
  62. >
  63. <span v-if="!loading">注 册</span>
  64. <span v-else>注 册 中...</span>
  65. </el-button>
  66. <div style="float: right;">
  67. <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
  68. </div>
  69. </el-form-item>
  70. </el-form>
  71. <!-- 底部 -->
  72. <div class="el-register-footer">
  73. <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
  74. </div>
  75. </div>
  76. </template>
  77. <script setup>
  78. import { ElMessageBox } from "element-plus"
  79. import { getCodeImg, register } from "@/api/login"
  80. const title = import.meta.env.VITE_APP_TITLE
  81. const router = useRouter()
  82. const { proxy } = getCurrentInstance()
  83. const registerForm = ref({
  84. username: "",
  85. password: "",
  86. confirmPassword: "",
  87. code: "",
  88. uuid: ""
  89. })
  90. const equalToPassword = (rule, value, callback) => {
  91. if (registerForm.value.password !== value) {
  92. callback(new Error("两次输入的密码不一致"))
  93. } else {
  94. callback()
  95. }
  96. }
  97. const registerRules = {
  98. username: [
  99. { required: true, trigger: "blur", message: "请输入您的账号" },
  100. { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  101. ],
  102. password: [
  103. { required: true, trigger: "blur", message: "请输入您的密码" },
  104. { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
  105. { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符:< > \" ' \\\ |", trigger: "blur" }
  106. ],
  107. confirmPassword: [
  108. { required: true, trigger: "blur", message: "请再次输入您的密码" },
  109. { required: true, validator: equalToPassword, trigger: "blur" }
  110. ],
  111. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  112. }
  113. const codeUrl = ref("")
  114. const loading = ref(false)
  115. const captchaEnabled = ref(true)
  116. function handleRegister() {
  117. proxy.$refs.registerRef.validate(valid => {
  118. if (valid) {
  119. loading.value = true
  120. register(registerForm.value).then(res => {
  121. const username = registerForm.value.username
  122. ElMessageBox.alert("<font color='red'>恭喜你,您的账号 " + username + " 注册成功!</font>", "系统提示", {
  123. dangerouslyUseHTMLString: true,
  124. type: "success",
  125. }).then(() => {
  126. router.push("/login")
  127. }).catch(() => {})
  128. }).catch(() => {
  129. loading.value = false
  130. if (captchaEnabled) {
  131. getCode()
  132. }
  133. })
  134. }
  135. })
  136. }
  137. function getCode() {
  138. getCodeImg().then(res => {
  139. captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
  140. if (captchaEnabled.value) {
  141. codeUrl.value = "data:image/gif;base64," + res.img
  142. registerForm.value.uuid = res.uuid
  143. }
  144. })
  145. }
  146. getCode()
  147. </script>
  148. <style lang='scss' scoped>
  149. .register {
  150. display: flex;
  151. justify-content: center;
  152. align-items: center;
  153. height: 100%;
  154. background-image: url("../assets/images/login-background.jpg");
  155. background-size: cover;
  156. }
  157. .title {
  158. margin: 0px auto 30px auto;
  159. text-align: center;
  160. color: #707070;
  161. }
  162. .register-form {
  163. border-radius: 6px;
  164. background: #ffffff;
  165. width: 400px;
  166. padding: 25px 25px 5px 25px;
  167. .el-input {
  168. height: 40px;
  169. input {
  170. height: 40px;
  171. }
  172. }
  173. .input-icon {
  174. height: 39px;
  175. width: 14px;
  176. margin-left: 0px;
  177. }
  178. }
  179. .register-tip {
  180. font-size: 13px;
  181. text-align: center;
  182. color: #bfbfbf;
  183. }
  184. .register-code {
  185. width: 33%;
  186. height: 40px;
  187. float: right;
  188. img {
  189. cursor: pointer;
  190. vertical-align: middle;
  191. }
  192. }
  193. .el-register-footer {
  194. height: 40px;
  195. line-height: 40px;
  196. position: fixed;
  197. bottom: 0;
  198. width: 100%;
  199. text-align: center;
  200. color: #fff;
  201. font-family: Arial;
  202. font-size: 12px;
  203. letter-spacing: 1px;
  204. }
  205. .register-code-img {
  206. height: 40px;
  207. padding-left: 12px;
  208. }
  209. </style>