register.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="page-container login-container">
  3. <el-card class="login-card">
  4. <h2 class="login-card__title login-card__item">注 册</h2>
  5. <el-input ref="name" v-model="name" placeholder="请输入昵称, 长度为1-10个字符" maxlength="10" class="login-card__item" autofocus></el-input>
  6. <el-input ref="password" v-model="password" type="password" placeholder="请输入密码,长度为6-20个字符" maxlength="20" class="login-card__item"></el-input>
  7. <el-input ref="passwordConfirm" v-model="passwordConfirm" type="password" placeholder="请再次输入密码" maxlength="20" class="login-card__item"></el-input>
  8. <el-input ref="phone" v-model="phone" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入手机号" minlength='11' maxlength='11' class="login-card__item"></el-input>
  9. <el-input ref="identifyCode" v-model="identifyCode" placeholder="请输入验证码" maxlength="6" class="login-card__short-item"></el-input>
  10. <el-button :disabled="disabled" @click="sendMessage" class="login-card__right-button">{{buttonText}}</el-button>
  11. <el-button type="primary" class="login-card__item" @click="register" :loading="loading" round>注 册</el-button>
  12. <div class="login-card__bottom"><router-link to="/login" class="login-card__link">已有账号?前往登录</router-link></div>
  13. </el-card>
  14. <div class="clear"></div>
  15. </div>
  16. </template>
  17. <script>
  18. import { mapState } from 'vuex'
  19. export default {
  20. name: "register",
  21. layout: "blank",
  22. data(){
  23. return{
  24. name: '',
  25. phone: '',
  26. password: '',
  27. identifyCode: '',
  28. passwordConfirm: '',
  29. buttonText: '获取验证码',
  30. disabled: false,
  31. loading: false
  32. }
  33. },
  34. methods: {
  35. sendMessage(){
  36. if(this.phone.length < 11){
  37. if(this.phone.length === 0){
  38. this.$message({
  39. showClose: true,
  40. message: '请填写手机号码',
  41. type: 'error'
  42. });
  43. }else{
  44. this.$message({
  45. showClose: true,
  46. message: '手机号码格式有误',
  47. type: 'error'
  48. });
  49. }
  50. this.$refs.phone.focus();
  51. return;
  52. }
  53. this.disabled = true;
  54. let time=60;
  55. let myScroll = setInterval(() => {
  56. time--;
  57. if(time>=0) {
  58. this.buttonText = time + "s后可重发";
  59. }else{
  60. this.buttonText = '获取验证码';
  61. this.disabled = false; //倒计时结束能够重新点击发送的按钮
  62. clearTimeout(myScroll); //清除定时器
  63. time = 60; //设置循环重新开始条件
  64. }
  65. }, 1000);
  66. this.$axios.post("/user/sendMessage", {"phone":this.phone}).then(res => {
  67. if(res.code === 0){
  68. this.$message({
  69. showClose: true,
  70. message: '发送成功',
  71. type: 'success'
  72. });
  73. }else{
  74. this.$message({
  75. showClose: true,
  76. message: res.message,
  77. type: 'error'
  78. });
  79. }
  80. });
  81. },
  82. checkValid(){
  83. if(this.name.length === 0){
  84. this.$message({
  85. showClose: true,
  86. message: '昵称不得为空',
  87. type: 'error'
  88. });
  89. this.$refs.name.focus();
  90. return false;
  91. }
  92. if(this.password.length < 6){
  93. if(this.password.length === 0){
  94. this.$message({
  95. showClose: true,
  96. message: '密码不得为空',
  97. type: 'error'
  98. });
  99. }else{
  100. this.$message({
  101. showClose: true,
  102. message: '密码长度不足',
  103. type: 'error'
  104. });
  105. }
  106. this.$refs.password.focus();
  107. return false;
  108. }
  109. if(this.passwordConfirm !== this.password){
  110. this.$message({
  111. showClose: true,
  112. message: '密码前后不一致',
  113. type: 'error'
  114. });
  115. this.$refs.passwordConfirm.focus();
  116. return false;
  117. }
  118. if(this.phone.length < 11){
  119. if(this.phone.length === 0){
  120. this.$message({
  121. showClose: true,
  122. message: '手机号码不得为空',
  123. type: 'error'
  124. });
  125. }else{
  126. this.$message({
  127. showClose: true,
  128. message: '手机号码格式有误',
  129. type: 'error'
  130. });
  131. }
  132. this.$refs.phone.focus();
  133. return false;
  134. }
  135. if(this.identifyCode.length === 0){
  136. this.$message({
  137. showClose: true,
  138. message: '未填写验证码',
  139. type: 'error'
  140. });
  141. this.$refs.identifyCode.focus();
  142. return false;
  143. }
  144. return true;
  145. },
  146. register() {
  147. let isValid = this.checkValid();
  148. if(!isValid){
  149. return;
  150. }
  151. this.loading = true;
  152. this.$api.$post('/user/register', {
  153. phone: this.phone,
  154. password: this.password,
  155. name: this.name,
  156. identifyCode: this.identifyCode
  157. }).then((res) =>
  158. {
  159. if(res.code === 0){
  160. this.$message({
  161. showClose: true,
  162. message: "注册成功,前往登录",
  163. type: 'success'
  164. });
  165. this.$router.push('/login');
  166. }else{
  167. this.$message({
  168. showClose: true,
  169. message: res.message,
  170. type: 'error'
  171. });
  172. }
  173. }
  174. );
  175. this.loading = false;
  176. },
  177. }
  178. };
  179. </script>
  180. <style lang="scss" scoped>
  181. @import '~/assets/css/login.scss';
  182. </style>