Commit 1644de7d by liang ce

token更新

parent e7ef8652
import axios from 'axios'
import qs from 'qs'
var instance = axios.create()
// axios.defaults.baseURL = 'http://192.168.1.106:8300/v1/isv' // 本地荣
// axios.defaults.baseURL = 'http://192.168.1.102:8300/v1/isv' // 本地钱
// axios.defaults.baseURL = 'http://139.196.213.18:8300/v1/isv' // beta环境
import { getToken, refreshToken } from './generateOrRefreshToken'
const BASE_URL = process.env.VUE_APP_API_URL
let loadingInstance // 创建Loading 的实例
// 配置发送请求前的拦截器可以设置token信息
// 配置发送请求前的拦截器设置token信息
axios.interceptors.request.use((config) => {
// loading开始loadingInstance.start()
let isRefreshToken = new Date().getTime() - sessionStorage.getItem('start_time') < sessionStorage.getItem('expires_in') - 5 * 60 * 1000
let isRefreshToken = new Date().getTime() - localStorage.getItem('start_time') < localStorage.getItem('expires_in') - 5 * 60 * 1000
if (isRefreshToken) {
config.headers['Authorization'] = `Bearer ${sessionStorage.getItem('access_token')}`
config.headers['Authorization'] = `Bearer ${localStorage.getItem('access_token')}`
return config
} else {
return new Promise((resolve, reject) => {
getToken().then((response) => {
sessionStorage.setItem('access_token', response.data.access_token)
sessionStorage.setItem('refresh_token', response.data.refresh_token)
sessionStorage.setItem('expires_in', response.data.expires_in * 1000)
sessionStorage.setItem('start_time', new Date().getTime())
resolve(config)
return config
refreshToken().then((res) => {
console.log('res', res)
}).catch((err) => {
console.log(err)
})
......@@ -32,24 +23,27 @@ axios.interceptors.request.use((config) => {
loadingInstance.close()
return Promise.reject(error)
})
function getToken () {
let loginData = qs.stringify({
refresh_token: sessionStorage.getItem('refresh_token'),
grant_type: 'refresh_token'
})
// 配置请求返回的拦截器,
axios.interceptors.response.use(res => {
return Promise.resolve(res)
}, err => {
debugger
return new Promise((resolve, reject) => {
instance.post(`${BASE_URL}/auth/oauth/token`, loginData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Authorization': 'Basic bWluZ3BheS13ZWI6bWluZ3BheS13ZWI='
}
}).then((res) => {
resolve(res)
}).catch((err) => {
console.log(err)
})
if (err && err.response.data.code === 401) {
refreshToken().then(res => {
console.log('refreshToken1', res)
resolve(err)
})
} else if (err && err.response.data.code === 426) {
getToken().then((res) => {
console.log('getToken1', res)
resolve(err)
})
} else {
reject(new Error(err))
}
})
}
})
const $http = {
post: (url, data) => {
return new Promise((resolve, reject) => {
......@@ -59,8 +53,6 @@ const $http = {
}
}).then((res) => {
return resolve(res)
}).catch((error) => {
return reject(error)
})
})
},
......@@ -68,8 +60,6 @@ const $http = {
return new Promise((resolve, reject) => {
axios.get(`${BASE_URL}${url}`).then((res) => {
return resolve(res)
}).catch((error) => {
return reject(error)
})
})
}
......
import * as dd from 'dingtalk-jsapi'
import axios from 'axios'
import qs from 'qs'
import { MINGPAY_CONSTANT } from './constant'
import { getToken, refreshToken } from './generateOrRefreshToken'
let basurl = process.env.VUE_APP_API_URL
// axios.defaults.headers['Authorization'] = `Bearer ${localStorage.getItem('token')}`
const config = {
......@@ -45,48 +45,18 @@ const config = {
ddpermission: () => {
// dd.ready参数为回调函数,在环境准备就绪时触发,jsapi的调用需要保证在该回调函数触发后调用,否则无效。
return new Promise((resolve, reject) => {
dd.runtime.permission.requestAuthCode({
corpId: localStorage.getItem('orgId'), // 企业id
onSuccess: function (info) {
let senddate = qs.stringify({
// version: 'v1',
code: info.code,
orgId: localStorage.getItem('orgId')
})
return axios.post(`${basurl}/mingpay/v1/skipAuth/getUserInfoByCode?${senddate}`).then((res) => {
console.log(res.data.resultCode)
if (res.data.resultCode === 0) {
return res.data.data.mingUserId
}
}).then((mingUserId) => {
let loginData = qs.stringify({
username: mingUserId,
password: mingUserId,
scope: 'server',
grant_type: 'password'
})
return axios.post(`${basurl}/auth/oauth/token`, loginData,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Authorization': 'Basic bWluZ3BheS13ZWI6bWluZ3BheS13ZWI='
}
}).then((response) => {
console.log(response)
sessionStorage.setItem('access_token', response.data.access_token)
sessionStorage.setItem('refresh_token', response.data.refresh_token)
sessionStorage.setItem('expires_in', response.data.expires_in * 1000)
sessionStorage.setItem('start_time', new Date().getTime())
resolve('ok')
}).catch(() => {
resolve('noPermission')
})
})
},
onFail: function (err) {
console.log(err)
}
})
let isRefreshToken = new Date().getTime() - localStorage.getItem('start_time') < localStorage.getItem('expires_in') - 5 * 60 * 1000
if (localStorage.getItem('access_token') && isRefreshToken) {
resolve('ok')
} else if (localStorage.getItem('refresh_token')) {
refreshToken().then((res) => {
resolve('ok')
})
} else {
getToken().then(() => {
resolve('ok')
})
}
})
},
ddchooseOne: () => {
......
const MINGPAY_CONSTANT = {
MINGPAY_CONSTANT_URL: 'http://mingpay-web-beta.mingwork.com:8888/',
// MINGPAY_CONSTANT_URL: 'http://ming.vaiwan.com/',
MINGPAY_CONSTANT_AGENTID: 270198704,
MINGPAY_CONSTANT_VERSION: 'v1'
}
const MINGPAY_ACCOUNT_STATUS = {
ACTIVE: 'ACTIVE', // 初始激活状态(冻结操作)
UNACTIVE: 'UNACTIVE', // 冻结状态(解冻操作 AND 注销操作)
......@@ -31,4 +25,4 @@ const MINGPAY_SUBSIDY_STATUS = {
REFUSE_PERMIT: 'REFUSE_PERMIT'
}
export { MINGPAY_CONSTANT, MINGPAY_ACCOUNT_STATUS, MINGPAY_CHARGE_ORDERSTATUS, MINGPAY_RECORDS_STATUS, MINGPAY_SUBSIDY_STATUS }
export { MINGPAY_ACCOUNT_STATUS, MINGPAY_CHARGE_ORDERSTATUS, MINGPAY_RECORDS_STATUS, MINGPAY_SUBSIDY_STATUS }
import * as dd from 'dingtalk-jsapi'
import axios from 'axios'
import qs from 'qs'
var instance = axios.create()
const BASE_URL = process.env.VUE_APP_API_URL
// function generateOrRefreshToken () {
// }
instance.interceptors.response.use(res => {
return Promise.resolve(res)
}, err => {
return new Promise((resolve, reject) => {
if (err && err.response.status === 401 && err.response.data.code === 401) {
refreshToken().then(res => {
console.log('refreshToken1', res)
resolve(err)
})
} else if (err && err.response.status === 426 && err.response.data.code === 1) {
getToken().then((res) => {
console.log('getToken1', res)
resolve(err)
})
}
})
})
function getToken () {
return new Promise((resolve, reject) => {
dd.runtime.permission.requestAuthCode({
corpId: localStorage.getItem('orgId'), // 企业id
onSuccess: function (info) {
let senddate = qs.stringify({
code: info.code,
orgId: localStorage.getItem('orgId')
})
return instance.post(`${BASE_URL}/mingpay/v1/skipAuth/getUserInfoByCode?${senddate}`).then((res) => {
console.log(res.data.resultCode)
if (res.data.resultCode === 0) {
return res.data.data.mingUserId
}
}).then((mingUserId) => {
let loginData = qs.stringify({
username: mingUserId,
password: mingUserId,
scope: 'server',
grant_type: 'password'
})
return instance.post(`${BASE_URL}/auth/oauth/token`, loginData,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Authorization': 'Basic bWluZ3BheS13ZWI6bWluZ3BheS13ZWI='
}
}).then((response) => {
console.log(response)
localStorage.setItem('access_token', response.data.access_token)
localStorage.setItem('refresh_token', response.data.refresh_token)
localStorage.setItem('expires_in', response.data.expires_in * 1000)
localStorage.setItem('start_time', new Date().getTime())
resolve('ok')
}).catch(() => {
resolve('noPermission')
})
})
},
onFail: function (err) {
console.log(err)
}
})
})
}
function refreshToken () {
let loginData = qs.stringify({
refresh_token: localStorage.getItem('refresh_token'),
grant_type: 'refresh_token'
})
return new Promise((resolve, reject) => {
instance.post(`${BASE_URL}/auth/oauth/token`, loginData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Authorization': 'Basic bWluZ3BheS13ZWI6bWluZ3BheS13ZWI='
}
}).then((res) => {
localStorage.setItem('access_token', res.data.access_token)
localStorage.setItem('refresh_token', res.data.refresh_token)
localStorage.setItem('expires_in', res.data.expires_in * 1000)
localStorage.setItem('start_time', new Date().getTime())
resolve('ok')
}).catch((err) => {
resolve(err)
})
})
}
export { getToken, refreshToken }
......@@ -221,6 +221,7 @@ export default {
_that.accountList = res.data.data.recordList
_that.pagination.total = parseInt(res.data.data.totalCount)
}).catch((err) => {
this.$message.error('网络异常请重试')
console.log(err)
})
},
......@@ -480,6 +481,7 @@ export default {
this.$message.error(res.data.message)
}
}).catch((err) => {
this.$message.error('网络异常请重试')
console.log(err)
})
},
......
......@@ -133,6 +133,7 @@ export default {
this.$message.error(res.data.message)
}
}).catch((err) => {
this.$message.error('网络异常请重试')
console.log(err)
})
})
......
......@@ -73,6 +73,7 @@ export default {
console.log(res)
this.OrgSettingData = res.data.data
}).catch((err) => {
this.$message.error('网络异常请重试')
console.log(err)
})
},
......
......@@ -133,6 +133,7 @@ export default {
this.$message.error(res.data.message)
}
}).catch((err) => {
this.$message.error('网络异常请重试')
console.log(err)
})
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment