123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <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="chooseMediaSource">
- <text class="click-t">{{sourceTypes[sourceTypeIndex].title}}</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="chooseMediaType">
- <text class="click-t">{{(mediaTypes[mediaTypeIndex] as ChooseSource).title}}</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" ref="refCountInput" :value="count" type="number" :maxlength="1" @blur="chooseMediaCount"/>
- </view>
- </view>
- <!-- #ifdef APP-ANDROID -->
- <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">{{orientationTypes[orientationTypeIndex].title}}</text>
- </view>
- </view>
- <!-- #endif -->
- <view class="uni-list-cell cell-pd">
- <view class="uni-list-cell-left uni-label">
- 摄像头
- </view>
- <view class="uni-list-cell-right" @click="chooseCameraType">
- <text class="click-t">{{cameraTypes[cameraTypeIndex].title}}</text>
- </view>
- </view>
- </view>
- <!-- #ifdef APP-IOS -->
- <input-data title="最长拍摄时间,单位秒" defaultValue="10" type="number" @confirm="onMaxDurationConfirm"></input-data>
- <!-- #endif -->
- <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">{{mediaList.length}}/{{count}}</text>
- </view>
- </view>
- <view class="uni-flex" style="flex-wrap: wrap;">
- <view v-for="(file,index) in mediaList" :key="index" class="uni-uploader__input-box" style="border: 0;">
- <image style="width: 104px; height: 104px;" :src="file.imagePath" :data-src="file.imagePath" @tap="previewMedia(index)">
- </image>
- <image src="/static/plus.png" class="image-remove" @click="removeMedia(index)"></image>
- </view>
- <image class="uni-uploader__input-box" @tap="chooseMedia" src="/static/plus.png"></image>
- </view>
- </view>
- </view>
- </view>
- <!-- #ifdef APP -->
- </scroll-view>
- <!-- #endif -->
- </template>
- <script>
- type FileSource = {
- imagePath : string;
- filePath : string;
- fileType : string;
- };
- type ChooseSource = {
- value : string[];
- title : string;
- };
- const sourceTypeList : ChooseSource[] = [
- {
- value: ['camera'],
- title: '拍摄',
- },
- {
- value: ['album'],
- title: '相册',
- },
- {
- value: ['camera', 'album'],
- title: '拍摄或相册',
- }
- ];
- const mediaTypeList : ChooseSource[] = [
- {
- value: ['image'],
- title: '仅图片',
- },
- {
- value: ['video'],
- title: '仅视频',
- },
- {
- value: ['image', 'video'],
- title: '不限制',
- }
- ];
- const orientationTypeList : ChooseSource[] = [
- {
- value: ['portrait'],
- title: '竖屏',
- },
- {
- value: ['landscape'],
- title: '横屏',
- },
- {
- value: ['auto'],
- title: '自动',
- }
- ];
- const cameraTypeList : ChooseSource[] = [
- {
- value: ['front'],
- title: '前置摄像头',
- },
- {
- value: ['back'],
- title: '后置摄像头',
- }
- ];
- export default {
- data() {
- return {
- title: 'chooseMedia',
- mediaList: [] as Array<FileSource>,
- sourceTypeIndex: 2,
- mediaTypeIndex: 2,
- cameraTypeIndex: 1,
- orientationTypeIndex: 0,
- albumModeTypeIndex: 0,
- count: 9,
- maxDuration: 10,
- sourceTypes: sourceTypeList as ChooseSource[],
- mediaTypes: mediaTypeList as ChooseSource[],
- cameraTypes: cameraTypeList as ChooseSource[],
- orientationTypes: orientationTypeList as ChooseSource[]
- }
- },
- methods: {
- chooseMediaSource() {
- uni.showActionSheet({
- itemList: ['拍摄', '相册', '拍摄或相册'],
- success: (e) => {
- this.sourceTypeIndex = e.tapIndex
- }
- })
- },
- chooseMediaType() {
- uni.showActionSheet({
- itemList: ['仅图片', '仅视频', '不限制'],
- success: (e) => {
- this.mediaTypeIndex = e.tapIndex
- }
- })
- },
- chooseMediaCount(event : UniInputBlurEvent) {
- let count = parseInt(event.detail.value)
- if (count < 1 || count > 9 || isNaN(count)) {
- uni.showToast({
- position: "bottom",
- title: "图片数量应该不小于1不大于9"
- })
- return
- }
- this.count = count
- },
- chooseOrientationType() {
- uni.showActionSheet({
- itemList: ['竖屏', '横屏', '自动'],
- success: (e) => {
- this.orientationTypeIndex = e.tapIndex
- }
- })
- },
- chooseCameraType() {
- uni.showActionSheet({
- itemList: ['前置', '后置'],
- success: (e) => {
- this.cameraTypeIndex = e.tapIndex
- }
- })
- },
- onMaxDurationConfirm(value : number) {
- this.maxDuration = value;
- },
- chooseMedia() {
- if (this.mediaList.length >= this.count) {
- const message = "已经有" + this.count + "个了,请删除部分后重新选择";
- uni.showToast({
- position: "bottom",
- title: message
- })
- return
- }
- uni.chooseMedia({
- count: this.count - this.mediaList.length,
- sourceType: sourceTypeList[this.sourceTypeIndex].value,
- mediaType: mediaTypeList[this.mediaTypeIndex].value,
- camera: cameraTypeList[this.cameraTypeIndex].value[0],
- // #ifdef APP-IOS
- maxDuration: this.maxDuration,
- // #endif
- // #ifdef APP-ANDROID
- pageOrientation: orientationTypeList[this.orientationTypeIndex].value[0],
- // #endif
- success: (res) => {
- const tempFiles : ChooseMediaTempFile[] = res.tempFiles as ChooseMediaTempFile[];
- for (let i = 0; i < tempFiles.length; i++) {
- const tempFile : ChooseMediaTempFile = tempFiles[i]
- const imagePath = tempFile.fileType == "image" ? tempFile.tempFilePath : tempFile.thumbTempFilePath;
- const file : FileSource = { imagePath: imagePath!, filePath: tempFile.tempFilePath, fileType: tempFile.fileType };
- this.mediaList.push(file);
- }
- },
- fail: (err) => {
- console.log("err: ", JSON.stringify(err));
- uni.showToast({
- title:"choose media error.code:" + err.errCode+";message:"+err.errMsg,
- position:"bottom"
- })
- }
- })
- },
- previewMedia: function (index : number) {
- const file : FileSource = this.mediaList[index];
- if (file.fileType == "image") {
- uni.previewImage({
- current: 0,
- urls: [file.filePath]
- })
- } else {
- uni.$once("__ONFULLVIDEOLOAD", () => {
- uni.$emit("__ONRECEIVEURL", {
- "url": file.filePath,
- "cover": file.imagePath
- })
- })
- const url = "/pages/API/choose-media/fullscreen-video";
- uni.navigateTo({
- url: url,
- })
- }
- },
- removeMedia(index : number) {
- this.mediaList.splice(index, 1)
- },
- }
- }
- </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;
- }
- </style>
|