vue?element?admin集成自己的接口实现登录跳转-mile米乐体育
vue?element?admin集成自己的接口实现登录跳转
1、先看一下请求配置文件,看axios.create这个方法,baseurl是基础路由
baseurl:process.env.vue_app_base_api,
路径:src-utils-request.js
2、然后再看service.interceptors.request.use,设置token请求头,我后端集成的是jwt,所以请求头是authentication,如图
config.headers['authentication'] = gettoken()
3.设置自己的状态码,看service.interceptors.response.use,如图,设置为自己的状态码
这是我服务器响应的数据,如下,1是正常响应数据
{ "code": 1, "data": { "token": "eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9.eyjby2nvdw50swqioijhzg1pbiisim5izii6mtyynde3ntm4miwizxhwijoxnji0mtc1ndqylcjpyxqioje2mjqxnzuzodj9.7p8ehmx1b4-yimrn7qxden3nzsdmbvevhef-3ovhfmg", "message": "登录成功", "state": true } }
4、改.env.production和.env.development里面的api都为空,图只展示.env.production
5、改基础路由配置,在devserver后面添加如下代码(before这行注释掉,这个用来模拟数据的,用不到),如图
// before: require('./mock/mock-server.js') proxy: { [process.env.vue_app_base_api]: { target: 'https://xiaoxingbobo.top', // target: 'http://192.168.1.119:8081', // target: 'http://192.168.1.253:8081', changeorigin: true, pathrewrite: { ['^' process.env.vue_app_base_api]: '' } } }
到这里基础路由基本配置好了
6、在src-view-login-index.html文件中,找到vue-element-admin的登录接口,添加如下代码,如图,把官方的请求方式注释掉,this.loginform是请求参数
this.loading = true this.$store.dispatch('user/login', this.loginform) .then(() => { this.$router.push({ path: this.redirect || '/', query: this.otherquery }) this.loading = false }) .catch((e) => { this.tool.log(e) this.loading = false })
7、设置用户登录成功后的跳转,登录后必须把token做缓存,不然登录页跳转不了
在src-store-moduls-use.js,如图
找到action下的login方法,修改代码如下
const actions = { // user login login({ commit }, userinfo) { const { accountid, password } = userinfo return new promise((resolve, reject) => { console.log('userinfo', userinfo) //服务器需要的登录参数 const payload = { accountid: accountid, password: password } //请求服务器 user.login(payload).then(response => { const { data } = response console.log('response', response) commit('set_token', data.token) settoken(data.token) resolve() }).catch(error => { reject(error) }) }) },
找到getinfo方法,修改代码如下,因为获取用户信息接口没写,所以数据直接写死,根据自己的做调整
getinfo({ commit, state }) { return new promise((resolve, reject) => { /** * 这里请求用户信息和权限,目前接口没做,只注释了,data写死 * */ // user.getinfo(state.token).then(response => { // const { // data // } = response const { data } = { data: { roles: ['admin'], introduction: 'administrator', avatar: 'https://cloud.xiaoxingbobo.top/nongzhibang/20210429/1107491622257669573', name: 'administrator' } } if (!data) { reject('verification failed, please login again.') } const { roles, name, avatar, introduction, token } = data // roles must be a non-empty array if (!roles || roles.length <= 0) { reject('getinfo: roles must be a non-null array!') } commit('set_roles', roles) commit('set_name', name) commit('set_avatar', avatar) commit('set_introduction', introduction) commit('set_token', token) resolve(data) // }).catch(error => { // reject(error) // }) }) },
这样就搞定了vue-element-admin,可以登录到mile米乐体育首页了
到此这篇关于vue-element-admin集成自己的接口实现登录跳转的文章就介绍到这了,更多相关vue-element-admin登录跳转内容请搜索趣讯吧以前的文章或继续浏览下面的相关文章希望大家以后多多支持趣讯吧!