imgUpload.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <view class="w-100">
  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="@/static/icon/delete.png"></image>
  8. </view>
  9. <view class="uploading flex_xy_center" v-if="closeLoading&&startUpload">
  10. <view><image src="@/static/icon/uploading4.png"></image>
  11. <view class="upload-txt">上传中...</view></view>
  12. </view>
  13. </view>
  14. <view v-if="imgArray.length<imgCount" class="upload-img-view flex_xy_center" @click="upPhoto">
  15. <image src="@/static/icon/jia.png"></image>
  16. </view>
  17. </view>
  18. <view v-if="!closeTip" class="tip">* 最多上传{{imgCount}}张图片(<label> {{imgArray.length}} </label>/{{imgCount}}) <text style="margin-left: 5rpx;">,安卓手机需单张上传</text></view>
  19. </view>
  20. </template>
  21. <script>
  22. // import $config from "@/appConfig.js"; //调用url前缀 *注意修改为自己的!!!
  23. import {$config } from '@/api/ask.js';
  24. export default {
  25. name: 'imgUpload',
  26. props: {
  27. imgArr: { //图片数组
  28. type: [Array],
  29. },
  30. uploadImgCount: { //一次上传图片数
  31. type: String,
  32. default: '6'
  33. },
  34. imgCount: { //可上传图片总数
  35. type: String,
  36. default: '6'
  37. },
  38. imgSize: { //图片大小 单位M
  39. type: Number,
  40. default: 40
  41. },
  42. imgType: { //如果是小程序,这个值则没用作用
  43. type: [Array],
  44. default: function() {
  45. return ['jpeg', 'png', 'jpg']
  46. }
  47. },
  48. closeTip: {
  49. type: Boolean,
  50. default: false
  51. },
  52. closeLoading: {
  53. type: Boolean,
  54. default: true
  55. },
  56. },
  57. data() {
  58. return {
  59. imgArray: [],
  60. canUpCount:'',
  61. startUpload:false,
  62. }
  63. },
  64. created() {
  65. this.imgArray = this.imgArr;
  66. this.canUpCount = Number(this.uploadImgCount);
  67. },
  68. watch: {
  69. imgArr(n,o){
  70. this.imgArray = n;
  71. },
  72. },
  73. methods: {
  74. upPhoto() {
  75. let that = this;
  76. if (Number(that.imgCount - that.imgArray.length) < Number(that.uploadImgCount)) {
  77. that.canUpCount = Number(that.imgCount - that.imgArray.length);
  78. }
  79. uni.chooseImage({
  80. count: Number(that.canUpCount),
  81. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  82. sourceType: ['album'], //从相册选择
  83. success: function(res) {
  84. if (res) {
  85. if (res.tempFiles) {
  86. for (let item of res.tempFiles) {
  87. if (item.size > (that.imgSize * 1024 * 1024)) {
  88. uni.showToast({
  89. title: `图片不能大于${that.imgSize}M`,
  90. icon: 'none'
  91. })
  92. return false;
  93. }
  94. if(item.type){
  95. let r = that.imgType.some(v => {
  96. let type = item.type.split('/');
  97. if (type.length)
  98. return (v === type[1]);
  99. });
  100. if (!r) {
  101. uni.showToast({
  102. title: `只允许上传${that.imgType}的类型`,
  103. icon: 'none'
  104. })
  105. return false;
  106. }
  107. }
  108. }
  109. }
  110. that.imgArray = [...that.imgArray, ...res.tempFilePaths];
  111. that.imgArray = that.imgArray.slice(0,6);
  112. // 开始上传图片
  113. // this.$refs.imgUpload.upload(res=>{
  114. // for(let i = 0 ;i<res.length;i++){
  115. // that.imgArray.push(res[i].data.thumb);
  116. // }
  117. // });
  118. }
  119. }
  120. });
  121. },
  122. preview(url, index) {
  123. // 预览图片
  124. uni.previewImage({
  125. urls: [url]
  126. });
  127. },
  128. delImg(i) {
  129. const _this = this;
  130. uni.showModal({
  131. title: '提示',
  132. content: '是否删除这张照片?',
  133. success: function(res) {
  134. if (res.confirm) {
  135. _this.imgArray.splice(i, 1);
  136. } else if (res.cancel) {}
  137. }
  138. });
  139. },
  140. upload(callback) {
  141. const _this = this;
  142. if (_this.imgArray.length) {
  143. this.startUpload=true;
  144. let successNum = 0;
  145. let urlArr = [];
  146. for (let item of _this.imgArray) {
  147. _this.uploadImg(item, res => {
  148. if (res.code == 0) {
  149. successNum++;
  150. urlArr.push(res.url);
  151. } else {
  152. urlArr.push(res);
  153. }
  154. if (urlArr.length == _this.imgArray.length) {
  155. if(successNum>0){
  156. this.startUpload=false;
  157. callback(_this.result(urlArr, successNum));
  158. }else{
  159. this.startUpload=false;
  160. callback(urlArr); //错误的返回
  161. }
  162. }
  163. });
  164. }
  165. } else {
  166. callback({
  167. result: 'warning',
  168. code: 400,
  169. msg: '请先上传图片!'
  170. });
  171. }
  172. },
  173. result(urlArr, successNum) {
  174. let result = {
  175. result: 'success',
  176. code: 0,
  177. urlArray: urlArr,
  178. success: successNum
  179. }
  180. return result;
  181. },
  182. uploadImg(item, callback) {
  183. const _this = this;
  184. console.log(item,'item');
  185. uni.uploadFile({
  186. url: 'http://mp.liuniu946.com/api/upload/image', //自行修改各自的对应的接口
  187. filePath: item,
  188. name: 'file',
  189. header: {
  190. "Authori-zation": 'Bearer ' + uni.getStorageSync('token')
  191. },
  192. success: (uploadFileRes) => {
  193. if (uploadFileRes) {
  194. let res = JSON.parse(uploadFileRes.data);
  195. callback(res);
  196. }
  197. },
  198. fail:(e)=>{
  199. callback({
  200. code:500,
  201. msg:'图片上传失败',
  202. localUrl:item,
  203. reason:e
  204. });
  205. }
  206. });
  207. }
  208. }
  209. }
  210. </script>
  211. <style scoped>
  212. .w-100 {
  213. width: 100%;
  214. }
  215. .flex {
  216. /* 转为弹性盒模型*/
  217. display: flex;
  218. }
  219. .flex_bet {
  220. /* 两端左右*/
  221. display: flex;
  222. justify-content: space-between;
  223. }
  224. .flex_wrap {
  225. /* 转为弹性盒模型并自动换行*/
  226. display: flex;
  227. flex-wrap: wrap;
  228. }
  229. .flex_xy_center{
  230. display: flex;
  231. justify-content: center;
  232. align-items: center;
  233. }
  234. .upload-img-view {
  235. height: 200upx;
  236. width: 32%;
  237. border-radius: 10upx;
  238. border: 4upx dotted #F1F1F1;
  239. /* background-color: #F1F1F1; */
  240. }
  241. .upload-img-view>image {
  242. width: 70upx;
  243. height: 70upx;
  244. }
  245. .upload-txt{
  246. font-size: 24upx;
  247. color: #FFFFFF;
  248. }
  249. .imgs-view {
  250. height: 200upx;
  251. width: 31.5%;
  252. border-radius: 10upx;
  253. margin-right: 1.8%;
  254. margin-bottom: 16upx;
  255. border: 1upx solid #F1F1F1;
  256. box-sizing: border-box;
  257. position: relative;
  258. }
  259. .uploading{
  260. position: absolute;
  261. background-color: rgba(0,0,0,.5);
  262. left: 0;
  263. top: 0;
  264. width: 100%;
  265. height: 100%;
  266. text-align: center;
  267. line-height: 100%;
  268. z-index: 999;
  269. }
  270. .uploading image{
  271. width: 60upx;
  272. height: 60upx;
  273. z-index: 1000;
  274. animation: rotation .6s linear infinite;
  275. -moz-animation: rotation .6s linear infinite;
  276. -webkit-animation: rotation .6s linear infinite;
  277. -o-animation: rotation .6s linear infinite;
  278. }
  279. @keyframes rotation {
  280. from {
  281. -webkit-transform: rotate(0deg);
  282. transform: rotate(0deg);
  283. -moz-transform: rotate(0deg);
  284. -o-transform: rotate(0deg);
  285. }
  286. to {
  287. -webkit-transform: rotate(360deg);
  288. transform: rotate(360deg);
  289. -moz-transform: rotate(360deg);
  290. -o-transform: rotate(360deg);
  291. }
  292. }
  293. .imgs-view>image {
  294. width: 100%;
  295. height: 100%;
  296. border-radius: 10upx;
  297. }
  298. .tip {
  299. font-size: 24upx;
  300. color: #FF0000;
  301. margin-top: 12upx;
  302. }
  303. .tip>label {
  304. color: #009100;
  305. }
  306. .del-btn {
  307. position: absolute;
  308. top: 0;
  309. right: 0;
  310. width: 32upx;
  311. height: 32upx;
  312. z-index: 999;
  313. }
  314. .del-btn>image {
  315. width: 100%;
  316. height: 100%;
  317. display: flex;
  318. }
  319. </style>