register.vue 7.3 KB

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