UNPKG

smart-core-util

Version:

smartx javascript utils

183 lines (158 loc) 6.35 kB
import iStorage from '../storage/index.js' import axios from 'axios' import qs from 'qs' import { indicator } from 'smart-indicator'; class smartHttp { constructor(options) { if((options && options.baseURL ? options.baseURL : undefined) || iStorage.getUrlGateway()){ this.api = axios.create({ baseURL: (options && options.baseURL ? options.baseURL : undefined) || iStorage.getUrlGateway() }); }else{ this.throwError( { stack:{ api:'Uris Error' }, message: 404, //报错状态 400 errorTxt:'Uris丢失' }, 'Uris Error', window.location.href, 404, 'Uris丢失' ) return; } this.options = options; this.autoExtractResponseError = true if(options.autoExtractResponseError === false)( this.autoExtractResponseError = false ) this.alert = options.alert || window.alert; this.isNeedBlock = true; if(typeof(options.isNeedBlock) == 'boolean'){ this.isNeedBlock = options.isNeedBlock; } this.block = options.block || indicator.open; this.unblock = options.unblock || indicator.close; this.only401 = true; this.handle401 = options.handle401 || function () { }; this.injectRequest(); this.injectResponse(); // 修改是否需要 抛错时的alert true/false let that = this; this.api.changeAutoExtractResponseError = function(isShow){ that.autoExtractResponseError = isShow; } } // 请求拦截 injectRequest() { this.api.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8'; this.api.interceptors.request.use(null, (error) => { this.alert(error.message); throw error; }); this.api.interceptors.request.use((config) => { if(this.isNeedBlock){ this.block(); } // get请求添加时间搓 if (config.method === 'get') { config.params = { ...config.params, rnd: new Date().getTime(), } } if (config.data && config.headers['Content-Type'] === 'application/x-www-form-urlencoded') { config.data = qs.stringify(config.data); } this.appendHeader(config); return config; },(err) => { return Promise.reject(err); }); } // 响应拦截 injectResponse() { let that = this; this.api.interceptors.response.use((response) => { if(this.isNeedBlock){ this.unblock(); } if (!response) { return; } let filterResponse = (response && response.data) ? response.data : null; if(filterResponse.success == false){ that.throwError(filterResponse,response.config.url,that.options.baseURL,response.status,filterResponse); // if(filterResponse.error){ // that.throwError(filterResponse,response.config.url,that.options.baseURL,response.status,filterResponse.error); // }else{ // // if(that.autoExtractResponseError){ // // that.alert(filterResponse.msg || filterResponse.error || '未知错误') // // } // that.throwError(filterResponse,response.config.url,that.options.baseURL,response.status,filterResponse.msg || '未知错误'); // } } return filterResponse; },(error) => { if(this.isNeedBlock){ this.unblock(); } // 400单独拦截 if(error.response.status == 400 && error.response.data.error == 'invalid_grant'){ // 密码错误拦截 if( error.response.data.LockMinute ){ let msg = ''; if(error.response.data.LimitFailedCount - error.response.data.AccessFailedCount > 0){ msg = "密码不正确,密码已输入错误" + error.response.data.AccessFailedCount + "次,输错" + error.response.data.LimitFailedCount + "次账号将被锁定" + error.response.data.LockMinute + "分钟" } if(error.response.data.LimitFailedCount - error.response.data.AccessFailedCount <= 0){ msg = "密码已被锁,请" + error.response.data.LockMinute + "分钟后再试" } this.alert(msg); }else{ // 其他400拦截(用户不存在) this.alert(error.response.data.error_description) } return; } that.throwError(error.response.data,error.response.config.url,that.options.baseURL,error.response.status,error.response.data.error_description ); return error; }); } // 抛错 throwError(data,api,url,status,errorTxt){ let xxx = data || {} xxx.api = api xxx.href = url let errorMsg = { stack:xxx, message: status, //报错状态 400 errorTxt: errorTxt || '网络异常' } console.error(errorMsg); } // 添加header appendHeader(config) { var token = iStorage.getAccessToken() if (token && token != null) { config.headers["Authorization"] = 'Bearer ' + token; } var Version = iStorage.get("apiVersion"); if(Version){ config.headers["Version"] = Version; } var tenantInfo = iStorage.get("tenantInfo"); if(tenantInfo){ config.headers["tenantInfo"] = tenantInfo; } var organization_type = iStorage.get("organization_type"); if (organization_type == "Tenant") { config.headers["tenant_code"] = iStorage.get("tenant_code") } } } export default smartHttp;