广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
210
211
<script setup lang="ts">
import { reactive, ref } from 'vue';
import type { UploadFileInfo } from 'naive-ui';
import { NButton, NModal, NUpload } from 'naive-ui';
import { Cropper } from 'vue-advanced-cropper';
import { useBoolean, useLoading } from '@sa/hooks';
import { fetchUpdateUserAvatar } from '@/service/api/system';
import { useAuthStore } from '@/store/modules/auth';
import defaultAvatar from '@/assets/imgs/soybean.jpg';
import 'vue-advanced-cropper/dist/style.css';
 
interface CropperOptions {
  img: string;
  fileName: string;
  stencilProps: {
    aspectRatio: number;
  };
}
 
interface CropperRef {
  getResult: () => {
    canvas: HTMLCanvasElement;
  };
}
 
const authStore = useAuthStore();
 
// 使用 useBoolean 管理模态框显示状态
const { bool: showModal, setTrue: showDrawer, setFalse: hideDrawer } = useBoolean();
// 使用 useLoading 管理加载状态
const { loading, startLoading, endLoading } = useLoading();
 
const imageUrl = ref(authStore.userInfo.user?.avatar || defaultAvatar);
const cropperRef = ref<CropperRef | null>(null);
 
// 图片裁剪数据
const options = reactive<CropperOptions>({
  img: imageUrl.value,
  fileName: '',
  stencilProps: {
    aspectRatio: 1
  }
});
 
/** 编辑头像 */
function handleEdit() {
  options.img = imageUrl.value;
  showDrawer();
}
 
/** 处理文件选择 */
async function handleFileSelect(data: { file: UploadFileInfo }) {
  const file = data.file.file;
  if (!file) return false;
 
  if (!file.type.includes('image/')) {
    window.$message?.error('请上传图片类型文件(JPG、PNG等)');
    return false;
  }
 
  const reader = new FileReader();
  reader.onload = () => {
    options.img = reader.result as string;
    options.fileName = file.name;
  };
  reader.readAsDataURL(file);
 
  return false;
}
 
/** 处理裁剪 */
async function handleCrop() {
  if (!cropperRef.value) return;
 
  startLoading();
  try {
    const { canvas } = cropperRef.value.getResult();
 
    // 将 canvas 转换为 blob
    canvas.toBlob(async (blob: Blob | null) => {
      if (!blob) return;
 
      const formData = new FormData();
      formData.append('avatarfile', blob, options.fileName || 'avatar.png');
 
      const { error } = await fetchUpdateUserAvatar(formData);
      if (!error) {
        window.$message?.success('头像更新成功!');
        imageUrl.value = URL.createObjectURL(blob);
        authStore.userInfo.user!.avatar = imageUrl.value;
        hideDrawer();
      }
    }, 'image/png');
  } finally {
    endLoading();
  }
}
 
/** 关闭对话框 */
function handleClose() {
  hideDrawer();
  options.img = imageUrl.value;
}
</script>
 
<template>
  <div class="cursor-pointer" @click="handleEdit">
    <div class="relative h-120px w-120px overflow-hidden rounded-full">
      <img :src="imageUrl" alt="user-avatar" class="h-full w-full object-cover" />
      <div
        class="absolute inset-0 flex-center bg-black/50 text-white opacity-0 transition-opacity duration-300 hover:opacity-100"
      >
        <SvgIcon icon="ep:plus" class="text-24px" />
      </div>
    </div>
 
    <NModal v-model:show="showModal" preset="card" title="修改头像" class="w-400px" @close="handleClose">
      <div class="flex-col-center gap-20px py-20px">
        <div class="h-300px w-full">
          <Cropper
            ref="cropperRef"
            class="h-full bg-gray-100"
            :src="options.img"
            :stencil-props="options.stencilProps"
          />
        </div>
        <div class="flex gap-12px">
          <NUpload accept=".jpg,.jpeg,.png,.gif" :max="1" :show-file-list="false" @before-upload="handleFileSelect">
            <NButton class="min-w-100px">选择图片</NButton>
          </NUpload>
          <NButton type="primary" class="min-w-100px" :loading="loading" @click="handleCrop">确认裁剪</NButton>
        </div>
      </div>
    </NModal>
  </div>
</template>
 
<style lang="scss" scoped>
.avatar-wrapper {
  display: inline-block;
  cursor: pointer;
}
 
.avatar-container {
  position: relative;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
 
  &:hover .avatar-overlay {
    opacity: 1;
  }
}
 
.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
 
.avatar-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.3s ease;
  color: #fff;
}
 
.upload-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px 0;
}
 
.cropper-container {
  width: 100%;
  height: 300px;
}
 
.cropper {
  height: 100%;
  background: #f8f8f8;
}
 
.preview-image {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid #eee;
}
 
.button-group {
  display: flex;
  gap: 12px;
}
 
.upload-button {
  min-width: 100px;
}
</style>