register.vue 5.8 KB

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