saveFile.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <template>
  2. <view class="bg-white">
  3. <!-- 顶部 -->
  4. <cu-custom bgColor="bg-blue" :isBack="true">
  5. <block slot="backText">新增{{type==0?'照片':'视频'}}</block>
  6. <block slot="content"></block>
  7. <block slot="right">
  8. <view class="flex justify-center">
  9. <button class="margin-top-ssm cu-btn round line-blue text-white" >
  10. <template v-if="handle != 'edit'">
  11. <button class="cu-btn line-blue" @tap="ChooseMediaList">
  12. <text class="text-white">继续添加</text>
  13. </button>
  14. <text> | </text>
  15. </template>
  16. <button class=" cu-btn line-blue" @tap="uploadClick">
  17. <text class="text-white">保存</text>
  18. </button>
  19. </button>
  20. </view>
  21. </block>
  22. </cu-custom>
  23. <view style="position: fixed;right: 0;top: calc(var(--status-bar-height) + 200rpx);z-index: 999999;" v-if="upIng">
  24. <view class="bg-blue text-center flex" style="border-radius:50rpx 0 0 50rpx ;width: 200rpx;height: 70rpx;" @click="">
  25. <view class="cuIcon-loading2"></view>
  26. <view class="text-sm">
  27. <view>上传中</view>
  28. <view>已完成{{upNum}}</view>
  29. </view>
  30. </view>
  31. </view>
  32. <form>
  33. <view v-for="(item,index) in fileList" :key="index">
  34. <view class="cu-bar bg-white">
  35. <view class="action">
  36. {{item.name}}
  37. </view>
  38. <view class="action text-blue" @click="delFile(index)" v-if="!upIng">
  39. 删除
  40. </view>
  41. </view>
  42. <view class="cu-form-group">
  43. <view class="grid col-4 grid-square flex-sub">
  44. <view class="bg-img" @tap="ViewImage" :data-url="item.url" v-if="item.url">
  45. <image :src="item.url" mode="aspectFill" v-if="type==0"></image>
  46. <!-- <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
  47. <text class='cuIcon-close'></text>
  48. </view> -->
  49. <video :src="item.url" object-fit="fill" :id="'myVideo'+index" :ref="'myVideo'+index" style="width: 152rpx;height: 152rpx;" v-if="type==1" @click="toplay(index)" @play="videoPlay(index)" @fullscreenchange="videoFullscreen" :poster="item.snapshot" ></video>
  50. </view>
  51. <view class="solids" @tap="ChooseMedia(item)">
  52. <text class='cuIcon-order' style="transform:rotate(90deg);" v-if="item.url"></text>
  53. <text class='cuIcon-cameraadd' v-else></text>
  54. </view>
  55. </view>
  56. </view>
  57. <view class="cu-form-group">
  58. <view class="title">
  59. <text style="color: #E54D42;">*</text>
  60. 题名
  61. </view>
  62. <input placeholder="请输入" v-model="item.fileName"></input>
  63. </view>
  64. <view class="cu-form-group align-start">
  65. <view class="title">
  66. <text style="color: #E54D42;">*</text>
  67. 文字说明
  68. </view>
  69. <textarea maxlength="-1" v-model="item.description" placeholder="请输入"></textarea>
  70. </view>
  71. <view class="cu-form-group">
  72. <view class="title">
  73. <text style="color: #E54D42;">*</text>
  74. 拍摄时间
  75. </view>
  76. <picker mode="date" :value="item.filmingTime" @change="DateChange" :data-index="index">
  77. <view class="picker" style="height: 100rpx;text-align: left;">
  78. {{item.filmingTime}}
  79. </view>
  80. </picker>
  81. </view>
  82. <view class="cu-form-group">
  83. <view class="title">
  84. <text style="color: #E54D42;">*</text>
  85. 拍摄者
  86. </view>
  87. <input placeholder="请输入" v-model="item.shooter"></input>
  88. </view>
  89. <view class="bg-gray padding-sm solid-top"></view>
  90. </view>
  91. </form>
  92. </view>
  93. </template>
  94. <script>
  95. import config from "../../core/api.js";
  96. export default {
  97. data() {
  98. return {
  99. type:0,//1:video,0:picture
  100. tagId:'',
  101. handle:'',
  102. nodeId:'',
  103. nodeName:'',
  104. userInfo:null,
  105. project:null,
  106. token : uni.getStorageSync("token"),
  107. url : config.api,
  108. fileList:[],
  109. // {
  110. // name:'',
  111. // fileName:'',
  112. // description:'',
  113. // shooter:'',
  114. // filmingTime:'',
  115. // url:'',
  116. // }
  117. videoContext:null,
  118. againNum:0,//重新上传次数
  119. upIng:false,
  120. }
  121. },
  122. computed:{
  123. upNum(){
  124. let num = 0;
  125. for (let i = 0; i < this.fileList.length; i++) {
  126. if(this.fileList[i].isUp){
  127. num++;
  128. }
  129. }
  130. return num + '/' + this.fileList.length;
  131. }
  132. },
  133. onLoad(e) {
  134. this.type=e.type;
  135. this.tagId=e.tagId;
  136. this.handle=e.handle;
  137. this.nodeId=e.nodeId;
  138. this.nodeName=e.nodeName;
  139. var userInfo = uni.getStorageSync("userInfo")
  140. if(userInfo){
  141. this.userInfo = userInfo;
  142. this.project = uni.getStorageSync("porject"+"_"+userInfo.id);
  143. }
  144. if(this.handle == 'add'){
  145. this.ChooseMediaList();
  146. }else if(this.handle == 'edit'){
  147. this.fileList = [];
  148. let fileList = uni.getStorageSync('medialist');
  149. //console.log(typeof fileList)
  150. fileList.forEach((item)=>{
  151. item.url = item.fileUrl;
  152. this.fileList.push(item);
  153. })
  154. uni.removeStorageSync('medialist');
  155. }
  156. },
  157. methods: {
  158. ViewImage(e) {
  159. if(this.type ==0){
  160. uni.previewImage({
  161. urls: [e.currentTarget.dataset.url],
  162. current: 0
  163. });
  164. }
  165. //console.log(e)
  166. },
  167. toplay(index){
  168. console.log(index)
  169. this.videoContext = uni.createVideoContext(('myVideo'+index));
  170. this.videoContext.play();
  171. },
  172. videoPlay(index){
  173. this.videoContext = uni.createVideoContext(('myVideo'+index));
  174. this.videoContext.requestFullScreen();
  175. },
  176. videoFullscreen(fullScreen, direction){
  177. if(!fullScreen.target.fullScreen){
  178. this.videoContext.pause();
  179. }
  180. },
  181. ChooseMedia(item){
  182. if(this.type == 0){
  183. this.ChooseImage(item);
  184. }else if(this.type == 1){
  185. this.ChooseVideo(item);
  186. }
  187. },
  188. ChooseImage(item) {
  189. uni.chooseImage({
  190. count: 1, //默认9
  191. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  192. sourceType: ['album', 'camera'], //从相册选择
  193. success: (res) => {
  194. // #ifdef APP-PLUS
  195. //console.log(res)
  196. if(res.tempFiles[0].size>1024*1024*15){
  197. this.$prompt.none('上传的单张图片大小不超过15MB!');
  198. return
  199. }
  200. // if(res.tempFiles[0].type.indexOf('image') == -1){
  201. // this.$prompt.none('请上传图片文件');
  202. // return
  203. // }
  204. let arr = res.tempFiles[0].path.split('/');
  205. arr = arr[arr.length-1].split('.');
  206. res.tempFiles[0].name = arr[0];
  207. item.url = res.tempFilePaths[0];
  208. item.name = res.tempFiles[0].name;
  209. item.file = res.tempFiles[0];
  210. if(!item.filmingTime){
  211. item.filmingTime = this.happenTimeFun(new Date());
  212. }
  213. if(!item.shooter){
  214. item.shooter = this.userInfo.userName;
  215. }
  216. item.fileUrl = '';
  217. this.getMD5(res.tempFilePaths[0],item);
  218. // #endif
  219. // #ifdef H5
  220. if(res.tempFiles[0].size>1024*1024*15){
  221. this.$prompt.none('上传的单张图片大小不超过15MB!');
  222. return
  223. }
  224. if(res.tempFiles[0].type.indexOf('image') == -1){
  225. this.$prompt.none('请上传图片文件');
  226. return
  227. }
  228. item.url = res.tempFilePaths[0];
  229. item.name = res.tempFiles[0].name;
  230. if(!item.filmingTime){
  231. item.filmingTime = this.happenTimeFun(new Date());
  232. }
  233. item.file = res.tempFiles[0];
  234. if(!item.shooter){
  235. item.shooter = this.userInfo.userName;
  236. }
  237. item.fileUrl = '';
  238. //this.getMD5(res.tempFilePaths[0],item);
  239. // #endif
  240. }
  241. });
  242. },
  243. ChooseVideo(item){
  244. uni.chooseVideo({
  245. count: 1,
  246. sourceType: ['album', 'camera'],
  247. success: (res) => {
  248. //console.log(res)
  249. // #ifdef APP-PLUS
  250. if(res.size>1024*1024*500){
  251. this.$prompt.none('上传的单个视频大小不超过500MB!');
  252. return
  253. }
  254. // if(res.tempFile.type.indexOf('video') == -1){
  255. // this.$prompt.none('请上传视频文件');
  256. // return
  257. // }
  258. let arr = res.tempFilePath.split('/');
  259. //console.log(arr)
  260. arr = arr[arr.length-1].split('.');
  261. res.name = arr[0];
  262. item.url = res.tempFilePath;
  263. item.name = res.name;
  264. item.file = res.tempFilePath;
  265. if(!item.filmingTime){
  266. item.filmingTime = this.happenTimeFun(new Date());
  267. }
  268. if(!item.shooter){
  269. item.shooter = this.userInfo.userName;
  270. }
  271. item.fileUrl = '';
  272. this.getMD5(res.tempFilePath,item);
  273. // #endif
  274. // #ifdef H5
  275. if(res.tempFile.size>1024*1024*500){
  276. this.$prompt.none('上传的单个视频大小不超过500MB!');
  277. return
  278. }
  279. if(res.tempFile.type.indexOf('video') == -1){
  280. this.$prompt.none('请上传视频文件');
  281. return
  282. }
  283. item.url = res.tempFilePath;
  284. item.name = res.name;
  285. if(!item.filmingTime){
  286. item.filmingTime = this.happenTimeFun(new Date());
  287. }
  288. item.file = res.tempFile;
  289. if(!item.shooter){
  290. item.shooter = this.userInfo.userName;
  291. }
  292. item.fileUrl = '';
  293. //this.getMD5(res.tempFilePath,item);
  294. // #endif
  295. }
  296. });
  297. },
  298. ChooseMediaList(){
  299. if(this.type == 0){
  300. this.ChooseImageList();
  301. }else if(this.type == 1){
  302. this.addNew();
  303. }
  304. },
  305. ChooseImageList() {
  306. uni.chooseImage({
  307. count: 9, //默认9
  308. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  309. sourceType: ['album', 'camera'], //从相册选择
  310. success: (res) => {
  311. //console.log(res)
  312. // #ifdef APP-PLUS
  313. res.tempFiles.forEach((tempFile,index,array)=>{
  314. if(tempFile.size>1024*1024*15){
  315. this.$prompt.none('上传的单张图片大小不超过15MB!');
  316. return
  317. }
  318. // if(tempFile.type.indexOf('image') == -1){
  319. // this.$prompt.none('请上传图片文件');
  320. // return
  321. // }
  322. let arr = tempFile.path.split('/');
  323. arr = arr[arr.length-1].split('.');
  324. tempFile.name = arr[0];
  325. this.addFileListOne();
  326. let item = this.fileList[this.fileList.length-1];
  327. item.url = res.tempFilePaths[index];
  328. item.name = tempFile.name;
  329. item.file = tempFile;
  330. if(!item.filmingTime){
  331. item.filmingTime = this.happenTimeFun(new Date());
  332. }
  333. if(!item.shooter){
  334. item.shooter = this.userInfo.userName;
  335. }
  336. item.fileUrl = '';
  337. this.getMD5(res.tempFilePaths[index],item);
  338. })
  339. // #endif
  340. // #ifdef H5
  341. res.tempFiles.forEach((tempFile,index,array)=>{
  342. if(tempFile.size>1024*1024*15){
  343. this.$prompt.none('上传的单张图片大小不超过15MB!');
  344. return
  345. }
  346. if(tempFile.type.indexOf('image') == -1){
  347. this.$prompt.none('请上传图片文件');
  348. return
  349. }
  350. this.addFileListOne();
  351. let item = this.fileList[this.fileList.length-1];
  352. item.url = res.tempFilePaths[index];
  353. item.name = tempFile.name;
  354. if(!item.filmingTime){
  355. item.filmingTime = this.happenTimeFun(new Date());
  356. }
  357. item.file = tempFile;
  358. if(!item.shooter){
  359. item.shooter = this.userInfo.userName;
  360. }
  361. item.fileUrl = '';
  362. //this.getMD5(res.tempFilePaths[0],item);
  363. })
  364. // #endif
  365. }
  366. });
  367. },
  368. DateChange(e) {
  369. this.fileList[e.currentTarget.dataset.index].filmingTime = e.detail.value;
  370. },
  371. //检测文件和数据是否填写
  372. checkData(){
  373. if(this.fileList.length <1){
  374. this.$prompt.none('请上传上传至少一个文件');
  375. return false;
  376. }
  377. for (var i = 0; i < this.fileList.length; i++) {
  378. if(!(this.fileList[i].file || this.fileList[i].fileUrl)){
  379. this.$prompt.none('请上传第'+(i+1)+'条的文件');
  380. return false;
  381. }
  382. if(!this.fileList[i].fileName || !this.fileList[i].description || !this.fileList[i].shooter || !this.fileList[i].filmingTime){
  383. this.$prompt.none('请完成第'+(i+1)+'条文件信息填写');
  384. return false;
  385. }
  386. }
  387. return true;
  388. },
  389. uploadClick(){
  390. if(!this.checkData()){
  391. return false;
  392. }
  393. uni.getNetworkType({
  394. success: (res)=>{
  395. if(res.networkType != 'wifi'){
  396. uni.showModal({
  397. title: '网络检测',
  398. content: '当前非wifi网络是否继续?',
  399. success:(confirm) => {
  400. if (confirm.confirm) {
  401. //console.log('用户点击确定');
  402. this.toUpLoad();
  403. }
  404. }
  405. });
  406. }else{
  407. this.toUpLoad();
  408. }
  409. }
  410. });
  411. },
  412. toUpLoad(){
  413. //console.log('uploadfile')
  414. this.uploadfile(0);
  415. uni.navigateBack({
  416. delta: 1
  417. });
  418. },
  419. uploadfile(i){
  420. if(i >= this.fileList.length){
  421. //数字超出认为上传完成
  422. this.upIng = false;
  423. this.http.request('/app/multimedia/countFileN', {
  424. id:this.tagId
  425. },null,true).then((result)=>{
  426. if(result.result == 1){
  427. console.log("更新文件数量成功");
  428. uni.$emit('uploadComplete','');
  429. uni.showModal({
  430. title: '提示',
  431. content: '影像资料上传完成',
  432. showCancel:false,
  433. });
  434. //this.$prompt.none('文件上传结束');
  435. }
  436. })
  437. return;
  438. }
  439. this.upIng = true;
  440. let file = this.fileList[i];
  441. //console.log('uploadFile')
  442. let obj = { id:file.id?file.id:'',
  443. fileName:file.fileName,
  444. description:file.description,
  445. shooter:file.shooter,
  446. fileSize:file.fileSize,
  447. filmingTime:file.filmingTime,
  448. tagId:this.tagId,
  449. fileUrl:file.fileUrl?file.fileUrl:'',
  450. fileType:this.type,
  451. md5:file.md5,
  452. }
  453. if(this.nodeId){
  454. obj.nodeIdMedia = this.nodeId;
  455. obj.nodeName = this.nodeName;
  456. }
  457. uni.uploadFile({
  458. url:this.url+ '/app/multimedia/uploadfile',
  459. filePath: file.fileUrl?'':file.url,
  460. name: 'file',
  461. header:{"Authorization": this.token,"StoreName":uni.getStorageSync("storeName")},
  462. formData:obj,
  463. success:(res)=>{
  464. //console.log(res)
  465. var data = JSON.parse(res.data);
  466. if(data.result=="1"){
  467. //上传成功
  468. this.$set(this.fileList[i],'isUp',true);
  469. //console.log(data)
  470. this.fileList[i].id = data.datas[0].id;
  471. this.fileList[i].fileUrl = data.datas[0].fileUrl;
  472. this.fileList[i].fileSize = data.datas[0].fileSize;
  473. this.fileList[i].file = '';
  474. setTimeout(()=>{
  475. this.uploadfile(i+1);
  476. },10)
  477. //this.uploadfile(i+1);
  478. }else{
  479. this.$prompt.none('第'+(i+1)+'条的文件上传失败');
  480. if(this.againNum <3){
  481. this.uploadfile(i);
  482. }else{
  483. this.againNum = 0;
  484. this.uploadfile(i+1);
  485. }
  486. }
  487. },
  488. });
  489. },
  490. addNew(){
  491. this.addFileListOne();
  492. let index = this.fileList.length-1;
  493. this.ChooseMedia(this.fileList[index]);
  494. },
  495. addFileListOne(){
  496. this.fileList.push({
  497. name:'',
  498. fileName:'',
  499. description:'',
  500. shooter:'',
  501. filmingTime:'',
  502. url:'',
  503. })
  504. let index = this.fileList.length-1;
  505. if(this.nodeName){
  506. this.fileList[index].fileName = this.nodeName+(this.type==1?'施工视频':'施工照片')
  507. }
  508. },
  509. delFile(index){
  510. this.fileList.splice(index,1);
  511. },
  512. getMD5(tempFilePaths,item){
  513. plus.io.getFileInfo({
  514. filePath: tempFilePaths,
  515. digestAlgorithm: 'md5',
  516. success: res => {
  517. item.md5 = res.digest;
  518. //console.log(item.md5)
  519. this.checkMD5(item.md5,item);
  520. }
  521. })
  522. },
  523. checkMD5(md5,item){
  524. this.http.request('/app/multimedia/isExist', {
  525. md5:md5
  526. }).then((result)=>{
  527. if(json.result === "1"){
  528. item.fileUrl = json.data.fileUrl;
  529. item.url = json.data.fileUrl;
  530. item.fileSize = json.data.fileSize;
  531. item.file = null;
  532. }
  533. })
  534. },
  535. happenTimeFun(num){//时间戳数据处理
  536. let date = new Date(num);
  537. //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  538. let y = date.getFullYear();
  539. let MM = date.getMonth() + 1;
  540. MM = MM < 10 ? ('0' + MM) : MM;//月补0
  541. let d = date.getDate();
  542. d = d < 10 ? ('0' + d) : d;//天补0
  543. // let h = date.getHours();
  544. // h = h < 10 ? ('0' + h) : h;//小时补0
  545. // let m = date.getMinutes();
  546. // m = m < 10 ? ('0' + m) : m;//分钟补0
  547. // let s = date.getSeconds();
  548. // s = s < 10 ? ('0' + s) : s;//秒补0
  549. // return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s;
  550. return y + '-' + MM + '-' + d;
  551. },
  552. },
  553. }
  554. </script>
  555. <style>
  556. .title-font{
  557. font-size: 30rpx;
  558. padding: 20rpx;
  559. }
  560. .color-black{
  561. color: #333333;
  562. }
  563. .icon-size{
  564. font-size: 60rpx;
  565. margin-right: 16rpx;
  566. }
  567. .cu-form-group .title {
  568. min-width: calc(4em + 40rpx);
  569. }
  570. .cu-btn{
  571. padding: 0px 5px !important;
  572. }
  573. .cuIcon-loading2{
  574. font-size: 40rpx;
  575. line-height: 70rpx;
  576. margin-left: 10rpx;
  577. margin-right: 8rpx;
  578. }
  579. .cuIcon-loading2:before {
  580. animation: cuIcon-spin 2s infinite linear;
  581. font-family: "cuIcon";
  582. display: inline-block;
  583. margin-right: 2px;
  584. }
  585. </style>