choose-image.uvue 11.1 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-common-mt">
        <view class="uni-list">
          <view class="uni-list-cell cell-pd">
            <view class="uni-list-cell-left uni-label">
              图片来源
            </view>
            <view class="uni-list-cell-right" @click="chooseImageSource">
              <text class="click-t">{{sourceType[sourceTypeIndex]}}</text>
            </view>
          </view>

          <view class="uni-list-cell cell-pd">
            <view class="uni-list-cell-left uni-label">
              图片质量
            </view>
            <view class="uni-list-cell-right" @click="chooseImageType">
              <text class="click-t">{{sizeType[sizeTypeIndex]}}</text>
            </view>
          </view>

          <view class="uni-list-cell cell-pd">
            <view class="uni-list-cell-left uni-label">
              数量限制
            </view>
            <view class="uni-list-cell-right">
              <input class="click-t" :value="countIndex+1" type="number" :maxlength="1" @confirm="chooseImageCount"
                confirm-type="done" />
            </view>
35
          </view>
36
          <!-- #ifdef APP -->
37 38 39 40 41 42 43
          <view class="uni-list-cell cell-pd">
            <view class="uni-list-cell-left uni-label">
              屏幕方向
            </view>
            <view class="uni-list-cell-right" @click="chooseOrientationType">
              <text class="click-t">{{orientationType[orientationTypeIndex]}}</text>
            </view>
44 45 46 47 48
          </view>
          <!-- #endif -->
          <!-- #ifdef APP-ANDROID -->
          <view class="uni-list-cell cell-pd">
            <view class="uni-list-cell-left uni-label">
张磊 已提交
49
              相册模式
50
            </view>
张磊 已提交
51 52
            <view class="uni-list-cell-right" @click="albumModeChange">
              <text class="click-t">{{albumModeType[albumModeTypeIndex]}}</text>
53 54 55
            </view>
          </view>
          <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
          <view class="uni-list-cell cell-pd">
            <view class="uni-list-cell-left uni-label">
              图像裁剪
            </view>
            <view class="uni-list-cell-right">
              <switch :checked="isCrop" @change="switchCrop"></switch>
            </view>
          </view>
          <view ref="cropOptionNode" class="crop-option"
            :style="{'height':isCrop?'200px':'0px','margin-bottom':isCrop?'11px':'0px'}">
            <view class="uni-list-cell cell-pd">
              <view class="uni-list-cell-left item_width">
                图片质量(%)
              </view>
              <view class="uni-list-cell-right">
                <input :value="cropPercent" @confirm="cropPercentConfim" type="number" maxlength="-1" />
              </view>
            </view>
            <view class="uni-list-cell cell-pd">
              <view class="uni-list-cell-left item_width">
                裁剪宽度(px)
              </view>
              <view class="uni-list-cell-right">
                <input :value="cropWidth" @confirm="cropWidthConfim" type="number" maxlength="-1" />
              </view>
            </view>
            <view class="uni-list-cell cell-pd">
              <view class="uni-list-cell-left item_width">
                裁剪高度(px)
              </view>
              <view class="uni-list-cell-right">
                <input :value="cropHeight" @confirm="cropHeightConfim" type="number" maxlength="-1" />
              </view>
            </view>
            <view class="uni-list-cell cell-pd">
              <view class="uni-list-cell-left item_width">
                保留原宽高
              </view>
              <view class="uni-list-cell-right">
                <switch :checked="cropResize" @change="cropResizeChange"></switch>
              </view>
            </view>
          </view>
        </view>

        <view class="uni-list list-pd" style="padding: 15px;">
          <view class="uni-flex" style="margin-bottom: 10px;">
            <view class="uni-list-cell-left">点击可预览选好的图片</view>
            <view style="margin-left: auto;">
              <text class="click-t">{{imageList.length}}/{{countIndex+1}}</text>
            </view>
          </view>
          <view class="uni-flex" style="flex-wrap: wrap;">
            <view v-for="(image,index) in imageList" :key="index" class="uni-uploader__input-box" style="border: 0;">
              <image style="width: 104px; height: 104px;" :src="image" :data-src="image" @tap="previewImage(index)">
              </image>
              <image src="/static/plus.png" class="image-remove" @click="removeImage(index)"></image>
            </view>
            <image class="uni-uploader__input-box" @tap="chooseImage" src="/static/plus.png"></image>
          </view>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  var sourceTypeArray = [
    ['camera'],
    ['album'],
    ['camera', 'album']
  ]
  var sizeTypeArray = [
    ['compressed'],
    ['original'],
    ['compressed', 'original']
134 135 136 137 138
  ]
  var orientationTypeArray = [
    'portrait',
    'landscape',
    'auto'
DCloud-WZF's avatar
DCloud-WZF 已提交
139
  ]
张磊 已提交
140 141 142 143
  var albumModeTypeArray = [
    "custom",
    "system"
  ]
DCloud-WZF's avatar
DCloud-WZF 已提交
144 145 146 147 148 149 150 151
  export default {
    data() {
      return {
        title: 'chooseImage',
        imageList: [] as Array<string>,
        sourceTypeIndex: 2,
        sourceType: ['拍照', '相册', '拍照或相册'],
        sizeTypeIndex: 2,
152 153 154
        sizeType: ['压缩', '原图', '压缩或原图'],
        orientationTypeIndex: 0,
        orientationType: ['竖屏', '横屏', '自动'],
张磊 已提交
155 156
        albumModeTypeIndex:0,
        albumModeType:["自定义相册","系统相册"],
DCloud-WZF's avatar
DCloud-WZF 已提交
157 158 159 160 161 162
        countIndex: 8,
        count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        isCrop: false,
        cropPercent: 80,
        cropWidth: 100,
        cropHeight: 100,
张磊 已提交
163
        cropResize: false
DCloud-WZF's avatar
DCloud-WZF 已提交
164 165 166 167 168 169 170 171
      }
    },
    onUnload() {
      this.imageList = [];
      this.sourceTypeIndex = 2
      this.sourceType = ['拍照', '相册', '拍照或相册']
      this.sizeTypeIndex = 2
      this.sizeType = ['压缩', '原图', '压缩或原图']
172 173 174
      this.countIndex = 8
      this.orientationTypeIndex = 0
      this.orientationType = ['竖屏', '横屏', '自动']
DCloud-WZF's avatar
DCloud-WZF 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    },
    methods: {
      cropHeightConfim(e : InputConfirmEvent) {
        let value = parseInt(e.detail.value)
        if (value > 0) {
          this.cropHeight = value
        } else {
          uni.showToast({
            position: "bottom",
            title: "裁剪高度需要大于0"
          })
        }
      },
      cropWidthConfim(e : InputConfirmEvent) {
        let value = parseInt(e.detail.value)
        if (value > 0) {
          this.cropWidth = value
        } else {
          uni.showToast({
            position: "bottom",
            title: "裁剪宽度需要大于0"
          })
        }
      },
      cropPercentConfim(e : InputConfirmEvent) {
        let value = parseInt(e.detail.value)
        if (value > 0 && value <= 100) {
          this.cropPercent = value
        } else {
          uni.showToast({
            position: "bottom",
            title: "请输入0~100之间的值"
          })
        }
      },
张磊 已提交
210 211 212 213 214 215 216
      albumModeChange(){
        uni.showActionSheet({
          itemList: this.albumModeType,
          success: (e) => {
            this.albumModeTypeIndex = e.tapIndex!
          }
        })
217
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      cropResizeChange(e : UniSwitchChangeEvent) {
        this.cropResize = e.detail.value
      },
      switchCrop(e : UniSwitchChangeEvent) {
        this.isCrop = e.detail.value
      },
      removeImage(index : number) {
        this.imageList.splice(index, 1)
      },
      chooseImageSource() {
        uni.showActionSheet({
          itemList: ['拍照', '相册', '拍照或相册'],
          success: (e) => {
            this.sourceTypeIndex = e.tapIndex!
          }
        })
      },
      chooseImageType() {
        uni.showActionSheet({
          itemList: ['压缩', '原图', '压缩或原图'],
          success: (e) => {
            this.sizeTypeIndex = e.tapIndex!
          }
        })
242 243 244 245 246 247 248 249
      },
      chooseOrientationType(){
        uni.showActionSheet({
          itemList: ['竖屏', '横屏', '自动'],
          success: (e) => {
            this.orientationTypeIndex = e.tapIndex!
          }
        })
DCloud-WZF's avatar
DCloud-WZF 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      },
      chooseImageCount(event : InputConfirmEvent) {
        let count = parseInt(event.detail.value) - 1
        if (count < 0) {
          uni.showToast({
            position: "bottom",
            title: "图片数量应该大于0"
          })
          return
        }
        this.countIndex = count
      },
      chooseImage: function () {
        // var cropOption:ChooseImageCropOptions|null = this.isCrop ? null : new ChooseImageCropOptions(  )
        if (this.imageList.length >= 9) {
          uni.showToast({
            position: "bottom",
            title: "已经有9张图片了,请删除部分图片之后重新选择"
          })
          return
        }
        uni.chooseImage({
          sourceType: sourceTypeArray[this.sourceTypeIndex],
          sizeType: sizeTypeArray[this.sizeTypeIndex],
          crop: this.isCrop ? { "quality": this.cropPercent, "width": this.cropWidth, "height": this.cropHeight, "resize": this.cropResize } as ChooseImageCropOptions : null,
275 276
          count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length : this.count[this.countIndex],
          pageOrientation: orientationTypeArray[this.orientationTypeIndex],
张磊 已提交
277
          albumMode: albumModeTypeArray[this.albumModeTypeIndex],
DCloud-WZF's avatar
DCloud-WZF 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
          success: (res) => {
            this.imageList = this.imageList.concat(res.tempFilePaths);
          },
          fail: (err) => {
            console.log("err: ", JSON.stringify(err));
          }
        })
      },
      previewImage: function (index : number) {
        uni.previewImage({
          current: index,
          urls: this.imageList
        })
      }
    }
  }
</script>

<style>
  .cell-pd {
    padding: 11px 15px;
  }

  .click-t {
    color: darkgray;
  }

  .list-pd {
    margin-top: 25px;
  }

  .uni-uploader__input-box {
    margin: 5px;
    width: 104px;
    height: 104px;
    border: 1px solid #D9D9D9;
  }

  .uni-uploader__input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }

  .image-remove {
    transform: rotate(45deg);
    width: 25px;
    height: 25px;
    position: absolute;
    top: 0;
    right: 0;
    border-radius: 13px;
    background-color: rgba(200, 200, 200, 0.8);
  }

  .item_width {
    width: 130px;
  }

  .crop-option {
    margin-left: 11px;
    margin-right: 11px;
    border-radius: 11px;
    background-color: #eee;
    transition-property: height, margin-bottom;
    transition-duration: 200ms;
  }
349
</style>