imgUpload.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <view class="w-100 imgupload">
  3. <view class="w-100 flex_wrap">
  4. <view class="imgs-view" v-for="(v,i) in imgArray" :mode="'aspectFill'" :key="i">
  5. <image @click="preview(v,i)" :src="v"></image>
  6. <view class="del-btn" @click="delImg(i)">
  7. <image :src="config.delIcon||'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA5UlEQVQ4T2NkQAP3fTXs//1njPvP8N+BkYFBCST9n4HhHiMD4wEmxv+LFDffOIishRGZc89HY9Z/BoZUdEPRNMxW2nIjDSYGN+COj8YORgYGd3yaYXL/GRh2qmy54QHigw0gxmZ0gxkZGMAuYYT4meEAMTajq2FiZHBgvOujOZeB4X8SOQYwMDDOY7zjo3EXFtqkGgKOnbs+Gv9hGsVKexm4jG3xmvPt7GGGV93FcDUoBvD7xTGwyijiNeD3k/sMHzctQhhABS9QGIgURyPFCQkWGhQlZZghxCRpWBLGyEwwAVKzMwB5V2gPAYXmLgAAAABJRU5ErkJggg=='"></image>
  8. </view>
  9. </view>
  10. <view v-if="imgArray.length<imgCount" class="upload-img-view flex_xy_center" @click="upPhoto">
  11. <image src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAArklEQVRIS2NkGCDAOED2Mgw9i9vb2xVAoVVZWfmAnFAj28ddXV33QRaWlZUp0tvi/1CLyXI8WZpAFnZ1dY1aTFJUjwY10cE1mriIDiqoQpTEBSoGmZmZ9zMwMICLQyqC08zMzDHFxcW3YGYODotJ8eFo4iIltEBqR0suokNsNHERHVTYSi5SNA9kUJ+CtrnMSHEw1iKTFAN6e3vVQOqRy19S9JOdj0mxBJvakWcxAFpiZB+QSLWnAAAAAElFTkSuQmCC"></image>
  12. </view>
  13. </view>
  14. <view v-if="!closeTip&&!tipObj.prompt" class="imgupload__tip">* 最多上传{{imgCount}}张图片(<label> {{imgArray.length}}
  15. </label>/{{imgCount}})</view>
  16. </view>
  17. </template>
  18. <script>
  19. import permision from "@/utils/permission.js"
  20. export default {
  21. name: 'imgUpload',
  22. props: {
  23. type:{
  24. type: String,
  25. default: 'other'
  26. },
  27. imgArr: { //图片数组
  28. type: [Array],
  29. },
  30. uploadImgCount: { //一次上传图片数
  31. type: String,
  32. default: '1'
  33. },
  34. imgCount: { //可上传图片总数
  35. type: String,
  36. default: '3'
  37. },
  38. imgSize: { //图片大小 单位M
  39. type: Number,
  40. default:4
  41. },
  42. formData: {
  43. type: Object,
  44. default: function() {
  45. return {}
  46. }
  47. },
  48. imgType: { //如果是小程序,这个值则没用作用
  49. type: [Array],
  50. default: function() {
  51. return ['jpeg', 'png', 'jpg']
  52. }
  53. },
  54. closeTip: {
  55. type: Boolean,
  56. default: false
  57. },
  58. loading: {
  59. type: Boolean,
  60. default: true
  61. },
  62. url: { //上传图片Url
  63. type: String,
  64. },
  65. async: {
  66. type: Boolean,
  67. default: false
  68. },
  69. header: {
  70. type: Array,
  71. default: function() {
  72. return []
  73. }
  74. },
  75. previewMany: { //多图预览
  76. type: Boolean,
  77. default: false
  78. },
  79. pressImg: { //压缩图片 H5暂不支持压缩api
  80. type: Number,
  81. default: -1
  82. },
  83. config: {
  84. type: Object,
  85. default: function() {
  86. return {
  87. delIcon: '', //删除图片icon
  88. resultTip: true, //结果提示
  89. resultType: '1', //结果展示类型
  90. loadIcon: '', //加载时的图标
  91. loadText: '' //加载时的文字
  92. }
  93. }
  94. }
  95. },
  96. data() {
  97. return {
  98. imgArray: [],
  99. canUpCount: '',
  100. startUpload: false,
  101. tipObj: {
  102. prompt: '',
  103. typeColor: '#009100',
  104. must: false, //必须要存在的时候
  105. success: '#009100', //成功-#009100; 可自定义修改
  106. warning: '#bb9300', // 警告 -#bb9300; 可自定义修改
  107. error: '#FF0000' // 失败--#FF0000; 可自定义修改
  108. },
  109. headers: {},
  110. curPlatform: '',
  111. currIndex: null,
  112. ossData:{},//oss参数
  113. }
  114. },
  115. created() {
  116. this.imgArray = this.imgArr || [];
  117. // this.formDataGet()
  118. this.canUpCount = Number(this.uploadImgCount);
  119. },
  120. watch: {
  121. imgArr(n, o) {
  122. this.imgArray = n || [];
  123. },
  124. imgCount(n, o) {
  125. this.uploadImgCount = n;
  126. this.canUpCount = Number(this.uploadImgCount);
  127. },
  128. },
  129. methods: {
  130. //上传凭证
  131. formDataGet(){
  132. let that = this
  133. uni.request({
  134. //url: 'https://pm.lidashine.com/get-oss-sign', //oss获取参数接口地址。
  135. url:this.$mConfig.imgURL,
  136. complete: (result) => {
  137. let res = JSON.parse(result.data.data);
  138. that.ossData = res
  139. }
  140. })
  141. },
  142. //预览图片
  143. preview(obj, index) {
  144. if (this.previewMany) {
  145. let urls = [];
  146. urls.push(obj.url)
  147. this.imgArray.map(item => {
  148. if (obj.id != item.id) {
  149. urls.push(item.url);
  150. }
  151. })
  152. uni.previewImage({
  153. urls: urls
  154. });
  155. } else {
  156. // 预览图片(单张)
  157. uni.previewImage({
  158. urls: [obj.url]
  159. });
  160. }
  161. },
  162. //删除图片
  163. delImg(i) {
  164. const _this = this;
  165. uni.showModal({
  166. title: '提示',
  167. content: '是否删除这张照片?',
  168. success: function(res) {
  169. if (res.confirm) {
  170. _this.imgArray.splice(i, 1);
  171. _this.$emit('result',_this.imgArray)
  172. } else if (res.cancel) {}
  173. }
  174. });
  175. },
  176. // 随机生成文件名
  177. random_string(len) {
  178. len = len || 32;
  179. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
  180. var maxPos = chars.length;
  181. var pwd = '';
  182. for (let i = 0; i < len; i++) {
  183. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  184. }
  185. return pwd;
  186. },
  187. //上传图片
  188. upPhoto() {
  189. let that = this;
  190. uni.showActionSheet({
  191. itemList: ['拍照上传', '从相册中选择'],
  192. async success(res) {
  193. if (res.tapIndex == 0) {
  194. // #ifdef APP-PLUS
  195. let flag = await permision.showAuthTipModal("android.permission.CAMERA")
  196. if (!flag) return
  197. // #endif
  198. uni.chooseImage({
  199. count: Number(that.canUpCount) - that.imgArray.length,
  200. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  201. sourceType: ['camera'], //从相册选择
  202. success: function(res) {
  203. if (res) {
  204. let filePath = res.tempFilePaths[0]
  205. let nameStr = that.random_string(8)+'.'+filePath.split('.').pop();
  206. if (res.tempFiles) {
  207. for (let item of res.tempFiles) {
  208. if (item.size > (that.imgSize * 1024 * 1024)) {
  209. uni.showToast({
  210. title: `图片不能大于${that.imgSize}M`,
  211. icon: 'none'
  212. })
  213. return false;
  214. }
  215. if (item.type) {
  216. let r = that.imgType.some(v => {
  217. let type = item.type.split('/');
  218. if (type.length)
  219. return (v === type[1]);
  220. });
  221. if (!r) {
  222. uni.showToast({
  223. title: `只允许上传${that.imgType}的类型`,
  224. icon: 'none'
  225. })
  226. return false;
  227. }
  228. }
  229. }
  230. }
  231. that.updataImg(filePath,nameStr)
  232. }
  233. },
  234. fail(fail){
  235. if ([0, 11, 12].includes(fail.code)) return;
  236. permision.showManualAuth('android.permission.CAMERA');
  237. },
  238. complete(e){
  239. console.log(e)
  240. }
  241. });
  242. } else if (res.tapIndex == 1) {
  243. // #ifdef APP-PLUS
  244. let flag = await permision.showAuthTipModal("android.permission.READ_EXTERNAL_STORAGE")
  245. if (!flag) return
  246. // #endif
  247. uni.chooseImage({
  248. count: Number(that.canUpCount) - that.imgArray.length,
  249. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  250. sourceType: ['album'], //从相册选择
  251. success: function(res) {
  252. if (res) {
  253. if (res.tempFiles) {
  254. for (let [index,item] of res.tempFiles.entries()) {
  255. if (item.size > (that.imgSize * 1024 * 1024)) {
  256. uni.showToast({
  257. title: `图片不能大于${that.imgSize}M`,
  258. icon: 'none'
  259. })
  260. return false;
  261. }
  262. if (item.type) {
  263. let r = that.imgType.some(v => {
  264. let type = item.type.split('/');
  265. if (type.length)
  266. return (v === type[1]);
  267. });
  268. if (!r) {
  269. uni.showToast({
  270. title: `只允许上传${that.imgType}的类型`,
  271. icon: 'none'
  272. })
  273. return false;
  274. }
  275. }
  276. let filePath =res.tempFilePaths[index]
  277. let nameStr = that.random_string(8)+'.'+res.tempFilePaths[index].split('.').pop();
  278. that.updataImg(filePath,nameStr)
  279. }
  280. }
  281. }
  282. },
  283. fail(fail){
  284. if ([0, 11, 12].includes(fail.code)) return;
  285. permision.showManualAuth('android.permission.READ_EXTERNAL_STORAGE');
  286. }
  287. });
  288. }
  289. },
  290. fail: function(res) {
  291. console.log(res.errMsg);
  292. }
  293. });
  294. },
  295. updataImg(filePath,nameStr){
  296. uni.showLoading();
  297. let date = new Date();
  298. let allData = date.getFullYear()+'-'+date.getMonth()+1+'-'+date.getDate();
  299. let shopid = uni.getStorageSync('shop')
  300. let id = 0
  301. if(!shopid){
  302. id=0
  303. }else{
  304. id=shopid.id
  305. }
  306. let key = 'img/'+id+'/'+this.type+'/'+allData+'/'+nameStr
  307. uni.uploadFile({
  308. // url: this.$mConfig.baseUrl+ '/minio/upload',
  309. url: this.$mConfig.baseUrl+ '/common/upload',
  310. filePath: filePath,
  311. fileType: 'image',
  312. name: 'file',
  313. success: (res) => {
  314. uni.hideLoading();
  315. if (res.statusCode != 200) {
  316. return;
  317. }
  318. let resObj = JSON.parse(res.data);
  319. let imgurl = resObj.data.url;
  320. this.imgArray.push(imgurl)
  321. this.$emit('result',this.imgArray)
  322. uni.showToast({
  323. title: '上传成功',
  324. icon: 'success',
  325. duration: 1000
  326. })
  327. // alert(ossRes.host+"/"+encodeURIComponent(fileName));
  328. },
  329. fail: (err) => {
  330. uni.hideLoading();
  331. console.log('uploadImage fail', err);
  332. uni.showModal({
  333. content: err.errMsg,
  334. showCancel: false
  335. });
  336. }
  337. });
  338. },
  339. }
  340. }
  341. </script>
  342. <style scoped>
  343. .w-100 {
  344. width: 100%;
  345. }
  346. .flex {
  347. /* 转为弹性盒模型*/
  348. display: flex;
  349. }
  350. .flex_bet {
  351. /* 两端左右*/
  352. display: flex;
  353. justify-content: space-between;
  354. }
  355. .flex_wrap {
  356. /* 转为弹性盒模型并自动换行*/
  357. display: flex;
  358. flex-wrap: wrap;
  359. }
  360. .flex_xy_center {
  361. display: flex;
  362. justify-content: center;
  363. align-items: center;
  364. }
  365. .upload-img-view {
  366. height: 200rpx;
  367. width: 32%;
  368. border-radius: 10rpx;
  369. border: 4rpx dotted #F1F1F1;
  370. /* background-color: #F1F1F1; */
  371. }
  372. .upload-img-view>image {
  373. width: 70rpx;
  374. height: 70rpx;
  375. }
  376. .upload-txt {
  377. font-size: 24rpx;
  378. color: #FFFFFF;
  379. }
  380. .imgs-view {
  381. height: 200rpx;
  382. width: 31.5%;
  383. border-radius: 10rpx;
  384. margin-right: 1.8%;
  385. margin-bottom: 16rpx;
  386. border: 1rpx solid #F1F1F1;
  387. box-sizing: border-box;
  388. position: relative;
  389. }
  390. .result {
  391. position: absolute;
  392. bottom: 0;
  393. width: 100%;
  394. height: 45rpx;
  395. font-size: 26rpx;
  396. left: 0;
  397. background-color: rgba(0, 0, 0, .6);
  398. text-align: center;
  399. line-height: 45rpx;
  400. border-bottom-left-radius: 10rpx;
  401. border-bottom-right-radius: 10rpx;
  402. }
  403. .result>.success {
  404. color: #00b900;
  405. }
  406. .result>.error {
  407. color: #b52e25;
  408. }
  409. .uploading {
  410. position: absolute;
  411. background-color: rgba(0, 0, 0, .5);
  412. left: 0;
  413. top: 0;
  414. width: 100%;
  415. height: 100%;
  416. text-align: center;
  417. line-height: 100%;
  418. z-index: 999;
  419. }
  420. .uploading image {
  421. width: 60rpx;
  422. height: 60rpx;
  423. z-index: 1000;
  424. animation: rotation .6s linear infinite;
  425. -moz-animation: rotation .6s linear infinite;
  426. -webkit-animation: rotation .6s linear infinite;
  427. -o-animation: rotation .6s linear infinite;
  428. }
  429. @keyframes rotation {
  430. from {
  431. -webkit-transform: rotate(0deg);
  432. transform: rotate(0deg);
  433. -moz-transform: rotate(0deg);
  434. -o-transform: rotate(0deg);
  435. }
  436. to {
  437. -webkit-transform: rotate(360deg);
  438. transform: rotate(360deg);
  439. -moz-transform: rotate(360deg);
  440. -o-transform: rotate(360deg);
  441. }
  442. }
  443. .imgs-view>image {
  444. width: 100%;
  445. height: 100%;
  446. border-radius: 10rpx;
  447. }
  448. .imgupload__tip {
  449. font-size: 24rpx;
  450. color: #FF0000;
  451. margin: 10rpx auto;
  452. }
  453. .imgupload__tip>label {
  454. color: #FA6138;
  455. }
  456. .del-btn {
  457. position: absolute;
  458. top: 0;
  459. right: 0;
  460. width: 32rpx;
  461. height: 32rpx;
  462. z-index: 98;
  463. }
  464. .del-btn>image {
  465. width: 100%;
  466. height: 100%;
  467. display: flex;
  468. }
  469. .css2 {
  470. border-style: solid;
  471. border-width: 0px 0px 100px 100px;
  472. border-color: transparent transparent blue transparent;
  473. width: 0px;
  474. height: 0px;
  475. }
  476. </style>