eight
2025-04-15 589bcdb26f8e9d3e0d5ef46d27acc901c96d50ea
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
<template>
  <div>
    <p>封面:</p>
    <div class="thumb-div">
      <el-image
        v-if="newsItem.thumbUrl"
        style="width: 300px; max-height: 300px"
        :src="newsItem.thumbUrl"
        fit="contain"
      />
      <Icon
        v-else
        icon="ep:plus"
        class="avatar-uploader-icon"
        :class="isFirst ? 'avatar' : 'avatar1'"
      />
      <div class="thumb-but">
        <el-upload
          :action="UPLOAD_URL"
          :headers="HEADERS"
          multiple
          :limit="1"
          :file-list="fileList"
          :data="uploadData"
          :before-upload="onBeforeUpload"
          :on-error="onUploadError"
          :on-success="onUploadSuccess"
        >
          <template #trigger>
            <el-button size="small" type="primary">本地上传</el-button>
          </template>
          <el-button
            size="small"
            type="primary"
            @click="showImageDialog = true"
            style="margin-left: 5px"
          >
            素材库选择
          </el-button>
          <template #tip>
            <div class="el-upload__tip">支持 bmp/png/jpeg/jpg/gif 格式,大小不超过 2M</div>
          </template>
        </el-upload>
      </div>
      <el-dialog
        title="选择图片"
        v-model="showImageDialog"
        width="80%"
        append-to-body
        destroy-on-close
      >
        <WxMaterialSelect
          type="image"
          :account-id="accountId!"
          @select-material="onMaterialSelected"
        />
      </el-dialog>
    </div>
  </div>
</template>
 
<script lang="ts" setup>
import WxMaterialSelect from '@/views/mp/components/wx-material-select'
import { getAccessToken } from '@/utils/auth'
import type { UploadFiles, UploadProps, UploadRawFile } from 'element-plus'
import { UploadType, useBeforeUpload } from '@/views/mp/hooks/useUpload'
import { NewsItem } from './types'
const message = useMessage()
 
const UPLOAD_URL = import.meta.env.VITE_BASE_URL + '/admin-api/mp/material/upload-permanent' // 上传永久素材的地址
const HEADERS = { Authorization: 'Bearer ' + getAccessToken() } // 设置上传的请求头部
 
const props = defineProps<{
  modelValue: NewsItem
  isFirst: boolean
}>()
 
const emit = defineEmits<{
  (e: 'update:modelValue', v: NewsItem)
}>()
const newsItem = computed<NewsItem>({
  get() {
    return props.modelValue
  },
  set(val) {
    emit('update:modelValue', val)
  }
})
 
const accountId = inject<number>('accountId')
const showImageDialog = ref(false)
 
const fileList = ref<UploadFiles>([])
interface UploadData {
  type: UploadType
  accountId: number
}
const uploadData: UploadData = reactive({
  type: UploadType.Image,
  accountId: accountId!
})
 
/** 素材选择完成事件*/
const onMaterialSelected = (item: any) => {
  showImageDialog.value = false
  newsItem.value.thumbMediaId = item.mediaId
  newsItem.value.thumbUrl = item.url
}
 
const onBeforeUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) =>
  useBeforeUpload(UploadType.Image, 2)(rawFile)
 
const onUploadSuccess: UploadProps['onSuccess'] = (res: any) => {
  if (res.code !== 0) {
    message.error('上传出错:' + res.msg)
    return false
  }
 
  // 重置上传文件的表单
  fileList.value = []
 
  // 设置草稿的封面字段
  newsItem.value.thumbMediaId = res.data.mediaId
  newsItem.value.thumbUrl = res.data.url
}
 
const onUploadError = (err: Error) => {
  message.error('上传失败: ' + err.message)
}
</script>
 
<style lang="scss" scoped>
.el-upload__tip {
  margin-left: 5px;
}
 
.thumb-div {
  display: inline-block;
  width: 100%;
  text-align: center;
 
  .avatar-uploader-icon {
    width: 120px;
    height: 120px;
    font-size: 28px;
    line-height: 120px;
    color: #8c939d;
    text-align: center;
    border: 1px solid #d9d9d9;
  }
 
  .avatar {
    width: 230px;
    height: 120px;
  }
 
  .avatar1 {
    width: 120px;
    height: 120px;
  }
 
  .thumb-but {
    margin: 5px;
  }
}
</style>