file-selector

This commit is contained in:
Xu Chang 2022-05-17 23:24:35 +08:00
parent 7d416a4cdd
commit 16791483c8
7 changed files with 100 additions and 18 deletions

View File

@ -6,7 +6,7 @@ export function composeFileUrl(extraFile: Pick<ExtraFile, 'type' | 'bucket' | 'f
// 未知第三方源
return extra1!;
}
throw new Error('not implemented yet');
return '';
}
export function decomposeFileUrl(url: string): Pick<ExtraFile, 'bucket' | 'filename' | 'origin' | 'type' | 'extra1'> {

View File

@ -4,12 +4,14 @@
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 10rpx;
}
.default-item {
width: 200rpx;
height: 300rpx;
border: 1ch solid black;
width: 160rpx;
height: 220rpx;
border: 1px solid black;
border-color: @primary-color;
}
.insert-btn {

View File

@ -1,22 +1,93 @@
import { WechatMpFileCarrier } from "oak-frontend-base";
Component({
externalClasses: ['item-container', 'item'],
properties: {
oakUpdateData: Object,
oakValue: Array,
maxCount: Number,
oakParent: String,
oakPath: String,
maxNumber: {
type: Number,
value: 100,
},
selectCount: {
type: Number,
value: 1,
},
sourceType: {
type: Array,
value: ['album', 'camera'],
},
mediaType: {
type: Array,
value: ['image'],
},
disableInsert: Boolean,
},
methods: {
onClick(touch: WechatMiniprogram.Touch) {
async onAdd() {
const { selectCount, mediaType, sourceType, oakUpdateData } = this.data;
try {
const { errMsg, tempFiles } = await wx.chooseMedia({
count: selectCount,
mediaType,
sourceType,
});
if (errMsg !== 'chooseMedia:ok') {
this.setData({
oakError: {
level: 'warning',
msg: errMsg,
},
});
}
else {
const { globalData: { features } } = getApp();
const { oakFullpath } = this.data;
for (const file of tempFiles) {
await features.runningNode.addNode({
parent: oakFullpath,
fileCarrier: new WechatMpFileCarrier(file),
updateData: oakUpdateData,
});
}
}
}
catch (err: any) {
if (err.errMsg !== 'chooseMedia:fail cancel') {
this.setData({
oakError: {
level: 'error',
msg: err.errMsg,
},
});
}
}
},
checkAllowInsert(value?: Array<any>, count?: number) {
const count2 = count || this.data.maxCount;
checkAllowInsert(value?: Array<any>, num?: number) {
const number2 = num || this.data.maxNumber;
const value2 = value || this.data.oakValue;
if (typeof count2 === 'number' && value2?.length >= count2) {
if (typeof number2 === 'number' && value2?.length >= number2) {
this.setData({
disableInsert: true,
});
}
},
async setFullpath(oakParent: string) {
const { globalData: { features }} = getApp();
console.log(this.data);
if (oakParent) {
const oakFullpath = `${oakParent}.${this.data.oakPath}`;
await features.runningNode.createNode({
path: this.data.oakPath,
parent: oakParent,
});
this.setData({
oakFullpath,
});
}
}
},
@ -24,14 +95,20 @@ Component({
oakValue: function (oakValue) {
this.checkAllowInsert(oakValue);
},
maxCount: function (maxCount) {
this.checkAllowInsert(undefined, maxCount);
maxNumber: function (maxNumber) {
this.checkAllowInsert(undefined, maxNumber);
},
oakParent: async function (oakParent) {
await this.setFullpath(oakParent);
}
},
lifetimes: {
ready() {
this.checkAllowInsert();
if (this.data.oakParent) {
this.setFullpath(this.data.oakParent);
}
}
}
});

View File

@ -1,8 +1,8 @@
<view class="default-item-container item-container">
<view class="default-item item" wx:for="{{oakValue}}" wx:key="index">
<item oak:value="item"/>
<item oakValue="{{item}}" oakPath="{{index}}" oakParent="{{oakFullpath}}" />
</view>
<view class="default-item item insert-btn" wx:if="{{!disableInsert}}">
<g-icon name="add"></g-icon>>
<view class="default-item item insert-btn" wx:if="{{!disableInsert}}" bind:tap="onAdd">
<g-icon name="add" size="80"/>
</view>
</view>

View File

@ -1,5 +1,6 @@
/** index.wxss **/
.container {
display: block;
.img {
max-width: 100%;
max-height: 100%;
}

View File

@ -1,12 +1,14 @@
import assert from 'assert';
import { EntityDict } from 'oak-app-domain';
import { WechatMpFileCarrier } from 'oak-frontend-base';
import { composeFileUrl } from '../../../../src/utils/extraFile';
OakComponent({
entity: 'extraFile',
formData: async (_rows, _features, _fileCarrier) => {
if (_fileCarrier) {
const file = (<WechatMpFileCarrier<EntityDict, 'extraFile'>>_fileCarrier).getFile();
return {
src: '11',
src: file.tempFilePath || file.thumbTempFilePath,
};
}
else {

View File

@ -1,2 +1,2 @@
<!-- index.wxml -->
<image src="{{src}}" mode="aspectFit" />
<image src="{{src}}" mode="aspectFit" class="img"/>