mk-upload.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <template>
  2. <view
  3. class="mk-upload"
  4. :class="{'reverse': reverse}"
  5. >
  6. <!--进行flex占位-->
  7. <block v-if="imgList.length >= column && reverse ">
  8. <block v-if="imgItemAppendLength === 0">
  9. <view
  10. class="mk-upload_item"
  11. :class="uploadItemColumn"
  12. v-for="(item, index) in (imgItemAppendLength + (column -1))"
  13. />
  14. </block>
  15. <block v-if="imgItemAppendLength === 1">
  16. <block v-if="column == 3">
  17. <view
  18. class="mk-upload_item"
  19. :class="uploadItemColumn"
  20. v-for="(item, index) in imgItemAppendLength"
  21. />
  22. </block>
  23. <block v-if="column == 4">
  24. <view
  25. class="mk-upload_item"
  26. :class="uploadItemColumn"
  27. v-for="(item, index) in (imgItemAppendLength + 1)"
  28. />
  29. </block>
  30. <block v-if="column == 5">
  31. <view
  32. class="mk-upload_item"
  33. :class="uploadItemColumn"
  34. v-for="(item, index) in (imgItemAppendLength + 2)"
  35. />
  36. </block>
  37. </block>
  38. <block v-if="imgItemAppendLength === 2">
  39. <block v-if="column == 4">
  40. <view
  41. class="mk-upload_item"
  42. :class="uploadItemColumn"
  43. v-for="(item, index) in (imgItemAppendLength - 1)"
  44. />
  45. </block>
  46. <block v-if="column == 5">
  47. <view
  48. class="mk-upload_item"
  49. :class="uploadItemColumn"
  50. v-for="(item, index) in imgItemAppendLength"
  51. />
  52. </block>
  53. </block>
  54. <block v-if="imgItemAppendLength === 3">
  55. <block v-if="column == 5">
  56. <view class="mk-upload_item" :class="uploadItemColumn"/>
  57. </block>
  58. </block>
  59. </block>
  60. <!--进行flex占位-->
  61. <view
  62. class="mk-upload_item"
  63. :class="uploadItemColumn"
  64. v-for="(item, index) in imgList"
  65. :key="index"
  66. >
  67. <view
  68. class="mk-upload_remove"
  69. :class="deletePosition"
  70. @tap="onDeleteThis(index)"
  71. v-if="deleteShow && !disabled"
  72. >
  73. <text>✕</text>
  74. </view>
  75. <image
  76. :src="upload_url+item.url"
  77. class="mk-upload_item_img"
  78. @tap="onPreviewImage(index)"
  79. mode="aspectFill"
  80. />
  81. </view>
  82. <!-- 列表end -->
  83. <view
  84. class="mk-upload_item"
  85. :class="uploadItemColumn"
  86. v-if="controlShow && (imgList.length < maxCount) && !disabled"
  87. >
  88. <view
  89. class="mk-upload_add"
  90. @tap="onChooseImage"
  91. >+</view>
  92. </view>
  93. <!-- 上传按钮 -->
  94. </view>
  95. </template>
  96. <script>
  97. import {upload_url} from "../../config.js"
  98. import permision from "@/js_sdk/wa-permission/permission.js"
  99. export default {
  100. //组件传参
  101. props: {
  102. //图片数组
  103. imgList: {
  104. type: Array,
  105. default: function() {
  106. return []
  107. }
  108. },
  109. //行数量
  110. column: {
  111. type: [Number, String],
  112. default: 3
  113. },
  114. reverse: {
  115. type: Boolean,
  116. default: false
  117. },
  118. //是否显示上传按钮
  119. controlShow: {
  120. type: Boolean,
  121. default: true
  122. },
  123. //是否显示删除按钮
  124. deleteShow: {
  125. type: Boolean,
  126. default: true
  127. },
  128. disabled: {
  129. type: Boolean,
  130. default: false
  131. },
  132. //删除按钮位置
  133. deletePosition: {
  134. type: String,
  135. default: 'topRight'
  136. },
  137. //album 从相册选图,camera 使用相机
  138. sourceType: {
  139. type:Array ,
  140. default: function() {
  141. return ['camera', 'album']
  142. }
  143. },
  144. //original 原图,compressed 压缩图
  145. sizeType: {
  146. type: Array,
  147. default: function() {
  148. return ['original', 'compressed']
  149. }
  150. },
  151. //最多可以选择的图片张
  152. maxChooseCount: {
  153. type: Number,
  154. default: 3
  155. },
  156. //最大条数
  157. maxCount: {
  158. type: Number,
  159. default: 100
  160. }
  161. },
  162. data() {
  163. return {
  164. upload_url:upload_url
  165. }
  166. },
  167. //计算属性
  168. computed: {
  169. uploadItemColumn(){
  170. return `mk-upload_item_column${this.column}`
  171. },
  172. imgItemAppendLength(){
  173. return this.imgList.length % this.column;
  174. }
  175. },
  176. //检测属性
  177. watch: {
  178. },
  179. //组件加载后
  180. mounted() {
  181. },
  182. methods: {
  183. //删除指定图片
  184. onDeleteThis(index){
  185. if(this.$listeners.onDeleteTake){
  186. this.$emit('onDeleteTake', index);
  187. return;
  188. }
  189. uni.showModal({
  190. title: '提示',
  191. content: '您确定删除吗?',
  192. success: (res)=> {
  193. if(res.confirm) {
  194. this.$emit('onDelete', index);
  195. }
  196. }
  197. });
  198. },
  199. //预览图片
  200. onPreviewImage(index) {
  201. if(this.$listeners.onPreviewTake){
  202. this.$emit('onPreviewTake', index);
  203. return;
  204. }
  205. let arr = []
  206. this.imgList.map(item =>{
  207. arr.push(this.upload_url+item.url)
  208. })
  209. const imgList = arr;
  210. const isReverse = this.reverse;
  211. const currentIndex = isReverse ? (imgList.length - index) - 1 : index
  212. const imgLists = isReverse ? imgList.reverse() : imgList;
  213. uni.previewImage({
  214. current: currentIndex,
  215. urls: imgLists
  216. });
  217. },
  218. //选择图片
  219. onChooseImage(){
  220. uni.chooseImage({
  221. sizeType: this.sizeType,
  222. sourceType: this.sourceType,
  223. count: this.maxChooseCount,
  224. success: (res) => {
  225. this.$emit('onChoose', res);
  226. },
  227. fail:(e) =>{
  228. uni.showToast({
  229. title: '请开启相机权限',
  230. icon: 'none'
  231. })
  232. setTimeout(() =>{
  233. permision.gotoAppPermissionSetting()
  234. },1000)
  235. console.log(e)
  236. }
  237. });
  238. }
  239. }
  240. }
  241. </script>
  242. <style lang="less" scoped>
  243. .mk-upload{
  244. display: flex;
  245. flex-wrap: wrap;
  246. box-sizing: border-box;
  247. padding-left: var(--mk-upload_item_margin, 15px);
  248. padding-bottom: var(--mk-upload_item_margin, 15px);
  249. flex-direction: row;
  250. &.reverse{
  251. flex-direction: row-reverse;
  252. flex-wrap: wrap-reverse;
  253. justify-content: flex-end;
  254. }
  255. .mk-upload_item{
  256. position: relative;
  257. margin-top: var(--mk-upload_item_margin, 15px);
  258. margin-right: var(--mk-upload_item_margin, 15px);
  259. flex: 0 calc(33.3% - var(--mk-upload_item_margin, 15px));
  260. .mk-upload_remove{
  261. background-color: var(--mk-upload_remove_color, #999);
  262. color: #fff;
  263. width: var(--mk-upload_remove_size, 24px);
  264. height: var(--mk-upload_remove_size, 24px);
  265. display: flex;
  266. justify-content: center;
  267. align-items: flex-start;
  268. opacity: 0.8;
  269. position: absolute;
  270. z-index: 2;
  271. cursor: pointer;
  272. box-sizing: border-box;
  273. text{
  274. transform: scale(0.8);
  275. position: absolute;
  276. top: -2upx;
  277. line-height: var(--mk-upload_remove_size, 24px);
  278. }
  279. &:active{
  280. opacity: 0.6;
  281. }
  282. &.topLeft{
  283. top: 0;
  284. left: 0;
  285. border-radius: 0 0 100% 0;
  286. text{
  287. left: 8upx;
  288. }
  289. }
  290. &.topRight{
  291. top: 0;
  292. right: 0;
  293. border-radius: 0 0 0 100%;
  294. text{
  295. left: 18upx;
  296. }
  297. }
  298. &.bottomLeft{
  299. bottom: 0;
  300. left: 0;
  301. border-radius: 0 100% 0 0;
  302. text{
  303. left: 8upx;
  304. top: 6upx;
  305. }
  306. }
  307. &.bottomRight{
  308. bottom: 0;
  309. right: 0;
  310. border-radius: 100% 0 0 0;
  311. text{
  312. top: 6upx;
  313. left: 16upx;
  314. }
  315. }
  316. }
  317. .mk-upload_add{
  318. display: flex;
  319. justify-content: center;
  320. align-items: center;
  321. background-color: var(--mk-upload_add_bgcolor, #f1f1f1);
  322. font-size: var(--mk-upload_add_size, 30px);
  323. height: var(--mk-upload_img_height, 100px);
  324. width: 100%;
  325. }
  326. .mk-upload_item_img{
  327. display: block;
  328. width: 100%;
  329. height: var(--mk-upload_img_height, 100px);
  330. }
  331. &.mk-upload_item_column2{
  332. flex: 0 calc(50% - var(--mk-upload_item_margin, 15px));
  333. }
  334. &.mk-upload_item_column3{
  335. flex: 0 calc(33.3% - var(--mk-upload_item_margin, 15px));
  336. }
  337. &.mk-upload_item_column4{
  338. flex: 0 calc(25% - var(--mk-upload_item_margin, 15px));
  339. .mk-upload_item_img,
  340. .mk-upload_add{
  341. height: var(--mk-upload_img_height, 70px);
  342. font-size: var(--mk-upload_add_size, 25px);
  343. }
  344. }
  345. &.mk-upload_item_column5{
  346. flex: 0 calc(20% - var(--mk-upload_item_margin, 15px));
  347. .mk-upload_item_img,
  348. .mk-upload_add{
  349. height: var(--mk-upload_img_height, 55px);
  350. font-size: var(--mk-upload_add_size, 25px);
  351. }
  352. }
  353. &.mk-upload_item_column4,
  354. &.mk-upload_item_column5{
  355. .mk-upload_remove{
  356. width: var(--mk-upload_remove_size, 20px);
  357. height: var(--mk-upload_remove_size, 20px);
  358. text{
  359. transform: scale(0.6);
  360. }
  361. &.topLeft{
  362. text{
  363. left: 6upx;
  364. top: -6upx;
  365. }
  366. }
  367. &.topRight{
  368. text{
  369. left: 10upx;
  370. top: -6upx;
  371. }
  372. }
  373. &.bottomLeft{
  374. text{
  375. left: 4upx;
  376. top: 0;
  377. }
  378. }
  379. &.bottomRight{
  380. text{
  381. left: 10upx;
  382. top: 0;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. </style>