Commit 754fee5b by Sweet Zhang

拦截器

parent 5e891439
import request from "../util/requset";
import request from "../util/request";
import {baseURL,apiURL,sfpURL} from "../environments/environment";
export default {
// 保单单利复利计算
irrAndSimpleCalcute(params) {
return request(`${baseURL}/api/sfp/calcuteUtil/irrAndSimpleCalcute`, "POST", params)
},
// 密码登录
passwordLogin(params) {
return request("/platform/metadata/logon", "GET", params)
},
export saveEducationEstimate(data) {
return request({
url: 'calcuteUtil/saveEducationEstimate',
method: 'post',
data: data
});
}
\ No newline at end of file
// 短信登录
noteLogin(params) {
return request("/platform/metadata/login", "GET", params)
},
// 获取验证码:用于短信登陆、密码找回、用户注销
getAuthCode(params) {
return request("/platform/metadata/sms", "GET", params)
},
// 获取注册账号验证码
getRegistAuthCode(params) {
return request("/platform/metadata/sms1", "GET", params)
},
// 登录者信息
getMyInfo(params) {
return request("/platform/metadata/bindinfo", "GET", params)
},
// 注册账号
registerAccount(params) {
return request("/platform/metadata/register", "GET", params)
},
// 找回密码
retrievePassword(params) {
return request("/platform/metadata/back", "GET", params)
},
// 注销账号
offAccount(params) {
return request("/platform/metadata/cancel", "GET", params)
},
}
export interface IResponse<T = any> {
success: boolean
message: string
data: T
}
\ No newline at end of file
class MinCache {
// 将数据存储在本地缓存中指定的 name 中
set (name, data) {
try {
uni.setStorageSync(name, data)
} catch (e) {
console.log(e)
}
}
// 从本地缓存中获取指定 name 对应的内容
get (name) {
let data
try {
data = uni.getStorageSync(name)
} catch (e) {
console.log(e)
}
return data
}
// 从本地缓存中移除指定 key
delete (name) {
try {
uni.removeStorageSync(name)
} catch (e) {
console.log(e)
}
}
// 返回一个布尔值,表示 name 是否在本地缓存之中
has (name) {
let value;
try {
const res = uni.getStorageInfoSync()
value = res.keys.includes(name)
} catch (e) {
console.log(e)
}
return value
}
// 清理本地数据缓存
clear () {
try {
uni.clearStorageSync()
} catch (e) {
console.log(e)
}
}
}
export default MinCache
\ No newline at end of file
export const environment = {
production: false,
BACKEND_URL_CONFIG_VALUE: 'https://mdev.zuihuibi.cn/api',
SFP: 'https://mdev.zuihuibi.cn/api/sfp/',
};
\ No newline at end of file
const env = 'dev';
const dev = {
base_url:'https://mdev.zuihuibi.cn',
api_url:'https://mdev.zuihuibi.cn/api',
sfp_url:'https://mdev.zuihuibi.cn/api/sfp'
}
const stage = {
base_url:'https://mstage.zuihuibi.cn',
api_url:'https://mstage.zuihuibi.cn/api',
sfp_url:'https://mstage.zuihuibi.cn/api/sfp'
}
const prod = {
base_url:'https://m.zuihuibi.cn',
api_url:'https://m.zuihuibi.cn/api',
sfp_url:'https://m.zuihuibi.cn/api/sfp'
}
const config = {
dev,
stage,
prod
}
let baseURL = config[env].base_url;
let apiURL = config[env].api_url;
let sfpURL = config[env].sfp_url;
export{
baseURL,
apiURL,
sfpURL
}
\ No newline at end of file
......@@ -11,7 +11,7 @@
<form @submit="formSubmit" @reset="formReset">
<!-- 保单信息 -->
<view class="policyInfoContent">
<view class="title">保单信息</view>
<view class="title" @click="irrAndSimpleCalte()">保单信息</view>
<view class="inputItem">
<text>交费年限:</text>
<view class="inputContent">
......@@ -155,6 +155,7 @@
</template>
<script>
import api from "../../api/api";
export default {
data() {
return {
......@@ -165,7 +166,6 @@
}
},
onLoad() {
},
methods: {
switchTab(e){
......@@ -181,7 +181,36 @@
url: '/pages/dataImport/data-import'
});
}
},
// 计算接口
irrAndSimpleCalte(){
console.log('...')
const params = {
"mobile": "17352689126",
"loginNo": "17352689126",
"wechat_unionid": "wx_17352689126",
"businessNo": "businessNo20220829000000001",
"calcuteMethod" : "2",
"businessType" : "1",
"calcuteType": "1",
"paymentPeriod": 5,
"annualPremium": 10000,
"decimal": 6,
"irrAndSimpleInfos": [
{
"nyear": 10,
"cashValue": 90000
},
{
"nyear": 15,
"cashValue": 99000
}
]
};
api.irrAndSimpleCalcute(params)
}
}
}
</script>
......
import api from "../api/api";
// 白名单,不需要携带token就允许被访问的接口
const whiteApiList = ['/api/authorize/obtainToken']
export const interceptor = () => {
uni.addInterceptor('request', {
// 请求拦截器
invoke(args) {
console.log('请求拦截器',args);
// 加载loading
uni.showLoading({
title: '加载中...'
})
// 当本地没有token,并且接口地址没在白名单内,需要重新获取token
if (!uni.getStorageSync('uni-token') && !whiteApiList.includes(args.url)) {
uni.request({
url:'/api/authorize/obtainToken',
method:'POST',
data:{ticket: 'uni-app'},
success: (res) => {
if(res.statusCode===200){
uni.setStorageSync('uni-token',res.data['data']['token']);
window.location.href = window.location.href + '?' + 't_reload=' + new Date().getTime();
}
console.log(res)
}
})
uni.hideLoading()
return false
}
// request 触发前拼接 url
//设置请求头及token
args.header = {
'content-type': args.method === 'POST' ? 'application/json' : 'application/x-www-form-urlencoded',
'X-Authorization': uni.getStorageSync('uni-token')
}
console.log(args.header)
},
// 响应拦截器,可以对数据进行预处理
success(args) {
uni.hideLoading()
console.log(args);
},
fail(err) {
console.log('interceptor-fail', err)
console.log('请求失败')
uni.hideLoading()
},
complete(res) {
uni.hideLoading()
}
})
}
import axios from 'axios'
import environment from '../environments/environment'
const API = environment.SFP;
// 创建axios实例
const service = axios.create({
// baseURL: 'http://192.168.1.69:5000',
baseURL: API,
timeout: 80000, // 请求超时时间
withCredentials: true,
// crossDomain: true
})
import {interceptor} from "./interceptor";
// 全局请求封装
export default (url, method, params) => {
interceptor();
return new Promise((resolve, reject) => {
uni.request({
url: url,
method: method,
data: {
...params
},
success(res) {
resolve(res.data);
},
fail(err) {
reject(err);
},
complete() {
uni.hideLoading();
}
});
});
};
// request拦截器
service.interceptors.request.use(
config => {
if (getToken()) {
config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
var lang = localStorage.getItem('lang')//因为项目中使用到了i18n国际化语言配置,请根据实际情况自行修改
if (!lang) {
lang = 'zh_CN'
}
config.headers['Accept-Language'] = lang.replace(/_/g, '-')
config.headers['Content-Type'] = 'application/json'
return config
},
error => {
Promise.reject(error)
}
)
// response 拦截器
service.interceptors.response.use(
response => {
return response.data
},
error => {
// 兼容blob下载出错json提示
if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
const reader = new FileReader()
reader.readAsText(error.response.data, 'utf-8')
reader.onload = function (e) {
const errorMsg = JSON.parse(reader.result).message
Notification.error({
title: errorMsg,
duration: 5000
})
}
} else {
let code = 0
try {
code = error.response.data.status
} catch (e) {
if (error.toString().indexOf('Error: timeout') !== -1) {
Notification.error({
title: '网络请求超时',
duration: 5000
})
return Promise.reject(error)
}
}
if (code) {
if (code === 401) {
store.dispatch('LogOut').then(() => {
// 用户登录界面提示
Cookies.set('point', 401)
location.reload()
})
} else if (code === 403) {
router.push({ path: '/401' })
} else {
const errorMsg = error.response.data.message
if (errorMsg !== undefined) {
Notification.error({
title: errorMsg,
duration: 0
})
}
}
} else {
Notification.error({
title: '接口请求失败',
duration: 5000
})
}
}
return Promise.reject(error)
}
)
export default service
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