register.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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" oninput="this.value=this.value.replace(/[^\w_]/g,'');" 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="email" v-model="email" placeholder="请输入邮箱" maxlength="320" class="login-card__item" autofocus></el-input>
  9. <el-input ref="phone" v-model="phone" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入手机号" minlength='11' maxlength='11' class="login-card__item"></el-input>
  10. <el-input ref="identifyCode" v-model="identifyCode" placeholder="请输入验证码" maxlength="6" class="login-card__short-item"></el-input>
  11. <el-button :disabled="disabled" @click="sendMessage" class="login-card__right-button">{{buttonText}}</el-button>
  12. <el-button type="primary" class="login-card__item" @click="register" :loading="loading" round>注 册</el-button>
  13. <div class="login-card__bottom"><router-link to="/login" class="login-card__link">已有账号?前往登录</router-link></div>
  14. </el-card>
  15. <div class="clear"></div>
  16. <router-link to="/"><img src="/image/seec-logo--horizontal.png" class="login-card__logo"/></router-link>
  17. </div>
  18. </template>
  19. <script>
  20. import { mapState } from 'vuex'
  21. export default {
  22. name: "register",
  23. layout: "blank",
  24. data(){
  25. return{
  26. name: '',
  27. phone: '',
  28. password: '',
  29. identifyCode: '',
  30. passwordConfirm: '',
  31. buttonText: '获取验证码',
  32. disabled: false,
  33. loading: false,
  34. email: ''
  35. }
  36. },
  37. methods: {
  38. sendMessage(){
  39. if(this.phone.length < 11){
  40. if(this.phone.length === 0){
  41. this.$message({
  42. showClose: true,
  43. message: '请填写手机号码',
  44. type: 'error'
  45. });
  46. }else{
  47. this.$message({
  48. showClose: true,
  49. message: '手机号码格式有误',
  50. type: 'error'
  51. });
  52. }
  53. this.$refs.phone.focus();
  54. return;
  55. }
  56. this.disabled = true;
  57. let time=60;
  58. let myScroll = setInterval(() => {
  59. time--;
  60. if(time>=0) {
  61. this.buttonText = time + "s后可重发";
  62. }else{
  63. this.buttonText = '获取验证码';
  64. this.disabled = false; //倒计时结束能够重新点击发送的按钮
  65. clearTimeout(myScroll); //清除定时器
  66. time = 60; //设置循环重新开始条件
  67. }
  68. }, 1000);
  69. this.$api.post("/user/sendMessage", {"phone":this.phone}).then(res => {
  70. if(res.data.code === 0){
  71. this.$message({
  72. showClose: true,
  73. message: '发送成功',
  74. type: 'success'
  75. });
  76. }else{
  77. this.$message({
  78. showClose: true,
  79. message: res.message,
  80. type: 'error'
  81. });
  82. }
  83. });
  84. },
  85. checkValid(){
  86. if(this.name.length === 0){
  87. this.$message({
  88. showClose: true,
  89. message: '昵称不得为空',
  90. type: 'error'
  91. });
  92. this.$refs.name.focus();
  93. return false;
  94. }
  95. if(this.name.length > 10){
  96. this.$message({
  97. showClose: true,
  98. message: '昵称过长',
  99. type: 'error'
  100. });
  101. this.$refs.name.focus();
  102. return false;
  103. }
  104. if(this.password.length < 6){
  105. if(this.password.length === 0){
  106. this.$message({
  107. showClose: true,
  108. message: '密码不得为空',
  109. type: 'error'
  110. });
  111. }else{
  112. this.$message({
  113. showClose: true,
  114. message: '密码长度不足',
  115. type: 'error'
  116. });
  117. }
  118. this.$refs.password.focus();
  119. return false;
  120. }
  121. if(this.password.length > 20){
  122. this.$message({
  123. showClose: true,
  124. message: '密码过长',
  125. type: 'error'
  126. });
  127. this.$refs.password.focus();
  128. return false;
  129. }
  130. if(this.passwordConfirm !== this.password){
  131. this.$message({
  132. showClose: true,
  133. message: '密码前后不一致',
  134. type: 'error'
  135. });
  136. this.$refs.passwordConfirm.focus();
  137. return false;
  138. }
  139. if(this.email.length === 0){
  140. this.$message({
  141. showClose: true,
  142. message: '邮箱不得为空',
  143. type: 'error'
  144. });
  145. this.$refs.email.focus();
  146. return false;
  147. }
  148. if(this.email.length > 255){
  149. this.$message({
  150. showClose: true,
  151. message: '邮箱过长',
  152. type: 'error'
  153. });
  154. this.$refs.email.focus();
  155. return false;
  156. }
  157. const myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
  158. if(!myReg.test(this.email)){
  159. this.$message({
  160. showClose: true,
  161. message: '邮箱格式有误',
  162. type: 'error'
  163. });
  164. this.$refs.email.focus();
  165. return false;
  166. }
  167. if(this.phone.length !== 11){
  168. if(this.phone.length === 0){
  169. this.$message({
  170. showClose: true,
  171. message: '手机号码不得为空',
  172. type: 'error'
  173. });
  174. }else{
  175. this.$message({
  176. showClose: true,
  177. message: '手机号码格式有误',
  178. type: 'error'
  179. });
  180. }
  181. this.$refs.phone.focus();
  182. return false;
  183. }
  184. if(this.identifyCode.length === 0){
  185. this.$message({
  186. showClose: true,
  187. message: '未填写验证码',
  188. type: 'error'
  189. });
  190. this.$refs.identifyCode.focus();
  191. return false;
  192. }
  193. if(this.identifyCode.length !== 6){
  194. this.$message({
  195. showClose: true,
  196. message: '验证码长度错误',
  197. type: 'error'
  198. });
  199. this.$refs.identifyCode.focus();
  200. return false;
  201. }
  202. return true;
  203. },
  204. register() {
  205. this.loading = true;
  206. let isValid = this.checkValid();
  207. if(!isValid){
  208. return;
  209. }
  210. this.$api.$post('/user/register', {
  211. phone: this.phone,
  212. password: this.password,
  213. name: this.name,
  214. email: this.email,
  215. identifyCode: this.identifyCode
  216. }).then((res) =>
  217. {
  218. if(res.code === 0){
  219. this.$message({
  220. showClose: true,
  221. message: "注册成功,前往登录",
  222. type: 'success'
  223. });
  224. this.$router.push('/login');
  225. }else{
  226. this.$message({
  227. showClose: true,
  228. message: res.message,
  229. type: 'error'
  230. });
  231. }
  232. }
  233. );
  234. this.loading = false;
  235. },
  236. }
  237. };
  238. </script>
  239. <style lang="scss" scoped>
  240. @import '~/assets/css/login.scss';
  241. </style>