tools.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import axios from 'axios'
  2. /**
  3. * 对象的深度拷贝方法
  4. * @params {obj} 对象值
  5. * return obj
  6. * */
  7. export function clone(obj) {
  8. // Handle the 3 simple types, and null or undefined or function
  9. if (null == obj || "object" != typeof obj) return obj;
  10. // Handle Date
  11. if (obj instanceof Date) {
  12. var copy = new Date();
  13. copy.setTime(obj.getTime());
  14. return copy;
  15. }
  16. // Handle Array or Object
  17. if (obj instanceof Array | obj instanceof Object) {
  18. var copy = (obj instanceof Array) ? [] : {};
  19. for (var attr in obj) {
  20. if (obj.hasOwnProperty(attr))
  21. copy[attr] = clone(obj[attr]);
  22. }
  23. return copy;
  24. }
  25. }
  26. export function Vuehttp(json = {}, url) {
  27. let params = new FormData()
  28. if ('object' != typeof json) {
  29. throw new Error("json is not a obj");
  30. }
  31. for (let i of Object.keys(json)) {
  32. if (json[i] instanceof Array) {
  33. params.append(i, json[i][0])
  34. } else if (typeof json[i] === 'object') {
  35. params.append('params', JSON.stringify(json[i]))
  36. } else {
  37. params.append(i, json[i])
  38. }
  39. }
  40. let config = {
  41. headers: {'Content-Type': 'multipart/form-data'}
  42. }
  43. // console.log(params)
  44. return axios.post(url, params, config)
  45. }