| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- import {
- set
- } from 'lodash'
- import WxRequest from 'mina-request'
- import errorCode from './errorCode.js'
- const {
- requestPath
- } = require('./../url.js')
- var app = getApp();
- // 对 WxRequest 进行实例化
- const instance = new WxRequest({
- baseURL: requestPath,
- timeout: 300000, // 超时时间
- isLoading: true, // 是否显示 loading 提示框
- header: {
- Authorization: "Basic bWluaTptaW5p",
- TENANT_ID: "1"
- }
- });
- // 添加请求拦截器
- instance.interceptors.request = (config) => {
- //console.log("app", app);
- config = Object.assign({
- ...instance.config,
- ...config
- });
- //console.log(config)
- const TENANT_ID = wx.getStorageSync("tenantId");
- const isToken = (config.header || {}).isToken === false;
- const token = wx.getStorageSync("access_token");
- if (token && !isToken) {
- config.header["Authorization"] = "Bearer " + token; // token
- } else {
- config.header["Authorization"] = "Basic bWluaTptaW5p";
- }
- delete config.header['isToken'];
- if (TENANT_ID) {
- config.header["TENANT_ID"] = TENANT_ID; // 租户ID
- }
- if (config.method === "post" && config.header.serialize) {
- config.data = serialize(config.data);
- delete config.data.serialize;
- }
- try {
- // 需要companyId和companyName参数
- if (!config.noCompany) {
- let companyId = wx.getStorageSync('companyId'),
- companyName = wx.getStorageSync('companyName');
- // companyName = wx.getStorageSync('companyName') || '';
- // let company_info = wx.getStorageSync('company') || {};
- // companyId = company_info && company_info.companyId ? company_info.companyId || '' : '';
- // if (!companyName) {
- // companyName = company_info && company_info.ownerCodeList ? company_info.ownerCodeList[0] || '' : '';
- // }
- if (config.method === 'get') {
- config.params = config.params || {};
- config.params = {
- companyId: companyId,
- companyName: companyName,
- ...config.params
- }
- } else {
- config.data = config.data || {};
- config.data = {
- companyId: companyId,
- companyName: companyName,
- ...config.data,
- }
- }
- }
- } catch (err) {
- console.error('参数companyId、companyName异常', err);
- }
- // 在发送请求之前做些什么
- return config
- }
- // 添加响应拦截器
- instance.interceptors.response = async (res, options) => {
- // response.isSuccess = true,代码执行了 wx.request 的 success 回调函数
- // response.isSuccess = false,代码执行了 wx.request 的 fail 回调函数
- // response.statusCode // http 响应状态码
- // response.config // 网络请求请求参数
- // response.data 服务器响应的真正数据
- //console.log(res)
- const status = Number(res.statusCode) || 200;
- let message = "";
- if (res.errMsg && res.errMsg == "request:fail ") {
- setTimeout(() => {
- wx.showToast({
- title: '网络超时,请检查网络',
- icon: 'none'
- })
- }, 200);
- return;
- }
- if (options["reSend"]) {
- if (options["reSend"] >= 5) {
- return "";
- }
- message = res.data.msg || errorCode[status] || errorCode["default"];
- } else {
- message = res.data.msg || errorCode[status] || errorCode["default"];
- }
- if (res.data.code == 1 && res.data.data == '用户不存在' && res.data.msg == '用户不存在') {
- wx.clearStorageSync();
- return Promise.reject(res);
- } else if (status === 401) {
- if (res.data.data === wx.getStorageSync('access_token') && res.data.msg === wx.getStorageSync('access_token') && res.data.code === 1) {
- //console.log(res);
- let userData = await instance.request(Object.assign({
- url: `/auth/oauth/token?grant_type=refresh_token&refresh_token=${wx.getStorageSync('refresh_token')}`,
- method: "post",
- header: {
- isToken: false,
- TENANT_ID: "1",
- Authorization: "Basic bWluaTptaW5p"
- },
- }))
- wx.setStorageSync('access_token', userData.data.access_token);
- wx.setStorageSync('refresh_token', userData.data.refresh_token);
- wx.setStorageSync('expires_in', userData.data.expires_in);
- wx.setStorageSync('isLock', false);
- wx.setStorageSync('lockPasswd', '');
- options.header.Authorization = `Basic ${userData.data.access_token}`;
- options["reSend"] = (options["reSend"] || 0) + 1;
- options.url = options.url.replace(options.baseURL, "");
- let reData = await instance.request(Object.assign({
- ...options
- }));
- return reData;
- }
- wx.clearStorageSync();
- setTimeout(() => {
- wx.showToast({
- title: '您的登录已过期,请重新登录或重新进入小程序',
- icon: "none",
- });
- }, 200);
- app.globalData.innerShow = null;
- app.globalData.changeCompanyName = null;
- app.globalData.token = null;
- app.globalData.company = null;
- app.globalData.userInfo = null;
- setTimeout(() => {
- wx.redirectTo({
- url: '/pages/login/login',
- });
- }, 2000);
- // store.dispatch("FedLogOut").then(() => {
- // router.push({
- // path: "/login"
- // });
- // });
- return;
- }
- if (res.data.access_token) {
- return Promise.resolve(res);
- }
- if (status !== 200 || res.data.code === 1) {
- if (res.data.data == 'invalid_exception') {
- wx.clearStorageSync();
- setTimeout(() => {
- wx.showToast({
- title: message,
- icon: "none",
- })
- }, 200);
- return;
- } else if (res.data.data == 'Full authentication is required to access this resource' || message.indexOf('Invalid refresh token:') != -1) {
- wx.clearStorageSync();
- setTimeout(() => {
- wx.showToast({
- title: '您的登录已过期,请重新登录或重新进入小程序',
- icon: "none",
- })
- }, 200);
- app.globalData.innerShow = null;
- app.globalData.changeCompanyName = null;
- app.globalData.token = null;
- app.globalData.company = null;
- app.globalData.userInfo = null;
- setTimeout(() => {
- wx.redirectTo({
- url: '/pages/login/login'
- })
- }, 1000);
- return Promise.reject(res);
- }
- if (res.config.skipError) {
- return Promise.reject(res);
- }
- setTimeout(() => {
- wx.showToast({
- title: message,
- icon: "none"
- })
- }, 200);
- return Promise.reject(new Error({
- message: message,
- response: res
- }));
- }
- // 对响应数据做点什么
- return res
- }
- instance.gets = (url) => {
- return instance.request(Object.assign({
- method: 'GET'
- }, {
- url
- }))
- }
- instance.get = (data) => {
- return instance.request(Object.assign({
- method: 'GET'
- }, data))
- }
- instance.post = (data) => {
- return instance.request(Object.assign({
- method: 'POST'
- }, data))
- }
- instance.upload = (data = {
- url,
- filePath,
- name: 'file'
- }) => {
- return instance.request(Object.assign({
- method: 'POST'
- }, data))
- }
- export default instance
|