图形验证码里获取不到sid,因为sid是在登录(Login.vue)页面里加入的,所以加一个路由守卫
    如果当页面不是在 login 进来,那就强制跳转到 login 页面
    image.png
    使用validation-observer来套取整个form

    1. <validation-observer ref="observer" v-slot="{validate}">
    2. <form>
    3. ...
    4. <button class="layui-btn" type="button" @click="validate().then(submit)" >立即注册</button>
    5. ...
    6. </form>
    7. <validation-observer>

    添加submit方法

    1. async submit() {
    2. const isValid = await this.$refs.observer.validate()
    3. if (!isValid) {
    4. // ABORT!!
    5. return
    6. }
    7. reg({
    8. username: this.username,
    9. password: this.password,
    10. name: this.email,
    11. code: this.code,
    12. sid: this.$store.state.sid
    13. })
    14. .then(res => {
    15. if (res.code === 200) {
    16. this.username = ''
    17. this.password = ''
    18. this.repass = ''
    19. this.email = ''
    20. this.code = ''
    21. requestAnimationFrame(() => {
    22. this.$refs.observer.reset()
    23. })
    24. // 跳转到登录页面, 让用户登录
    25. this.$router.push('/login')
    26. console.log(res)
    27. } else {
    28. this.$refs.observer.setErrors(res.msg)
    29. }
    30. })
    31. .catch(err => {
    32. const data = err.response.data
    33. if (data.code === 500) {
    34. this.$alert('用户名密码校验失败,请检查!')
    35. } else {
    36. this.$alert('服务器错误!')
    37. }
    38. console.log(err.response.data)
    39. })
    40. }

    创建api接口

    1. /**
    2. * 注册接口
    3. * @param {*} regInfo 用户注册信息
    4. */
    5. const reg = (regInfo) => {
    6. return axios.post('/login/login', {
    7. ...regInfo
    8. })
    9. }