releaseImg.vue 11 KB

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