9 changed files with 131 additions and 75 deletions
			
			
		@ -1,87 +1,90 @@ | 
				
			|||
/*--------------------------------------------------------------- | 
				
			|||
 |                           请求接口封装                           | | 
				
			|||
 ---------------------------------------------------------------*/ | 
				
			|||
import axios from 'axios' | 
				
			|||
import log from 'dai-js/modules/log' | 
				
			|||
import curry from 'dai-js/tools/curry' | 
				
			|||
import axios from "axios"; | 
				
			|||
import message from "dai-js/modules/message/message"; | 
				
			|||
import curry from "dai-js/tools/curry"; | 
				
			|||
 | 
				
			|||
const request = curry( | 
				
			|||
  (method, url, data = {}, headers = {}, progress = () => {}) => { | 
				
			|||
    return new Promise((reslove) => { | 
				
			|||
      let returnIniData = { | 
				
			|||
        httpCode: '', | 
				
			|||
        httpCode: "", | 
				
			|||
        data: {}, | 
				
			|||
        msg: '', | 
				
			|||
        code: '' | 
				
			|||
      } | 
				
			|||
        msg: "", | 
				
			|||
        code: "", | 
				
			|||
      }; | 
				
			|||
 | 
				
			|||
      // 添加服务器端URL
 | 
				
			|||
      function processUrl(url) { | 
				
			|||
        if (url.indexOf('http://') > -1 || url.indexOf('https://') > -1) { | 
				
			|||
          return url | 
				
			|||
        if (url.indexOf("http://") > -1 || url.indexOf("https://") > -1) { | 
				
			|||
          return url; | 
				
			|||
        } | 
				
			|||
        return process.env.VUE_APP_API_SERVER + url | 
				
			|||
        return process.env.VUE_APP_API_SERVER + url; | 
				
			|||
      } | 
				
			|||
 | 
				
			|||
      url = processUrl(url) | 
				
			|||
      url = processUrl(url); | 
				
			|||
 | 
				
			|||
      const succFn = (res) => { | 
				
			|||
        // log(`[request成功] ${url}`, data, res);
 | 
				
			|||
        let retData = { | 
				
			|||
          ...returnIniData, | 
				
			|||
          ...res.data, | 
				
			|||
          httpCode: res.statusCode | 
				
			|||
        } | 
				
			|||
          httpCode: res.statusCode, | 
				
			|||
        }; | 
				
			|||
        // if(typeof Vue.$afterRequestHook == 'function'){
 | 
				
			|||
        // 	retData = Vue.$afterRequestHook(retData);
 | 
				
			|||
        // }
 | 
				
			|||
 | 
				
			|||
        reslove(retData) | 
				
			|||
        if (res.data.code > 8000 && res.data.code < 10000) { | 
				
			|||
          message.error(res.data.msg); | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
        reslove(retData); | 
				
			|||
      }; | 
				
			|||
 | 
				
			|||
      const failFn = (err) => { | 
				
			|||
        // log(`[request失败] ${url}`, data, err);
 | 
				
			|||
 | 
				
			|||
        reslove( | 
				
			|||
          Object.assign({}, returnIniData, { | 
				
			|||
            httpCode: '9999', //访问出现意外
 | 
				
			|||
            msg: '网络错误' | 
				
			|||
            httpCode: "9999", //访问出现意外
 | 
				
			|||
            msg: "网络错误", | 
				
			|||
          }) | 
				
			|||
        ) | 
				
			|||
      } | 
				
			|||
        ); | 
				
			|||
      }; | 
				
			|||
 | 
				
			|||
      if (method.toUpperCase() == 'POST') { | 
				
			|||
      if (method.toUpperCase() == "POST") { | 
				
			|||
        axios | 
				
			|||
          .post(url, data, { | 
				
			|||
            headers, | 
				
			|||
            responseType: 'json' | 
				
			|||
            responseType: "json", | 
				
			|||
            // progress,
 | 
				
			|||
            // credentials: false,
 | 
				
			|||
          }) | 
				
			|||
          .then(succFn) | 
				
			|||
          .catch(failFn) | 
				
			|||
          .catch(failFn); | 
				
			|||
      } else { | 
				
			|||
        axios | 
				
			|||
          .get(url, { | 
				
			|||
            params: data, | 
				
			|||
            headers, | 
				
			|||
            responseType: 'json' | 
				
			|||
            responseType: "json", | 
				
			|||
            // credentials: true,
 | 
				
			|||
          }) | 
				
			|||
          .then(succFn) | 
				
			|||
          .catch(failFn) | 
				
			|||
          .catch(failFn); | 
				
			|||
      } | 
				
			|||
    }) | 
				
			|||
    }); | 
				
			|||
  } | 
				
			|||
) | 
				
			|||
); | 
				
			|||
 | 
				
			|||
export const requestGet = request('get') | 
				
			|||
export const requestGet = request("get"); | 
				
			|||
 | 
				
			|||
export const requestPost = request('post') | 
				
			|||
export const requestPost = request("post"); | 
				
			|||
 | 
				
			|||
export default { | 
				
			|||
  install(Vue) { | 
				
			|||
    Vue.prototype.$requestGet = requestGet | 
				
			|||
    Vue.prototype.$requestPost = requestPost | 
				
			|||
  } | 
				
			|||
} | 
				
			|||
    Vue.prototype.$requestGet = requestGet; | 
				
			|||
    Vue.prototype.$requestPost = requestPost; | 
				
			|||
  }, | 
				
			|||
}; | 
				
			|||
 | 
				
			|||
					Loading…
					
					
				
		Reference in new issue