importUploadPhp.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <block>
  3. <el-upload style="display: inline-block; margin: 0 10px;" :action="upload_host" :data="dataObj" :multiple="false" :before-upload="beforeUpload" :on-success="handleUploadSuccess" :file-list="importFileList">
  4. <el-button @click="upload" size="small" type="primary" :loading="importLoading">{{ importLoading ? '导入中...' : title }}</el-button>
  5. </el-upload>
  6. <el-dialog title="错误提示" :visible.sync="isShowMsg" width="50%" :close-on-click-modal="false" :destroy-on-close="true">
  7. <div class="html" v-html="html"></div>
  8. <div slot="footer" class="dialog-footer">
  9. <el-button type="primary" @click="isShowMsg = false">确定</el-button>
  10. </div>
  11. </el-dialog>
  12. </block>
  13. </template>
  14. <script>
  15. import request from '@/utils/request';
  16. import SparkMD5 from 'spark-md5'
  17. export default {
  18. name: 'importUploadPhp',
  19. props: {
  20. requestUrl: {
  21. type: String,
  22. default: ''
  23. },
  24. title: {
  25. type: String,
  26. default: '导入'
  27. },
  28. batch_month: {
  29. type: String,
  30. default: ''
  31. },
  32. },
  33. data() {
  34. return {
  35. importFileList: [],
  36. importLoading: false,
  37. timer: '',
  38. aliosstoken: '',
  39. upload_host: '',
  40. dataObj: {
  41. policy: '',
  42. signature: '',
  43. key: '',
  44. ossaccessKeyId: '',
  45. },
  46. md5: '',
  47. isShowMsg: false,
  48. html: ''
  49. };
  50. },
  51. created() {
  52. },
  53. methods: {
  54. getAliOssToken(){
  55. const that = this;
  56. request({
  57. url: '/qviKHUYsyN.php/index/getAliOssToken',
  58. method: 'get',
  59. params: {}
  60. }).then(res => {
  61. that.aliosstoken = res.data.aliosstoken;
  62. that.upload_host = res.data.upload_host
  63. });
  64. },
  65. close(){
  66. this.isShowMsg = false
  67. },
  68. emitInput(val) {
  69. this.$emit('input', val);
  70. },
  71. upload(){
  72. this.getAliOssToken()
  73. },
  74. beforeUpload(file) {
  75. const that = this;
  76. return new Promise((resolve, reject) => {
  77. var fileReader = new FileReader();
  78. //此处打印file可看到file下的raw为文件属性
  79. let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
  80. var spark = new SparkMD5.ArrayBuffer();
  81. //获取文件二进制数据
  82. fileReader.readAsArrayBuffer(file)
  83. //异步执行函数
  84. fileReader.onload = function(e){
  85. spark.append(e.target.result);
  86. var md5 = spark.end()
  87. that.md5 = md5
  88. console.log(md5)
  89. request({
  90. url: '/addons/alioss/index/params',
  91. method: 'post',
  92. data: {
  93. method: 'POST',
  94. md5: md5,
  95. name: md5 + file.name.substring(file.name.lastIndexOf(".")),
  96. type: file.type,
  97. size: file.size,
  98. aliosstoken: that.aliosstoken
  99. }
  100. })
  101. .then(res => {
  102. that.dataObj = res.data;
  103. resolve(true);
  104. })
  105. .catch(err => {
  106. reject(false);
  107. });
  108. }
  109. });
  110. },
  111. handleUploadSuccess(res, file, fileList) {
  112. this.getNotify(file)
  113. this.toLead()
  114. },
  115. toLead() {
  116. let params = {
  117. file: this.dataObj.key.replace('${filename}')
  118. }
  119. request({
  120. url: this.requestUrl,
  121. method: 'post',
  122. params
  123. }).then(res => {
  124. if(res.data.key){
  125. this.timer = setInterval(()=> {
  126. this.$message({
  127. type: 'success',
  128. message: '导入中,请等待...'
  129. });
  130. this.getCache(res.data.key)
  131. },3000)
  132. }else if(res.data.length > 0){
  133. this.$emit('importSuccess',res.data)
  134. } else {
  135. this.$message.error(res.msg);
  136. }
  137. this.importLoading = false;
  138. this.importFileList = [];
  139. })
  140. },
  141. getNotify(file){
  142. request({
  143. url: '/addons/alioss/index/notify',
  144. method: 'post',
  145. data: {
  146. method: 'POST',
  147. url: this.dataObj.key.replace('${filename}'),
  148. md5: this.md5,
  149. name: this.md5 + file.name.substring(file.name.lastIndexOf(".")),
  150. type: file.type,
  151. size: file.size,
  152. aliosstoken: this.aliosstoken
  153. }
  154. }).then(res => {
  155. resolve(true);
  156. }).catch(err => {
  157. reject(false);
  158. });
  159. },
  160. getCache(key){
  161. let params = {
  162. key: key
  163. }
  164. if(this.batch_month){
  165. params.batch_month = this.batch_month
  166. }
  167. request({
  168. url: '/qviKHUYsyN.php/index/getCache',
  169. method: 'get',
  170. params
  171. }).then(res => {
  172. if(res.data.import_bool){
  173. clearInterval(this.timer)
  174. this.$emit('importSuccess')
  175. this.$message({
  176. type: 'success',
  177. message: '导入成功!'
  178. });
  179. }else if(res.code != 1){
  180. clearInterval(this.timer)
  181. // let html = ''
  182. // let arr = res.msg.split('<br />\n')
  183. // arr.forEach(item => {
  184. // html += `<div>${item}</div>`
  185. // })
  186. this.html = res.msg
  187. this.isShowMsg = true
  188. if(res.msg.length < 500){
  189. setTimeout(() => {
  190. this.isShowMsg = false
  191. },5000)
  192. }
  193. }
  194. });
  195. },
  196. }
  197. };
  198. </script>
  199. <style type="text/css" scoped="scoped">
  200. .html{
  201. font-size: 16px;
  202. line-height: 20px;
  203. }
  204. </style>