import_mixin.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function isObject(value) {
  2. return value !== null && typeof value === 'object'
  3. }
  4. function isFunction(value) {
  5. return typeof value === 'function'
  6. }
  7. function identifyType(value) {
  8. if (isFunction(value)) {
  9. return 'Function'
  10. } else if (isObject(value)) {
  11. return 'Object'
  12. } else {
  13. return 'Neither Object nor Function'
  14. }
  15. }
  16. export default {
  17. methods: {
  18. //导入按钮
  19. importButton(func, name = '导入', params, fun1, fun2) {
  20. return (
  21. <el-upload
  22. action={'_'}
  23. show-file-list={false}
  24. http-request={async data => {
  25. var formdata = new FormData()
  26. formdata.append('file', data.file)
  27. if (identifyType(params) == 'Object') {
  28. for (const key in params) {
  29. if (Object.hasOwnProperty.call(params, key)) {
  30. formdata.append(key, params[key])
  31. }
  32. }
  33. } else if (identifyType(params) == 'Function') {
  34. try {
  35. var chanshu = await params()
  36. for (const key in chanshu || {}) {
  37. if (Object.hasOwnProperty.call(chanshu, key)) {
  38. formdata.append(key, chanshu[key])
  39. }
  40. }
  41. } catch (error) {
  42. return
  43. }
  44. }
  45. const loading = this.$loading({
  46. lock: true,
  47. text: '正在导入',
  48. spinner: 'el-icon-loading',
  49. background: 'rgba(0, 0, 0, 0.7)'
  50. })
  51. fun1 && fun1()
  52. func({ formdata })
  53. .then(res => {
  54. fun2 && fun2(res)
  55. this.$refs.pageRef.refreshList()
  56. loading.close()
  57. this.$message({
  58. type: 'success',
  59. message: '导入成功!'
  60. })
  61. })
  62. .catch(err => {
  63. fun2 && fun2()
  64. loading.close()
  65. this.$message({
  66. type: 'error',
  67. message: err.message || '导入失败'
  68. })
  69. })
  70. }}
  71. >
  72. <span class="teshudeshangchuananniu">{name}</span>
  73. </el-upload>
  74. )
  75. }
  76. }
  77. }