upload-image.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="uni-file-picker__container">
  3. <view class="file-picker__box" v-for="(url, index) in list" :key="index" :style="boxStyle">
  4. <view class="file-picker__box-content" :style="borderStyle">
  5. <image
  6. class="file-image"
  7. :src="getImageUrl(url)"
  8. mode="aspectFill"
  9. @click.stop="previewImage(url, index)"
  10. ></image>
  11. <view v-if="delIcon && !readonly" class="icon-del-box" @click.stop="delFile(index)">
  12. <view class="icon-del"></view>
  13. <view class="icon-del rotate"></view>
  14. </view>
  15. <!-- <view v-if="item.errMsg" class="file-picker__mask" @click.stop="uploadFiles(item, index)">
  16. 点击重试
  17. </view> -->
  18. </view>
  19. </view>
  20. <view v-if="list.length < limit && !readonly" class="file-picker__box" :style="boxStyle">
  21. <view class="file-picker__box-content is-add" :style="borderStyle" @click="choose">
  22. <slot>
  23. <view class="icon-add"></view>
  24. <view class="icon-add rotate"></view>
  25. </slot>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import sheep from '@/sheep';
  32. export default {
  33. name: 'uploadImage',
  34. emits: ['uploadFiles', 'choose', 'delFile'],
  35. props: {
  36. filesList: {
  37. type: [Array, String],
  38. default() {
  39. return [];
  40. },
  41. },
  42. disabled: {
  43. type: Boolean,
  44. default: false,
  45. },
  46. disablePreview: {
  47. type: Boolean,
  48. default: false,
  49. },
  50. limit: {
  51. type: [Number, String],
  52. default: 9,
  53. },
  54. imageStyles: {
  55. type: Object,
  56. default() {
  57. return {
  58. width: 'auto',
  59. height: 'auto',
  60. border: {},
  61. };
  62. },
  63. },
  64. delIcon: {
  65. type: Boolean,
  66. default: true,
  67. },
  68. readonly: {
  69. type: Boolean,
  70. default: false,
  71. },
  72. },
  73. computed: {
  74. list() {
  75. if (typeof this.filesList === 'string') {
  76. return [this.filesList];
  77. }
  78. return this.filesList;
  79. },
  80. styles() {
  81. let styles = {
  82. width: 'auto',
  83. height: 'auto',
  84. border: {},
  85. };
  86. return Object.assign(styles, this.imageStyles);
  87. },
  88. boxStyle() {
  89. const { width = 'auto', height = 'auto' } = this.styles;
  90. let obj = {};
  91. if (height === 'auto') {
  92. if (width !== 'auto') {
  93. obj.height = this.value2px(width);
  94. obj['padding-top'] = 0;
  95. } else {
  96. obj.height = 0;
  97. }
  98. } else {
  99. obj.height = this.value2px(height);
  100. obj['padding-top'] = 0;
  101. }
  102. if (width === 'auto') {
  103. if (height !== 'auto') {
  104. obj.width = this.value2px(height);
  105. } else {
  106. obj.width = '33.3%';
  107. }
  108. } else {
  109. obj.width = this.value2px(width);
  110. }
  111. let classles = '';
  112. for (let i in obj) {
  113. classles += `${i}:${obj[i]};`;
  114. }
  115. return classles;
  116. },
  117. borderStyle() {
  118. let { border } = this.styles;
  119. let obj = {};
  120. const widthDefaultValue = 1;
  121. const radiusDefaultValue = 3;
  122. if (typeof border === 'boolean') {
  123. obj.border = border ? '1px #eee solid' : 'none';
  124. } else {
  125. let width = (border && border.width) || widthDefaultValue;
  126. width = this.value2px(width);
  127. let radius = (border && border.radius) || radiusDefaultValue;
  128. radius = this.value2px(radius);
  129. obj = {
  130. 'border-width': width,
  131. 'border-style': (border && border.style) || 'solid',
  132. 'border-color': (border && border.color) || '#eee',
  133. 'border-radius': radius,
  134. };
  135. }
  136. let classles = '';
  137. for (let i in obj) {
  138. classles += `${i}:${obj[i]};`;
  139. }
  140. return classles;
  141. },
  142. },
  143. methods: {
  144. getImageUrl(url) {
  145. if ('blob:http:' === url.substr(0, 10)) {
  146. return url;
  147. } else {
  148. return sheep.$url.cdn(url);
  149. }
  150. },
  151. uploadFiles(item, index) {
  152. this.$emit('uploadFiles', item);
  153. },
  154. choose() {
  155. this.$emit('choose');
  156. },
  157. delFile(index) {
  158. this.$emit('delFile', index);
  159. },
  160. previewImage(img, index) {
  161. let urls = [];
  162. if (Number(this.limit) === 1 && this.disablePreview && !this.disabled) {
  163. this.$emit('choose');
  164. }
  165. if (this.disablePreview) return;
  166. this.list.forEach((i) => {
  167. urls.push(this.getImageUrl(i));
  168. });
  169. uni.previewImage({
  170. urls: urls,
  171. current: index,
  172. });
  173. },
  174. value2px(value) {
  175. if (typeof value === 'number') {
  176. value += 'px';
  177. } else {
  178. if (value.indexOf('%') === -1) {
  179. value = value.indexOf('px') !== -1 ? value : value + 'px';
  180. }
  181. }
  182. return value;
  183. },
  184. },
  185. };
  186. </script>
  187. <style lang="scss">
  188. .uni-file-picker__container {
  189. /* #ifndef APP-NVUE */
  190. display: flex;
  191. box-sizing: border-box;
  192. /* #endif */
  193. flex-wrap: wrap;
  194. margin: -5px;
  195. }
  196. .file-picker__box {
  197. position: relative;
  198. // flex: 0 0 33.3%;
  199. width: 33.3%;
  200. height: 0;
  201. padding-top: 33.33%;
  202. /* #ifndef APP-NVUE */
  203. box-sizing: border-box;
  204. /* #endif */
  205. }
  206. .file-picker__box-content {
  207. position: absolute;
  208. top: 0;
  209. right: 0;
  210. bottom: 0;
  211. left: 0;
  212. margin: 5px;
  213. border: 1px #eee solid;
  214. border-radius: 5px;
  215. overflow: hidden;
  216. }
  217. .file-picker__progress {
  218. position: absolute;
  219. bottom: 0;
  220. left: 0;
  221. right: 0;
  222. /* border: 1px red solid; */
  223. z-index: 2;
  224. }
  225. .file-picker__progress-item {
  226. width: 100%;
  227. }
  228. .file-picker__mask {
  229. /* #ifndef APP-NVUE */
  230. display: flex;
  231. /* #endif */
  232. justify-content: center;
  233. align-items: center;
  234. position: absolute;
  235. right: 0;
  236. top: 0;
  237. bottom: 0;
  238. left: 0;
  239. color: #fff;
  240. font-size: 12px;
  241. background-color: rgba(0, 0, 0, 0.4);
  242. }
  243. .file-image {
  244. width: 100%;
  245. height: 100%;
  246. }
  247. .is-add {
  248. /* #ifndef APP-NVUE */
  249. display: flex;
  250. /* #endif */
  251. align-items: center;
  252. justify-content: center;
  253. }
  254. .icon-add {
  255. width: 50px;
  256. height: 5px;
  257. background-color: #f1f1f1;
  258. border-radius: 2px;
  259. }
  260. .rotate {
  261. position: absolute;
  262. transform: rotate(90deg);
  263. }
  264. .icon-del-box {
  265. /* #ifndef APP-NVUE */
  266. display: flex;
  267. /* #endif */
  268. align-items: center;
  269. justify-content: center;
  270. position: absolute;
  271. top: 3px;
  272. right: 3px;
  273. height: 26px;
  274. width: 26px;
  275. border-radius: 50%;
  276. background-color: rgba(0, 0, 0, 0.5);
  277. z-index: 2;
  278. transform: rotate(-45deg);
  279. }
  280. .icon-del {
  281. width: 15px;
  282. height: 2px;
  283. background-color: #fff;
  284. border-radius: 2px;
  285. }
  286. </style>