register.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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>{{ footerContent }}</span>
  74. </div>
  75. </div>
  76. </template>
  77. <script setup>
  78. import { ElMessageBox } from "element-plus"
  79. import { getCodeImg, register } from "@/api/login"
  80. import defaultSettings from '@/settings'
  81. const title = import.meta.env.VITE_APP_TITLE
  82. const footerContent = defaultSettings.footerContent
  83. const router = useRouter()
  84. const { proxy } = getCurrentInstance()
  85. const registerForm = ref({
  86. username: "",
  87. password: "",
  88. confirmPassword: "",
  89. code: "",
  90. uuid: ""
  91. })
  92. const equalToPassword = (rule, value, callback) => {
  93. if (registerForm.value.password !== value) {
  94. callback(new Error("两次输入的密码不一致"))
  95. } else {
  96. callback()
  97. }
  98. }
  99. const registerRules = {
  100. username: [
  101. { required: true, trigger: "blur", message: "请输入您的账号" },
  102. { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  103. ],
  104. password: [
  105. { required: true, trigger: "blur", message: "请输入您的密码" },
  106. { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
  107. { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符:< > \" ' \\\ |", trigger: "blur" }
  108. ],
  109. confirmPassword: [
  110. { required: true, trigger: "blur", message: "请再次输入您的密码" },
  111. { required: true, validator: equalToPassword, trigger: "blur" }
  112. ],
  113. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  114. }
  115. const codeUrl = ref("")
  116. const loading = ref(false)
  117. const captchaEnabled = ref(true)
  118. function handleRegister() {
  119. proxy.$refs.registerRef.validate(valid => {
  120. if (valid) {
  121. loading.value = true
  122. register(registerForm.value).then(res => {
  123. const username = registerForm.value.username
  124. ElMessageBox.alert("<font color='red'>恭喜你,您的账号 " + username + " 注册成功!</font>", "系统提示", {
  125. dangerouslyUseHTMLString: true,
  126. type: "success",
  127. }).then(() => {
  128. router.push("/login")
  129. }).catch(() => {})
  130. }).catch(() => {
  131. loading.value = false
  132. if (captchaEnabled) {
  133. getCode()
  134. }
  135. })
  136. }
  137. })
  138. }
  139. function getCode() {
  140. getCodeImg().then(res => {
  141. captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
  142. if (captchaEnabled.value) {
  143. codeUrl.value = "data:image/gif;base64," + res.img
  144. registerForm.value.uuid = res.uuid
  145. }
  146. })
  147. }
  148. getCode()
  149. </script>
  150. <style lang='scss' scoped>
  151. .register {
  152. display: flex;
  153. justify-content: center;
  154. align-items: center;
  155. height: 100%;
  156. background-image: url("../assets/images/login-background.png");
  157. background-size: cover;
  158. }
  159. .title {
  160. margin: 0px auto 30px auto;
  161. text-align: center;
  162. color: #707070;
  163. }
  164. .register-form {
  165. border-radius: 6px;
  166. background: #ffffff;
  167. width: 400px;
  168. padding: 25px 25px 5px 25px;
  169. .el-input {
  170. height: 40px;
  171. input {
  172. height: 40px;
  173. }
  174. }
  175. .input-icon {
  176. height: 39px;
  177. width: 14px;
  178. margin-left: 0px;
  179. }
  180. }
  181. .register-tip {
  182. font-size: 13px;
  183. text-align: center;
  184. color: #bfbfbf;
  185. }
  186. .register-code {
  187. width: 33%;
  188. height: 40px;
  189. float: right;
  190. img {
  191. cursor: pointer;
  192. vertical-align: middle;
  193. }
  194. }
  195. .el-register-footer {
  196. height: 40px;
  197. line-height: 40px;
  198. position: fixed;
  199. bottom: 0;
  200. width: 100%;
  201. text-align: center;
  202. color: #fff;
  203. font-family: Arial;
  204. font-size: 12px;
  205. letter-spacing: 1px;
  206. }
  207. .register-code-img {
  208. height: 40px;
  209. padding-left: 12px;
  210. }
  211. </style>