upload-image.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. if (this.filesList) {
  77. return [this.filesList];
  78. } else {
  79. return [];
  80. }
  81. }
  82. return this.filesList;
  83. },
  84. styles() {
  85. let styles = {
  86. width: 'auto',
  87. height: 'auto',
  88. border: {},
  89. };
  90. return Object.assign(styles, this.imageStyles);
  91. },
  92. boxStyle() {
  93. const { width = 'auto', height = 'auto' } = this.styles;
  94. let obj = {};
  95. if (height === 'auto') {
  96. if (width !== 'auto') {
  97. obj.height = this.value2px(width);
  98. obj['padding-top'] = 0;
  99. } else {
  100. obj.height = 0;
  101. }
  102. } else {
  103. obj.height = this.value2px(height);
  104. obj['padding-top'] = 0;
  105. }
  106. if (width === 'auto') {
  107. if (height !== 'auto') {
  108. obj.width = this.value2px(height);
  109. } else {
  110. obj.width = '33.3%';
  111. }
  112. } else {
  113. obj.width = this.value2px(width);
  114. }
  115. let classles = '';
  116. for (let i in obj) {
  117. classles += `${i}:${obj[i]};`;
  118. }
  119. return classles;
  120. },
  121. borderStyle() {
  122. let { border } = this.styles;
  123. let obj = {};
  124. const widthDefaultValue = 1;
  125. const radiusDefaultValue = 3;
  126. if (typeof border === 'boolean') {
  127. obj.border = border ? '1px #eee solid' : 'none';
  128. } else {
  129. let width = (border && border.width) || widthDefaultValue;
  130. width = this.value2px(width);
  131. let radius = (border && border.radius) || radiusDefaultValue;
  132. radius = this.value2px(radius);
  133. obj = {
  134. 'border-width': width,
  135. 'border-style': (border && border.style) || 'solid',
  136. 'border-color': (border && border.color) || '#eee',
  137. 'border-radius': radius,
  138. };
  139. }
  140. let classles = '';
  141. for (let i in obj) {
  142. classles += `${i}:${obj[i]};`;
  143. }
  144. return classles;
  145. },
  146. },
  147. methods: {
  148. getImageUrl(url) {
  149. if ('blob:http:' === url.substr(0, 10)) {
  150. return url;
  151. } else {
  152. return sheep.$url.cdn(url);
  153. }
  154. },
  155. uploadFiles(item, index) {
  156. this.$emit('uploadFiles', item);
  157. },
  158. choose() {
  159. this.$emit('choose');
  160. },
  161. delFile(index) {
  162. this.$emit('delFile', index);
  163. },
  164. previewImage(img, index) {
  165. let urls = [];
  166. if (Number(this.limit) === 1 && this.disablePreview && !this.disabled) {
  167. this.$emit('choose');
  168. }
  169. if (this.disablePreview) return;
  170. this.list.forEach((i) => {
  171. urls.push(this.getImageUrl(i));
  172. });
  173. uni.previewImage({
  174. urls: urls,
  175. current: index,
  176. });
  177. },
  178. value2px(value) {
  179. if (typeof value === 'number') {
  180. value += 'px';
  181. } else {
  182. if (value.indexOf('%') === -1) {
  183. value = value.indexOf('px') !== -1 ? value : value + 'px';
  184. }
  185. }
  186. return value;
  187. },
  188. },
  189. };
  190. </script>
  191. <style lang="scss">
  192. .uni-file-picker__container {
  193. /* #ifndef APP-NVUE */
  194. display: flex;
  195. box-sizing: border-box;
  196. /* #endif */
  197. flex-wrap: wrap;
  198. margin: -5px;
  199. }
  200. .file-picker__box {
  201. position: relative;
  202. // flex: 0 0 33.3%;
  203. width: 33.3%;
  204. height: 0;
  205. padding-top: 33.33%;
  206. /* #ifndef APP-NVUE */
  207. box-sizing: border-box;
  208. /* #endif */
  209. }
  210. .file-picker__box-content {
  211. position: absolute;
  212. top: 0;
  213. right: 0;
  214. bottom: 0;
  215. left: 0;
  216. margin: 5px;
  217. border: 1px #eee solid;
  218. border-radius: 5px;
  219. overflow: hidden;
  220. }
  221. .file-picker__progress {
  222. position: absolute;
  223. bottom: 0;
  224. left: 0;
  225. right: 0;
  226. /* border: 1px red solid; */
  227. z-index: 2;
  228. }
  229. .file-picker__progress-item {
  230. width: 100%;
  231. }
  232. .file-picker__mask {
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. /* #endif */
  236. justify-content: center;
  237. align-items: center;
  238. position: absolute;
  239. right: 0;
  240. top: 0;
  241. bottom: 0;
  242. left: 0;
  243. color: #fff;
  244. font-size: 12px;
  245. background-color: rgba(0, 0, 0, 0.4);
  246. }
  247. .file-image {
  248. width: 100%;
  249. height: 100%;
  250. }
  251. .is-add {
  252. /* #ifndef APP-NVUE */
  253. display: flex;
  254. /* #endif */
  255. align-items: center;
  256. justify-content: center;
  257. }
  258. .icon-add {
  259. width: 50px;
  260. height: 5px;
  261. background-color: #f1f1f1;
  262. border-radius: 2px;
  263. }
  264. .rotate {
  265. position: absolute;
  266. transform: rotate(90deg);
  267. }
  268. .icon-del-box {
  269. /* #ifndef APP-NVUE */
  270. display: flex;
  271. /* #endif */
  272. align-items: center;
  273. justify-content: center;
  274. position: absolute;
  275. top: 3px;
  276. right: 3px;
  277. height: 26px;
  278. width: 26px;
  279. border-radius: 50%;
  280. background-color: rgba(0, 0, 0, 0.5);
  281. z-index: 2;
  282. transform: rotate(-45deg);
  283. }
  284. .icon-del {
  285. width: 15px;
  286. height: 2px;
  287. background-color: #fff;
  288. border-radius: 2px;
  289. }
  290. </style>