图形验证码里获取不到sid,因为sid是在登录(Login.vue)页面里加入的,所以加一个路由守卫
如果当页面不是在 login 进来,那就强制跳转到 login 页面
使用validation-observer来套取整个form
<validation-observer ref="observer" v-slot="{validate}"><form>...<button class="layui-btn" type="button" @click="validate().then(submit)" >立即注册</button>...</form><validation-observer>
添加submit方法
async submit() {const isValid = await this.$refs.observer.validate()if (!isValid) {// ABORT!!return}reg({username: this.username,password: this.password,name: this.email,code: this.code,sid: this.$store.state.sid}).then(res => {if (res.code === 200) {this.username = ''this.password = ''this.repass = ''this.email = ''this.code = ''requestAnimationFrame(() => {this.$refs.observer.reset()})// 跳转到登录页面, 让用户登录this.$router.push('/login')console.log(res)} else {this.$refs.observer.setErrors(res.msg)}}).catch(err => {const data = err.response.dataif (data.code === 500) {this.$alert('用户名密码校验失败,请检查!')} else {this.$alert('服务器错误!')}console.log(err.response.data)})}
创建api接口
/*** 注册接口* @param {*} regInfo 用户注册信息*/const reg = (regInfo) => {return axios.post('/login/login', {...regInfo})}
