register.vue 8.4 KB

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