123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /**
- * Created by Administrator on 2017/9/22.
- */
- // 配置API接口地址
- var root = 'crmServiceInfo';
- // 引用superagent
- var request = require('superagent');
- // 引用superagent
- //自定义判断元素类型
- function toType(obj)
- {
- return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
- }
- // 参数过滤函数
- function filter_param(o)
- {
- for(var key in o)
- {
- if(o[key]==null)
- delete o[key];
- if(toType(o[key])=="string")
- {
- o[key] = o[key].trim();
- if(o[key].length==0)
- {
- delete o[key];
- }
- }
- }
- return o;
- }
- /*
- 接口处理函数
- */
- function _api_req(method,url,params,success,fail)
- {
- var res = request(method,url).type("text/plain");
- if(params)
- {
- params = filter_param(params);
- if(method == "POST" || method == "PUT")
- {
- if(toType(params) === "object")
- {
- params = JSON.Stringify(params);
- }
- res.send(params);
- }
- else if(method === "GET" || method ==="DELETE")
- {
- res.query(params);
- }
- }
- res.end(function(err,r){
- if(err)
- {
- alert("请求出错,错误信息:"+r.status);
- return;
- }
- if(r.body)
- {
- if(success)
- {
- success(r.body);
- }
- }
- else
- {
- if(fail)
- {
- fail(r.body);
- }
- else
- {
- alert("err:"+JSON.stringify(r.body));
- }
- }
- })
- }
- export default {
- get:function(url,params,success,fail){
- return _api_req("GET",root+"/"+url,params,success,fail);
- },
- put:function(url,params,success,fail)
- {
- return _api_req("PUT",root+"/"+url,params,success,fail);
- },
- post:function(url,params,success,fail)
- {
- return _api_req("POST",root+"/"+url,params,success,fail);
- },
- delete:function(url,params,success,fail)
- {
- return _api_req("DELETE",root+"/"+url,params,success,fail);
- }
- }
|