index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <QStemplate :title="title" :titleHide="titleHide" :fontSize="fontSize" :width="width" :titleFlex="titleFlex"
  3. :contentFlex="contentFlex" :titleStyle="titleStyle" :contentStyle="contentStyle" :required="required" :requiredSign="requiredSign"
  4. :layout="layout" :titleLayout="titleLayout" :itemDisabled="itemDisabled" :titleColor="titleColor">
  5. <view class="width100 padding_10rpx_15rpx wrap" :class="itemLayout_computed">
  6. <view
  7. class="flex_column_c_c border_radius_4px transition_border_point6s padding_10rpx"
  8. v-for="(picsItem, picsIndex) in itemArray"
  9. :key="picsIndex">
  10. <view
  11. class="flex_row_c_c border1pxf2f2f2 position_relative border_radius_4px backgrounColor_f8f8f8 picsBox">
  12. <image
  13. :src="picsItem.path"
  14. class="border_radius_4px box_shadow_2px_2px_5px_ADADAD picsBox"
  15. mode="aspectFill"
  16. @tap.stop.prevent="showImg"
  17. :data-picsindex="picsIndex">
  18. </image>
  19. <view
  20. class="picsClear"
  21. @tap.prevent.stop="clearPic"
  22. :data-picsindex="picsIndex">
  23. <uni-icon type="clear" :color="picsItem.clearColor||clearColor||'#f5105c'" :size="34"/>
  24. </view>
  25. <view
  26. class="sortSelection flex_row_c_c"
  27. :style="{
  28. 'background-color': sortSelectionArray[picsIndex]?(selectedColor||'#0099FF'):(unSelectedColor||'rgba(0,0,0,.7)'),
  29. 'border': (selectBorderColor||'1px solid rgba(255,255,255,.7)'),
  30. 'color': selectColor||'#fff'
  31. }"
  32. v-if="sortSelection" @tap.stop.prevent="sortSelectionFc" :data-picsindex="picsIndex">
  33. {{
  34. sortSelectionArray[picsIndex] || ''
  35. }}
  36. </view>
  37. </view>
  38. </view>
  39. <view
  40. class="flex_column_c_c border_radius_4px transition_border_point6s padding_10rpx"
  41. v-if="max?(itemArray.length>=max?false:true):true">
  42. <view
  43. class="flex_row_c_c border1pxf2f2f2 position_relative border_radius_4px backgrounColor_f8f8f8 box_shadow_2px_2px_5px_ADADAD picsBox"
  44. @tap="chooseImg">
  45. <uni-icon type="image" :size="45" color="#999" />
  46. </view>
  47. </view>
  48. </view>
  49. </QStemplate>
  50. </template>
  51. <script>
  52. import _app from '../../js/app.js';
  53. import QStemplate from '../../template/template.vue';
  54. import QSInputsMixin from '../../js/QSInputsMixin.js';
  55. import uniIcon from '../../uniIcons/uni-icons.vue';
  56. export default {
  57. components: {
  58. QStemplate,
  59. uniIcon
  60. },
  61. props: {
  62. max: { //最多选择图片数量
  63. type: [String, Number],
  64. default: ''
  65. },
  66. sortSelection: { //排序选择模式
  67. type: Boolean,
  68. default: false
  69. },
  70. defaultSelectAll: { //在排序选择模式下,用户未选择任何图片时是否默认选择全部
  71. type: Boolean,
  72. default: true
  73. },
  74. clearColor: {
  75. type: String,
  76. default: '#f5105c'
  77. },
  78. selectedColor: {
  79. type: String,
  80. default: '#0099FF'
  81. },
  82. unSelectedColor: {
  83. type: String,
  84. default: 'rgba(0,0,0,.7)'
  85. },
  86. selectBorderColor: {
  87. type: String,
  88. default: '1px solid rgba(255,255,255,.7)'
  89. },
  90. selectColor: {
  91. type: String,
  92. default: '#fff'
  93. }
  94. },
  95. data() {
  96. let itemArray;
  97. if(this.value && this.value instanceof Array && this.value.length > 0) {
  98. itemArray = this.value;
  99. }else{
  100. itemArray = [];
  101. }
  102. return {
  103. itemArray,
  104. sortSelectionArray: [],
  105. sortSelectionCheckArray: []
  106. }
  107. },
  108. methods: {
  109. chooseImg() {
  110. uni.chooseImage({
  111. success: res => {
  112. const tempFilePaths = res.tempFilePaths;
  113. tempFilePaths.forEach(item=>{
  114. this.itemArray.push({path: item});
  115. })
  116. this.setValue(this.getItemArray());
  117. }
  118. })
  119. },
  120. clearPic(
  121. {currentTarget: { dataset: { picsindex } } } = {}
  122. ) {
  123. const oldPath = this.itemArray[picsindex].path;
  124. this.itemArray.splice(picsindex, 1);
  125. if(this.sortSelection) {
  126. const selectionIndex = this.sortSelectionCheckArray.findIndex(item=>item.path === oldPath);
  127. if(selectionIndex >= 0) {
  128. this.sortSelectionCheckArray.splice(selectionIndex, 1);
  129. }
  130. this.checkPicsCheckObj();
  131. }
  132. this.setValue(this.getItemArray());
  133. },
  134. showImg(
  135. {currentTarget: { dataset: { picsindex } } } = {}
  136. ) {
  137. _app.previewImage(this.itemArray.map(item=>item.path), picsindex);
  138. },
  139. sortSelectionFc(
  140. { currentTarget: { dataset: { picsindex } } } = {},
  141. ) {
  142. const hasIndex = this.sortSelectionCheckArray.findIndex(item=>item.path === this.itemArray[picsindex].path);
  143. if(hasIndex >= 0) {
  144. this.sortSelectionCheckArray.splice(hasIndex, 1);
  145. }else{
  146. this.sortSelectionCheckArray.push(this.itemArray[picsindex]);
  147. }
  148. this.setValue(this.getItemArray());
  149. this.checkPicsCheckObj();
  150. },
  151. checkPicsCheckObj(vbName) {
  152. const newArray = [];
  153. const itemArray = this.itemArray;
  154. const sortSelectionCheckArray = this.sortSelectionCheckArray;
  155. for(let i = 0; i < itemArray.length; i++) {
  156. const index = sortSelectionCheckArray.findIndex(ite=>ite.path === itemArray[i].path);
  157. if(index >= 0) {
  158. newArray.push(index+1);
  159. }else{
  160. newArray.push('');
  161. }
  162. }
  163. this.sortSelectionArray = newArray
  164. },
  165. setData(newArr) {
  166. this.itemArray = newArr;
  167. if(this.sortSelection) {
  168. this.sortSelectionCheckArray = [];
  169. }
  170. this.setValue(this.getItemArray());
  171. this.checkPicsCheckObj();
  172. },
  173. setUpLoadData(obj) {
  174. this.upLoadData = obj;
  175. if(this.sortSelection) {
  176. this.sortSelectionCheckArray = [];
  177. }
  178. this.setValue(this.getItemArray());
  179. this.checkPicsCheckObj();
  180. },
  181. getUpLoadPromiseArray() {
  182. return _app.getUpLoadPromiseArray({
  183. itemArray: [...this.getItemArray()],
  184. customId: this.customId,
  185. upLoadData: this.upLoadData,
  186. required: this.required
  187. });
  188. },
  189. getItemArray() {
  190. if(this.sortSelection) {
  191. if(this.sortSelectionCheckArray.length > 0) {
  192. return this.sortSelectionCheckArray;
  193. }else{
  194. if(this.defaultSelectAll) {
  195. return this.itemArray;
  196. }else{
  197. return [];
  198. }
  199. }
  200. }else{
  201. return this.itemArray;
  202. }
  203. }
  204. },
  205. mixins: [QSInputsMixin({
  206. QSInputsType: _app.typeObj.infinitePics
  207. })]
  208. };
  209. </script>
  210. <style scoped>
  211. @import url("../../css/inputs.css");
  212. @import url("../../config/css/picsAndInfinitePics.css");
  213. </style>