欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)

 更新時(shí)間:2021年06月23日 11:26:51   作者:小星博博  
關(guān)于這個Vue-element-admin中的流程可能對于新的同學(xué)不是很友好,所以本文將結(jié)合實(shí)例代碼,介紹Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn),感興趣的小伙伴們可以參考一下

1、先看一下請求配置文件,看axios.create這個方法,baseURL是基礎(chǔ)路由

baseURL:process.env.VUE_APP_BASE_API,

路徑:src-utils-request.js

2、然后再看service.interceptors.request.use,設(shè)置token請求頭,我后端集成的是jwt,所以請求頭是Authentication,如圖

config.headers['Authentication'] = getToken()

3.設(shè)置自己的狀態(tài)碼,看service.interceptors.response.use,如圖,設(shè)置為自己的狀態(tài)碼

這是我服務(wù)器響應(yīng)的數(shù)據(jù),如下,1是正常響應(yīng)數(shù)據(jù)

{
    "code": 1,
    "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SWQiOiJhZG1pbiIsIm5iZiI6MTYyNDE3NTM4MiwiZXhwIjoxNjI0MTc1NDQyLCJpYXQiOjE2MjQxNzUzODJ9.7p8EHMx1b4-yIMRN7Qxden3nZsDmBvevHEf-3oVhFMg",
    "message": "登錄成功",
    "state": true
    }
}

4、改.env.production和.env.development里面的api都為空,圖只展示.env.production

5、改基礎(chǔ)路由配置,在devServer后面添加如下代碼(before這行注釋掉,這個用來模擬數(shù)據(jù)的,用不到),如圖

// 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]: ''
        }
      }
    }

到這里基礎(chǔ)路由基本配置好了

6、在src-view-login-index.html文件中,找到Vue-Element-Admin的登錄接口,添加如下代碼,如圖,把官方的請求方式注釋掉,this.loginForm是請求參數(shù)

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、設(shè)置用戶登錄成功后的跳轉(zhuǎn),登錄后必須把token做緩存,不然登錄頁跳轉(zhuǎn)不了

在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)
      //服務(wù)器需要的登錄參數(shù)
      const payload = {
        accountId: accountId,
        password: password
      }
      //請求服務(wù)器
      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方法,修改代碼如下,因?yàn)楂@取用戶信息接口沒寫,所以數(shù)據(jù)直接寫死,根據(jù)自己的做調(diào)整

getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      /**
       * 這里請求用戶信息和權(quán)限,目前接口沒做,只注釋了,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,可以登錄到首頁了

到此這篇關(guān)于Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Vue-Element-Admin登錄跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論