index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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>
  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. :headers="headers"
  75. :file-list="[]"
  76. :on-progress="handleProgress"
  77. :before-upload="beforeUpload"
  78. :on-success="handleSuccess"
  79. :data="{type: 1}"
  80. multiple
  81. >
  82. <el-button size="small" type="primary">批量上传</el-button>
  83. </el-upload>
  84. </el-col>
  85. </el-row>
  86. </div>
  87. <div v-loading="tableLoading">
  88. <el-alert
  89. v-if="tableData.length <= 0"
  90. title="暂无数据"
  91. type="info"
  92. :closable="false"
  93. center
  94. show-icon
  95. />
  96. <el-row :gutter="5">
  97. <el-checkbox-group v-model="urls" :max="num - value.length">
  98. <el-col v-for="(item,index) in tableData" :key="index" :span="4">
  99. <el-card :body-style="{ padding: '5px' }">
  100. <el-image
  101. style="width: 100%;height: 100px"
  102. :src="item.url"
  103. fit="contain"
  104. :preview-src-list="[item.url]"
  105. :z-index="9999"
  106. />
  107. <div>
  108. <el-checkbox class="material-name" :label="item.url">
  109. 选择
  110. </el-checkbox>
  111. <el-row>
  112. <el-col :span="24" class="col-do">
  113. <el-button type="text" size="medium" @click="materialDel(item)">删除</el-button>
  114. </el-col>
  115. </el-row>
  116. </div>
  117. </el-card>
  118. </el-col>
  119. </el-checkbox-group>
  120. </el-row>
  121. <pagination
  122. v-show="total>0"
  123. :total="total"
  124. :page.sync="queryParams.pageNum"
  125. :limit.sync="queryParams.pageSize"
  126. @pagination="getMaterialList"
  127. />
  128. </div>
  129. </el-card>
  130. </el-main>
  131. </el-container>
  132. <span slot="footer" class="dialog-footer">
  133. <el-button @click="listDialogVisible = false">取 消</el-button>
  134. <el-button type="primary" @click="submit">确 定</el-button>
  135. </span>
  136. </el-dialog>
  137. </div>
  138. </template>
  139. <script>
  140. import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial, exportMaterial } from "@/api/store/material";
  141. import { getAllMaterialGroup,listMaterialGroup, getMaterialGroup, delMaterialGroup, addMaterialGroup, updateMaterialGroup, exportMaterialGroup } from "@/api/store/materialGroup";
  142. import { getToken } from "@/utils/auth";
  143. export default {
  144. name: 'ImageSelect',
  145. props: {
  146. // 素材数据
  147. value: {
  148. type: Array,
  149. default() {
  150. return []
  151. }
  152. },
  153. // 素材类型
  154. type: {
  155. type: String
  156. },
  157. // 素材限制数量,默认5个
  158. num: {
  159. type: Number,
  160. default() {
  161. return 5
  162. }
  163. },
  164. // 宽度
  165. width: {
  166. type: Number,
  167. default() {
  168. return 150
  169. }
  170. },
  171. // 宽度
  172. height: {
  173. type: Number,
  174. default() {
  175. return 150
  176. }
  177. }
  178. },
  179. data() {
  180. return {
  181. uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
  182. // 上传鉴权头,上传前会刷新 Token
  183. headers: {
  184. Authorization: "Bearer " + getToken(),
  185. },
  186. dialogVisible: false,
  187. url: '',
  188. listDialogVisible: false,
  189. materialGroupList: [],
  190. materialGroupLoading: false,
  191. materialGroup:{},
  192. tableData: [],
  193. resultNumber: 0,
  194. total: 0,
  195. queryParams: {
  196. pageNum: 1,
  197. pageSize: 10,
  198. type: null,
  199. groupId: null,
  200. name: null,
  201. url: null,
  202. isDel: null,
  203. createUserId: null,
  204. },
  205. tableLoading: false,
  206. urls: []
  207. }
  208. },
  209. mounted(){
  210. this.getAllMaterialGroup();
  211. },
  212. methods: {
  213. clos(){
  214. this.urls=[];
  215. },
  216. selectGroup(item){
  217. this.materialGroup=item;
  218. this.queryParams.groupId=item.groupId;
  219. this.getMaterialList();
  220. },
  221. materialgroupAdd() {
  222. const that = this
  223. this.$prompt('请输入分组名', '提示', {
  224. confirmButtonText: '确定',
  225. cancelButtonText: '取消'
  226. }).then(({ value }) => {
  227. addMaterialGroup({
  228. name: value
  229. }).then(function() {
  230. that.materialGroup={};
  231. that.getAllMaterialGroup()
  232. })
  233. }).catch(() => {
  234. })
  235. },
  236. materialgroupDelete(materialgroupObj) {
  237. const that = this
  238. this.$confirm('是否确认删除该分组?', '提示', {
  239. confirmButtonText: '确定',
  240. cancelButtonText: '取消',
  241. type: 'warning'
  242. }).then(function() {
  243. delMaterialGroup(materialgroupObj.groupId)
  244. .then(function() {
  245. that.materialGroup={};
  246. that.getAllMaterialGroup()
  247. })
  248. })
  249. },
  250. materialgroupEdit(materialgroupObj) {
  251. const that = this
  252. this.$prompt('请输入分组名', '提示', {
  253. confirmButtonText: '确定',
  254. cancelButtonText: '取消',
  255. inputValue: materialgroupObj.name
  256. }).then(({ value }) => {
  257. updateMaterialGroup({
  258. groupId: materialgroupObj.groupId,
  259. name: value
  260. }).then(function() {
  261. that.materialGroup={};
  262. that.getAllMaterialGroup()
  263. })
  264. }).catch(() => {
  265. })
  266. },
  267. getAllMaterialGroup() {
  268. this.materialGroupLoading = true;
  269. getAllMaterialGroup({}).then(response => {
  270. this.materialGroupList = response.data
  271. console.log(this.materialGroupList)
  272. this.materialGroupLoading = false;
  273. });
  274. },
  275. getMaterialList() {
  276. this.tableLoading = true;
  277. listMaterial(this.queryParams).then(response => {
  278. this.tableData = response.rows;
  279. this.total = response.total;
  280. this.tableLoading = false;
  281. });
  282. },
  283. moveMaterial(index, type) {
  284. if (type == 'up') {
  285. const tempOption = this.value[index - 1]
  286. this.$set(this.value, index - 1, this.value[index])
  287. this.$set(this.value, index, tempOption)
  288. }
  289. if (type == 'down') {
  290. const tempOption = this.value[index + 1]
  291. this.$set(this.value, index + 1, this.value[index])
  292. this.$set(this.value, index, tempOption)
  293. }
  294. },
  295. zoomMaterial(index) {
  296. this.dialogVisible = true
  297. this.url = this.value[index]
  298. },
  299. deleteMaterial(index) {
  300. const that = this
  301. this.$confirm('是否确认删除?', '提示', {
  302. confirmButtonText: '确定',
  303. cancelButtonText: '取消',
  304. type: 'warning'
  305. }).then(function() {
  306. that.value.splice(index, 1)
  307. that.urls = []
  308. })
  309. },
  310. toSeleteMaterial() {
  311. this.listDialogVisible = true
  312. this.getAllMaterialGroup()
  313. this.getMaterialList();
  314. },
  315. materialDel(item) {
  316. const that = this
  317. this.$confirm('是否确认删除该素材?', '提示', {
  318. confirmButtonText: '确定',
  319. cancelButtonText: '取消',
  320. type: 'warning'
  321. }).then(function() {
  322. delMaterial(item.materialId)
  323. .then(function() {
  324. that.queryParams.pageNum=0;
  325. that.getMaterialList();
  326. })
  327. })
  328. },
  329. handleProgress(event, file, fileList) {
  330. },
  331. handleSuccess(response, file, fileList) {
  332. // 后端未鉴权时仍可能 HTTP 200 + code:401,需拦截避免空 url
  333. if (!response || response.code === 401 || !response.url) {
  334. this.$message.error((response && response.msg) || "上传失败,请重新登录后重试");
  335. return;
  336. }
  337. const that = this
  338. addMaterial({
  339. type: '1',
  340. groupId: this.queryParams.groupId,
  341. name: file.name,
  342. url: response.url
  343. }).then(() => {
  344. this.resultNumber++
  345. if (fileList.length === this.resultNumber) {
  346. that.getMaterialList()
  347. this.resultNumber = 0
  348. }
  349. })
  350. },
  351. beforeUpload(file) {
  352. this.headers.Authorization = "Bearer " + getToken();
  353. const isPic =
  354. file.type === 'image/jpeg' ||
  355. file.type === 'image/png' ||
  356. file.type === 'image/gif' ||
  357. file.type === 'image/jpg'
  358. const isLt2M = file.size / 1024 / 1024 < 2
  359. if (!isPic) {
  360. this.$message.error('上传图片只能是 JPG、JPEG、PNG、GIF 格式!')
  361. return false
  362. }
  363. if (!isLt2M) {
  364. this.$message.error('上传头像图片大小不能超过 2MB!')
  365. }
  366. return isPic && isLt2M
  367. },
  368. submit() {
  369. this.urls.forEach(item => {
  370. this.$set(this.value, this.value.length, item)
  371. })
  372. this.listDialogVisible = false
  373. this.urls = []
  374. }
  375. }
  376. }
  377. </script>
  378. <style rel="stylesheet/scss" lang="scss" scoped>
  379. ::v-deep .el-icon-circle-close{
  380. color: red;
  381. }
  382. .material-name{
  383. padding: 8px 0px;
  384. }
  385. .col-do{
  386. text-align: center;
  387. }
  388. .button-do{
  389. padding: unset!important;
  390. font-size: 12px;
  391. }
  392. .group-list{
  393. display: flex;
  394. flex-direction:column;
  395. align-items: flex-start;
  396. }
  397. .group-item{
  398. margin: 5px;
  399. }
  400. </style>