|
|
|
|
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: 90000
|
|
|
|
|
})
|
|
|
|
|
const loginFunc = (res, userInfo) => {
|
|
|
|
|
const params = {
|
|
|
|
|
code: res.code,
|
|
|
|
|
encryptedData: userInfo.encryptedData,
|
|
|
|
|
iv: userInfo.iv
|
|
|
|
|
}
|
|
|
|
|
// 用户授权登录
|
|
|
|
|
loginTiktok(params).then(res => {
|
|
|
|
|
if (res && res.data.code === 200) {
|
|
|
|
|
uni.setStorageSync({
|
|
|
|
|
key: 'token',
|
|
|
|
|
data: res.data.data.access_token,
|
|
|
|
|
});
|
|
|
|
|
uni.setStorageSync({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
data: res.data.data.userInfo,
|
|
|
|
|
})
|
|
|
|
|
this.userInfo = res.data.data.userInfo;
|
|
|
|
|
uni.setStorageSync({
|
|
|
|
|
key: 'platform',
|
|
|
|
|
data: res.data.data.platform,
|
|
|
|
|
})
|
|
|
|
|
} 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');
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers["TiktokAuthorization"] = token;
|
|
|
|
|
}
|
|
|
|
|
const platform = uni.getStorageSync('platform');
|
|
|
|
|
if (platform) {
|
|
|
|
|
config.headers["platform"] = platform;
|
|
|
|
|
}
|
|
|
|
|
config.headers['Content-Type'] = 'application/json'
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//配置成功后的拦截器
|
|
|
|
|
service.interceptors.response.use(res => {
|
|
|
|
|
//未授权
|
|
|
|
|
if(res.data.code === 401){
|
|
|
|
|
//清理缓存,解决一进入页面未登录但有头像的情况
|
|
|
|
|
uni.clearStorage();
|
|
|
|
|
uni.clearStorageSync();
|
|
|
|
|
uni.showModal({
|
|
|
|
|
title:'提示',
|
|
|
|
|
content:'您还没有登录!点击确定跳转登录界面以体验服务',
|
|
|
|
|
success(res) {
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
//跳转登录界面
|
|
|
|
|
uni.switchTab({
|
|
|
|
|
url: '/pages/userInfo/userInfo'
|
|
|
|
|
});
|
|
|
|
|
}else if(res.cancel){
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title:'登录获取更好的服务体验哟!',
|
|
|
|
|
duration: 2000
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}else if(res.data.code != 200){
|
|
|
|
|
//拦截响应,做统一处理
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: '网络错误,请稍后再试~',
|
|
|
|
|
icon:"none",
|
|
|
|
|
duration: 2000,
|
|
|
|
|
success() {
|
|
|
|
|
console.log('接口请求错误',res.data.msg)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}else{
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}, error => {
|
|
|
|
|
console.log('触发了401error')
|
|
|
|
|
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
|