ExcelUploader.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="leo-excel-uploader">
  3. <el-upload
  4. class="excel-uploader-box"
  5. v-show="uploaderShow"
  6. :action="uploaderRequestUrl"
  7. name="file"
  8. :headers="uploaderHeaders"
  9. :data="uploaderFormData"
  10. :show-file-list="false"
  11. :before-upload="uploaderHandleBefore"
  12. :on-success="uploaderHandleSuccess"
  13. :disabled="uploaderDisabled"
  14. v-loading="uploaderLoading"
  15. >
  16. <el-button type="primary">{{uploadBtnTitle}}</el-button>
  17. </el-upload>
  18. <el-dialog
  19. title="进度提示"
  20. :visible.sync="dialogVisible"
  21. :close-on-click-modal="false"
  22. :close-on-press-escape="false"
  23. :show-close="false"
  24. width="30%">
  25. <el-progress :percentage="importPercent"></el-progress>
  26. </el-dialog>
  27. </div>
  28. </template>
  29. <script>
  30. import {REQUEST_URL, ACCESS_TOKEN_PREFIX} from '@/utils/config'
  31. import userInfo from '@/utils/userInfo'
  32. import network from '../utils/network'
  33. export default {
  34. name: 'LeoExcelUploader',
  35. props: {
  36. uploadBtnTitle: {
  37. type: String,
  38. default: '上传',
  39. },
  40. requestUploadRoute: {
  41. type: String,
  42. required: true,
  43. },
  44. requestImportToExcelTableRoute: {
  45. type: String,
  46. required: true,
  47. },
  48. requestImportExcelTableToDataRoute: {
  49. type: String,
  50. required: true,
  51. },
  52. importRowCount: {
  53. required: true,
  54. },
  55. uploaderSuccessCanChange: {
  56. type: Boolean,
  57. default: true,
  58. },
  59. otherParams: {
  60. type: Object,
  61. default: {},
  62. },
  63. finishCallbackFunc: {
  64. type:Function,
  65. default:null,
  66. },
  67. excelOption: {
  68. type: String,
  69. },
  70. },
  71. data() {
  72. return {
  73. uploaderShow: true,
  74. uploaderLoading: false,
  75. uploaderFormData: {
  76. 'uploadToken': '',
  77. 'excelOption': this.excelOption,
  78. },
  79. uploaderRequestUrl: `${REQUEST_URL}${this.requestUploadRoute}`,
  80. importRequestUrl: `${REQUEST_URL}${this.requestImportRoute}`,
  81. uploaderHeaders: {
  82. 'Device-Type': 'pc',
  83. 'Suppress-Response-Code': '1',
  84. 'Authorization': '',
  85. },
  86. uploaderDisabled: false,
  87. successImageUrl: null,
  88. dialogVisible: false,
  89. importPercent: 0,
  90. }
  91. },
  92. methods: {
  93. uploaderHandleBefore() {
  94. if (!this.importRowCount || this.importRowCount <= 1) {
  95. this.$message({
  96. message: '请填写文件总行数,并且总行数大于1',
  97. type: 'warning'
  98. })
  99. return false;
  100. }
  101. return network.updateToken().then(accessToken => {
  102. this.uploaderHeaders.Authorization = ACCESS_TOKEN_PREFIX + accessToken
  103. return network.getData(`file/token`)
  104. }).then(response => {
  105. this.uploaderFormData.uploadToken = response
  106. this.uploaderLoading = true
  107. }).catch(() => {
  108. this.uploaderLoading = true
  109. })
  110. },
  111. uploaderHandleSuccess(response, file) {
  112. if (response.success) {
  113. this.$message({
  114. message: '上传成功',
  115. type: 'success'
  116. })
  117. // 显示进度框
  118. this.dialogVisible = true
  119. this.importPercent = 0
  120. // 导入excel
  121. this.importToExcelTable(response.data.excelImportId, 1, 1000)
  122. if (!this.uploaderSuccessCanChange) {
  123. this.uploaderShow = false
  124. this.uploaderDisabled = true
  125. this.uploaderLoading = false
  126. }
  127. this.$emit('on-success', response.data)
  128. } else {
  129. this.$message({
  130. message: response.data.message,
  131. type: 'warning'
  132. })
  133. this.uploaderLoading = false
  134. }
  135. },
  136. importToExcelTable(excelImportId, startRow = 0, limit = 1000) {
  137. let _this = this
  138. let importRowCount = Number.parseInt(_this.importRowCount)
  139. let postParams = {
  140. excelImportId: excelImportId,
  141. rowCount: importRowCount,
  142. startRow: startRow,
  143. limit: limit > importRowCount ? importRowCount : limit,
  144. }
  145. for (let key in _this.otherParams ) {
  146. postParams[key] = _this.otherParams[key]
  147. }
  148. network.postData(_this.requestImportToExcelTableRoute, postParams).then(response => {
  149. if (!response.finish) {
  150. // 完成一次的进度占50%的多少
  151. let percent = parseInt(startRow / importRowCount * 50)
  152. if (_this.importPercent + percent >= 50) {
  153. _this.importPercent = 50
  154. } else {
  155. if (_this.importPercent + percent >= 100) {
  156. _this.importPercent = 100
  157. } else {
  158. _this.importPercent += percent
  159. }
  160. }
  161. _this.importToExcelTable(excelImportId, startRow + limit, limit)
  162. } else {
  163. _this.importPercent = 50
  164. _this.importExcelTableToData(excelImportId, 0, 2)
  165. }
  166. }).catch(() => {
  167. _this.importPercent = 0
  168. _this.dialogVisible = false
  169. _this.uploaderLoading = false
  170. })
  171. },
  172. importExcelTableToData(excelImportId, offset = 0, limit = 100) {
  173. let _this = this
  174. let postParams = {
  175. excelImportId: excelImportId,
  176. offset: offset,
  177. limit: limit,
  178. }
  179. for (let key in this.otherParams ) {
  180. postParams[key] = this.otherParams[key]
  181. }
  182. network.postData(_this.requestImportExcelTableToDataRoute, postParams).then(response => {
  183. if (!response.finish) {
  184. let percent = Number.parseInt(offset / _this.importRowCount * 50)
  185. if (_this.importPercent + percent >= 100) {
  186. _this.importPercent = 100
  187. } else {
  188. _this.importPercent += percent
  189. }
  190. _this.importExcelTableToData(excelImportId, offset + limit, limit)
  191. } else {
  192. _this.importPercent = 100
  193. _this.dialogVisible = false
  194. _this.uploaderLoading = false
  195. if( _this.finishCallbackFunc !== null ) {
  196. _this.finishCallbackFunc()
  197. }
  198. _this.$message({
  199. message: '导入完成',
  200. type: 'success'
  201. })
  202. }
  203. }).catch(() => {
  204. _this.importPercent = 0
  205. _this.dialogVisible = false
  206. _this.uploaderLoading = false
  207. })
  208. }
  209. }
  210. }
  211. </script>
  212. <style scoped>
  213. </style>