comSelectPicture.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. <template>
  2. <view>
  3. <u-popup v-model="allShow" mode="bottom" border-radius="20" @close="cancel">
  4. <view class="main text-center font-30 color-11 text-bold">
  5. <view v-for="(item,index) in list" :key="index" class="select-item"
  6. :class="[index+1!=list.length?'border':'']" @tap="sheetClick(item.type)">
  7. {{item.text}}
  8. </view>
  9. <view class="cancel-item" @tap="cancel">取 消</view>
  10. </view>
  11. </u-popup>
  12. <u-modal v-model="showS" :show-title="false" :show-cancel-button="true" confirm-color='#fc6035'
  13. confirm-text="设置" cancel-text="取消" :content="contentS" @confirm="seting"></u-modal>
  14. <u-toast ref="uToast" />
  15. </view>
  16. </template>
  17. <script>
  18. import permision from "@/js_sdk/wa-permission/permission.js"; //app设置权限
  19. export default {
  20. data() {
  21. return {
  22. list: [
  23. //选项数组
  24. {
  25. text: "图片",
  26. type: "image"
  27. },
  28. {
  29. text: "视频",
  30. type: "video"
  31. },
  32. ],
  33. last: false, //是否是最后一次选择
  34. crop: {}, //剪裁
  35. uploadFileType: "", //上传类型
  36. sourceType: "", //拍摄/相册
  37. popupShow: true,
  38. allShow: false,
  39. fileObj: {}, //oss上传参数
  40. expire: 0, //oss上传过期时间
  41. showS: false,
  42. contentS: "",
  43. };
  44. },
  45. props: {
  46. // 最大上传总数量
  47. maxLength: {
  48. type: Number,
  49. default: 9,
  50. },
  51. //最多上传视频总数量
  52. maxVideoLength: {
  53. type: Number,
  54. default: 1,
  55. },
  56. // 已上传的数组(用来判断最大上传数量限制)
  57. arr: {
  58. type: Array,
  59. default: () => {
  60. return []
  61. }
  62. },
  63. //已上传数量
  64. arrLength: {
  65. type: Number,
  66. default: 0,
  67. },
  68. //是否显示
  69. show: {
  70. type: Boolean,
  71. default: false,
  72. },
  73. //是否剪裁(仅支持APP)
  74. isCrop: {
  75. type: Boolean,
  76. default: false,
  77. },
  78. // 剪裁尺寸
  79. cropSize: {
  80. type: Object,
  81. default:()=>({width:750,height:550})
  82. },
  83. //是否压缩(仅支持APP)
  84. isZip: {
  85. type: Boolean,
  86. default: false,
  87. },
  88. // 默认上传类型(图片、视频)
  89. chooseUploadFileType: {
  90. type: String,
  91. default: "",
  92. },
  93. //是否只能拍照
  94. isCamera: {
  95. type: Boolean,
  96. default: false,
  97. },
  98. //是否限制时长
  99. isDuration: {
  100. type: Boolean,
  101. default: false,
  102. },
  103. // 最大视频时长
  104. maxDuration: {
  105. type: Number,
  106. default: 15,
  107. },
  108. // 是否限制上传大小
  109. isMaxSize: {
  110. type: Boolean,
  111. default: false,
  112. },
  113. // 最大上传视频大小 (单位MB)
  114. maxVideoSize: {
  115. type: Number,
  116. default: 50,
  117. },
  118. // 最大上传视频大小 (单位MB)
  119. maxImageSize: {
  120. type: Number,
  121. default: 5,
  122. },
  123. // 是否直传oss
  124. isOss: {
  125. type: Boolean,
  126. default: false,
  127. },
  128. PermissionsTime: ''
  129. },
  130. watch: {
  131. popupShow() {
  132. this.allShow = this.popupShow && this.show && !this.isCamera;
  133. },
  134. show() {
  135. this.allShow = this.popupShow && this.show && !this.isCamera;
  136. },
  137. isCamera() {
  138. this.allShow = this.popupShow && this.show && !this.isCamera;
  139. if (this.isCamera) {
  140. this.sourceType = "camera";
  141. this.uploadFileAll();
  142. }
  143. },
  144. },
  145. created() {
  146. if (this.chooseUploadFileType != "") {
  147. this.uploadFileType = this.chooseUploadFileType;
  148. this.last = true;
  149. this.list = [{
  150. text: "从手机相册选择",
  151. type: "album"
  152. },
  153. {
  154. text: "拍摄",
  155. type: "camera"
  156. },
  157. ];
  158. }
  159. },
  160. mounted() {
  161. },
  162. methods: {
  163. //获取权限,APP嵌入H5时使用,由App返回权限回来
  164. getHPermissions() {
  165. this.uploadFileAll();
  166. // let that = this
  167. // if (that.sourceType == "camera") {
  168. // OpenCamera.postMessage('123');
  169. // if (uni.getStorageSync('cameraPermissionsTime') === 0) {
  170. // // this.PermissionsTime = 1
  171. // uni.setStorageSync('cameraPermissionsTime', 1)
  172. // } else {
  173. // window.cameraResult = function(res) { //res 原生APP给的数据
  174. // // this.$api.msg(res)
  175. // if (res != 4) {
  176. // that.contentS = "需要访问相机,请到设置里开启。";
  177. // that.showS = true;
  178. // } else {
  179. // that.uploadFileAll();
  180. // }
  181. // }
  182. // }
  183. // } else {
  184. // that.uploadFileAll();
  185. // }
  186. },
  187. // 判断权限 (仅支持APP)
  188. async getPermissionsState() {
  189. // this.uploadFileAll();
  190. console.log("App");
  191. // const checkPermission = uni.requireNativePlugin("CL-Permission"); //付费权限判断
  192. let that = this;
  193. if (that.sourceType == "camera") {
  194. //检测相机权限;
  195. // checkPermission.checkCamera((result) => {
  196. // // console.log(result)
  197. // if (result.code == 1) {
  198. // that.cameraState = true;
  199. // } else {
  200. // that.cameraState = false;
  201. // }
  202. // if (that.cameraState) {
  203. // that.uploadFileAll();
  204. // } else {
  205. // that.contentS = "需要访问相机,请到设置里开启";
  206. // that.showS = true;
  207. // }
  208. // });
  209. if (plus.os.name == "Android") {
  210. let cameraGetResult = await permision.requestAndroidPermission("android.permission.CAMERA"); //摄像头权限
  211. if (cameraGetResult == 1) {
  212. // "已获得授权";
  213. that.cameraState = true;
  214. } else {
  215. // "未获得授权";
  216. that.cameraState = false;
  217. }
  218. if (that.cameraState) {
  219. that.uploadFileAll();
  220. } else {
  221. that.contentS = "需要访问相机,请到设置里开启";
  222. that.showS = true;
  223. }
  224. } else if (plus.os.name == "iOS") {
  225. that.cameraState = await permision.judgeIosPermission("camera")
  226. if (that.cameraState) {
  227. that.uploadFileAll();
  228. } else {
  229. that.contentS = "需要访问相机,请到设置里开启";
  230. that.showS = true;
  231. }
  232. }
  233. } else {
  234. if (plus.os.name == "Android") {
  235. let photoLibraryGetResult = await permision.requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE"); //相册读取权限
  236. if (photoLibraryGetResult == 1) {
  237. // "已获得授权";
  238. that.photoLibraryGetState = true;
  239. } else {
  240. // "未获得授权";
  241. that.photoLibraryGetState = false;
  242. }
  243. if (that.photoLibraryGetState) {
  244. that.uploadFileAll();
  245. } else {
  246. that.contentS = "需要访问存储权限,请到设置里开启";
  247. that.showS = true;
  248. }
  249. } else if (plus.os.name == "iOS") {
  250. that.photoLibraryGetState = await permision.judgeIosPermission("photoLibrary")
  251. if (that.photoLibraryGetState) {
  252. that.uploadFileAll();
  253. } else {
  254. that.contentS = "需要访问存储权限,请到设置里开启";
  255. that.showS = true;
  256. }
  257. // checkPermission.checkPhotos((result) => {
  258. // // console.log(result)
  259. // if (result.code == 1) {
  260. // that.photoLibraryGetState = true;
  261. // } else {
  262. // that.photoLibraryGetState = false;
  263. // }
  264. // if (that.photoLibraryGetState) {
  265. // that.uploadFileAll();
  266. // } else {
  267. // that.contentS = "需要访问存储权限,请到设置里开启";
  268. // that.showS = true;
  269. // }
  270. // });
  271. }
  272. }
  273. },
  274. // 去设置页面设置权限
  275. seting() {
  276. // #ifdef H5
  277. // APP嵌入H5时使用,通知app跳转设置页面
  278. OpenCamera.postMessage('setting');
  279. window.cameraPermission = function(res) { //res 原生APP给的数据
  280. }
  281. // #endif
  282. // #ifdef APP-PLUS
  283. permision.gotoAppPermissionSetting();
  284. // #endif
  285. },
  286. // 取消
  287. cancel() {
  288. if (this.chooseUploadFileType == "") {
  289. this.last = false;
  290. this.list = [{
  291. text: "图片",
  292. type: "image"
  293. },
  294. {
  295. text: "视频",
  296. type: "video"
  297. },
  298. ];
  299. }
  300. this.$emit("cancelSheet");
  301. },
  302. // 选择上传类型,方式
  303. sheetClick(type) {
  304. if (this.last) {
  305. this.sourceType = type;
  306. // #ifdef H5
  307. this.getHPermissions()
  308. // #endif
  309. // #ifdef APP-PLUS
  310. this.getPermissionsState(); //判断权限
  311. // #endif
  312. } else if (!this.last) {
  313. this.uploadFileType = type;
  314. }
  315. this.last = true;
  316. this.list = [{
  317. text: "从手机相册选择",
  318. type: "album"
  319. },
  320. {
  321. text: "拍摄",
  322. type: "camera"
  323. },
  324. ];
  325. },
  326. //上传文件
  327. uploadFileAll() {
  328. if (this.uploadFileType == "image") {
  329. this.chooseImages();
  330. } else if (this.uploadFileType == "video") {
  331. // 选择视频时,图片和视频存在同一个数组Arr里,判断是否已经上传过视频,是否最多只能上传一个视频
  332. if(this.arr.length>0) {
  333. let num = 0
  334. this.arr.forEach(item => {
  335. if (item.type == '002') {
  336. num+=1
  337. }
  338. });
  339. if(this.maxVideoLength>1) {
  340. if(num>=this.maxVideoLength) {
  341. this.$refs.uToast.show({
  342. title: `最多只能上传${this.maxVideoLength}个视频`,
  343. duration: 1500,
  344. })
  345. } else {
  346. this.chooseVideo();
  347. }
  348. } else {
  349. if(num!=0) {
  350. this.$refs.uToast.show({
  351. title: "最多只能上传1个视频",
  352. duration: 1500,
  353. })
  354. } else {
  355. this.chooseVideo();
  356. }
  357. }
  358. } else {
  359. this.chooseVideo();
  360. }
  361. }
  362. },
  363. //上传图片
  364. chooseImages() {
  365. let that = this;
  366. if (that.sourceType == "camera" &&that.isCrop) {
  367. that.crop =that.cropSize;
  368. } else {
  369. that.crop = {};
  370. }
  371. let count = that.maxLength - that.arrLength
  372. uni.chooseImage({
  373. count: count, //默认9
  374. sourceType: [that.sourceType], //从相册选择/拍摄
  375. crop: that.crop,
  376. success: (res) => {
  377. // console.log(this.Arr, res)
  378. if (this.arrLength + res.tempFiles.length > this.maxLength) {
  379. that.$refs.uToast.show({
  380. title: `最多上传${this.maxLength}张图片`,
  381. duration: 1500,
  382. })
  383. return
  384. } else if(res.size <= (that.maxImageSize* 1048576)) {
  385. that.$refs.uToast.show({
  386. title: `图片最大${this.maxImageSize}MB`,
  387. duration: 1500,
  388. })
  389. return
  390. } else {
  391. let igmFile = res.tempFiles;
  392. if(that.isOss) {
  393. igmFile.map((item) => {
  394. // that.uploadFile(item);
  395. let fileName = ""
  396. let date = new Date()
  397. let time = date.getTime()
  398. // 获取图片类型,拼接时间戳作为文件名称(H5直接获取,app另外获取)
  399. // #ifdef H5
  400. // console.log("h5");
  401. fileName = that.$timefilter(date,'YY/MM/DD') +'/'+ time+'.'+item.type.substring(item.type.lastIndexOf('/')+1) || 'jpg'
  402. // #endif
  403. // #ifdef APP-PLUS
  404. // console.log("App");
  405. uni.getImageInfo({
  406. src: item.path,
  407. success: function (image) {
  408. // console.log( image.type,'image.typeimage.type')
  409. fileName = that.$timefilter(date,'YY/MM/DD') +'/'+ time+'.'+image.type
  410. }
  411. });
  412. // #endif
  413. // console.log(fileName)
  414. that.getAliOssInfo(item.path,fileName)
  415. });
  416. } else {
  417. igmFile.map((item) => {
  418. that.uploadFile(item.path);
  419. });
  420. }
  421. that.cancel();
  422. }
  423. },
  424. fail(err) {
  425. if (that.isCamera) {
  426. that.$emit("cancelSheet");
  427. }
  428. // console.log('选择图片文件失败或取消')
  429. // that.cancel();
  430. // console.log(JSON.stringify(err))
  431. // that.$refs.uToast.show({
  432. // title: err,
  433. // duration: 1500,
  434. // })
  435. },
  436. });
  437. },
  438. //上传视频
  439. async chooseVideo() {
  440. let that = this;
  441. uni.chooseVideo({
  442. maxDuration: that.maxDuration,
  443. count: 1,
  444. sourceType: [that.sourceType],
  445. duration: that.maxDuration,
  446. compressed: true,
  447. success: (res) => {
  448. // uni.showModal({
  449. // content: res.duration,
  450. // showCancel: false
  451. // });
  452. console.log(res)
  453. if(that.isDuration&&(res.duration >= (that.maxDuration+1))) {
  454. that.$refs.uToast.show({
  455. title: `视频最多时长${that.maxDuration}s`,
  456. duration: 1500,
  457. })
  458. return
  459. } else if(res.size > (that.maxVideoSize* 1048576)) {
  460. that.$refs.uToast.show({
  461. title: `视频最大${maxVideoSize}MB`,
  462. duration: 1500,
  463. })
  464. } else {
  465. if(that.isOss) {
  466. // console.log(res)
  467. // console.log(res.tempFile.type)
  468. let fileName = ""
  469. let date = new Date()
  470. let time = date.getTime()
  471. // 获取视频类型,拼接时间戳作为文件名称(H5直接获取,app另外获取)
  472. // #ifdef H5
  473. // console.log("h5");
  474. fileName = that.$timefilter(date,'YY/MM/DD') +'/'+ time+'.'+res.tempFile.type.substring(res.tempFile.type.lastIndexOf('/')+1) || 'mp4'
  475. // #endif
  476. // #ifdef APP-PLUS
  477. // console.log("App");
  478. uni.getVideoInfo({
  479. src: res.tempFile,
  480. success: function (video) {
  481. // console.log( image.type,'image.typeimage.type')
  482. fileName = that.$timefilter(date,'YY/MM/DD') +'/'+ time+'.'+video.type
  483. }
  484. });
  485. // #endif
  486. // console.log(fileName)
  487. that.cancel();
  488. that.getAliOssInfo(res.tempFilePath,fileName)
  489. } else {
  490. // console.log(res)
  491. // console.log(res.tempFile.type)
  492. that.cancel();
  493. that.uploadFile(res.tempFilePath);
  494. }
  495. }
  496. },
  497. fail(err) {
  498. // that.$refs.uToast.show({
  499. // title: err,
  500. // duration: 1500,
  501. // })
  502. },
  503. });
  504. },
  505. //获取oss信息
  506. async getAliOssInfo(upfile,fileName) {
  507. // console.log(fileName,'fileName')
  508. let that = this
  509. let time = new Date().getTime()
  510. let resultUrl = ""
  511. let zip = "";
  512. try {
  513. if(time > Number(that.expire)) {
  514. const res = await that.$u.api.ossInfo()
  515. // console.log(res)
  516. if(res.code==200) {
  517. that.fileObj.dir = res.data.dir
  518. that.fileObj.host = res.data.host
  519. that.fileObj.policy = res.data.policy
  520. that.fileObj.signature = res.data.signature
  521. that.fileObj.accessid = res.data.accessid
  522. that.expire = res.data.expire * 1000
  523. // console.log(that.$timefilter('YY-MM-DD HH:MM:SS',that.expire))
  524. // console.log(that.fileObj,'pppoooo')
  525. } else {
  526. that.$api.msg('获取上传信息失败')
  527. }
  528. }
  529. let filePath = "";
  530. // #ifdef H5
  531. // console.log("h5");
  532. filePath = upfile;
  533. // #endif
  534. // #ifdef APP-PLUS
  535. // console.log("App压缩");
  536. if(that.isZip) {
  537. if (that.uploadFileType == "image") {
  538. zip = await that.compress(upfile);
  539. } else if (that.uploadFileType == "video") {
  540. zip = await that.videoCompress(upfile); // await--上传图片之前先压缩,否则上传会比压缩先执行
  541. }
  542. filePath = zip;
  543. } else {
  544. filePath = upfile;
  545. }
  546. // #endif
  547. uni.showLoading({
  548. mask: true,
  549. title: "上传中...",
  550. });
  551. resultUrl = await that.uploadOss(filePath,fileName);
  552. // console.log(resultUrl,'resultUrl=resultUrl=resultUrl')
  553. if(that.uploadFileType=='image') {
  554. const obj = {
  555. kind: "image",
  556. url: resultUrl,
  557. type: '001'
  558. }
  559. that.$emit("addProperties", obj);
  560. } else if(that.uploadFileType=='video') {
  561. const obj = {
  562. kind: "video",
  563. url: resultUrl,
  564. faceImg: resultUrl+'?x-oss-process=video/snapshot,t_1000,f_jpg,w_0,h_0,m_fast,ar_auto',
  565. type: '002'
  566. }
  567. that.$emit("addProperties", obj);
  568. }
  569. uni.hideLoading();
  570. } catch (error) {
  571. uni.hideLoading();
  572. that.$refs.uToast.show({
  573. title: "上传失败",
  574. duration: 1000,
  575. })
  576. }
  577. },
  578. //直接上传oss
  579. async uploadOss(filePath,fileName) {
  580. let that = this
  581. let claims = uni.getStorageSync("claims");
  582. // console.log(that.fileObj,'mmmmmmm')
  583. // console.log(fileName,'+fileName')
  584. let [err, result] = await uni.uploadFile({
  585. url: that.fileObj.host, //阿里云的存储地址,可以后端返回给你,也可以前端自己写上
  586. filePath:filePath, //要上传的临时文件路径
  587. name: 'file',
  588. header:{
  589. Authorization: claims
  590. },
  591. formData: {
  592. key: that.fileObj.dir+'/'+fileName, //文件名
  593. policy: that.fileObj.policy, //后台获取超时时间
  594. OSSAccessKeyId: that.fileObj.accessid, //后台获取临时ID
  595. success_action_status: '200', //让服务端返回200,不然,默认会返回204
  596. signature: that.fileObj.signature //后台获取签名
  597. },
  598. })
  599. if (err) {
  600. // 上传错误了。。。可以做点什么
  601. } else {
  602. let url = that.fileObj.host+'/'+that.fileObj.dir+'/'+fileName
  603. // console.log(url)
  604. return url //上传成功后,将我们所自定义的key(文件路径名)返回出去
  605. }
  606. },
  607. //上传
  608. async uploadFile(upfile) {
  609. let that = this;
  610. let zip = "";
  611. let uploadUrl = "";
  612. uni.showLoading({
  613. mask: true,
  614. title: "上传中...",
  615. });
  616. if (this.uploadFileType == "image") {
  617. uploadUrl = `${this.$u.http.config.ossUrl}/common/uploadImage`;
  618. } else if (this.uploadFileType == "video") {
  619. uploadUrl = `${this.$u.http.config.ossUrl}/common/uploadVideo`;
  620. }
  621. let filePath = "";
  622. // #ifdef H5
  623. // console.log("h5");
  624. filePath = upfile;
  625. // #endif
  626. // #ifdef APP-PLUS
  627. // console.log("App压缩");
  628. if(that.isZip) {
  629. if (that.uploadFileType == "image") {
  630. zip = await that.compress(upfile);
  631. } else if (that.uploadFileType == "video") {
  632. zip = await that.videoCompress(upfile); // await--上传图片之前先压缩,否则上传会比压缩先执行
  633. }
  634. filePath = zip;
  635. } else {
  636. filePath = upfile;
  637. }
  638. // #endif
  639. console.log('upfile',filePath)
  640. if (that.uploadFileType == "image") {
  641. const obj = {
  642. kind: "image",
  643. url: filePath,
  644. type: '001'
  645. }
  646. that.$emit("addProperties", obj);
  647. } else if (that.uploadFileType == "video") {
  648. const obj = {
  649. kind: "video",
  650. url: filePath,
  651. faceImg: filePath+'?x-oss-process=video/snapshot,t_1000,f_jpg,w_0,h_0,m_fast,ar_auto',
  652. type: '002'
  653. }
  654. that.$emit("addProperties", obj);
  655. }
  656. uni.hideLoading();
  657. // console.log(filePath)
  658. // console.log(upfile)
  659. //开始上传,此处可先注释看压缩效果
  660. // uni.uploadFile({
  661. // // 需要上传的地址
  662. // url: uploadUrl,
  663. // filePath: filePath,
  664. // name: "file",
  665. // onUploadProgress: (res) => {
  666. // },
  667. // success: (res) => {
  668. // // console.log(res)
  669. // let Urls = JSON.parse(res.data);
  670. // // console.log(Urls);
  671. // if (Urls.code == 200) {
  672. // if (this.uploadFileType == "image") {
  673. // const obj = {
  674. // kind: "image",
  675. // url: Urls.url,
  676. // type: '001'
  677. // }
  678. // this.$emit("addProperties", obj);
  679. // } else if (this.uploadFileType == "video") {
  680. // const obj = {
  681. // kind: "video",
  682. // url: resultUrl,
  683. // faceImg: resultUrl+'?x-oss-process=video/snapshot,t_1000,f_jpg,w_0,h_0,m_fast,ar_auto',
  684. // type: '002'
  685. // }
  686. // this.$emit("addProperties", obj);
  687. // }
  688. // uni.hideLoading();
  689. // } else {
  690. // uni.hideLoading();
  691. // that.$refs.uToast.show({
  692. // title: Urls.msg,
  693. // duration: 1000,
  694. // })
  695. // }
  696. // },
  697. // fail: (err) => {
  698. // uni.hideLoading();
  699. // that.$refs.uToast.show({
  700. // title: "上传失败",
  701. // duration: 1000,
  702. // })
  703. // },
  704. // });
  705. },
  706. //压缩图片
  707. compress(img) {
  708. // console.log('开始压缩图片');
  709. let that = this;
  710. return new Promise((res) => {
  711. plus.io.resolveLocalFileSystemURL(
  712. img,
  713. (entry) => {
  714. //通过URL参数获取目录对象或文件对象
  715. entry.file((file) => {
  716. // 可通过entry对象操作图片
  717. // console.log('压缩前图片信息:' + JSON.stringify(file)); //压缩前图片信息
  718. if (file.size > 521000) {
  719. plus.zip.compressImage({
  720. src: img, //src: 压缩原始图片的路径
  721. dst: img
  722. .replace(".png", "fl.png")
  723. .replace(".PNG", "fl.PNG")
  724. .replace(".jpg", "fl.jpg")
  725. .replace(".JPG", "fl.JPG"),
  726. // width: '50%', //dst: (String 类型 )压缩转换目标图片的路径,这里先在后面原始名后面加一个区分一下
  727. // height: '50%', //width,height: (String 类型 )缩放图片的宽度,高度
  728. quality: 30, //quality: (Number 类型 )压缩图片的质量
  729. overwrite: true, //overwrite: (Boolean 类型 )覆盖生成新文件
  730. format: "jpg", //format: (String 类型 )压缩转换后的图片格式
  731. },
  732. (event) => {
  733. // console.log('压缩后图片信息:' + JSON.stringify(event));// 压缩后图片信息
  734. this.imageInfo = event;
  735. let newImg = event.target;
  736. if (event.size < 1048576) {
  737. res(newImg);
  738. } else {
  739. //如果大于1mb
  740. uni.hideLoading();
  741. that.$refs.uToast.show({
  742. title: "图片过大",
  743. duration: 1000,
  744. })
  745. }
  746. //返回新的图片地址,在uploadFile之前接收
  747. },
  748. function(err) {
  749. // console.log('Resolve file URL failed: ' + err.message);
  750. uni.hideLoading();
  751. that.$refs.uToast.show({
  752. title: err.message,
  753. duration: 1000,
  754. })
  755. // that.cancel();
  756. }
  757. );
  758. } else {
  759. res(img);
  760. uni.hideLoading();
  761. }
  762. });
  763. },
  764. (e) => {
  765. // 返回错误信息
  766. // console.log('Resolve file URL failed: ' + e.message);
  767. that.$refs.uToast.show({
  768. title: e.message,
  769. duration: 1000,
  770. })
  771. uni.hideLoading();
  772. // that.cancel();
  773. }
  774. );
  775. });
  776. },
  777. //压缩视频
  778. videoCompress(video) {
  779. // console.log('开始压缩视频');
  780. let that = this;
  781. return new Promise((res) => {
  782. plus.io.resolveLocalFileSystemURL(
  783. video,
  784. (entry) => {
  785. entry.file((file) => {
  786. // console.log('压缩前视频信息:' + JSON.stringify(file));
  787. if (file.size > 1048576) { //h5+ (app)压缩
  788. plus.zip.compressVideo({
  789. src: video,
  790. overwrite: true,
  791. filename: '_doc/'
  792. }, (event) => {
  793. // console.log('压缩后视频信息:' + JSON.stringify(event));
  794. let newVideo = event.tempFilePath;
  795. res(newVideo);
  796. uni.hideLoading();
  797. }, function(err) {
  798. uni.hideLoading();
  799. that.$refs.uToast.show({
  800. title: "压缩视频失败",
  801. duration: 1000,
  802. })
  803. // console.log('压缩视频失败',err)
  804. that.cancel();
  805. });
  806. } else {
  807. // console.log('不用压缩')
  808. res(video);
  809. uni.hideLoading();
  810. }
  811. });
  812. },
  813. (e) => {
  814. // 返回错误信息
  815. // console.log("Resolve file URL failed: " + e.message);
  816. uni.hideLoading();
  817. that.$refs.uToast.show({
  818. title: e.message,
  819. duration: 1000,
  820. })
  821. }
  822. );
  823. });
  824. },
  825. },
  826. };
  827. </script>
  828. <style lang="less" scoped>
  829. /deep/ .u-drawer__scroll-view {
  830. padding-bottom: 28rpx;
  831. }
  832. .select-item {
  833. height: 120rpx;
  834. line-height: 120rpx;
  835. }
  836. .cancel-item {
  837. height: 98rpx;
  838. line-height: 98rpx;
  839. width: 640rpx;
  840. margin-left: 55rpx;
  841. background: #f4f4f4;
  842. border-radius: 50rpx;
  843. }
  844. .border {
  845. border-bottom: 2rpx solid #f4f4f4;
  846. }
  847. </style>