import urlConfig from './config.js' // import store from '@/store/index' const request = {} request.globalRequest = (url, options = {}, power) => { /* 权限判断 因为有的接口请求头可能需要添加的参数不一样,所以这里做了区分 1 == 不通过access_token校验的接口 2 == 文件下载接口列表 3 == 验证码登录 */ // switch (power){ // case 1: // headers['Authorization'] = 'Basic a3N1ZGk6a3N1ZGk=' // break; // case 2: // headers['Authorization'] = 'Basic a3N1ZGlfcGM6a3N1ZGlfcGM=' // break; // case 3: // responseType = 'blob' // break; // default: // headers['Authorization'] = `Bearer ${ // this.$store.getters.userInfo // }` // headers['TENANT-ID'] = this.$store.getters.userInfo.tenant_id // break; // } let contentType = localStorage.token_type ? "application/json" : "application/x-www-form-urlencoded"; return uni.request({ url: url, ...options, //data,method... header: { "content-type": contentType, "Authorization": localStorage.token_type, 'withCredentials': true, // 'Blade-Auth':'bearer '+store.state.token, 'token_type': localStorage.token_type, ...options.header, }, timeout:60000, }) .then(res => { if (res != null && res[1] != null) { if (res[1].statusCode != null) { let statusCode = res[1].statusCode.toString(); //状态码类型 2开头的全是成功 // let code = statusCode.substring(0, 1); if (statusCode == '200') { return res[1].data; } else if (statusCode == '204') { if (options.method == 'get') { return null; } else { return res[1].data; } } else if (statusCode == '400') { if (res[1].data.error) { if (res[1].data.error.message) { throw res[1].data.error.message; } } else if (res[1].data.errors) { let errors = res[1].data.errors; let errorMsg = '请求参数错误:\n'; let keys = Object.keys(errors); for (var i = 0; i < keys.length; i++) { let key = keys[i]; let value = errors[key]; errorMsg += i + 1 + ':' + value + '\n'; } throw errorMsg; } } else if (statusCode == '404') { if (res[1].data === '') { throw '未找到接口'; } else { throw res[1].data; } } else if (statusCode == '403') { let message = res[1].data.error?.message; if (message) { let temp; try { temp = JSON.parse(message) } catch (err) { throw message; } if (temp) { var hintError = temp.error; var hintErrorDes = temp.error_description; if (hintError == 'invalid_grant') { if (hintErrorDes.includes('Invalid username or password!')) { throw '用户名或密码错误'; } else if (hintErrorDes.includes( 'The user account has been locked out due to invalid login attempts' )) { throw "账号已被锁定,请稍后再试"; } } else { throw message;; } } } else { throw res[1].statusCode + "错误"; } } else { let message = res[1].data.error?.message; if (message != undefined) { throw res[1].data.error.message; } else { throw res[1].statusCode + "错误"; } } } else { throw url + "返回的状态码类型为空" } } else { throw url + "返回的res为空" } }) .catch(params => { throw new Error("后台提示 : "+params); }) } export default request.globalRequest // 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下: // 对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18。 // 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。 // 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。