register.vue 5.9 KB

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