123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import store from '../store'
- let service = {
- baseURL: store.state.baseURL, // 请求头
- header: {
- 'Content-Type': "application/x-www-form-urlencoded"
- },
- // 请求拦截器
- interceptors: {
- // 安装请求前拦截
- request: function(success, iserror) {
- service.request.success = success;
- service.request.error = iserror;
- },
- // 安装请求完成后拦截
- response: function(success, iserror) {
- service.response.success = success;
- service.response.error = iserror;
- }
- },
- // 请求前拦截
- request: {
- // 回调成功执行方法
- success: function(e) {
- return e
- },
- // 回调失败
- error: function(e) {
- return e
- }
- },
- // 请求完成后拦截
- response: {
- // 回调成功执行方法
- success: function(e) {
- return e
- },
- // 回调失败
- error: function(e) {
- return e
- }
- },
- // 开始请求
- open: async function(data) {
- return await new Promise(function(resolve, reject) {
- let requestData = ''; //保存处理完成后请求数据
- //console.log(data);
- // 请求前数据处理
- requestData = service.request.success(data);
- // 数据复制用于请求
- let url = '';
- if (requestData.url.indexOf('http') > -1) {
- url = requestData.url
- } else {
- url = service.baseURL + requestData.url
- }
- uni.request({
- url: url,
- method: requestData.method || 'GET',
- data: requestData.data,
- header: data.header || service.header,
- success: res => {
- if (res.statusCode == 200) {
- try {
- resolve(service.response.success(res));
- } catch (e) {
- service.response.error(e)
- }
- }
- },
- // 请求失败数据处理
- fail: (err) => {
- service.response.error(err)
- }
- });
- })
- },
- // 上传文件
- upFilse: async function(data) {
- return await new Promise(function(resolve, reject) {
- let requestData = ''; //保存处理完成后请求数据
- try {
- // 请求前数据处理
- requestData = service.request.success(data);
- } catch (e) {
- // 请求报错拦截
- service.request.error(e);
- }
- // #ifdef H5
- let input = event.target.querySelector('input');
- input.setAttribute("type", 'file');
- input.setAttribute("name", Object.getOwnPropertyNames(requestData.data)[0]);
- input.click();
- input.addEventListener('change', function(e) {
- console.log(input.files);
- let f = new FormData();
- f.append("file", input.files[0]);
- let xhr = new XMLHttpRequest();
- xhr.open(requestData.method, service.baseURL + requestData.url, true);
- // xhr.setRequestHeader("content-type","multipart/form-data");
- xhr.send(f);
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4 && xhr.status == 200) {
- let data = JSON.parse(xhr.responseText)
- resolve(service.response.success(data));
- } else {
- reject(service.response.error(xhr.status));
- }
- }
- })
- // #endif
- // #ifndef H5
- // 获取请求key值
- let fileName = Object.getOwnPropertyNames(requestData.data)[0];
- // 获取传输的图片
- let file = requestData.data[fileName][0];
- // 数据复制用于请求
- uni.uploadFile({
- url: service.baseURL + requestData.url,
- filePath: file,
- name: fileName,
- header: {
- 'Content-Type': 'multipart/form-data'
- },
- success: res => {
- console.log(res);
- // 请求成功数据处理
- try {
- resolve(service.response.success(res));
- } catch (e) {
- reject(service.response.error(e));
- }
- },
- // 请求失败数据处理
- fail: (err) => {
- console.log(err);
- reject(service.response.error(err))
- }
- });
- // #endif
- })
- },
- };
- export default service
|