Merge branch 'v1.2_crop' into v1.3
* v1.2_crop: feat: 裁剪添加放大缩小 feat: 优化为先选择图片再裁剪 feat:裁剪头像
This commit is contained in:
@@ -0,0 +1,506 @@
|
||||
<template>
|
||||
<view class="avatar-cropper">
|
||||
<view
|
||||
ref="stageEl"
|
||||
class="crop-stage"
|
||||
@touchstart="onTouchStart"
|
||||
@touchmove="onTouchMove"
|
||||
@touchend="onTouchEnd"
|
||||
@touchcancel="onTouchEnd"
|
||||
@wheel.stop.prevent="onWheel"
|
||||
@mousewheel.stop.prevent="onWheel"
|
||||
>
|
||||
<view class="crop-stage-inner">
|
||||
<movable-area
|
||||
v-if="imagePath"
|
||||
class="stage-area"
|
||||
:style="{ width: stageWidthPx + 'px', height: stageHeightPx + 'px' }"
|
||||
>
|
||||
<movable-view
|
||||
class="stage-movable"
|
||||
direction="all"
|
||||
:out-of-bounds="true"
|
||||
:x="posX"
|
||||
:y="posY"
|
||||
:style="{ width: drawWidthPx + 'px', height: drawHeightPx + 'px' }"
|
||||
@change="onMoveChange"
|
||||
>
|
||||
<image class="stage-image" :src="imagePath" mode="scaleToFill" />
|
||||
</movable-view>
|
||||
</movable-area>
|
||||
|
||||
<view v-else class="empty-state flex-center" @click="chooseImage">
|
||||
<view class="empty-btn">选择图片</view>
|
||||
</view>
|
||||
|
||||
<view v-if="imagePath" class="mask-layer" :style="{ width: stageWidthPx + 'px', height: stageHeightPx + 'px' }">
|
||||
<view class="mask-top" :style="{ height: cropTopPx + 'px' }"></view>
|
||||
<view class="mask-middle" :style="{ height: cropSizePx + 'px' }">
|
||||
<view class="mask-left" :style="{ width: cropLeftPx + 'px' }"></view>
|
||||
<view class="mask-hole" :style="{ width: cropSizePx + 'px', height: cropSizePx + 'px' }"></view>
|
||||
<view class="mask-right" :style="{ width: stageWidthPx - cropLeftPx - cropSizePx + 'px' }"></view>
|
||||
</view>
|
||||
<view class="mask-bottom" :style="{ height: stageHeightPx - cropTopPx - cropSizePx + 'px' }"></view>
|
||||
<view class="crop-border" :style="{ left: cropLeftPx + 'px', top: cropTopPx + 'px', width: cropSizePx + 'px', height: cropSizePx + 'px' }"></view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="actions">
|
||||
<view class="btn btn-ghost" @click="chooseImage">重新选择</view>
|
||||
<view class="btn btn-primary" :class="{ disabled: !imagePath }" @click="confirmCrop">确认</view>
|
||||
</view>
|
||||
|
||||
<canvas
|
||||
canvas-id="avatarCropCanvas"
|
||||
class="hidden-canvas"
|
||||
:style="{ width: outputSizePx + 'px', height: outputSizePx + 'px' }"
|
||||
/>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, nextTick, getCurrentInstance, onMounted, onUnmounted, watch } from 'vue';
|
||||
|
||||
const props = defineProps({
|
||||
src: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
outputSize: {
|
||||
type: Number,
|
||||
default: 600,
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(['confirm', 'error']);
|
||||
const { proxy } = getCurrentInstance();
|
||||
|
||||
const outputSizePx = computed(() => Number(props.outputSize) || 600);
|
||||
|
||||
const stageEl = ref(null);
|
||||
const stageDomEl = ref(null);
|
||||
const cropSizePx = ref(0);
|
||||
const stageWidthPx = ref(0);
|
||||
const stageHeightPx = ref(0);
|
||||
const imagePath = ref('');
|
||||
const imgWidth = ref(0);
|
||||
const imgHeight = ref(0);
|
||||
const baseScale = ref(1);
|
||||
const zoom = ref(1);
|
||||
const minZoom = 1;
|
||||
const maxZoom = 4;
|
||||
|
||||
const totalScale = computed(() => (baseScale.value || 1) * (zoom.value || 1));
|
||||
|
||||
const drawWidthPx = computed(() => Math.ceil(imgWidth.value * totalScale.value));
|
||||
const drawHeightPx = computed(() => Math.ceil(imgHeight.value * totalScale.value));
|
||||
|
||||
const posX = ref(0);
|
||||
const posY = ref(0);
|
||||
|
||||
const clamp = (val, min, max) => Math.min(max, Math.max(min, val));
|
||||
|
||||
const cropLeftPx = computed(() => Math.max(0, Math.round((stageWidthPx.value - cropSizePx.value) / 2)));
|
||||
const cropTopPx = computed(() => Math.max(0, Math.round((stageHeightPx.value - cropSizePx.value) / 2)));
|
||||
|
||||
const calcBaseScale = () => {
|
||||
if (!cropSizePx.value || !imgWidth.value || !imgHeight.value) return 1;
|
||||
const sw = cropSizePx.value / imgWidth.value;
|
||||
const sh = cropSizePx.value / imgHeight.value;
|
||||
return Math.max(sw, sh);
|
||||
};
|
||||
|
||||
const resetPosition = () => {
|
||||
const w = drawWidthPx.value;
|
||||
const h = drawHeightPx.value;
|
||||
const left = cropLeftPx.value;
|
||||
const top = cropTopPx.value;
|
||||
const box = cropSizePx.value;
|
||||
posX.value = Math.round(left + box / 2 - w / 2);
|
||||
posY.value = Math.round(top + box / 2 - h / 2);
|
||||
clampPosition();
|
||||
};
|
||||
|
||||
const clampPosition = () => {
|
||||
const w = drawWidthPx.value;
|
||||
const h = drawHeightPx.value;
|
||||
const left = cropLeftPx.value;
|
||||
const top = cropTopPx.value;
|
||||
const box = cropSizePx.value;
|
||||
const minX = Math.round(left + box - w);
|
||||
const maxX = Math.round(left);
|
||||
const minY = Math.round(top + box - h);
|
||||
const maxY = Math.round(top);
|
||||
posX.value = clamp(posX.value, minX, maxX);
|
||||
posY.value = clamp(posY.value, minY, maxY);
|
||||
};
|
||||
|
||||
const measureStage = async () => {
|
||||
const rect = await new Promise((resolve) => {
|
||||
uni
|
||||
.createSelectorQuery()
|
||||
.in(proxy)
|
||||
.select('.crop-stage-inner')
|
||||
.boundingClientRect((r) => resolve(r))
|
||||
.exec();
|
||||
}).catch(() => null);
|
||||
if (!rect) return;
|
||||
stageWidthPx.value = Math.max(1, Math.round(Number(rect.width) || 0));
|
||||
stageHeightPx.value = Math.max(1, Math.round(Number(rect.height) || 0));
|
||||
};
|
||||
|
||||
const loadImage = async (src) => {
|
||||
if (!src) return;
|
||||
const info = await new Promise((resolve, reject) => {
|
||||
uni.getImageInfo({
|
||||
src,
|
||||
success: resolve,
|
||||
fail: reject,
|
||||
});
|
||||
}).catch(() => null);
|
||||
|
||||
if (!info) {
|
||||
emit('error', 'getImageInfo_failed');
|
||||
return;
|
||||
}
|
||||
|
||||
imagePath.value = src;
|
||||
imgWidth.value = Number(info.width) || 0;
|
||||
imgHeight.value = Number(info.height) || 0;
|
||||
zoom.value = 1;
|
||||
baseScale.value = calcBaseScale();
|
||||
await nextTick();
|
||||
await measureStage();
|
||||
resetPosition();
|
||||
};
|
||||
|
||||
const chooseImage = () => {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
sizeType: ['original', 'compressed'],
|
||||
success: async (res) => {
|
||||
const filePath = res?.tempFilePaths?.[0] || '';
|
||||
if (!filePath) return;
|
||||
await loadImage(filePath);
|
||||
},
|
||||
fail: () => {},
|
||||
});
|
||||
};
|
||||
|
||||
const onMoveChange = (e) => {
|
||||
if (!e?.detail) return;
|
||||
posX.value = Math.round(Number(e.detail.x) || 0);
|
||||
posY.value = Math.round(Number(e.detail.y) || 0);
|
||||
clampPosition();
|
||||
};
|
||||
|
||||
const pinchState = ref({
|
||||
active: false,
|
||||
startDist: 0,
|
||||
startZoom: 1,
|
||||
});
|
||||
|
||||
const getTouchDist = (touches) => {
|
||||
if (!touches || touches.length < 2) return 0;
|
||||
const t1 = touches[0];
|
||||
const t2 = touches[1];
|
||||
const dx = Number(t2.clientX) - Number(t1.clientX);
|
||||
const dy = Number(t2.clientY) - Number(t1.clientY);
|
||||
return Math.sqrt(dx * dx + dy * dy);
|
||||
};
|
||||
|
||||
const onTouchStart = (e) => {
|
||||
const touches = e?.touches || [];
|
||||
if (touches.length !== 2) return;
|
||||
pinchState.value = {
|
||||
active: true,
|
||||
startDist: getTouchDist(touches),
|
||||
startZoom: zoom.value,
|
||||
};
|
||||
};
|
||||
|
||||
const onTouchMove = (e) => {
|
||||
const touches = e?.touches || [];
|
||||
if (!pinchState.value.active || touches.length !== 2) return;
|
||||
const dist = getTouchDist(touches);
|
||||
if (!dist || !pinchState.value.startDist) return;
|
||||
const nextZoom = clamp(pinchState.value.startZoom * (dist / pinchState.value.startDist), minZoom, maxZoom);
|
||||
if (Math.abs(nextZoom - zoom.value) < 0.001) return;
|
||||
applyZoom(nextZoom);
|
||||
};
|
||||
|
||||
const onTouchEnd = () => {
|
||||
pinchState.value.active = false;
|
||||
};
|
||||
|
||||
const onWheel = (e) => {
|
||||
if (!imagePath.value) return;
|
||||
let dy = 0;
|
||||
if (typeof e?.deltaY === 'number' && e.deltaY) {
|
||||
dy = e.deltaY;
|
||||
} else if (typeof e?.detail?.deltaY === 'number' && e.detail.deltaY) {
|
||||
dy = e.detail.deltaY;
|
||||
} else if (typeof e?.wheelDelta === 'number' && e.wheelDelta) {
|
||||
dy = -e.wheelDelta;
|
||||
} else if (typeof e?.detail?.wheelDelta === 'number' && e.detail.wheelDelta) {
|
||||
dy = -e.detail.wheelDelta;
|
||||
} else if (typeof e?.detail === 'number' && e.detail) {
|
||||
dy = e.detail;
|
||||
}
|
||||
if (!dy) return;
|
||||
const factor = dy > 0 ? 0.92 : 1.08;
|
||||
applyZoom(clamp(zoom.value * factor, minZoom, maxZoom));
|
||||
};
|
||||
|
||||
const applyZoom = (nextZoom) => {
|
||||
if (!imagePath.value) return;
|
||||
const prevScale = totalScale.value || 1;
|
||||
const nextScale = (baseScale.value || 1) * (nextZoom || 1);
|
||||
const left = cropLeftPx.value;
|
||||
const top = cropTopPx.value;
|
||||
const box = cropSizePx.value;
|
||||
const centerX = left + box / 2;
|
||||
const centerY = top + box / 2;
|
||||
const imgX = (centerX - posX.value) / prevScale;
|
||||
const imgY = (centerY - posY.value) / prevScale;
|
||||
zoom.value = nextZoom;
|
||||
posX.value = Math.round(centerX - imgX * nextScale);
|
||||
posY.value = Math.round(centerY - imgY * nextScale);
|
||||
nextTick(() => clampPosition());
|
||||
};
|
||||
|
||||
const confirmCrop = async () => {
|
||||
if (!imagePath.value) return;
|
||||
clampPosition();
|
||||
await nextTick();
|
||||
|
||||
const box = cropSizePx.value;
|
||||
const left = cropLeftPx.value;
|
||||
const top = cropTopPx.value;
|
||||
const scale = totalScale.value || 1;
|
||||
const sx = Math.max(0, Math.round(((left - posX.value) / scale) * 1000) / 1000);
|
||||
const sy = Math.max(0, Math.round(((top - posY.value) / scale) * 1000) / 1000);
|
||||
const sWidth = Math.min(imgWidth.value, Math.round((box / scale) * 1000) / 1000);
|
||||
const sHeight = Math.min(imgHeight.value, Math.round((box / scale) * 1000) / 1000);
|
||||
|
||||
const ctx = uni.createCanvasContext('avatarCropCanvas', proxy);
|
||||
ctx.clearRect(0, 0, outputSizePx.value, outputSizePx.value);
|
||||
ctx.drawImage(imagePath.value, sx, sy, sWidth, sHeight, 0, 0, outputSizePx.value, outputSizePx.value);
|
||||
ctx.draw(false, () => {
|
||||
uni.canvasToTempFilePath(
|
||||
{
|
||||
canvasId: 'avatarCropCanvas',
|
||||
width: outputSizePx.value,
|
||||
height: outputSizePx.value,
|
||||
destWidth: outputSizePx.value,
|
||||
destHeight: outputSizePx.value,
|
||||
fileType: 'jpg',
|
||||
quality: 0.92,
|
||||
success: (res) => {
|
||||
const tempFilePath = res?.tempFilePath || '';
|
||||
if (!tempFilePath) {
|
||||
emit('error', 'canvasToTempFilePath_empty');
|
||||
return;
|
||||
}
|
||||
emit('confirm', tempFilePath);
|
||||
},
|
||||
fail: () => emit('error', 'canvasToTempFilePath_failed'),
|
||||
},
|
||||
proxy,
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
const sys = uni.getSystemInfoSync();
|
||||
const w = Number(sys.windowWidth) || 375;
|
||||
cropSizePx.value = Math.floor(w * 0.78);
|
||||
if (cropSizePx.value < 220) cropSizePx.value = 220;
|
||||
if (cropSizePx.value > 360) cropSizePx.value = 360;
|
||||
nextTick(() => measureStage());
|
||||
if (props.src) loadImage(props.src);
|
||||
|
||||
const el = stageEl.value?.$el || stageEl.value;
|
||||
stageDomEl.value = el || null;
|
||||
if (el && typeof el.addEventListener === 'function') {
|
||||
const handler = (evt) => {
|
||||
try {
|
||||
evt.preventDefault && evt.preventDefault();
|
||||
evt.stopPropagation && evt.stopPropagation();
|
||||
} catch (e) {}
|
||||
onWheel(evt);
|
||||
};
|
||||
el.__avatarCropWheelHandler = handler;
|
||||
el.addEventListener('wheel', handler, { passive: false });
|
||||
el.addEventListener('mousewheel', handler, { passive: false });
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
const el = stageDomEl.value || stageEl.value?.$el || stageEl.value;
|
||||
const handler = el && el.__avatarCropWheelHandler;
|
||||
if (el && handler && typeof el.removeEventListener === 'function') {
|
||||
el.removeEventListener('wheel', handler);
|
||||
el.removeEventListener('mousewheel', handler);
|
||||
try {
|
||||
delete el.__avatarCropWheelHandler;
|
||||
} catch (e) {}
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => cropSizePx.value,
|
||||
() => {
|
||||
if (!imagePath.value) return;
|
||||
baseScale.value = calcBaseScale();
|
||||
nextTick(() => {
|
||||
measureStage();
|
||||
resetPosition();
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
() => [stageWidthPx.value, stageHeightPx.value, zoom.value],
|
||||
() => {
|
||||
if (!imagePath.value) return;
|
||||
nextTick(() => clampPosition());
|
||||
},
|
||||
);
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.avatar-cropper {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
box-sizing: border-box;
|
||||
padding: 30rpx 32rpx 40rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.crop-stage {
|
||||
width: 100%;
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.crop-stage-inner {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: relative;
|
||||
border-radius: 18rpx;
|
||||
overflow: hidden;
|
||||
background: #000;
|
||||
}
|
||||
|
||||
.stage-area {
|
||||
position: relative;
|
||||
background: #000;
|
||||
}
|
||||
|
||||
.stage-movable {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
}
|
||||
|
||||
.stage-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.mask-layer {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.mask-top,
|
||||
.mask-bottom {
|
||||
width: 100%;
|
||||
background: rgba(0, 0, 0, 0.45);
|
||||
}
|
||||
|
||||
.mask-middle {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.mask-left,
|
||||
.mask-right {
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.45);
|
||||
}
|
||||
|
||||
.mask-hole {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.crop-border {
|
||||
position: absolute;
|
||||
border: 2rpx solid rgba(255, 255, 255, 0.95);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.65);
|
||||
}
|
||||
|
||||
.empty-btn {
|
||||
padding: 18rpx 28rpx;
|
||||
border-radius: 999rpx;
|
||||
background: rgba(255, 255, 255, 0.18);
|
||||
color: #fff;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
|
||||
.actions {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
gap: 24rpx;
|
||||
margin-top: 28rpx;
|
||||
}
|
||||
|
||||
.btn {
|
||||
flex: 1;
|
||||
height: 84rpx;
|
||||
border-radius: 16rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 30rpx;
|
||||
font-weight: 600;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.btn-ghost {
|
||||
background: transparent;
|
||||
color: rgba(153, 153, 153, 1);
|
||||
border: 2rpx solid rgba(220, 220, 220, 1);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: #2556ec;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.btn-primary.disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.hidden-canvas {
|
||||
position: absolute;
|
||||
left: -9999px;
|
||||
top: -9999px;
|
||||
}
|
||||
</style>
|
||||
+12
@@ -467,6 +467,18 @@
|
||||
"group": "通用"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": "avatarCrop",
|
||||
"style": {
|
||||
"navigationBarTitleText": "裁剪头像",
|
||||
"navigationStyle": "custom"
|
||||
},
|
||||
"meta": {
|
||||
"sync": true,
|
||||
"title": "裁剪头像",
|
||||
"group": "通用"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": "richtext",
|
||||
"style": {
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<template>
|
||||
<s-pages title="裁剪头像" :purple_bg="true">
|
||||
<template #navRight>
|
||||
<view class="nav-cancel" @click="cancel">取消</view>
|
||||
</template>
|
||||
<view class="avatar-crop-page">
|
||||
<avatarCropper :src="src" @confirm="onConfirm" @error="onError" />
|
||||
<view v-if="uploading" class="loading-mask">
|
||||
<view class="loading-box">上传中...</view>
|
||||
</view>
|
||||
</view>
|
||||
</s-pages>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, getCurrentInstance } from 'vue';
|
||||
import { onLoad } from '@dcloudio/uni-app';
|
||||
import { baseUrl } from '@/sheep/config';
|
||||
import avatarCropper from '@/components/avatar_uploader/avatar_cropper.vue';
|
||||
|
||||
const { proxy } = getCurrentInstance();
|
||||
const src = ref('');
|
||||
const uploading = ref(false);
|
||||
const eventChannel = ref(null);
|
||||
|
||||
onLoad((options) => {
|
||||
src.value = decodeURIComponent(String(options?.src || '').trim());
|
||||
eventChannel.value = proxy.getOpenerEventChannel ? proxy.getOpenerEventChannel() : null;
|
||||
});
|
||||
|
||||
const uploadAvatar = async (filePath) => {
|
||||
const token = uni.getStorageSync('token') ? uni.getStorageSync('token') : undefined;
|
||||
const tenantId = uni.getStorageSync('tenant-id');
|
||||
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
url: baseUrl + '/app-api/infra/file/upload',
|
||||
filePath,
|
||||
name: 'file',
|
||||
formData: {
|
||||
directory: 'head',
|
||||
'tenant-id': tenantId,
|
||||
token: token,
|
||||
},
|
||||
header: {},
|
||||
success: (res) => resolve(res),
|
||||
fail: (err) => reject(err),
|
||||
});
|
||||
}).catch(() => null);
|
||||
|
||||
if (!result) throw new Error('upload_failed');
|
||||
|
||||
let data = null;
|
||||
try {
|
||||
data = typeof result.data === 'string' ? JSON.parse(result.data) : result.data;
|
||||
} catch (e) {
|
||||
throw new Error('upload_parse_failed');
|
||||
}
|
||||
|
||||
const url = data?.data || '';
|
||||
if (!url) throw new Error('upload_no_url');
|
||||
return url;
|
||||
};
|
||||
|
||||
const onConfirm = async (croppedFilePath) => {
|
||||
if (uploading.value) return;
|
||||
uploading.value = true;
|
||||
try {
|
||||
const url = await uploadAvatar(croppedFilePath);
|
||||
if (eventChannel.value && eventChannel.value.emit) {
|
||||
eventChannel.value.emit('success', { url });
|
||||
}
|
||||
uni.navigateBack();
|
||||
} catch (e) {
|
||||
uni.showToast({ title: '上传失败,请重试', icon: 'none' });
|
||||
} finally {
|
||||
uploading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const onError = () => {
|
||||
uni.showToast({ title: '处理失败,请重试', icon: 'none' });
|
||||
};
|
||||
|
||||
const cancel = () => {
|
||||
if (uploading.value) return;
|
||||
uni.navigateBack();
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.avatar-crop-page {
|
||||
position: relative;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.nav-cancel {
|
||||
padding: 0 24rpx;
|
||||
height: 88rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 30rpx;
|
||||
color: rgba(51, 51, 51, 0.9);
|
||||
}
|
||||
|
||||
.loading-mask {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: 999;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: rgba(0, 0, 0, 0.35);
|
||||
}
|
||||
|
||||
.loading-box {
|
||||
padding: 18rpx 26rpx;
|
||||
border-radius: 14rpx;
|
||||
background: rgba(0, 0, 0, 0.55);
|
||||
color: #fff;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
</style>
|
||||
+16
-25
@@ -30,7 +30,6 @@
|
||||
import { ref, computed, reactive, onMounted } from 'vue';
|
||||
import AuthUtil from '@/sheep/api/member/auth';
|
||||
import card_list from '@/components/card_list/card_list.vue';
|
||||
import { baseUrl } from '@/sheep/config';
|
||||
import UserApi from '@/sheep/api/member/user';
|
||||
const appInfo = computed(() => sheep.$store('app').info);
|
||||
const isLogin = computed(() => sheep.$store('user').isLogin);
|
||||
@@ -178,35 +177,27 @@
|
||||
|
||||
const upImg = () => {
|
||||
uni.chooseImage({
|
||||
count: 1, //默认9
|
||||
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
|
||||
success: function (res) {
|
||||
const tempFilePaths = res.tempFilePaths;
|
||||
const tempFiles = res.tempFiles;
|
||||
let resSize = tempFiles[0].size;
|
||||
if (resSize > 1048576) {
|
||||
count: 1,
|
||||
sizeType: ['original', 'compressed'],
|
||||
success: (res) => {
|
||||
const filePath = res?.tempFilePaths?.[0] || '';
|
||||
const size = res?.tempFiles?.[0]?.size;
|
||||
if (typeof size === 'number' && size > 1048576) {
|
||||
uni.showToast({
|
||||
title: '上传图片大小不能超过1MB',
|
||||
icon: 'error',
|
||||
icon: 'none',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const token = uni.getStorageSync('token') ? uni.getStorageSync('token') : undefined;
|
||||
let tenant_id = uni.getStorageSync('tenant-id');
|
||||
uni.uploadFile({
|
||||
url: baseUrl + '/app-api/infra/file/upload', //仅为示例,非真实的接口地址
|
||||
filePath: tempFilePaths[0],
|
||||
name: 'file',
|
||||
formData: {
|
||||
directory: 'head',
|
||||
'tenant-id': tenant_id,
|
||||
token: token,
|
||||
},
|
||||
header: {},
|
||||
success: (uploadFileRes) => {
|
||||
let url = JSON.parse(uploadFileRes.data).data;
|
||||
upData({
|
||||
avatar: url,
|
||||
if (!filePath) return;
|
||||
uni.navigateTo({
|
||||
url: `/pages/public/avatarCrop?src=${encodeURIComponent(filePath)}`,
|
||||
success: (navRes) => {
|
||||
const eventChannel = navRes?.eventChannel;
|
||||
if (!eventChannel) return;
|
||||
eventChannel.on('success', ({ url }) => {
|
||||
if (!url) return;
|
||||
upData({ avatar: url });
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user