unloadimg.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" :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: '1'
  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. formDataGet(){
  130. let that = this
  131. uni.request({
  132. url: 'https://pm.lidashine.com/get-oss-sign', //oss获取参数接口地址。
  133. complete: (result) => {
  134. let res = JSON.parse(result.data.data);
  135. that.ossData = res
  136. }
  137. })
  138. },
  139. //预览图片
  140. preview(obj, index) {
  141. if (this.previewMany) {
  142. let urls = [];
  143. urls.push(obj.url)
  144. this.imgArray.map(item => {
  145. if (obj.id != item.id) {
  146. urls.push(item.url);
  147. }
  148. })
  149. uni.previewImage({
  150. urls: urls
  151. });
  152. } else {
  153. // 预览图片(单张)
  154. uni.previewImage({
  155. urls: [obj.url]
  156. });
  157. }
  158. },
  159. //删除图片
  160. delImg(i) {
  161. const _this = this;
  162. uni.showModal({
  163. title: '提示',
  164. content: '是否删除这张照片?',
  165. success: function(res) {
  166. if (res.confirm) {
  167. _this.imgArray.splice(i, 1);
  168. _this.$emit('result',_this.imgArray)
  169. } else if (res.cancel) {}
  170. }
  171. });
  172. },
  173. // 随机生成文件名
  174. random_string(len) {
  175. len = len || 32;
  176. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
  177. var maxPos = chars.length;
  178. var pwd = '';
  179. for (let i = 0; i < len; i++) {
  180. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  181. }
  182. return pwd;
  183. },
  184. //上传图片
  185. upPhoto() {
  186. let that = this;
  187. uni.showActionSheet({
  188. itemList: ['拍照上传', '从相册中选择'],
  189. success: function(res) {
  190. console.log(res)
  191. if (res.tapIndex == 0) {
  192. uni.chooseImage({
  193. count: Number(that.canUpCount),
  194. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  195. sourceType: ['camera'], //从相册选择
  196. success: function(res) {
  197. if (res) {
  198. let filePath = res.tempFilePaths[0]
  199. let nameStr = that.random_string(8)+'.'+res.tempFilePaths[0].split('.').pop();
  200. if (res.tempFiles) {
  201. for (let item of res.tempFiles) {
  202. if (item.size > (that.imgSize * 1024 * 1024)) {
  203. uni.showToast({
  204. title: `图片不能大于${that.imgSize}M`,
  205. icon: 'none'
  206. })
  207. return false;
  208. }
  209. if (item.type) {
  210. let r = that.imgType.some(v => {
  211. let type = item.type.split('/');
  212. if (type.length)
  213. return (v === type[1]);
  214. });
  215. if (!r) {
  216. uni.showToast({
  217. title: `只允许上传${that.imgType}的类型`,
  218. icon: 'none'
  219. })
  220. return false;
  221. }
  222. }
  223. }
  224. }
  225. that.updataImg(filePath,nameStr)
  226. }
  227. }
  228. });
  229. } else if (res.tapIndex == 1) {
  230. uni.chooseImage({
  231. count: Number(that.canUpCount),
  232. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  233. sourceType: ['album'], //从相册选择
  234. success: function(res) {
  235. if (res) {
  236. let filePath = res.tempFilePaths[0]
  237. console.log(filePath)
  238. let nameStr = that.random_string(8)+'.'+res.tempFilePaths[0].split('.').pop();
  239. if (res.tempFiles) {
  240. for (let item of res.tempFiles) {
  241. if (item.size > (that.imgSize * 1024 * 1024)) {
  242. uni.showToast({
  243. title: `图片不能大于${that.imgSize}M`,
  244. icon: 'none'
  245. })
  246. return false;
  247. }
  248. if (item.type) {
  249. let r = that.imgType.some(v => {
  250. let type = item.type.split('/');
  251. if (type.length)
  252. return (v === type[1]);
  253. });
  254. if (!r) {
  255. uni.showToast({
  256. title: `只允许上传${that.imgType}的类型`,
  257. icon: 'none'
  258. })
  259. return false;
  260. }
  261. }
  262. }
  263. }
  264. that.updataImg(filePath,nameStr)
  265. }
  266. }
  267. });
  268. }
  269. },
  270. fail: function(res) {
  271. console.log(res.errMsg);
  272. }
  273. });
  274. },
  275. updataImg(filePath,nameStr){
  276. console.log(filePath)
  277. console.log(nameStr)
  278. let date = new Date();
  279. let allData = date.getFullYear()+'-'+date.getMonth()+1+'-'+date.getDate()
  280. let ossData = this.ossData
  281. let shopid = uni.getStorageSync('shop')
  282. let id = 0
  283. if(!shopid){
  284. id=0
  285. }else{
  286. id=shopid.id
  287. }
  288. let key = 'img/'+id+'/'+this.type+'/'+allData+'/'+nameStr
  289. uni.uploadFile({
  290. url: ossData.host,
  291. filePath: filePath,
  292. fileType: 'image',
  293. name: 'file',
  294. formData: {
  295. 'name': nameStr,
  296. 'key': key,
  297. 'policy': ossData.policy,
  298. 'OSSAccessKeyId': ossData.accessid,
  299. 'success_action_status': '200',
  300. 'signature': ossData.signature,
  301. },
  302. success: (res) => {
  303. uni.hideLoading();
  304. if (res.statusCode != 200) {
  305. return;
  306. }
  307. let imgurl = ossData.host+'/'+key
  308. this.imgArray.push(imgurl)
  309. this.$emit('result',this.imgArray)
  310. uni.showToast({
  311. title: '上传成功',
  312. icon: 'success',
  313. duration: 1000
  314. })
  315. // alert(ossRes.host+"/"+encodeURIComponent(fileName));
  316. },
  317. fail: (err) => {
  318. uni.hideLoading();
  319. console.log('uploadImage fail', err);
  320. uni.showModal({
  321. content: err.errMsg,
  322. showCancel: false
  323. });
  324. }
  325. });
  326. },
  327. }
  328. }
  329. </script>
  330. <style scoped>
  331. .w-100 {
  332. width: 100%;
  333. }
  334. .flex {
  335. /* 转为弹性盒模型*/
  336. display: flex;
  337. }
  338. .flex_bet {
  339. /* 两端左右*/
  340. display: flex;
  341. justify-content: space-between;
  342. }
  343. .flex_wrap {
  344. /* 转为弹性盒模型并自动换行*/
  345. display: flex;
  346. flex-wrap: wrap;
  347. }
  348. .flex_xy_center {
  349. display: flex;
  350. justify-content: center;
  351. align-items: center;
  352. }
  353. .upload-img-view {
  354. height: 158rpx;
  355. width: 176rpx;
  356. border-radius: 10rpx;
  357. border: 4rpx dotted #F1F1F1;
  358. /* background-color: #F1F1F1; */
  359. }
  360. .upload-img-view>image {
  361. width: 70rpx;
  362. height: 70rpx;
  363. }
  364. .upload-txt {
  365. font-size: 24rpx;
  366. color: #FFFFFF;
  367. }
  368. .imgs-view {
  369. height: 158rpx;
  370. width: 176rpx;
  371. border-radius: 10rpx;
  372. margin-right: 1.8%;
  373. margin-bottom: 16rpx;
  374. border: 1rpx solid #F1F1F1;
  375. box-sizing: border-box;
  376. position: relative;
  377. }
  378. .result {
  379. position: absolute;
  380. bottom: 0;
  381. width: 100%;
  382. height: 45rpx;
  383. font-size: 26rpx;
  384. left: 0;
  385. background-color: rgba(0, 0, 0, .6);
  386. text-align: center;
  387. line-height: 45rpx;
  388. border-bottom-left-radius: 10rpx;
  389. border-bottom-right-radius: 10rpx;
  390. }
  391. .result>.success {
  392. color: #00b900;
  393. }
  394. .result>.error {
  395. color: #b52e25;
  396. }
  397. .uploading {
  398. position: absolute;
  399. background-color: rgba(0, 0, 0, .5);
  400. left: 0;
  401. top: 0;
  402. width: 100%;
  403. height: 100%;
  404. text-align: center;
  405. line-height: 100%;
  406. z-index: 999;
  407. }
  408. .uploading image {
  409. width: 60rpx;
  410. height: 60rpx;
  411. z-index: 1000;
  412. animation: rotation .6s linear infinite;
  413. -moz-animation: rotation .6s linear infinite;
  414. -webkit-animation: rotation .6s linear infinite;
  415. -o-animation: rotation .6s linear infinite;
  416. }
  417. @keyframes rotation {
  418. from {
  419. -webkit-transform: rotate(0deg);
  420. transform: rotate(0deg);
  421. -moz-transform: rotate(0deg);
  422. -o-transform: rotate(0deg);
  423. }
  424. to {
  425. -webkit-transform: rotate(360deg);
  426. transform: rotate(360deg);
  427. -moz-transform: rotate(360deg);
  428. -o-transform: rotate(360deg);
  429. }
  430. }
  431. .imgs-view>image {
  432. width: 100%;
  433. height: 100%;
  434. border-radius: 10rpx;
  435. }
  436. .imgupload__tip {
  437. font-size: 24rpx;
  438. color: #FF0000;
  439. margin: 10rpx auto;
  440. }
  441. .imgupload__tip>label {
  442. color: #009100;
  443. }
  444. .del-btn {
  445. position: absolute;
  446. top: 0;
  447. right: 0;
  448. width: 32rpx;
  449. height: 32rpx;
  450. z-index: 999;
  451. }
  452. .del-btn>image {
  453. width: 100%;
  454. height: 100%;
  455. display: flex;
  456. }
  457. .css2 {
  458. border-style: solid;
  459. border-width: 0px 0px 100px 100px;
  460. border-color: transparent transparent blue transparent;
  461. width: 0px;
  462. height: 0px;
  463. }
  464. </style>