You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.5 KiB
191 lines
4.5 KiB
import axios from "axios"
|
|
import qs from "qs"
|
|
import Vue from "vue"
|
|
import {loginTiktok} from "../api/auth";
|
|
|
|
const service = axios.create({
|
|
withCredentials: true,
|
|
crossDomain: true,
|
|
baseURL: Vue.prototype.baseURL, //这个baseURL是我在main.js下配置的请求url
|
|
timeout: 10000
|
|
})
|
|
const loginFunc = (res, userInfo) => {
|
|
const params = {
|
|
code: res.code,
|
|
encryptedData: userInfo.encryptedData,
|
|
iv: userInfo.iv
|
|
}
|
|
// 用户授权登录
|
|
loginTiktok(params).then(res => {
|
|
if (res.data.code === 200) {
|
|
uni.setStorage({
|
|
key: 'userInfo',
|
|
data: res.data.data.userInfo,
|
|
})
|
|
this.userInfo = res.data.data.userInfo;
|
|
uni.setStorage({
|
|
key: 'token',
|
|
data: res.data.data.access_token
|
|
})
|
|
} else {
|
|
uni.showToast({
|
|
title: res.data.msg,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}).catch(res => {})
|
|
}
|
|
|
|
const refreshToken = () => {
|
|
uni.showModal({
|
|
title: '登录信息已过期',
|
|
content: '重新授权登录?',
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
uni.getSetting({
|
|
success: (settingObj) => {
|
|
if (settingObj.authSetting['scope.userInfo'] === undefined || settingObj.authSetting['scope.userInfo']) {
|
|
uni.login({
|
|
force: true,
|
|
success: (res) => {
|
|
uni.getUserInfo({
|
|
withCredentials: true,
|
|
success: (userInfo) => {
|
|
loginFunc(res, userInfo)
|
|
},
|
|
complete: () => {
|
|
|
|
},
|
|
fail: (errMsg) => {
|
|
|
|
}
|
|
})
|
|
},
|
|
complete: () => {
|
|
|
|
},
|
|
fail: (errMsg) => {
|
|
|
|
}
|
|
})
|
|
} else {
|
|
uni.showModal({
|
|
title: '您已拒绝授权用户信息',
|
|
content: '是否进入权限管理,调整授权?',
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
uni.openSetting({
|
|
success: (settingRes) => {
|
|
if (settingRes.authSetting['scope.userInfo']) {
|
|
uni.login({
|
|
force: true,
|
|
success: (res) => {
|
|
uni.getUserInfo({
|
|
withCredentials: true,
|
|
success: (userInfo) => {
|
|
loginFunc(res, userInfo)
|
|
},
|
|
complete: () => {
|
|
|
|
},
|
|
fail: (errMsg) => {
|
|
|
|
}
|
|
})
|
|
},
|
|
complete: () => {
|
|
|
|
},
|
|
fail: (errMsg) => {
|
|
|
|
}
|
|
})
|
|
}
|
|
},
|
|
fail: () => {
|
|
|
|
}
|
|
})
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
})
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消');
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// request拦截器,在请求之前做一些处理
|
|
service.interceptors.request.use(
|
|
config => {
|
|
const token = uni.getStorageSync('token');
|
|
// console.log('获取到的token',token)
|
|
if (token) {
|
|
// 给请求头添加user-token
|
|
// console.log('进入携带token方法')
|
|
config.headers["TiktokAuthorization"] = token;
|
|
// console.log('token放入headers',config.headers)
|
|
}
|
|
config.headers['Content-Type'] = 'application/json'
|
|
// config.method === 'post' ?
|
|
// config.data = qs.stringify({ ...config.data
|
|
// }) :
|
|
// config.params = { ...config.params
|
|
// };
|
|
return config;
|
|
},
|
|
error => {
|
|
// console.log(error); // for debug
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
//配置成功后的拦截器
|
|
service.interceptors.response.use(res => {
|
|
return res
|
|
// if (res.data.status == 200) {
|
|
// return res
|
|
// } else {
|
|
// return Promise.reject(res.data.msg);
|
|
// }
|
|
}, error => {
|
|
if(error.response.status === 401){
|
|
refreshToken() //刷新token并重置token
|
|
} else {
|
|
return Promise.reject(error)
|
|
}
|
|
})
|
|
|
|
axios.defaults.adapter = function(config) { //自己定义个适配器,用来适配uniapp的语法
|
|
return new Promise((resolve, reject) => {
|
|
// console.log(config)
|
|
var settle = require('axios/lib/core/settle');
|
|
var buildURL = require('axios/lib/helpers/buildURL');
|
|
uni.request({
|
|
method: config.method.toUpperCase(),
|
|
url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
|
|
header: config.headers,
|
|
data: config.data,
|
|
dataType: config.dataType,
|
|
responseType: config.responseType,
|
|
sslVerify: config.sslVerify,
|
|
complete: function complete(response) {
|
|
response = {
|
|
data: response.data,
|
|
status: response.statusCode,
|
|
errMsg: response.errMsg,
|
|
header: response.header,
|
|
config: config
|
|
};
|
|
settle(resolve, reject, response);
|
|
}
|
|
})
|
|
})
|
|
}
|
|
export default service
|