var methods = {
  POST_METHOD: 'POST',
  DELETE_METHOD: 'DELETE',
  PUT_METHOD: 'PUT',
  GET_METHOD: 'GET'
};

/**
 * 新增
 * @param url
 * @param dataObj
 * @param args
 * @param successCallback
 * @param errorCallback
 * @param completeCallback
 */
function postJson(url, dataObj, args, successCallback, errorCallback, completeCallback) {
  doAjax(url, methods.POST_METHOD, dataObj, args, successCallback, errorCallback, completeCallback);
}

/**
 * 删除
 * @param url
 * @param dataObj
 * @param args
 * @param successCallback
 * @param errorCallback
 * @param completeCallback
 */
function deleteForm(url, dataObj, args, successCallback, errorCallback, completeCallback) {
  doAjax(url, methods.DELETE_METHOD, dataObj, args, successCallback, errorCallback, completeCallback);
}

/**
 * 修改
 * @param url
 * @param dataObj
 * @param args
 * @param successCallback
 * @param errorCallback
 * @param completeCallback
 */
function putJson(url, dataObj, args, successCallback, errorCallback, completeCallback) {
  doAjax(url, methods.PUT_METHOD, dataObj, args, successCallback, errorCallback, completeCallback);
}

/**
 * 查询
 * @param url
 * @param dataObj
 * @param args
 * @param successCallback
 * @param errorCallback
 * @param completeCallback
 */
function getForm(url, dataObj, args, successCallback, errorCallback, completeCallback) {
  doAjax(url, methods.GET_METHOD, dataObj, args, successCallback, errorCallback, completeCallback);
}

/**
 * 执行上传
 * @param url
 * @param method
 * @param dataObj
 * @param args
 * @param successCallback
 * @param errorCallback
 * @param completeCallback
 */
function doAjax(url, method, dataObj, args, successCallback, errorCallback, completeCallback) {
  var headers = {};
  if (args != null && typeof (args.headers) != 'undefined' && args.headers != null) {
    headers = args.headers;
  }
  wx.request({
    url: url,
    method: method,
    data: dataObj,
    header: headers,
    dataType: 'json',
    success: function (response) {
      if (response.statusCode == 200) {
        successCallback(response.statusCode, response.data, args);
      } else {
        if (errorCallback && typeof (errorCallback) == 'function') {
          errorCallback(response.statusCode, response.data);
        }
      }
    },
    fail: function (response) {
      console.log(response);
    },
    complete: function () {
      if (completeCallback && typeof (completeCallback) == 'function') {
        completeCallback();
      }
    }
  })
}

/**
 * 检测是路径参数有重复值
 * @param pathArgArray
 * @returns {boolean}
 */
function pathArgsHasSameValue(pathArgArray) {
  var tempArgIndex = 0;
  var tempArgs = pathArgArray[tempArgIndex];
  for (var i = (tempArgIndex + 1), item; item = pathArgArray[i]; i++) {
    if (tempArgs == item) {
      throw new Error('参数' + item + '有重复值!!!');
    }
    if (i == pathArgArray.length - 1) {
      tempArgs = pathArgArray[++tempArgIndex];
      i = tempArgIndex;
      continue;
    }
  }
}

/**
 * 获取页面间传递的参数
 * @param url
 */
function getParamsArg(url) {
  var params = url.split('?')[1];
  var paramsObj = {};
  if (typeof (params) == 'undefined' || params == null) {
    return paramsObj;
  }
  var paramsKVs = params.split('&');
  for (var i = 0, item = null; item = paramsKVs[i++];) {
    var kvs = item.split('=');
    if (kvs.length == 1) {
      paramsObj[kvs[0]] = null;
    }
    if (kvs.length == 2) {
      paramsObj[kvs[0]] = decodeURI(kvs[1]);
    }
  }
  return paramsObj;
}

/**
 * 构建路径
 * @param basePath 请求路径，{参数},
 * @param pathArgs 替换的路径参数，不能重复
 * @returns {*}
 */
function buildPath(basePath, pathArgs) {
  var path = basePath;
  if (!basePath || !(typeof (basePath) == 'string')) {
    throw new Error('basePath必须为字符串!!!');
  }
  if (!pathArgs || !Array.isArray(pathArgs)) {
    throw new Error('pathArgs必须为数组!!!');
  }
  var pathArgArray = basePath.match(/\{\w+\}/g);
  if (!pathArgArray) {
    return path;
  }
  pathArgsHasSameValue(pathArgArray);
  for (var i = 0, item; item = pathArgArray[i]; i++) {
    path = path.replace(item, pathArgs[i]);
  }
  return path;
}

/**
 * 通过form对象上传文件
 * @param url
 * @param formData
 * @param args
 * @param successCallback
 * @param errorCallback
 * @param beforeCallback
 * @param completeCallback
 */
function postFile(url, path, name, args, successCallback, errorCallback, completeCallback) {
  var headers = {};
  if (args != null && typeof (args.headers) != 'undefined' && args.headers != null) {
    headers = args.headers;
  }
  wx.uploadFile({
    filePath: path,
    name: name,
    url: url,
    header: headers,
    success: function (response) {
      if (response.statusCode == 200) {
        successCallback(response.statusCode, response.data, args);
      } else {
        if (errorCallback && typeof (errorCallback) == 'function') {
          errorCallback(response.statusCode, response.data);
        }
      }
    },
    fail: function (response) {
      console.log(response);
    },
    complete: function () {
      if (completeCallback && typeof (completeCallback) == 'function') {
        completeCallback();
      }
    }
  })
};

/**
 * xss 转义
 * @param html
 * @returns {string}
 */
function escape(html) {
  return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
    .replace(/</g, '&lt;').replace(/>/g, '&gt;')
    .replace(/'/g, '&#39;').replace(/"/g, '&quot;');
}

module.exports.post = postJson;
module.exports.delete = deleteForm;
module.exports.put = putJson;
module.exports.get = getForm;
module.exports.params = getParamsArg;
module.exports.path = buildPath;
module.exports.escape = escape;
module.exports.file = postFile;
module.exports.baseUrl='http://v3.xzszwhy.cn/xzszwhy';
module.exports.url="http://v3.xzszwhy.cn/"