index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <div v-if="type == 'image'">
  3. <ul v-for="(item,index) in value" :key="index" class="el-upload-list el-upload-list--picture-card">
  4. <li tabindex="0" class="el-upload-list__item is-ready" :style="'width: '+width+'px;height: '+height+'px'">
  5. <div>
  6. <img :src="item" alt="" class="el-upload-list__item-thumbnail">
  7. <span class="el-upload-list__item-actions">
  8. <span v-if="index != 0" class="el-upload-list__item-preview" @click="moveMaterial(index,'up')">
  9. <i class="el-icon-back" />
  10. </span>
  11. <span class="el-upload-list__item-preview" @click="zoomMaterial(index)">
  12. <i class="el-icon-view" />
  13. </span>
  14. <span class="el-upload-list__item-delete" @click="deleteMaterial(index)">
  15. <i class="el-icon-delete" />
  16. </span>
  17. <span v-if="index != value.length-1" class="el-upload-list__item-preview" @click="moveMaterial(index,'down')">
  18. <i class="el-icon-right" />
  19. </span>
  20. </span>
  21. </div>
  22. </li>
  23. </ul>
  24. <div v-if="num > value.length" tabindex="0" class="el-upload el-upload--picture-card" :style="'width: '+width+'px;height: '+height+'px;'+'line-height:'+height+'px;'" @click="toSeleteMaterial">
  25. <i class="el-icon-plus" />
  26. </div>
  27. <!-- 查看 -->
  28. <el-dialog
  29. append-to-body
  30. :visible.sync="dialogVisible"
  31. width="35%"
  32. >
  33. <img :src="url" alt="" style="width: 100%">
  34. </el-dialog>
  35. <!-- 素材列表 -->
  36. <el-dialog
  37. @close="clos"
  38. title="图片素材库"
  39. append-to-body
  40. :visible.sync="listDialogVisible"
  41. width="70%"
  42. >
  43. <el-container style="height: 600px;">
  44. <el-aside width="unset">
  45. <div style="margin-bottom: 10px">
  46. <el-button
  47. class="el-icon-plus"
  48. size="small"
  49. @click="materialgroupAdd()"
  50. >
  51. 添加分组
  52. </el-button>
  53. </div>
  54. <div class="group-list">
  55. <div class="group-item" v-for="(group) in materialGroupList">
  56. <el-button @click="selectGroup(group)" type="primary" plain >{{group.name}}</el-button>
  57. </div>
  58. </div>
  59. </el-aside>
  60. <el-main>
  61. <el-card>
  62. <div slot="header">
  63. <el-row>
  64. <el-col :span="12">
  65. <span>{{ materialGroup.name }}</span>
  66. <span v-if="materialGroup.groupId >0">
  67. <el-button size="small" type="text" class="el-icon-edit" style="margin-left: 10px;" @click="materialgroupEdit(materialGroup)">重命名</el-button>
  68. <el-button size="small" type="text" class="el-icon-delete" style="margin-left: 10px;color: red" @click="materialgroupDelete(materialGroup)">删除</el-button>
  69. </span>
  70. </el-col>
  71. <el-col :span="12" style="text-align: right;">
  72. <el-upload
  73. :action="uploadUrl"
  74. :file-list="[]"
  75. :on-progress="handleProgress"
  76. :before-upload="beforeUpload"
  77. :on-success="handleSuccess"
  78. :data="{type: 1}"
  79. multiple
  80. >
  81. <el-button size="small" type="primary">批量上传</el-button>
  82. </el-upload>
  83. </el-col>
  84. </el-row>
  85. </div>
  86. <div v-loading="tableLoading">
  87. <el-alert
  88. v-if="tableData.length <= 0"
  89. title="暂无数据"
  90. type="info"
  91. :closable="false"
  92. center
  93. show-icon
  94. />
  95. <el-row :gutter="5">
  96. <el-checkbox-group v-model="urls" :max="num - value.length">
  97. <el-col v-for="(item,index) in tableData" :key="index" :span="4">
  98. <el-card :body-style="{ padding: '5px' }">
  99. <el-image
  100. style="width: 100%;height: 100px"
  101. :src="item.url"
  102. fit="contain"
  103. :preview-src-list="[item.url]"
  104. :z-index="9999"
  105. />
  106. <div>
  107. <el-checkbox class="material-name" :label="item.url">
  108. 选择
  109. </el-checkbox>
  110. <el-row>
  111. <el-col :span="24" class="col-do">
  112. <el-button type="text" size="medium" @click="materialDel(item)">删除</el-button>
  113. </el-col>
  114. </el-row>
  115. </div>
  116. </el-card>
  117. </el-col>
  118. </el-checkbox-group>
  119. </el-row>
  120. <pagination
  121. v-show="total>0"
  122. :total="total"
  123. :page.sync="queryParams.pageNum"
  124. :limit.sync="queryParams.pageSize"
  125. @pagination="getMaterialList"
  126. />
  127. </div>
  128. </el-card>
  129. </el-main>
  130. </el-container>
  131. <span slot="footer" class="dialog-footer">
  132. <el-button @click="listDialogVisible = false">取 消</el-button>
  133. <el-button type="primary" @click="submit">确 定</el-button>
  134. </span>
  135. </el-dialog>
  136. </div>
  137. </template>
  138. <script>
  139. import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial, exportMaterial } from "@/api/store/material";
  140. import { getAllMaterialGroup,listMaterialGroup, getMaterialGroup, delMaterialGroup, addMaterialGroup, updateMaterialGroup, exportMaterialGroup } from "@/api/store/materialGroup";
  141. import { Loading } from 'element-ui';
  142. export default {
  143. name: 'ImageSelect',
  144. props: {
  145. // 素材数据
  146. value: {
  147. type: Array,
  148. default() {
  149. return []
  150. }
  151. },
  152. // 素材类型
  153. type: {
  154. type: String
  155. },
  156. // 素材限制数量,默认5个
  157. num: {
  158. type: Number,
  159. default() {
  160. return 5
  161. }
  162. },
  163. // 宽度
  164. width: {
  165. type: Number,
  166. default() {
  167. return 150
  168. }
  169. },
  170. // 宽度
  171. height: {
  172. type: Number,
  173. default() {
  174. return 150
  175. }
  176. }
  177. },
  178. data() {
  179. return {
  180. finalQuality:1,
  181. uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
  182. dialogVisible: false,
  183. url: '',
  184. listDialogVisible: false,
  185. materialGroupList: [],
  186. materialGroupLoading: false,
  187. materialGroup:{},
  188. tableData: [],
  189. resultNumber: 0,
  190. total: 0,
  191. queryParams: {
  192. pageNum: 1,
  193. pageSize: 10,
  194. type: null,
  195. groupId: null,
  196. name: null,
  197. url: null,
  198. isDel: null,
  199. createUserId: null,
  200. },
  201. tableLoading: false,
  202. urls: []
  203. }
  204. },
  205. mounted(){
  206. this.getAllMaterialGroup();
  207. },
  208. methods: {
  209. clos(){
  210. this.urls=[];
  211. },
  212. selectGroup(item){
  213. this.materialGroup=item;
  214. this.queryParams.groupId=item.groupId;
  215. this.getMaterialList();
  216. },
  217. materialgroupAdd() {
  218. const that = this
  219. this.$prompt('请输入分组名', '提示', {
  220. confirmButtonText: '确定',
  221. cancelButtonText: '取消'
  222. }).then(({ value }) => {
  223. addMaterialGroup({
  224. name: value
  225. }).then(function() {
  226. that.materialGroup={};
  227. that.getAllMaterialGroup()
  228. })
  229. }).catch(() => {
  230. })
  231. },
  232. materialgroupDelete(materialgroupObj) {
  233. const that = this
  234. this.$confirm('是否确认删除该分组?', '提示', {
  235. confirmButtonText: '确定',
  236. cancelButtonText: '取消',
  237. type: 'warning'
  238. }).then(function() {
  239. delMaterialGroup(materialgroupObj.groupId)
  240. .then(function() {
  241. that.materialGroup={};
  242. that.getAllMaterialGroup()
  243. })
  244. })
  245. },
  246. materialgroupEdit(materialgroupObj) {
  247. const that = this
  248. this.$prompt('请输入分组名', '提示', {
  249. confirmButtonText: '确定',
  250. cancelButtonText: '取消',
  251. inputValue: materialgroupObj.name
  252. }).then(({ value }) => {
  253. updateMaterialGroup({
  254. groupId: materialgroupObj.groupId,
  255. name: value
  256. }).then(function() {
  257. that.materialGroup={};
  258. that.getAllMaterialGroup()
  259. })
  260. }).catch(() => {
  261. })
  262. },
  263. getAllMaterialGroup() {
  264. this.materialGroupLoading = true;
  265. getAllMaterialGroup({}).then(response => {
  266. this.materialGroupList = response.data
  267. console.log(this.materialGroupList)
  268. this.materialGroupLoading = false;
  269. });
  270. },
  271. getMaterialList() {
  272. this.tableLoading = true;
  273. listMaterial(this.queryParams).then(response => {
  274. this.tableData = response.rows;
  275. this.total = response.total;
  276. this.tableLoading = false;
  277. });
  278. },
  279. moveMaterial(index, type) {
  280. if (type == 'up') {
  281. const tempOption = this.value[index - 1]
  282. this.$set(this.value, index - 1, this.value[index])
  283. this.$set(this.value, index, tempOption)
  284. }
  285. if (type == 'down') {
  286. const tempOption = this.value[index + 1]
  287. this.$set(this.value, index + 1, this.value[index])
  288. this.$set(this.value, index, tempOption)
  289. }
  290. },
  291. zoomMaterial(index) {
  292. this.dialogVisible = true
  293. this.url = this.value[index]
  294. },
  295. deleteMaterial(index) {
  296. const that = this
  297. this.$confirm('是否确认删除?', '提示', {
  298. confirmButtonText: '确定',
  299. cancelButtonText: '取消',
  300. type: 'warning'
  301. }).then(function() {
  302. that.value.splice(index, 1)
  303. that.urls = []
  304. })
  305. },
  306. toSeleteMaterial() {
  307. this.listDialogVisible = true
  308. this.getAllMaterialGroup()
  309. this.getMaterialList();
  310. },
  311. materialDel(item) {
  312. const that = this
  313. this.$confirm('是否确认删除该素材?', '提示', {
  314. confirmButtonText: '确定',
  315. cancelButtonText: '取消',
  316. type: 'warning'
  317. }).then(function() {
  318. delMaterial(item.materialId)
  319. .then(function() {
  320. that.queryParams.pageNum=0;
  321. that.getMaterialList();
  322. })
  323. })
  324. },
  325. handleProgress(event, file, fileList) {
  326. },
  327. handleSuccess(response, file, fileList) {
  328. const that = this
  329. addMaterial({
  330. type: '1',
  331. groupId: this.queryParams.groupId,
  332. name: file.name,
  333. url: response.url
  334. }).then(() => {
  335. this.resultNumber++
  336. if (fileList.length === this.resultNumber) {
  337. that.getMaterialList()
  338. this.resultNumber = 0
  339. }
  340. })
  341. },
  342. beforeUpload(file) {
  343. const isPic =
  344. file.type === 'image/jpeg' ||
  345. file.type === 'image/png' ||
  346. file.type === 'image/gif' ||
  347. file.type === 'image/jpg'
  348. // const isLt2M = file.size / 1024 / 1024 < 2
  349. if (!isPic) {
  350. this.$message.error('上传图片只能是 JPG、JPEG、PNG、GIF 格式!')
  351. return false
  352. }
  353. return new Promise((resolve, reject) => {
  354. if (file.size / 1024 / 1024 > 3) {
  355. this.$message.error('上传的图片不能超过3MB');
  356. reject();
  357. return;
  358. }
  359. if (file.size / 1024 / 1024 > 1) {
  360. const loadingInstance = Loading.service({ text: '图片内存过大正在压缩图片...' });
  361. // 文件大于1MB时进行压缩
  362. this.compressImage(file).then((compressedFile) => {
  363. loadingInstance.close();
  364. if (compressedFile.size / 1024 > 500) {
  365. this.$message.error('图片压缩后仍大于500KB');
  366. reject();
  367. } else {
  368. // this.$message.success(`图片压缩成功,最终质量为: ${this.finalQuality.toFixed(2)}`);
  369. console.log(`图片压缩成功,最终质量为: ${this.finalQuality.toFixed(2)}`);
  370. console.log(`最终内存大小为: ${(compressedFile.size/1024).toFixed(2)}KB`);
  371. resolve(compressedFile);
  372. }
  373. }).catch((err) => {
  374. loadingInstance.close();
  375. console.error(err);
  376. reject();
  377. });
  378. } else {
  379. resolve(file);
  380. }
  381. // this.loading = this.$loading({
  382. // lock: true,
  383. // text: "上传中",
  384. // background: "rgba(0, 0, 0, 0.7)",
  385. // });
  386. });
  387. },
  388. compressImage(file) {
  389. return new Promise((resolve, reject) => {
  390. const reader = new FileReader();
  391. reader.readAsDataURL(file);
  392. reader.onload = (event) => {
  393. const img = new Image();
  394. img.src = event.target.result;
  395. img.onload = () => {
  396. const canvas = document.createElement('canvas');
  397. const ctx = canvas.getContext('2d');
  398. const width = img.width;
  399. const height = img.height;
  400. canvas.width = width;
  401. canvas.height = height;
  402. ctx.drawImage(img, 0, 0, width, height);
  403. let quality = 1; // 初始压缩质量
  404. let dataURL = canvas.toDataURL('image/jpeg', quality);
  405. // 逐步压缩,直到图片大小小于500KB并且压缩质量不再降低
  406. while (dataURL.length / 1024 > 500 && quality > 0.1) {
  407. quality -= 0.01;
  408. dataURL = canvas.toDataURL('image/jpeg', quality);
  409. }
  410. this.finalQuality = quality; // 存储最终的压缩质量
  411. if (dataURL.length / 1024 > 500) {
  412. reject(new Error('压缩后图片仍然大于500KB'));
  413. return;
  414. }
  415. const arr = dataURL.split(',');
  416. const mime = arr[0].match(/:(.*?);/)[1];
  417. const bstr = atob(arr[1]);
  418. let n = bstr.length;
  419. const u8arr = new Uint8Array(n);
  420. while (n--) {
  421. u8arr[n] = bstr.charCodeAt(n);
  422. }
  423. const compressedFile = new Blob([u8arr], { type: mime });
  424. compressedFile.name = file.name;
  425. resolve(compressedFile);
  426. };
  427. img.onerror = (error) => {
  428. reject(error);
  429. };
  430. };
  431. reader.onerror = (error) => {
  432. reject(error);
  433. };
  434. });
  435. },
  436. submit() {
  437. this.urls.forEach(item => {
  438. this.$set(this.value, this.value.length, item)
  439. })
  440. this.listDialogVisible = false
  441. this.urls = []
  442. }
  443. }
  444. }
  445. </script>
  446. <style rel="stylesheet/scss" lang="scss" scoped>
  447. ::v-deep .el-icon-circle-close{
  448. color: red;
  449. }
  450. .material-name{
  451. padding: 8px 0px;
  452. }
  453. .col-do{
  454. text-align: center;
  455. }
  456. .button-do{
  457. padding: unset!important;
  458. font-size: 12px;
  459. }
  460. .group-list{
  461. display: flex;
  462. flex-direction:column;
  463. align-items: flex-start;
  464. }
  465. .group-item{
  466. margin: 5px;
  467. }
  468. </style>