国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

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

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:小星博博 欄目: JavaScript 歡迎投稿:712375056

1、先看一下請求配置文件,看axios.create這個(gè)方法,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.設置自己的狀態(tài)碼,看service.interceptors.response.use,如圖,設置為自己的狀態(tài)碼

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

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

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

5、改基礎路由配置,在devServer后面添加如下代碼(before這行注釋掉,這個(gè)用來(lái)模擬數據的,用不到),如圖

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

在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ù)器需要的登錄參數
      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方法,修改代碼如下,因為獲取用戶(hù)信息接口沒(méi)寫(xiě),所以數據直接寫(xiě)死,根據自己的做調整

getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      /**
       * 這里請求用戶(hù)信息和權限,目前接口沒(méi)做,只注釋了,data寫(xiě)死
       * */
      // 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,可以登錄到首頁(yè)了

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

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

V
偷柏自拍亚洲综合在线| 日本特黄A级高清免费大片| 国产精品麻豆成人AV网| 中文午夜人妻无码看片| 69堂人成无码免费视频果冻传媒| 亚洲欧美日韩中文字幕在线一区|